The Battle for Wesnoth  1.19.14+dev
preprocessor.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2025
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 #include "deprecation.hpp"
20 #include "exceptions.hpp"
21 #include "filesystem.hpp"
22 #include "game_version.hpp"
23 #include "utils/optional_fwd.hpp"
24 
25 #include <iosfwd>
26 #include <map>
27 #include <string>
28 #include "utils/optional_fwd.hpp"
29 #include <vector>
30 
31 class config_writer;
32 class config;
33 
34 typedef std::map<std::string, struct preproc_define> preproc_map;
35 
37 {
38  preproc_define() = default;
39 
40  explicit preproc_define(const std::string& val)
41  : value(val)
42  {
43  }
44 
45  explicit preproc_define(const config& cfg)
46  {
47  read(cfg);
48  }
49 
50  preproc_define(const std::string& val,
51  const std::vector<std::string>& args,
52  const std::map<std::string, std::string>& optargs,
53  const std::string& domain,
54  int line,
55  const std::string& loc,
56  const std::string& dep_msg,
57  utils::optional<DEP_LEVEL> dep_lvl, const version_info& dep_ver)
58  : value(val)
59  , arguments(args)
60  , optional_arguments(optargs)
61  , textdomain(domain)
62  , location(loc)
63  , deprecation_message(dep_msg)
64  , deprecation_version(dep_ver)
65  , deprecation_level(dep_lvl)
66  , linenum(line)
67  {
68  }
69 
70  std::string value;
71 
72  std::vector<std::string> arguments;
73 
74  std::map<std::string, std::string> optional_arguments;
75 
76  std::string textdomain;
77 
78  std::string location;
79 
80  std::string deprecation_message;
81 
83 
84  utils::optional<DEP_LEVEL> deprecation_level;
85 
86  int linenum{0};
87 
88  bool is_deprecated() const {
89  return deprecation_level.has_value();
90  }
91 
92  void write(config_writer&, const std::string&) const;
93  void write_argument(config_writer&, const std::string&) const;
94  void write_argument(config_writer&, const std::string&, const std::string&) const;
95 
96  void read(const config&);
97  void read_argument(const config&);
98 
99  static void insert(preproc_map&, const config&);
100 
101  bool operator==(const preproc_define&) const;
102 
103  bool operator<(const preproc_define&) const;
104 
105  bool operator!=(const preproc_define& v) const
106  {
107  return !operator==(v);
108  }
109 };
110 
111 std::ostream& operator<<(std::ostream& stream, const preproc_define& def);
112 
114 {
115  struct error : public game::error
116  {
117  error(const std::string& message)
118  : game::error(message)
119  {
120  }
121  };
122 };
123 
124 std::string lineno_string(const std::string& lineno);
125 
126 std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& def);
127 
128 /**
129  * Function to use the WML preprocessor on a file.
130  *
131  * @param defines A map of symbols defined.
132  * @param fname The file to be preprocessed.
133  *
134  * @returns The resulting preprocessed file data.
135  */
136 filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map* defines = nullptr);
137 
138 /**
139  * Function to use the WML preprocessor on a string.
140  *
141  * @param defines A map of symbols defined.
142  * @param contents The string to be preprocessed.
143  * @param textdomain The textdomain to associate the contents.
144  * Default: wesnoth
145  *
146  * @returns The resulting preprocessed string.
147  */
148 std::string preprocess_string(
149  const std::string& contents,
150  preproc_map* defines,
151  const std::string& textdomain = "wesnoth");
152 
154  const std::string& res_name,
155  preproc_map* defines_map,
156  bool write_cfg = false,
157  bool write_plain_cfg = false,
158  const std::string& target_directory = "");
map_location loc
Definition: move.cpp:172
Class for writing a config out to a file in pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Represents version numbers.
const config * cfg
Declarations for File-IO.
Interfaces for manipulating version numbers of engine, add-ons, etc.
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:189
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:53
std::string lineno_string(const std::string &lineno)
std::ostream & operator<<(std::ostream &stream, const preproc_define &def)
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines=nullptr)
Function to use the WML preprocessor on a file.
std::map< std::string, struct preproc_define > preproc_map
void preprocess_resource(const std::string &res_name, preproc_map *defines_map, bool write_cfg=false, bool write_plain_cfg=false, const std::string &target_directory="")
std::string preprocess_string(const std::string &contents, preproc_map *defines, const std::string &textdomain="wesnoth")
Function to use the WML preprocessor on a string.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
error(const std::string &message)
bool operator!=(const preproc_define &v) const
void write(config_writer &, const std::string &) const
std::string value
version_info deprecation_version
bool operator<(const preproc_define &) const
std::string textdomain
void write_argument(config_writer &, const std::string &) const
void read(const config &)
void read_argument(const config &)
preproc_define(const std::string &val, const std::vector< std::string > &args, const std::map< std::string, std::string > &optargs, const std::string &domain, int line, const std::string &loc, const std::string &dep_msg, utils::optional< DEP_LEVEL > dep_lvl, const version_info &dep_ver)
std::string deprecation_message
preproc_define(const config &cfg)
std::vector< std::string > arguments
static void insert(preproc_map &, const config &)
preproc_define(const std::string &val)
preproc_define()=default
std::map< std::string, std::string > optional_arguments
bool operator==(const preproc_define &) const
utils::optional< DEP_LEVEL > deprecation_level
std::string location
bool is_deprecated() const