36 #define ERR_CF LOG_STREAM(err, log_config)
37 #define DBG_CF LOG_STREAM(debug, log_config)
40 #define ERR_WML LOG_STREAM(err, log_wml)
45 template<
typename Map,
typename Key>
48 auto res = map.lower_bound(key);
50 if(res == map.end() || key != res->first) {
51 res = map.emplace_hint(res, std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>());
58 template<
typename Map,
typename Key>
59 int map_erase_key(Map& map, Key&& key)
61 auto i = map.find(key);
83 : values_(
cfg.values_)
133 }
else if(name ==
"_") {
137 return std::all_of(name.begin(), name.end(), [](
const char&
c)
145 return (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') ||
146 (c >=
'0' && c <=
'9') || (c ==
'_');
205 add_child(child_key, std::move(child_value));
246 add_child(key, std::move(merged_children));
255 typedef std::map<std::string, config> config_map;
256 config_map merged_children_map;
262 for(
const config_map::value_type&
i : merged_children_map) {
281 child_map::const_iterator
i =
children_.find(key);
293 child_map::const_iterator
i =
children_.find(key);
295 return i->second.size();
313 child_map::const_iterator
i =
children_.find(key);
318 template<
class Tchildren>
319 auto get_child_impl(Tchildren& children, std::string_view key,
int n) ->
optional_config_impl<std::remove_reference_t<decltype(**(*children.begin()).second.begin())>>
322 auto i = children.find(key);
323 if(
i == children.end()) {
324 DBG_CF <<
"The config object has no child named ‘" << key <<
"’.";
325 return utils::nullopt;
329 n =
static_cast<int>(
i->second.size()) +
n;
333 return *
i->second.at(
n);
334 }
catch(
const std::out_of_range&) {
335 DBG_CF <<
"The config object has only ‘" <<
i->second.size() <<
"’ children named ‘" << key
336 <<
"’; request for the index ‘" <<
n <<
"’ cannot be honored.";
337 return utils::nullopt;
345 if(
auto res = get_child_impl(
children_, key, 0)) {
348 throw error(
"Mandatory WML child ‘[" + std::string(key) +
"]’ missing in ‘" + parent +
"’. Please report this bug.");
354 if(
auto res = get_child_impl(
children_, key, 0)) {
357 throw error(
"Mandatory WML child ‘[" + std::string(key) +
"]’ missing in ‘" + parent +
"’. Please report this bug.");
363 if(
auto res = get_child_impl(
children_, key,
n)) {
366 throw error(
"Child [" + std::string(key) +
"] at index " + std::to_string(
n) +
" not found");
372 if(
auto res = get_child_impl(
children_, key,
n)) {
375 throw error(
"Child [" + std::string(key) +
"] at index " + std::to_string(
n) +
" not found");
391 static const config empty_cfg;
392 child_map::const_iterator
i =
children_.find(key);
394 return *
i->second.front();
402 child_map::const_iterator
i =
children_.find(key);
404 return *
i->second.front();
413 const std::string what =
formatter() <<
"[" << in_tag <<
"][" << old_key <<
"]";
418 return utils::nullopt;
427 const std::string what =
formatter() <<
"[" << in_tag <<
"][" << old_key <<
"]";
439 v.emplace_back(
new config());
448 v.emplace_back(
new config(val));
458 v.emplace_back(
new config(std::move(val)));
468 if(
index > v.size()) {
469 throw error(
"illegal index to add child at");
474 bool inserted =
false;
480 if(ord->pos != value.
pos)
482 if(!inserted && ord->index ==
index) {
485 }
else if(ord->index >=
index) {
500 const std::size_t npos =
static_cast<std::size_t
>(-1);
521 auto next = std::find_if(pos_it, end,[&](
const child_pos&
p){
return p.pos->first == key; });
532 const auto index = next->index;
535 for(
auto ord = next; ord != end; ++ord) {
560 if(i_src ==
src.children_.end()) {
569 const auto before =
dst.
size();
571 std::move_iterator{i_src->second.begin()},
572 std::move_iterator{i_src->second.end()});
574 src.children_.erase(i_src);
577 for(std::size_t j = before; j <
dst.
size(); ++j) {
586 for(std::pair<const std::string, child_list>&
p :
children_) {
587 for(
auto&
cfg :
p.second) {
597 std::size_t found = 0;
605 }
else if(
p.index >
index) {
611 pos->second.erase(pos->second.begin() +
index);
626 ERR_CF <<
"Error: attempting to delete non-existing child: " << key <<
"[" <<
index <<
"]";
640 const auto predicate = [
p](
const std::unique_ptr<config>& child)
642 return !
p ||
p(*child);
645 auto child_it = std::find_if(pos->second.begin(), pos->second.end(), predicate);
646 while(child_it != pos->second.end()) {
647 const auto index = std::distance(pos->second.begin(), child_it);
649 child_it = std::find_if(pos->second.begin() +
index, pos->second.end(), predicate);
655 const attribute_map::const_iterator
i =
values_.find(key);
661 return empty_attribute;
666 attribute_map::const_iterator
i =
values_.find(key);
667 return i !=
values_.end() ? &
i->second :
nullptr;
678 auto res =
values_.lower_bound(key);
680 if(res ==
values_.end() || key != res->first) {
681 res =
values_.emplace_hint(res, std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>());
690 const std::string what =
formatter() <<
"[" << in_tag <<
"]" << old_key <<
"=";
691 const std::string
msg =
formatter() <<
"Use " << key <<
"= instead. " << message;
695 attribute_map::const_iterator
i =
values_.find(key);
706 return empty_attribute;
712 const std::string what =
formatter() <<
"[" << in_tag <<
"]" << old_key <<
"=";
718 return empty_attribute;
723 assert(
this != &
cfg);
725 if(key.substr(0, 7) ==
"add_to_") {
726 std::string add_to = key.substr(7);
727 values_[add_to] =
values_[add_to].to_double() + value.to_double();
728 }
else if(key.substr(0, 10) ==
"concat_to_") {
729 std::string concat_to = key.substr(10);
744 while(range.begin() != range.end() && range.begin()->second.blank()) {
756 while(range.begin() != range.end() && range.begin()->second.blank()) {
767 DBG_CF <<
"Key ‘" << name <<
"’ value ‘" << value <<
"’ pair not found as child of key ‘" << key <<
"’.";
768 return utils::nullopt;
772 [&](
const std::unique_ptr<config>& pcfg) ->
const auto& { return (*pcfg)[name]; });
774 if(j !=
i->second.end()) {
778 DBG_CF <<
"Key ‘" << name <<
"’ value ‘" << value <<
"’ pair not found as child of key ‘" << key <<
"’.";
780 return utils::nullopt;
789 throw error(
"Cannot find child [" + std::string(key) +
"] with " + name +
"=" + value);
798 throw error(
"Cannot find child [" + std::string(key) +
"] with " + name +
"=" + value);
834 return any_child(&i_->pos->first, i_->pos->second[i_->index].get());
892 config* inserts =
nullptr;
894 for(
const auto& [key, value] :
values_) {
899 const attribute_map::const_iterator j =
c.values_.find(key);
900 if(j ==
c.values_.end() || (value != j->second && !value.blank())) {
901 if(inserts ==
nullptr) {
905 (*inserts)[key] = value;
909 config* deletes =
nullptr;
911 for(
const auto& [key, value] :
c.values_) {
916 const attribute_map::const_iterator itor =
values_.find(key);
917 if(itor ==
values_.end() || itor->second.blank()) {
918 if(deletes ==
nullptr) {
922 (*deletes)[key] =
"x";
926 std::vector<std::string> entities;
929 entities.push_back(child.first);
932 for(
const auto& child :
c.children_) {
934 entities.push_back(child.first);
938 for(
const std::string& entity : entities) {
939 const child_map::const_iterator itor_a =
children_.find(entity);
940 const child_map::const_iterator itor_b =
c.children_.find(entity);
948 std::size_t ndeletes = 0;
949 std::size_t
ai = 0, bi = 0;
950 while(
ai != a.size() || bi !=
b.size()) {
952 if(
ai < a.size() && bi <
b.size() && *a[
ai] == *
b[bi]) {
958 std::stringstream buf;
962 if(
b.size() - bi > a.size() -
ai) {
964 buf << bi - ndeletes;
965 new_delete.
values_[
"index"] = buf.str();
974 else if(
b.size() - bi < a.size() -
ai) {
977 new_insert.
values_[
"index"] = buf.str();
988 new_change.
values_[
"index"] = buf.str();
1006 for(
const auto& [key, value] : inserts->attribute_range()) {
1012 for(
const attribute& v : deletes->attribute_range()) {
1018 const std::size_t
index =
i[
"index"].to_size_t();
1019 for(
const auto [key,
cfg] :
i.all_children_view()) {
1026 throw error(
"error in diff: could not find element '" + key +
"'");
1029 itor->second[
index]->apply_diff(
cfg, track);
1034 const std::size_t
index =
i[
"index"].to_size_t();
1035 for(
const auto [key,
cfg] :
i.all_children_view()) {
1044 const std::size_t
index =
i[
"index"].to_size_t();
1045 for(
const auto [key,
cfg] :
i.all_children_view()) {
1051 throw error(
"error in diff: could not find element '" + key +
"'");
1064 const std::size_t
index =
i[
"index"].to_size_t();
1065 for(
const auto [key,
cfg] :
i.all_children_view()) {
1071 const std::size_t
index =
i[
"index"].to_size_t();
1072 for(
const auto [key,
cfg] :
i.all_children_view()) {
1079 throw error(
"error in diff: could not find element '" + key +
"'");
1082 itor->second[
index]->clear_diff_track(
cfg);
1086 for(std::pair<const std::string, child_list>&
p :
children_) {
1087 for(
auto&
cfg :
p.second) {
1098 std::vector<child_pos> to_remove;
1099 std::map<std::string, unsigned> visitations;
1107 const std::string&
tag =
i->pos->first;
1108 const child_map::const_iterator j =
c.children_.find(
tag);
1110 if(j !=
c.children_.end()) {
1111 unsigned& visits = visitations[
tag];
1113 if(visits < j->second.size()) {
1115 const config& merge_child = *j->second[visits++];
1117 if(merge_child[
"__remove"].to_bool()) {
1118 to_remove.push_back(*
i);
1127 for(
const auto& [
tag, list] :
c.children_) {
1128 unsigned& visits = visitations[
tag];
1129 while(visits < list.size()) {
1135 std::map<std::string, std::size_t> removals;
1137 const std::string&
tag = pos.pos->first;
1138 auto& removes = removals[
tag];
1160 for(
const auto& [key, value] :
cfg.
values_) {
1171 for(
const auto& [key, value] :
filter.attribute_range()) {
1172 if(key.compare(0, 8,
"glob_on_") == 0) {
1180 if(!v || *v != value) {
1187 for(
const auto [key,
cfg] :
filter.all_children_view()) {
1191 }
else if(key ==
"and") {
1194 }
else if(key ==
"or") {
1201 if(j.matches(
cfg)) {
1207 result = result && found;
1215 std::ostringstream outstream;
1217 return outstream.str();
1230 for(
int j = 0; j <
i - 1; j++) {
1234 outstream << key <<
" = " << value <<
'\n';
1238 for(
int j = 0; j <
i - 1; ++j) {
1242 outstream <<
"[" << key <<
"]\n";
1245 for(
int j = 0; j <
i - 1; ++j) {
1249 outstream <<
"[/" << key <<
"]\n";
1258 static const unsigned int hash_length = 128;
1259 static const char hash_string[] =
"+-,.<>0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1263 for(
i = 0;
i != hash_length; ++
i) {
1270 for(
const auto& [key, value] :
values_) {
1277 if(++
i == hash_length) {
1282 std::string base_str = value.t_str().base_str();
1283 for(
const char c : base_str) {
1285 if(++
i == hash_length) {
1292 std::string child_hash =
cfg.
hash();
1293 for(
char c : child_hash) {
1296 if(
i == hash_length) {
1302 for(
i = 0;
i != hash_length; ++
i) {
1325 return valid_tag(pair.first) &&
1326 std::all_of(pair.second.begin(), pair.second.end(),
1327 [](const auto& c) { return c->validate_wml(); });
1342 for(; !x.empty() && !y.empty(); x.pop_front(), y.pop_front()) {
1343 if(x.front().key != y.front().key || x.front().cfg != y.front().cfg) {
1348 return x.empty() && y.empty();
static unsigned int hash_str(const std::string &str)
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
bool blank() const
Tests for an attribute that was never set.
A config object defines a single node in a WML file, with access to child nodes.
void remove_attribute(std::string_view key)
static bool valid_tag(std::string_view name)
config & add_child(std::string_view key)
void merge_children_by_attribute(std::string_view key, std::string_view attribute)
All children with the given key and with equal values of the specified attribute will be merged into ...
void append(const config &cfg)
Append data from another config object to this one.
const attribute_value & get_or(const std::string_view key, const std::string_view default_key) const
Chooses a value.
all_children_iterator erase(const all_children_iterator &i)
const_all_children_iterator ordered_begin() const
void merge_children(std::string_view key)
All children with the given key will be merged into the first element with that key.
boost::iterator_range< const_all_children_iterator > const_all_children_itors
std::size_t attribute_count() const
Count the number of non-blank attributes.
optional_config_impl< config > optional_child(std::string_view key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
bool matches(const config &filter) const
const attribute_value & get_old_attribute(std::string_view key, const std::string &old_key, const std::string &in_tag, const std::string &message="") const
Function to handle backward compatibility Get the value of key and if missing try old_key and log a d...
void append_children_by_move(config &cfg, std::string_view key)
Moves children with the given name from the given config to this one.
void recursive_clear_value(std::string_view key)
const_attr_itors attribute_range() const
attribute_map values_
All the attributes of this node.
bool validate_wml() const
Returns true if this object represents valid WML, i.e.
config & child_or_add(std::string_view key)
Returns a reference to the first child with the given key.
auto all_children_view() const
In-order iteration over all children.
void merge_attributes(const config &)
const_all_children_iterator ordered_end() const
boost::iterator_range< child_iterator > child_itors
config & find_mandatory_child(std::string_view key, const std::string &name, const std::string &value)
std::vector< std::unique_ptr< config > > child_list
const attribute_value & get_deprecated_attribute(std::string_view old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated attribute without a direct substitute, and log a deprecation message.
child_itors child_range(std::string_view key)
const attribute_value * get(std::string_view key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
boost::iterator_range< attribute_iterator > attr_itors
const_all_children_iterator ordered_cbegin() const
static bool valid_attribute(std::string_view name)
void merge_with(const config &c)
Merge config 'c' into this config, overwriting this config's values.
config & add_child_at_total(std::string_view key, const config &val, std::size_t pos)
void swap(config &cfg) noexcept
optional_config_impl< config > find_child(std::string_view key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
void clear_all_children()
void inherit_from(const config &c)
Merge config 'c' into this config, preserving this config's values.
const_all_children_itors all_children_range() const
In-order iteration over all children.
static const char * diff_track_attribute
The name of the attribute used for tracking diff changes.
boost::iterator_range< all_children_iterator > all_children_itors
void append_attributes(const config &cfg)
Adds attributes from cfg.
boost::iterator_range< const_attribute_iterator > const_attr_itors
void splice_children(config &src, std::string_view key)
Moves all the children with tag key from src to this.
void apply_diff(const config &diff, bool track=false)
A function to apply a diff config onto this config object.
std::size_t all_children_count() const
child_map children_
A list of all children of this node.
attribute_value & operator[](std::string_view key)
Returns a reference to the attribute with the given key.
const_all_children_iterator ordered_cend() const
bool has_attribute(std::string_view key) const
config get_diff(const config &c) const
A function to get the differences between this object, and 'c', as another config object.
std::string debug() const
std::vector< child_pos > ordered_children_
attribute_map::value_type attribute
void inherit_attributes(const config &c)
Merge the attributes of config 'c' into this config, preserving this config's values.
void remove_child(std::string_view key, std::size_t index)
std::size_t child_count(std::string_view key) const
optional_config_impl< const config > get_deprecated_child(std::string_view old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated child and log a deprecation message.
bool has_child(std::string_view key) const
Determine whether a config has a child or not.
void clear_diff_track(const config &diff)
Clear any tracking info from a previous apply_diff call with tracking.
const config & child_or_empty(std::string_view key) const
Returns the first child with the given key, or an empty config if there is none.
config & add_child_at(std::string_view key, const config &val, std::size_t index)
std::size_t find_total_first_of(std::string_view key, std::size_t start=0)
boost::iterator_range< const_child_iterator > const_child_itors
void append_children(const config &cfg)
Adds children from cfg.
config & mandatory_child(std::string_view key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
void clear_children_impl(std::string_view key)
friend bool operator==(const config &a, const config &b)
config & operator=(const config &)
const_child_itors get_deprecated_child_range(std::string_view old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated child range and log a deprecation message.
void remove_children(std::string_view key, const std::function< bool(const config &)> &p={})
Removes all children with tag key for which p returns true.
std::ostream & operator<<(std::ostream &outstream, const config &cfg)
void swap(config &lhs, config &rhs) noexcept
Implement non-member swap function for std::swap (calls config::swap).
static lg::log_domain log_wml("wml")
static lg::log_domain log_config("config")
Definitions for the interface to Wesnoth Markup Language (WML).
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
DEP_LEVEL
See https://wiki.wesnoth.org/CompatibilityStandards for more info.
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
void clear()
Clear the current render target.
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified tag.
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
auto find(Container &container, const Value &value, const Projection &projection={})
bool wildcard_string_match(std::string_view str, std::string_view pat) noexcept
Performs pattern matching with wildcards.
void erase_if(Container &container, const Predicate &predicate)
Convenience wrapper for using std::remove_if on a container.
std::string::const_iterator iterator
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
std::vector< child_pos >::iterator Itor
reference operator*() const
const child_map::key_type & key
reference operator*() const
constexpr point size() const
static map_location::direction n