The Battle for Wesnoth  1.17.0-dev
type.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
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;
59  void allow_translatable() {allow_translatable_ = true;}
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 };
73 
74 /**
75  * Stores information about a schema type.
76  * This is an abstract base class for composite types.
77  */
78 class wml_type_composite : public wml_type {
79 protected:
80  std::vector<std::shared_ptr<wml_type>> subtypes_;
81 public:
82  explicit wml_type_composite(const std::string& name) : wml_type(name) {}
83  void add_type(std::shared_ptr<wml_type> type)
84  {
85  subtypes_.push_back(type);
86  }
87 };
88 
89 /**
90  * Stores information about a schema type.
91  * Represents a union type, which matches if any of its subtypes match.
92  */
94 public:
95  explicit wml_type_union(const std::string& name) : wml_type_composite(name) {}
96  bool matches(const config_attribute_value& value, const map& type_map) const override;
97 };
98 
99 /**
100  * Stores information about a schema type.
101  * Represents an intersection type, which matches if all of its subtypes match.
102  */
104 public:
105  explicit wml_type_intersection(const std::string& name) : wml_type_composite(name) {}
106  bool matches(const config_attribute_value& value, const map& type_map) const override;
107 };
108 
109 /**
110  * Stores information about a schema type.
111  * Represents a list type, where each list element is itself a union.
112  */
114  boost::regex split_;
115  int min_ = 0, max_ = -1;
116 public:
117  wml_type_list(const std::string& name, const std::string& pattern, int min, int max)
118  : wml_type_union(name)
119  , split_(pattern)
120  , min_(min)
121  , max_(max)
122  {}
123  bool matches(const config_attribute_value& value, const map& type_map) const override;
124 };
125 
126 class wml_type_tstring : public wml_type {
127 public:
128  wml_type_tstring() : wml_type("t_string") {}
129  bool matches(const config_attribute_value& value, const map& type_map) const override;
130 };
131 
132 }
wml_type_union(const std::string &name)
Definition: type.hpp:95
wml_type_alias(const std::string &name, const std::string &link)
Definition: type.hpp:70
Stores information about a schema type.
Definition: type.hpp:93
Variant for storing WML attributes.
Stores information about a schema type.
Definition: type.hpp:36
std::map< std::string, ptr > map
Definition: type.hpp:43
wml_type_list(const std::string &name, const std::string &pattern, int min, int max)
Definition: type.hpp:117
Stores information about a schema type.
Definition: type.hpp:53
Stores information about a schema type.
Definition: type.hpp:113
Stores information about a schema type.
Definition: type.hpp:103
virtual bool matches(const config_attribute_value &value, const map &type_map) const =0
wml_type_composite(const std::string &name)
Definition: type.hpp:82
static std::shared_ptr< wml_type > from_config(const config &cfg)
Definition: type.cpp:52
Stores information about a schema type.
Definition: type.hpp:66
wml_type_simple(const std::string &name, const std::string &pattern)
Definition: type.hpp:57
std::shared_ptr< wml_type > cached_
Definition: type.hpp:67
std::shared_ptr< wml_type > ptr
Definition: type.hpp:42
wml_type_intersection(const std::string &name)
Definition: type.hpp:105
std::vector< std::shared_ptr< wml_type > > subtypes_
Definition: type.hpp:80
wml_type(const std::string &name)
Definition: type.hpp:41
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
Stores information about a schema type.
Definition: type.hpp:78
void add_type(std::shared_ptr< wml_type > type)
Definition: type.hpp:83