The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tag.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Sytyi Nick <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 objects "tag" and "key", which are used to store
18  * information about tags and keys while annotation parsing.
19  */
20 
21 #ifndef TOOLS_SCHEMA_TAG_HPP_INCLUDED
22 #define TOOLS_SCHEMA_TAG_HPP_INCLUDED
23 
24 #include <algorithm>
25 #include <iostream>
26 #include <map>
27 #include <sstream>
28 #include <string>
29 
30 class config;
31 
32 namespace schema_validation{
33 /**
34  * class_key is used to save the information about one key.
35  * Key has next info: name, type, default value or key is mandatory.
36  */
37 class class_key{
38 public:
39  class_key():name_(""),type_(""),default_("\"\""),mandatory_(false)
40  { }
42  const std::string &type,
43  const std::string &def="\"\"")
44  : name_(name)
45  , type_(type)
46  , default_(def)
47  , mandatory_(def.empty())
48  {
49  }
50  class_key(const config&);
51 
52  const std::string & get_name() const{
53  return name_ ;
54  }
55  const std::string & get_type() const{
56  return type_ ;
57  }
58  const std::string & get_default() const{
59  return default_;
60  }
61  bool is_mandatory () const{
62  return mandatory_ ;
63  }
64 
65  void set_name(const std::string& name){
66  name_ = name;
67  }
68  void set_type(const std::string& type){
69  type_ = type;
70  }
71  void set_default(const std::string& def){
72  default_ = def;
73  if (def.empty()){
74  mandatory_ = true;
75  }
76  }
77  void set_mandatory(bool mandatory){
78  mandatory_ = mandatory;
79  }
80  /** is used to print key info
81  * the format is next
82  * [key]
83  * name="name"
84  * type="type"
85  * default="default"
86  * mandatory="true/false"
87  * [/key]
88  */
89  void print(std::ostream& os,int level) const;
90 
91  /**
92  *Compares keys by name. Used in std::sort, i.e.
93  */
94  bool operator < ( const class_key& k) const{
95  return (get_name() < k.get_name());
96  }
97 private:
98  /** Name of key*/
100  /** Type of key*/
102  /** Default value*/
104  /** Shows, if key is a mandatory key.*/
106 };
107 
108 /**
109  * Stores information about tag.
110  * Each tags is an element of great tag tree. This tree is close to filesystem:
111  * you can use links and special include directory global/
112  * Normally root is not mentioned in path.
113  * Each tag has name, minimum and maximum occasions number,
114  * and lists of subtags, keys and links.
115  */
116 class class_tag{
117 public:
118  typedef std::map<std::string,class_tag> tag_map;
119  typedef std::pair<std::string,class_tag> tag_map_value;
120 
121  typedef std::map<std::string,class_key> key_map;
122  typedef std::pair<std::string,class_key> key_map_value;
123 
124  typedef std::map<std::string,std::string> link_map;
125  typedef std::pair<std::string,std::string> link_map_value;
126 
128  typedef std::pair<key_iterator,key_iterator> all_key_iterators;
129 
130  typedef key_map::const_iterator const_key_iterator;
131  typedef std::pair<const_key_iterator,const_key_iterator>
133 
135  typedef std::pair<tag_iterator,tag_iterator> all_tag_iterators;
136 
137  typedef tag_map::const_iterator const_tag_iterator;
138  typedef std::pair<const_tag_iterator,const_tag_iterator>
140 
142  typedef std::pair<link_iterator,link_iterator> all_link_iterators;
143 
144  typedef link_map::const_iterator const_link_iterator;
145  typedef std::pair<const_link_iterator,const_link_iterator>
147 
149  : name_("")
150  , min_(0)
151  , max_(0)
152  , super_("")
153  , tags_()
154  , keys_()
155  , links_()
156  {
157  }
158 
160  int min,
161  int max,
162  const std::string & super=""
163  )
164  : name_(name)
165  , min_(min)
166  , max_(max)
167  , super_(super)
168  , tags_()
169  , keys_()
170  , links_()
171  {
172  }
173  class_tag(const config&);
175 
176  /** Prints information about tag to outputstream, recursively
177  * is used to print tag info
178  * the format is next
179  * [tag]
180  * subtags
181  * keys
182  * name="name"
183  * min="min"
184  * max="max"
185  * [/tag]
186  */
187  void print(std::ostream& os);
188 
189  const std::string & get_name() const{
190  return name_ ;
191  }
192  int get_min() const{
193  return min_;
194  }
195  int get_max() const{
196  return max_;
197  }
198  const std::string & get_super() const{
199  return super_ ;
200  }
201  bool is_extension() const{
202  return ! super_.empty();
203  }
204 
205  void set_name(const std::string& name){
206  name_ = name;
207  }
208  void set_min(int o){
209  min_ = o;
210  }
211  void set_max(int o){
212  max_ = o;
213  }
214  void set_min( std::string const& s){
215  std::istringstream i(s);
216  if (!(i >> min_)){
217  min_ = 0;
218  }
219  }
220  void set_max( std::string const & s){
221  std::istringstream i(s);
222  if (!(i >> max_)){
223  max_ = 0;
224  }
225  }
226  void set_super(std::string const & s){
227  super_= s;
228  }
229  void add_key(const class_key& new_key){
230  keys_.insert(key_map_value(new_key.get_name(),new_key));
231  }
232  void add_tag(const class_tag& new_tag){
233  tags_.insert(tag_map_value(new_tag.name_,new_tag));
234  }
235  void add_link(const std::string & link);
236 
237  /**
238  * Tags are usually organized in tree.
239  * This fuction helps to add tag to his exact place in tree
240  * @param path - path in subtree to exact place of tag
241  * @param tag - tag to add
242  * @param root - root of schema tree - use to support of adding to link.
243  * Path is getting shotter and shoter with each call.
244  * Path schould look like tag1/tag2/parent/ Slash at end is mandatory.
245  */
246  void add_tag (const std::string & path,const class_tag & tag,
247  class_tag &root);
248 
249  bool operator < ( const class_tag& t) const{
250  return name_ < t.name_;
251  }
252  bool operator == (const class_tag & other) const {
253  return name_ == other.name_;
254  }
255  /**
256  * Returns pointer to child key
257  */
258  const class_key * find_key(const std::string & name) const;
259  /**
260  * Returns pointer to child link
261  */
262  const std::string * find_link(const std::string & name) const;
263 
264  /**
265  * Returns pointer to tag using full path to it.
266  * Also work with links
267  */
268  const class_tag * find_tag(const std::string & fullpath,
269  const class_tag & root) const;
270  /**
271  * Calls the expansion on each child
272  */
273  void expand_all(class_tag &root);
274 
276  return all_const_tag_iterators(tags_.begin(),tags_.end());
277  }
279  return all_const_key_iterators(keys_.begin(),keys_.end());
280  }
282  return all_const_link_iterators(links_.begin(),links_.end());
283  }
284 
286  keys_.erase (name);
287  }
288  /** Removes all keys with this type. Works recursively */
289  void remove_keys_by_type(const std::string &type);
290 
291 #ifdef _MSC_VER
292  // MSVC throws an error if this method is private.
293  // so, define it as public to prevent that. The error is:
294  // error C2248: 'schema_validation::class_tag::find_tag' : cannot
295  // access private member declared in class 'schema_validation::class_tag'
296 
297  class_tag * find_tag(const std::string & fullpath,
298  class_tag & root) ;
299 #endif
300 
301 // class_tag & operator= (class_tag const& );
302 
303 private:
304  /** name of tag*/
306  /** number of minimum occasions*/
307  int min_;
308  /** number of maximum occasions*/
309  int max_;
310  /**
311  * name of tag to extend "super-tag"
312  * Extension is smth like inheritance and is used in case
313  * when you need to use another tag with all his
314  * keys, childs, etc. But you also want to allow extra subtags of that tags,
315  * so just linking that tag wouldn't help at all.
316  */
318  /** children tags*/
319  tag_map tags_;
320  /** keys*/
321  key_map keys_;
322  /** links to possible children. */
323  link_map links_;
324  /**
325  * the same as class_tag::print(std::ostream&)
326  * but indents different levels with step space.
327  * @param os stream to print
328  * @param level current level of indentation
329  * @param step step to next indent
330  */
331  void printl(std::ostream &os,int level, int step = 4);
332 
333 #ifndef _MSC_VER
334  // this is complementary with the above #ifdef for the MSVC error
335  class_tag * find_tag(const std::string & fullpath,
336  class_tag & root) ;
337 #endif
338 
339  void add_tags (const tag_map & list){
340  tags_.insert(list.begin(),list.end());
341  }
342  void add_keys (const key_map & list){
343  keys_.insert(list.begin(),list.end());
344  }
345  void add_links (const link_map & list){
346  links_.insert(list.begin(),list.end());
347  }
348  /**
349  * Copies tags, keys and links of tag to this
350  */
351  void append_super(const class_tag & tag,const std::string & super);
352  /**
353  * Expands all "super" copying their data to this.
354  */
355  void expand(class_tag & root);
356 };
357 
358 }
359 #endif // TOOLS_SCHEMA_TAG_HPP_INCLUDED
void remove_keys_by_type(const std::string &type)
Removes all keys with this type.
Definition: tag.cpp:158
std::map< std::string, class_tag > tag_map
Definition: tag.hpp:118
bool is_mandatory() const
Definition: tag.hpp:61
std::string name_
Name of key.
Definition: tag.hpp:99
const std::string & get_super() const
Definition: tag.hpp:198
bool operator<(const class_tag &t) const
Definition: tag.hpp:249
void expand(class_tag &root)
Expands all "super" copying their data to this.
Definition: tag.cpp:313
std::pair< std::string, class_key > key_map_value
Definition: tag.hpp:122
const class_tag * find_tag(const std::string &fullpath, const class_tag &root) const
Returns pointer to tag using full path to it.
Definition: tag.cpp:124
const class_key * find_key(const std::string &name) const
Returns pointer to child key.
Definition: tag.cpp:108
std::pair< std::string, class_tag > tag_map_value
Definition: tag.hpp:119
GLint level
Definition: glew.h:1220
void set_max(std::string const &s)
Definition: tag.hpp:220
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
const std::string * find_link(const std::string &name) const
Returns pointer to child link.
Definition: tag.cpp:116
int max_
number of maximum occasions
Definition: tag.hpp:309
tag_map::iterator tag_iterator
Definition: tag.hpp:134
void remove_key_by_name(const std::string &name)
Definition: tag.hpp:285
void set_min(std::string const &s)
Definition: tag.hpp:214
void add_key(const class_key &new_key)
Definition: tag.hpp:229
bool operator<(const class_key &k) const
Compares keys by name.
Definition: tag.hpp:94
const std::string & get_name() const
Definition: tag.hpp:189
std::pair< const_link_iterator, const_link_iterator > all_const_link_iterators
Definition: tag.hpp:146
key_map::const_iterator const_key_iterator
Definition: tag.hpp:130
void append_super(const class_tag &tag, const std::string &super)
Copies tags, keys and links of tag to this.
Definition: tag.cpp:303
int min_
number of minimum occasions
Definition: tag.hpp:307
const std::string & get_name() const
Definition: tag.hpp:52
std::string default_
Default value.
Definition: tag.hpp:103
void add_tags(const tag_map &list)
Definition: tag.hpp:339
void set_name(const std::string &name)
Definition: tag.hpp:205
link_map::iterator link_iterator
Definition: tag.hpp:141
void set_name(const std::string &name)
Definition: tag.hpp:65
std::string type_
Type of key.
Definition: tag.hpp:101
GLdouble GLdouble t
Definition: glew.h:1366
std::pair< const_key_iterator, const_key_iterator > all_const_key_iterators
Definition: tag.hpp:132
class_key is used to save the information about one key.
Definition: tag.hpp:37
void print(std::ostream &os, int level) const
is used to print key info the format is next [key] name="name" type="type" default="default" mandator...
Definition: tag.cpp:53
void expand_all(class_tag &root)
Calls the expansion on each child.
Definition: tag.cpp:152
void set_type(const std::string &type)
Definition: tag.hpp:68
std::string super_
name of tag to extend "super-tag" Extension is smth like inheritance and is used in case when you nee...
Definition: tag.hpp:317
GLsizei const char ** path
Definition: glew.h:4654
all_const_key_iterators keys() const
Definition: tag.hpp:278
void add_keys(const key_map &list)
Definition: tag.hpp:342
class_key(const std::string &name, const std::string &type, const std::string &def="\"\"")
Definition: tag.hpp:41
void set_default(const std::string &def)
Definition: tag.hpp:71
std::pair< tag_iterator, tag_iterator > all_tag_iterators
Definition: tag.hpp:135
void printl(std::ostream &os, int level, int step=4)
the same as class_tag::print(std::ostream&) but indents different levels with step space...
Definition: tag.cpp:197
void set_super(std::string const &s)
Definition: tag.hpp:226
bool is_extension() const
Definition: tag.hpp:201
void set_mandatory(bool mandatory)
Definition: tag.hpp:77
const std::string & get_default() const
Definition: tag.hpp:58
void add_links(const link_map &list)
Definition: tag.hpp:345
int get_max() const
Definition: tag.hpp:195
std::pair< link_iterator, link_iterator > all_link_iterators
Definition: tag.hpp:142
std::map< std::string, class_key > key_map
Definition: tag.hpp:121
void add_link(const std::string &link)
Definition: tag.cpp:101
std::pair< key_iterator, key_iterator > all_key_iterators
Definition: tag.hpp:128
all_const_link_iterators links() const
Definition: tag.hpp:281
size_t i
Definition: function.cpp:1057
all_const_tag_iterators tags() const
Definition: tag.hpp:275
link_map links_
links to possible children.
Definition: tag.hpp:323
link_map::const_iterator const_link_iterator
Definition: tag.hpp:144
tag_map tags_
children tags
Definition: tag.hpp:319
class_tag(const std::string &name, int min, int max, const std::string &super="")
Definition: tag.hpp:159
std::map< std::string, std::string > link_map
Definition: tag.hpp:124
GLuint const GLchar * name
Definition: glew.h:1782
void add_tag(const class_tag &new_tag)
Definition: tag.hpp:232
std::string name_
name of tag
Definition: tag.hpp:305
int get_min() const
Definition: tag.hpp:192
std::pair< const_tag_iterator, const_tag_iterator > all_const_tag_iterators
Definition: tag.hpp:139
bool mandatory_
Shows, if key is a mandatory key.
Definition: tag.hpp:105
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
key_map::iterator key_iterator
Definition: tag.hpp:127
GLdouble s
Definition: glew.h:1358
std::pair< std::string, std::string > link_map_value
Definition: tag.hpp:125
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Stores information about tag.
Definition: tag.hpp:116
void print(std::ostream &os)
Prints information about tag to outputstream, recursively is used to print tag info the format is nex...
Definition: tag.cpp:97
tag_map::const_iterator const_tag_iterator
Definition: tag.hpp:137
bool operator==(const class_tag &other) const
Definition: tag.hpp:252
const std::string & get_type() const
Definition: tag.hpp:55