The Battle for Wesnoth  1.19.1+dev
game_config_view.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2024
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 static lg::log_domain log_config("config");
21 #define ERR_CONFIG LOG_STREAM(err, log_config)
22 #define WRN_CONFIG LOG_STREAM(warn, log_config)
23 #define LOG_CONFIG LOG_STREAM(info, log_config)
24 
25 
27 {
29  if(cfgs_.size() <= 1 || key != "terrain_graphics") {
30  for(const config& cfg : cfgs_) {
31  for (const config& child : cfg.child_range(key)) {
32  res.push_back(child);
33  }
34  }
35  }
36  else {
37  //use mainline [terrain_graphics] last. cfgs_.front() is the main game configs while the later ones are add-ons.
38  for(const config& cfg : boost::make_iterator_range(cfgs_.begin() + 1, cfgs_.end())) {
39  for (const config& child : cfg.child_range(key)) {
40  res.push_back(child);
41  }
42  }
43  for (const config& child : cfgs_.front().get().child_range(key)) {
44  res.push_back(child);
45  }
46  }
47  return res;
48 }
49 
50 optional_const_config game_config_view::find_child(config_key_type key, const std::string &name, const std::string &value) const
51 {
52  for(const config& cfg : cfgs_) {
53  if(optional_const_config res = cfg.find_child(key, name, value)) {
54  return res;
55  }
56  }
57  LOG_CONFIG << "gcv : cannot find [" << key << "] with " << name << "=" << value << ", count = " << cfgs_.size();
58  return optional_const_config();
59 }
60 
61 const config& game_config_view::find_mandatory_child(config_key_type key, const std::string &name, const std::string &value) const
62 {
63  auto res = find_child(key, name, value);
64  if(res) {
65  return *res;
66  }
67  throw config::error("Cannot find child [" + std::string(key) + "] with " + name + "=" + value);
68 }
69 
71 {
72  for(const config& cfg : cfgs_) {
73  if(const auto res = cfg.optional_child(key)) {
74  return res.value();
75  }
76  }
77  throw config::error("missing WML tag [" + std::string(key) + "]");
78 }
79 
81 {
82  for(const config& cfg : cfgs_) {
83  if(const auto res = cfg.optional_child(key)) {
84  return res.value();
85  }
86  }
87  return optional_const_config();
88 }
89 
90 
92 {
93  for(const config& cfg : cfgs_) {
94  if(const auto res = cfg.optional_child(key)) {
95  return res.value();
96  }
97  }
98  static const config cfg;
99  return cfg;
100 }
101 
103 {
104  game_config_view res;
105  for(const config& cfg : cfgs_) {
106 
107  for(const config& child : cfg.child_range(key)) {
108  res.cfgs_.push_back(child);
109  }
110  }
111  return res;
112 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
child_itors child_range(config_key_type key)
Definition: config.cpp:273
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const config & child_or_empty(config_key_type key) const
config_array_view cfgs_
const config & mandatory_child(config_key_type key) const
optional_const_config optional_child(config_key_type key) const
optional_const_config find_child(config_key_type key, const std::string &name, const std::string &value) const
config_array_view child_range(config_key_type key) const
const config & find_mandatory_child(config_key_type key, const std::string &name, const std::string &value) const
game_config_view merged_children_view(config_key_type key) const
T & value() const
Definition: config.hpp:70
std::string_view config_key_type
Definition: config.hpp:49
optional_config_impl< const config > optional_const_config
Definition: config.hpp:934
#define LOG_CONFIG
static lg::log_domain log_config("config")
std::vector< std::reference_wrapper< const config > > config_array_view
Standard logging facilities (interface).