The Battle for Wesnoth  1.19.7+dev
type.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
3  by Sytyi Nick <nsytyi@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * This file contains object "type", which is used to store
19  * information about types while annotation parsing.
20  */
21 
22 #pragma once
23 
24 #include <boost/regex.hpp>
25 
26 class config;
28 
29 namespace schema_validation
30 {
31 
32 /**
33  * Stores information about a schema type.
34  * This is an abstract base class for several variants of schema type.
35  */
36 class wml_type {
37 protected:
38  std::string name_;
39 public:
40  wml_type() = delete;
41  explicit wml_type(const std::string& name) : name_(name) {}
42  using ptr = std::shared_ptr<wml_type>;
43  using map = std::map<std::string, ptr>;
44  virtual bool matches(const config_attribute_value& value, const map& type_map) const = 0;
45  static std::shared_ptr<wml_type> from_config(const config& cfg);
46  virtual ~wml_type() {}
47 };
48 
49 /**
50  * Stores information about a schema type.
51  * This type represents a simple pattern match.
52  */
53 class wml_type_simple : public wml_type {
54  boost::regex pattern_;
55  bool allow_translatable_ = false;
56 public:
57  wml_type_simple(const std::string& name, const std::string& pattern) : wml_type(name), pattern_(pattern) {}
58  bool matches(const config_attribute_value& value, const map& type_map) const override;
60 };
61 
62 /**
63  * Stores information about a schema type.
64  * This type represents a name alias for another type.
65  */
66 class wml_type_alias : public wml_type {
67  mutable std::shared_ptr<wml_type> cached_;
68  std::string link_;
69 public:
70  wml_type_alias(const std::string& name, const std::string& link) : wml_type(name), link_(link) {}
71  bool matches(const config_attribute_value& value, const map& type_map) const override;
72  const std::string& link() const {return link_;}
73 };
74 
75 /**
76  * Stores information about a schema type.
77  * This is an abstract base class for composite types.
78  */
79 class wml_type_composite : public wml_type {
80 protected:
81  std::vector<std::shared_ptr<wml_type>> subtypes_;
82 public:
83  explicit wml_type_composite(const std::string& name) : wml_type(name) {}
84  void add_type(std::shared_ptr<wml_type> type)
85  {
86  subtypes_.push_back(type);
87  }
88  const std::vector<std::shared_ptr<wml_type>>& subtypes() const {return subtypes_;}
89 };
90 
91 /**
92  * Stores information about a schema type.
93  * Represents a union type, which matches if any of its subtypes match.
94  */
96 public:
97  explicit wml_type_union(const std::string& name) : wml_type_composite(name) {}
98  bool matches(const config_attribute_value& value, const map& type_map) const override;
99 };
100 
101 /**
102  * Stores information about a schema type.
103  * Represents an intersection type, which matches if all of its subtypes match.
104  */
106 public:
107  explicit wml_type_intersection(const std::string& name) : wml_type_composite(name) {}
108  bool matches(const config_attribute_value& value, const map& type_map) const override;
109 };
110 
111 /**
112  * Stores information about a schema type.
113  * Represents a list type, where each list element is itself a union.
114  */
116  boost::regex split_;
117  int min_ = 0, max_ = -1;
118 public:
119  wml_type_list(const std::string& name, const std::string& pattern, int min, int max)
120  : wml_type_union(name)
121  , split_(pattern)
122  , min_(min)
123  , max_(max)
124  {}
125  bool matches(const config_attribute_value& value, const map& type_map) const override;
126 };
127 
128 class wml_type_tstring : public wml_type {
129 public:
130  wml_type_tstring() : wml_type("t_string") {}
131  bool matches(const config_attribute_value& value, const map& type_map) const override;
132 };
133 
134 }
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Stores information about a schema type.
Definition: type.hpp:66
wml_type_alias(const std::string &name, const std::string &link)
Definition: type.hpp:70
std::shared_ptr< wml_type > cached_
Definition: type.hpp:67
const std::string & link() const
Definition: type.hpp:72
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:93
Stores information about a schema type.
Definition: type.hpp:79
void add_type(std::shared_ptr< wml_type > type)
Definition: type.hpp:84
std::vector< std::shared_ptr< wml_type > > subtypes_
Definition: type.hpp:81
wml_type_composite(const std::string &name)
Definition: type.hpp:83
const std::vector< std::shared_ptr< wml_type > > & subtypes() const
Definition: type.hpp:88
Stores information about a schema type.
Definition: type.hpp:105
wml_type_intersection(const std::string &name)
Definition: type.hpp:107
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:116
Stores information about a schema type.
Definition: type.hpp:115
wml_type_list(const std::string &name, const std::string &pattern, int min, int max)
Definition: type.hpp:119
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:126
Stores information about a schema type.
Definition: type.hpp:53
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:86
wml_type_simple(const std::string &name, const std::string &pattern)
Definition: type.hpp:57
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:142
Stores information about a schema type.
Definition: type.hpp:95
bool matches(const config_attribute_value &value, const map &type_map) const override
Definition: type.cpp:106
wml_type_union(const std::string &name)
Definition: type.hpp:97
Stores information about a schema type.
Definition: type.hpp:36
virtual bool matches(const config_attribute_value &value, const map &type_map) const =0
wml_type(const std::string &name)
Definition: type.hpp:41
static std::shared_ptr< wml_type > from_config(const config &cfg)
Definition: type.cpp:53
std::map< std::string, ptr > map
Definition: type.hpp:43
std::shared_ptr< wml_type > ptr
Definition: type.hpp:42