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