The Battle for Wesnoth  1.19.16+dev
game_config_view.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2025
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(std::string_view 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(std::string_view 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 
70 const config& game_config_view::mandatory_child(std::string_view key) const
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 
91 const config& game_config_view::child_or_empty(std::string_view key) const
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:157
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.
Definition: config.cpp:379
child_itors child_range(std::string_view key)
Definition: config.cpp:267
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.
Definition: config.cpp:763
A class grating read only view to a vector of config objects, viewed as one config with all children ...
game_config_view merged_children_view(std::string_view key) const
const config & child_or_empty(std::string_view key) const
config_array_view child_range(std::string_view key) const
const config & mandatory_child(std::string_view key) const
config_array_view cfgs_
const config & find_mandatory_child(std::string_view key, const std::string &name, const std::string &value) const
optional_const_config optional_child(std::string_view key) const
optional_const_config find_child(std::string_view key, const std::string &name, const std::string &value) const
T & value() const
Definition: config.hpp:68
Definitions for the interface to Wesnoth Markup Language (WML).
optional_config_impl< const config > optional_const_config
Definition: config.hpp:925
const config * cfg
#define LOG_CONFIG
static lg::log_domain log_config("config")
std::vector< std::reference_wrapper< const config > > config_array_view
Standard logging facilities (interface).