The Battle for Wesnoth  1.17.0-dev
game_config_view.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2021
3  by Andrius Silinskas <silinskas.andrius@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "game_config_view.hpp"
17 #include "config.hpp"
18 #include "log.hpp"
19 
20 #include <boost/range/iterator_range.hpp>
21 
22 static lg::log_domain log_config("config");
23 #define ERR_CONFIG LOG_STREAM(err, log_config)
24 #define WRN_CONFIG LOG_STREAM(warn, log_config)
25 #define LOG_CONFIG LOG_STREAM(info, log_config)
26 
27 
29 {
31  if(cfgs_.size() <= 1 || key != "terrain_graphics") {
32  for(const config& cfg : cfgs_) {
33  for (const config& child : cfg.child_range(key)) {
34  res.push_back(child);
35  }
36  }
37  }
38  else {
39  //use mainline [terrain_graphics] last. cfgs_.front() is the main game configs while the later ones are add-ons.
40  for(const config& cfg : boost::make_iterator_range(cfgs_.begin() + 1, cfgs_.end())) {
41  for (const config& child : cfg.child_range(key)) {
42  res.push_back(child);
43  }
44  }
45  for (const config& child : cfgs_.front().get().child_range(key)) {
46  res.push_back(child);
47  }
48  }
49  return res;
50 }
51 
52 const config& game_config_view::find_child(config_key_type key, const std::string &name, const std::string &value) const
53 {
54  for(const config& cfg : cfgs_) {
55  if(const config& res = cfg.find_child(key, name, value)) {
56  return res;
57  }
58  }
59  LOG_CONFIG << "gcv : cannot find [" << key << "] with " << name << "=" << value << ", count = " << cfgs_.size() <<"\n";
60  const config cfg;
61  return cfg.child("invalid");
62 }
63 
65 {
66  for(const config& cfg : cfgs_) {
67  if(const auto res = cfg.optional_child(key)) {
68  return res.value();
69  }
70  }
71  const config cfg;
72  return cfg.child("invalid");
73 }
74 
75 
77 {
78  for(const config& cfg : cfgs_) {
79  if(const auto res = cfg.optional_child(key)) {
80  return res.value();
81  }
82  }
83  static const config cfg;
84  return cfg;
85 }
86 
88 {
89  game_config_view res;
90  for(const config& cfg : cfgs_) {
91 
92  for(const config& child : cfg.child_range(key)) {
93  res.cfgs_.push_back(child);
94  }
95  }
96  return res;
97 }
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:402
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:885
config_array_view child_range(config_key_type key) const
child_itors child_range(config_key_type key)
Definition: config.cpp:344
Definitions for the interface to Wesnoth Markup Language (WML).
std::string_view config_key_type
Definition: config.hpp:50
static lg::log_domain log_config("config")
game_config_view merged_children_view(config_key_type key) const
const config & child_or_empty(config_key_type key) const
config_array_view cfgs_
const config & find_child(config_key_type key, const std::string &name, const std::string &value) const
std::vector< std::reference_wrapper< const config > > config_array_view
Standard logging facilities (interface).
#define LOG_CONFIG
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
const config & child(config_key_type key) const