The Battle for Wesnoth  1.19.0-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 
24 #include <iosfwd>
25 #include <map>
26 #include <string>
27 #include <optional>
28 #include <vector>
29 
30 class config_writer;
31 class config;
32 
33 typedef std::map<std::string, struct preproc_define> preproc_map;
34 
36 {
38  : value()
39  , arguments()
41  , textdomain()
42  , linenum(0)
43  , location()
44  {
45  }
46 
47  explicit preproc_define(const std::string& val)
48  : value(val)
49  , arguments()
51  , textdomain()
52  , linenum(0)
53  , location()
54  {
55  }
56 
57  preproc_define(const std::string& val,
58  const std::vector<std::string>& args,
59  const std::map<std::string, std::string>& optargs,
60  const std::string& domain,
61  int line,
62  const std::string& loc,
63  const std::string& dep_msg,
64  std::optional<DEP_LEVEL> dep_lvl, const version_info& dep_ver)
65  : value(val)
66  , arguments(args)
67  , optional_arguments(optargs)
68  , textdomain(domain)
69  , linenum(line)
70  , location(loc)
71  , deprecation_message(dep_msg)
72  , deprecation_level(dep_lvl)
73  , deprecation_version(dep_ver)
74  {
75  }
76 
77  std::string value;
78 
79  std::vector<std::string> arguments;
80 
81  std::map<std::string, std::string> optional_arguments;
82 
83  std::string textdomain;
84 
85  int linenum;
86 
87  std::string location;
88 
89  std::string deprecation_message;
90 
91  std::optional<DEP_LEVEL> deprecation_level;
92 
94 
95  bool is_deprecated() const {
96  return deprecation_level.has_value();
97  }
98 
99  void write(config_writer&, const std::string&) const;
100  void write_argument(config_writer&, const std::string&) const;
101  void write_argument(config_writer&, const std::string&, const std::string&) const;
102 
103  void read(const config&);
104  void read_argument(const config&);
105 
106  static preproc_map::value_type read_pair(const config&);
107 
108  bool operator==(const preproc_define&) const;
109 
110  bool operator<(const preproc_define&) const;
111 
112  bool operator!=(const preproc_define& v) const
113  {
114  return !operator==(v);
115  }
116 };
117 
118 std::ostream& operator<<(std::ostream& stream, const preproc_define& def);
119 
121 {
122  struct error : public game::error
123  {
124  error(const std::string& message)
125  : game::error(message)
126  {
127  }
128  };
129 };
130 
131 std::string lineno_string(const std::string& lineno);
132 
133 std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& def);
134 
135 /**
136  * Function to use the WML preprocessor on a file.
137  *
138  * @param defines A map of symbols defined.
139  * @param fname The file to be preprocessed.
140  *
141  * @returns The resulting preprocessed file data.
142  */
143 filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map* defines = nullptr);
144 
145 void preprocess_resource(const std::string& res_name,
146  preproc_map* defines_map,
147  bool write_cfg = false,
148  bool write_plain_cfg = false,
149  const std::string& target_directory = "");
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:159
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:50
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="")
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)
std::optional< DEP_LEVEL > deprecation_level
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 &)
std::string deprecation_message
std::vector< std::string > arguments
preproc_define(const std::string &val)
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, std::optional< DEP_LEVEL > dep_lvl, const version_info &dep_ver)
std::map< std::string, std::string > optional_arguments
bool operator==(const preproc_define &) const
static preproc_map::value_type read_pair(const config &)
std::string location
bool is_deprecated() const