Skip to content

Commit

Permalink
Cleaned up usage of vconfig::all_children_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 7, 2021
1 parent cd120f5 commit 4d4e8e6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 111 deletions.
10 changes: 5 additions & 5 deletions src/game_events/action_wml.cpp
Expand Up @@ -317,10 +317,10 @@ WML_HANDLER_FUNCTION(do_command,, cfg)
ERR_NG << "[do_command] can only be used from clients that control the currently playing side" << std::endl;
return;
}
for(vconfig::all_children_iterator i = cfg.ordered_begin(); i != cfg.ordered_end(); ++i)
for(const auto& [key, child] : cfg.all_ordered())
{
if(allowed_tags.find( i.get_key()) == allowed_tags.end()) {
ERR_NG << "unsupported tag [" << i.get_key() << "] in [do_command]" << std::endl;
if(allowed_tags.find(key) == allowed_tags.end()) {
ERR_NG << "unsupported tag [" << key << "] in [do_command]" << std::endl;
std::stringstream o;
std::copy(allowed_tags.begin(), allowed_tags.end(), std::ostream_iterator<std::string>(o, " "));
ERR_NG << "allowed tags: " << o.str() << std::endl;
Expand All @@ -332,8 +332,8 @@ WML_HANDLER_FUNCTION(do_command,, cfg)
//Note that this fires related events and everything else that also happens normally.
//have to watch out with the undo stack, therefore forbid [auto_shroud] and [update_shroud] here...
synced_context::run_in_synced_context_if_not_already(
/*commandname*/ i.get_key(),
/*data*/ i.get_child().get_parsed_config(),
/*commandname*/ key,
/*data*/ child.get_parsed_config(),
/*use_undo*/ true,
/*show*/ true,
/*error_handler*/ &on_replay_error
Expand Down
39 changes: 13 additions & 26 deletions src/game_events/conditional_wml.cpp
Expand Up @@ -152,19 +152,15 @@ namespace { // Support functions
return false;
}

vconfig::all_children_iterator cond_end = cond.ordered_end();
static const std::set<std::string> skip =
static const std::set<std::string> skip
{"then", "else", "elseif", "not", "and", "or", "do"};

for(vconfig::all_children_iterator it = cond.ordered_begin(); it != cond_end; ++it) {
std::string key = it.get_key();
bool result = true;
for(const auto& [key, filter] : cond.all_ordered()) {
if(std::find(skip.begin(), skip.end(), key) == skip.end()) {
assert(resources::lua_kernel);
result = resources::lua_kernel->run_wml_conditional(key, it.get_child());
}
if (!result) {
return false;
if(!resources::lua_kernel->run_wml_conditional(key, filter)) {
return false;
}
}
}

Expand All @@ -179,30 +175,21 @@ bool conditional_passed(const vconfig& cond)
bool matches = internal_conditional_passed(cond);

// Handle [and], [or], and [not] with in-order precedence
vconfig::all_children_iterator cond_i = cond.ordered_begin();
vconfig::all_children_iterator cond_end = cond.ordered_end();
while(cond_i != cond_end)
{
const std::string& cond_name = cond_i.get_key();
const vconfig& cond_filter = cond_i.get_child();

for(const auto& [key, filter] : cond.all_ordered()) {
// Handle [and]
if(cond_name == "and")
{
matches = matches && conditional_passed(cond_filter);
if(key == "and") {
matches = matches && conditional_passed(filter);
}
// Handle [or]
else if(cond_name == "or")
{
matches = matches || conditional_passed(cond_filter);
else if(key == "or") {
matches = matches || conditional_passed(filter);
}
// Handle [not]
else if(cond_name == "not")
{
matches = matches && !conditional_passed(cond_filter);
else if(key == "not") {
matches = matches && !conditional_passed(filter);
}
++cond_i;
}

return matches;
}

Expand Down
32 changes: 12 additions & 20 deletions src/side_filter.cpp
Expand Up @@ -268,29 +268,21 @@ bool side_filter::match(const team& t) const
{
bool matches = match_internal(t);

//handle [and], [or], and [not] with in-order precedence
vconfig::all_children_iterator cond = cfg_.ordered_begin();
vconfig::all_children_iterator cond_end = cfg_.ordered_end();
while (cond != cond_end) {
const std::string& cond_name = cond.get_key();
const vconfig& cond_cfg = cond.get_child();

//handle [and]
if(cond_name == "and")
{
matches = matches && side_filter(cond_cfg, fc_, flat_).match(t);
// Handle [and], [or], and [not] with in-order precedence
for(const auto& [key, filter] : cfg_.all_ordered()) {
// Handle [and]
if(key == "and") {
matches = matches && side_filter(filter, fc_, flat_).match(t);
}
//handle [or]
else if(cond_name == "or")
{
matches = matches || side_filter(cond_cfg, fc_, flat_).match(t);
// Handle [or]
else if(key == "or") {
matches = matches || side_filter(filter, fc_, flat_).match(t);
}
//handle [not]
else if(cond_name == "not")
{
matches = matches && !side_filter(cond_cfg, fc_, flat_).match(t);
// Handle [not]
else if(key == "not") {
matches = matches && !side_filter(filter, fc_, flat_).match(t);
}
++cond;
}

return matches;
}
20 changes: 8 additions & 12 deletions src/storyscreen/parser.cpp
Expand Up @@ -29,11 +29,7 @@ namespace storyscreen
void story_parser::resolve_wml(const vconfig& cfg)
{
// Execution flow/branching/[image]
for(vconfig::all_children_iterator i = cfg.ordered_begin(); i != cfg.ordered_end(); ++i) {
// i->first and i->second are goddamn temporaries; do not make references
const std::string key = i->first;
const vconfig node = i->second;

for(const auto& [key, node] : cfg.all_ordered()) {
// Execute any special actions derived classes provide.
if(resolve_wml_helper(key, node)) {
continue;
Expand Down Expand Up @@ -78,27 +74,27 @@ void story_parser::resolve_wml(const vconfig& cfg)
const std::string var_actual_value = resources::gamedata->get_variable_const(var_name);
bool case_not_found = true;

for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
if(j->first != "case") {
for(const auto& [switch_key, switch_node] : node.all_ordered()) {
if(switch_key != "case") {
continue;
}

// Enter all matching cases.
const std::string var_expected_value = (j->second)["value"];
const std::string var_expected_value = switch_node["value"];
if(var_actual_value == var_expected_value) {
case_not_found = false;
resolve_wml(j->second);
resolve_wml(switch_node);
}
}

if(case_not_found) {
for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
if(j->first != "else") {
for(const auto& [else_key, else_node] : node.all_ordered()) {
if(else_key != "else") {
continue;
}

// Enter all elses.
resolve_wml(j->second);
resolve_wml(else_node);
}
}
}
Expand Down
73 changes: 25 additions & 48 deletions src/terrain/filter.cpp
Expand Up @@ -90,14 +90,6 @@ terrain_filter::terrain_filter_cache::terrain_filter_cache() :
ufilter_()
{}

namespace {
struct cfg_isor {
bool operator() (std::pair<const std::string,const vconfig> val) const {
return val.first == "or";
}
};
} //end anonymous namespace

bool terrain_filter::match_internal(const map_location& loc, const unit* ref_unit, const bool ignore_xy) const
{
if (!this->fc_->get_disp_context().map().on_board_with_border(loc)) {
Expand Down Expand Up @@ -411,31 +403,22 @@ bool terrain_filter::match_impl(const map_location& loc, const unit* ref_unit) c
for(i = hexes.begin(); i != hexes.end(); ++i) {
bool matches = match_internal(*i, ref_unit, false);

//handle [and], [or], and [not] with in-order precedence
vconfig::all_children_iterator cond = cfg_.ordered_begin();
vconfig::all_children_iterator cond_end = cfg_.ordered_end();
while(cond != cond_end)
{
const std::string& cond_name = cond.get_key();
const vconfig& cond_cfg = cond.get_child();

//handle [and]
if(cond_name == "and")
{
matches = matches && terrain_filter(cond_cfg, *this).match_impl(*i, ref_unit);
// Handle [and], [or], and [not] with in-order precedence
for(const auto& [key, filter] : cfg_.all_ordered()) {
// Handle [and]
if(key == "and") {
matches = matches && terrain_filter(filter, *this).match_impl(*i, ref_unit);
}
//handle [or]
else if(cond_name == "or")
{
matches = matches || terrain_filter(cond_cfg, *this).match_impl(*i, ref_unit);
// Handle [or]
else if(key == "or") {
matches = matches || terrain_filter(filter, *this).match_impl(*i, ref_unit);
}
//handle [not]
else if(cond_name == "not")
{
matches = matches && !terrain_filter(cond_cfg, *this).match_impl(*i, ref_unit);
// Handle [not]
else if(key == "not") {
matches = matches && !terrain_filter(filter, *this).match_impl(*i, ref_unit);
}
++cond;
}

if(matches) {
return true;
}
Expand Down Expand Up @@ -604,24 +587,19 @@ void terrain_filter::get_locs_impl(std::set<map_location>& locs, const unit* ref
}
}

//handle [and], [or], and [not] with in-order precedence
vconfig::all_children_iterator cond = cfg_.ordered_begin();
vconfig::all_children_iterator cond_end = cfg_.ordered_end();
int ors_left = std::count_if(cond, cond_end, cfg_isor());
while(cond != cond_end)
{
int ors_left = std::count_if(cfg_.ordered_begin(), cfg_.ordered_end(), [](const auto& val) { return val.first == "or"; });

// Handle [and], [or], and [not] with in-order precedence
for(const auto& [key, filter] : cfg_.all_ordered()) {
//if there are no locations or [or] conditions left, go ahead and return empty
if(match_set.empty() && ors_left <= 0) {
return;
}

const std::string& cond_name = cond.get_key();
const vconfig& cond_cfg = cond.get_child();

//handle [and]
if(cond_name == "and") {
// Handle [and]
if(key == "and") {
std::set<map_location> intersect_hexes;
terrain_filter(cond_cfg, *this).get_locations(intersect_hexes, with_border);
terrain_filter(filter, *this).get_locations(intersect_hexes, with_border);
std::set<map_location>::iterator intersect_itor = match_set.begin();
while(intersect_itor != match_set.end()) {
if(intersect_hexes.find(*intersect_itor) == intersect_hexes.end()) {
Expand All @@ -631,27 +609,26 @@ void terrain_filter::get_locs_impl(std::set<map_location>& locs, const unit* ref
}
}
}
//handle [or]
else if(cond_name == "or") {
// Handle [or]
else if(key == "or") {
std::set<map_location> union_hexes;
terrain_filter(cond_cfg, *this).get_locations(union_hexes, with_border);
terrain_filter(filter, *this).get_locations(union_hexes, with_border);
//match_set.insert(union_hexes.begin(), union_hexes.end()); //doesn't compile on MSVC
std::set<map_location>::iterator insert_itor = union_hexes.begin();
while(insert_itor != union_hexes.end()) {
match_set.insert(*insert_itor++);
}
--ors_left;
}
//handle [not]
else if(cond_name == "not") {
// Handle [not]
else if(key == "not") {
std::set<map_location> removal_hexes;
terrain_filter(cond_cfg, *this).get_locations(removal_hexes, with_border);
terrain_filter(filter, *this).get_locations(removal_hexes, with_border);
std::set<map_location>::iterator erase_itor = removal_hexes.begin();
while(erase_itor != removal_hexes.end()) {
match_set.erase(*erase_itor++);
}
}
++cond;
}
if(match_set.empty()) {
return;
Expand Down

0 comments on commit 4d4e8e6

Please sign in to comment.