The Battle for Wesnoth  1.19.5+dev
config_filters.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "config.hpp"
18 
19 /**
20  * Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
21  *
22  * For example, a filter of `x=1` puts a requirement on the value of `x` but accepts any value of `y`.
23  *
24  * Both `int` and `double` assume a default value of zero, so a filter that accepts zero will match an
25  * unset value as well as a present value.
26  */
28 {
29 /**
30  * Checks whether the filter matches the value of @a cfg[@a attribute]. If @a cfg doesn't have that
31  * attribute, assume that an unset value is equivalent to @a def.
32  *
33  * Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute].
34  */
35 bool bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def);
36 
37 /**
38  * Checks whether the filter matches the value of @a cfg[@a attribute]. If @a cfg doesn't have that
39  * attribute, assume that an unset value is equivalent to @a def if exist, else value false is returned.
40  *
41  * Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute].
42  */
43 bool double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<double> def = utils::nullopt);
44 bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<int> def = utils::nullopt);
45 
46 /**
47  * Supports filters using "add" and "sub" attributes, for example a filter `add=1` matching a cfg containing either
48  * `add=1` or `sub=-1`; this assumes that code elsewhere has already checked that cfg contains at most one of those
49  * keys.
50  *
51  * This only checks for the presence of @a attribute and @a opposite in the filter, so the caller should call this function a second
52  * time, with @a attribute and @a opposite reversed and if none of these attribute is here value false is returned.
53  *
54  * The function is named "negative" in case we later want to add a "reciprocal" for the "multiply"/"divide" pair.
55  */
57  const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, utils::optional<int> def = utils::nullopt);
58 
60  const config& filter, const config& cfg, const std::string& attribute, const std::string& def);
61 
62 /**
63  * filter[attribute] and cfg[attribute] are assumed to be comma-separated lists.
64  * If the filter is present, each item in filter[attribute] must match an item in cfg[attribute]
65  * for the function to return true.
66  */
67 bool set_includes_if_present(const config& filter, const config& cfg, const std::string& attribute);
68 
69 bool bool_or_empty(const config& filter, const config& cfg, const std::string& attribute);
70 
71 } // namespace utils::config_filters
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Definitions for the interface to Wesnoth Markup Language (WML).
Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
bool int_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, utils::optional< int > def=utils::nullopt)
bool set_includes_if_present(const config &filter, const config &cfg, const std::string &attribute)
filter[attribute] and cfg[attribute] are assumed to be comma-separated lists.
bool double_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, utils::optional< double > def=utils::nullopt)
Checks whether the filter matches the value of cfg[attribute].
bool int_matches_if_present_or_negative(const config &filter, const config &cfg, const std::string &attribute, const std::string &opposite, utils::optional< int > def=utils::nullopt)
Supports filters using "add" and "sub" attributes, for example a filter add=1 matching a cfg containi...
bool string_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, const std::string &def)
bool bool_or_empty(const config &filter, const config &cfg, const std::string &attribute)
bool bool_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, bool def)
Checks whether the filter matches the value of cfg[attribute].