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