New config methods for getting deprecated attributes or children

This commit is contained in:
Celtic Minstrel 2021-07-25 20:03:39 -04:00 committed by Celtic Minstrel
parent 03ee904513
commit fa6bcb0a37
3 changed files with 85 additions and 36 deletions

View File

@ -214,23 +214,6 @@ bool config::has_attribute(config_key_type key) const
return values_.find(key) != values_.end();
}
bool config::has_old_attribute(config_key_type key, const std::string& old_key, const std::string& msg) const
{
check_valid();
if(values_.find(key) != values_.end()) {
return true;
} else if(values_.find(old_key) != values_.end()) {
if(!msg.empty()) {
lg::log_to_chat() << msg << '\n';
ERR_WML << msg;
}
return true;
}
return false;
}
void config::remove_attribute(config_key_type key)
{
check_valid();
@ -502,6 +485,32 @@ config& config::child_or_add(config_key_type key)
return add_child(key);
}
utils::optional_reference<const config> config::get_deprecated_child(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const {
check_valid();
if(auto i = children_.find(old_key); i != children_.end() && !i->second.empty()) {
const std::string what = formatter() << "[" << in_tag << "][" << old_key << "]";
deprecated_message(what, level, "", message);
return *i->second.front();
}
return std::nullopt;
}
config::const_child_itors config::get_deprecated_child_range(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const {
check_valid();
static child_list dummy;
const child_list* p = &dummy;
if(auto i = children_.find(old_key); i != children_.end() && !i->second.empty()) {
const std::string what = formatter() << "[" << in_tag << "][" << old_key << "]";
deprecated_message(what, level, "", message);
p = &i->second;
}
return const_child_itors(const_child_iterator(p->begin()), const_child_iterator(p->end()));
}
config& config::add_child(config_key_type key)
{
check_valid();
@ -788,10 +797,15 @@ config::attribute_value& config::operator[](config_key_type key)
return res->second;
}
const config::attribute_value& config::get_old_attribute(
config_key_type key, const std::string& old_key, const std::string& in_tag) const
const config::attribute_value& config::get_old_attribute(config_key_type key, const std::string& old_key, const std::string& in_tag, const std::string& message) const
{
check_valid();
if(has_attribute(old_key)) {
const std::string what = formatter() << "[" << in_tag << "]" << old_key << "=";
const std::string msg = formatter() << "Use " << key << "= instead. " << message;
deprecated_message(what, DEP_LEVEL::INDEFINITE, "", msg);
}
attribute_map::const_iterator i = values_.find(key);
if(i != values_.end()) {
@ -800,14 +814,6 @@ const config::attribute_value& config::get_old_attribute(
i = values_.find(old_key);
if(i != values_.end()) {
if(!in_tag.empty()) {
const std::string what = formatter() << "[" << in_tag << "]" << old_key << "=";
const std::string msg = formatter() << "Use " << key << "= instead.";
deprecated_message(what, DEP_LEVEL::INDEFINITE, "", msg);
lg::log_to_chat() << msg << '\n';
ERR_WML << msg;
}
return i->second;
}
@ -815,6 +821,19 @@ const config::attribute_value& config::get_old_attribute(
return empty_attribute;
}
const config::attribute_value& config::get_deprecated_attribute(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const {
check_valid();
if(auto i = values_.find(old_key); i != values_.end()) {
const std::string what = formatter() << "[" << in_tag << "]" << old_key << "=";
deprecated_message(what, level, "", message);
return i->second;
}
static const attribute_value empty_attribute;
return empty_attribute;
}
void config::merge_attributes(const config& cfg)
{
check_valid(cfg);

View File

@ -48,6 +48,7 @@
#include <boost/range/iterator_range.hpp>
using config_key_type = std::string_view;
enum class DEP_LEVEL : uint8_t;
class config;
class enum_tag;
@ -388,6 +389,26 @@ public:
* @returns The wanted child node.
*/
const config& child(config_key_type key, const std::string& parent) const;
/**
* Get a deprecated child and log a deprecation message
* @param old_key The deprecated child to return if present
* @param in_tag The name of the tag this child appears in
* @param level The deprecation level
* @param message An explanation of the deprecation, possibly mentioning an alternative
* @note The deprecation message will be a level 3 deprecation.
*/
utils::optional_reference<const config> get_deprecated_child(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const;
/**
* Get a deprecated child rangw and log a deprecation message
* @param old_key The deprecated child to return if present
* @param in_tag The name of the tag this child appears in
* @param level The deprecation level
* @param message An explanation of the deprecation, possibly mentioning an alternative
* @note The deprecation message will be a level 3 deprecation.
*/
const_child_itors get_deprecated_child_range(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const;
config& add_child(config_key_type key);
config& add_child(config_key_type key, const config& val);
@ -457,9 +478,24 @@ public:
/**
* Function to handle backward compatibility
* Get the value of key and if missing try old_key
* and log msg as a WML error (if not empty)
* and log a deprecation message
* @param key The non-deprecated attribute to return
* @param old_key The deprecated attribute to return if @a key is missing
* @param in_tag The name of the tag these attributes appear in
* @param message An explanation of the deprecation, to be output if @a old_key is present.
* @note The deprecation message will be a level 1 deprecation.
*/
const attribute_value &get_old_attribute(config_key_type key, const std::string &old_key, const std::string& in_tag = "") const;
const attribute_value &get_old_attribute(config_key_type key, const std::string &old_key, const std::string& in_tag, const std::string& message = "") const;
/**
* Get a deprecated attribute without a direct substitute,
* and log a deprecation message
* @param old_key The deprecated attribute to return if present
* @param in_tag The name of the tag this attribute appears in
* @param level The deprecation level
* @param message An explanation of the deprecation, possibly mentioning an alternative
*/
const attribute_value& get_deprecated_attribute(config_key_type old_key, const std::string& in_tag, DEP_LEVEL level, const std::string& message) const;
/**
* Inserts an attribute into the config
@ -479,12 +515,6 @@ public:
config &child_or_add(config_key_type key);
bool has_attribute(config_key_type key) const;
/**
* Function to handle backward compatibility
* Check if has key or old_key
* and log msg as a WML error (if not empty)
*/
bool has_old_attribute(config_key_type key, const std::string &old_key, const std::string& msg = "") const;
void remove_attribute(config_key_type key);
void merge_attributes(const config &);

View File

@ -17,7 +17,7 @@
#include <string>
/** See https://wiki.wesnoth.org/CompatibilityStandards for more info. */
enum class DEP_LEVEL { INDEFINITE = 1, PREEMPTIVE, FOR_REMOVAL, REMOVED };
enum class DEP_LEVEL : uint8_t { INDEFINITE = 1, PREEMPTIVE, FOR_REMOVAL, REMOVED };
/**
* Prints a message to the deprecation log domain informing players that a given feature