The Battle for Wesnoth  1.19.7+dev
preprocessor.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2024
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 {
39  : value()
40  , arguments()
42  , textdomain()
43  , linenum(0)
44  , location()
45  {
46  }
47 
48  explicit preproc_define(const std::string& val)
49  : value(val)
50  , arguments()
52  , textdomain()
53  , linenum(0)
54  , location()
55  {
56  }
57 
58  preproc_define(const std::string& val,
59  const std::vector<std::string>& args,
60  const std::map<std::string, std::string>& optargs,
61  const std::string& domain,
62  int line,
63  const std::string& loc,
64  const std::string& dep_msg,
65  utils::optional<DEP_LEVEL> dep_lvl, const version_info& dep_ver)
66  : value(val)
67  , arguments(args)
68  , optional_arguments(optargs)
69  , textdomain(domain)
70  , linenum(line)
71  , location(loc)
72  , deprecation_message(dep_msg)
73  , deprecation_level(dep_lvl)
74  , deprecation_version(dep_ver)
75  {
76  }
77 
78  std::string value;
79 
80  std::vector<std::string> arguments;
81 
82  std::map<std::string, std::string> optional_arguments;
83 
84  std::string textdomain;
85 
86  int linenum;
87 
88  std::string location;
89 
90  std::string deprecation_message;
91 
92  utils::optional<DEP_LEVEL> deprecation_level;
93 
95 
96  bool is_deprecated() const {
97  return deprecation_level.has_value();
98  }
99 
100  void write(config_writer&, const std::string&) const;
101  void write_argument(config_writer&, const std::string&) const;
102  void write_argument(config_writer&, const std::string&, const std::string&) const;
103 
104  void read(const config&);
105  void read_argument(const config&);
106 
107  static preproc_map::value_type read_pair(const config&);
108 
109  bool operator==(const preproc_define&) const;
110 
111  bool operator<(const preproc_define&) const;
112 
113  bool operator!=(const preproc_define& v) const
114  {
115  return !operator==(v);
116  }
117 };
118 
119 std::ostream& operator<<(std::ostream& stream, const preproc_define& def);
120 
122 {
123  struct error : public game::error
124  {
125  error(const std::string& message)
126  : game::error(message)
127  {
128  }
129  };
130 };
131 
132 std::string lineno_string(const std::string& lineno);
133 
134 std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& def);
135 
136 /**
137  * Function to use the WML preprocessor on a file.
138  *
139  * @param defines A map of symbols defined.
140  * @param fname The file to be preprocessed.
141  *
142  * @returns The resulting preprocessed file data.
143  */
144 filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map* defines = nullptr);
145 
146 /**
147  * Function to use the WML preprocessor on a string.
148  *
149  * @param defines A map of symbols defined.
150  * @param contents The string to be preprocessed.
151  * @param textdomain The textdomain to associate the contents.
152  * Default: wesnoth
153  *
154  * @returns The resulting preprocessed string.
155  */
156 std::string preprocess_string(
157  const std::string& contents,
158  preproc_map* defines,
159  const std::string& textdomain = "wesnoth");
160 
162  const std::string& res_name,
163  preproc_map* defines_map,
164  bool write_cfg = false,
165  bool write_plain_cfg = false,
166  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:172
Represents version numbers.
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:180
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
std::vector< std::string > arguments
preproc_define(const std::string &val)
std::map< std::string, std::string > optional_arguments
bool operator==(const preproc_define &) const
utils::optional< DEP_LEVEL > deprecation_level
static preproc_map::value_type read_pair(const config &)
std::string location
bool is_deprecated() const