The Battle for Wesnoth  1.15.2+dev
key.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 "key", which is used to store
18  * information about keys while annotation parsing.
19  */
20 
21 #pragma once
22 
23 #include <iosfwd>
24 #include <string>
25 
26 class config;
27 
28 namespace schema_validation
29 {
30 
31 /**
32  * wml_key is used to save the information about one key.
33  * Key has next info: name, type, default value or key is mandatory.
34  */
35 class wml_key
36 {
37 public:
39  : name_("")
40  , type_("")
41  , default_("\"\"")
42  , mandatory_(false)
43  , fuzzy_(false)
44  {
45  }
46 
47  wml_key(const std::string& name, const std::string& type, const std::string& def = "\"\"")
48  : name_(name)
49  , type_(type)
50  , default_(def)
51  , mandatory_(def.empty())
52  , fuzzy_(name.find_first_of("*?") != std::string::npos)
53  {
54  }
55 
56  wml_key(const config&);
57 
58  const std::string& get_name() const
59  {
60  return name_;
61  }
62 
63  const std::string& get_type() const
64  {
65  return type_;
66  }
67 
68  const std::string& get_default() const
69  {
70  return default_;
71  }
72 
73  bool is_mandatory() const
74  {
75  return mandatory_;
76  }
77 
78  bool is_fuzzy() const {
79  return fuzzy_;
80  }
81 
82  void set_name(const std::string& name)
83  {
84  name_ = name;
85  }
86 
87  void set_type(const std::string& type)
88  {
89  type_ = type;
90  }
91 
92  void set_default(const std::string& def)
93  {
94  default_ = def;
95  if(def.empty()) {
96  mandatory_ = true;
97  }
98  }
99 
100  void set_mandatory(bool mandatory)
101  {
102  mandatory_ = mandatory;
103  }
104 
105  void set_fuzzy(bool f)
106  {
107  fuzzy_ = f;
108  }
109 
110  /** is used to print key info
111  * the format is next
112  * [key]
113  * name="name"
114  * type="type"
115  * default="default"
116  * mandatory="true/false"
117  * [/key]
118  */
119  void print(std::ostream& os, int level) const;
120 
121  /** Compares keys by name. Used in std::sort, i.e. */
122  bool operator<(const wml_key& k) const
123  {
124  return (get_name() < k.get_name());
125  }
126 
127 private:
128  /** Name of key. */
129  std::string name_;
130 
131  /** Type of key. */
132  std::string type_;
133 
134  /** Default value. */
135  std::string default_;
136 
137  /** Shows, if key is a mandatory key. */
139 
140  /** Whether the key is a fuzzy match. */
141  bool fuzzy_;
142 };
143 }
bool is_mandatory() const
Definition: key.hpp:73
STL namespace.
const std::string & get_name() const
Definition: key.hpp:58
std::string default_
Default value.
Definition: key.hpp:135
void set_type(const std::string &type)
Definition: key.hpp:87
void set_fuzzy(bool f)
Definition: key.hpp:105
bool mandatory_
Shows, if key is a mandatory key.
Definition: key.hpp:138
const std::string & get_type() const
Definition: key.hpp:63
std::string name_
Name of key.
Definition: key.hpp:129
wml_key is used to save the information about one key.
Definition: key.hpp:35
std::string type_
Type of key.
Definition: key.hpp:132
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: key.cpp:43
void set_default(const std::string &def)
Definition: key.hpp:92
wml_key(const std::string &name, const std::string &type, const std::string &def="\")
Definition: key.hpp:47
bool fuzzy_
Whether the key is a fuzzy match.
Definition: key.hpp:141
bool is_fuzzy() const
Definition: key.hpp:78
#define f
void set_mandatory(bool mandatory)
Definition: key.hpp:100
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
const std::string & get_default() const
Definition: key.hpp:68
void set_name(const std::string &name)
Definition: key.hpp:82
bool operator<(const wml_key &k) const
Compares keys by name.
Definition: key.hpp:122