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