The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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"
24
25#include <iosfwd>
26#include <map>
27#include <string>
29#include <vector>
30
31class config_writer;
32class config;
33
34typedef 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
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
111std::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
124std::string lineno_string(const std::string& lineno);
125
126std::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 */
136filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map& defines);
137filesystem::scoped_istream preprocess_file(const std::string& fname);
138
139/**
140 * Function to use the WML preprocessor on a string.
141 *
142 * @param defines A map of symbols defined.
143 * @param contents The string to be preprocessed.
144 * @param textdomain The textdomain to associate the contents.
145 *
146 * @returns The resulting preprocessed string.
147 */
148filesystem::scoped_istream preprocess_string(const std::string& contents, const std::string& textdomain, preproc_map& defines);
149filesystem::scoped_istream preprocess_string(const std::string& contents, const std::string& textdomain);
150
152 const std::string& res_name,
153 preproc_map* defines_map,
154 bool write_cfg = false,
155 bool write_plain_cfg = false,
156 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:157
Represents version numbers.
const config * cfg
Declarations for File-IO.
Interfaces for manipulating version numbers of engine, add-ons, etc.
std::unique_ptr< std::istream > scoped_istream
std::string lineno_string(const std::string &lineno)
std::ostream & operator<<(std::ostream &stream, const preproc_define &def)
filesystem::scoped_istream preprocess_string(const std::string &contents, const std::string &textdomain, preproc_map &defines)
Function to use the WML preprocessor on a string.
std::map< std::string, struct preproc_define > preproc_map
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map &defines)
Function to use the WML preprocessor on a file.
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.
std::string message
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