SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
PropertiesParser.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_core_config_PropertiesParser_h
8 #define swcdb_core_config_PropertiesParser_h
9 
10 
12 
13 
14 namespace SWC { namespace Config {
15 
16 
17 /* cfg methods for types
18 * @param v The default Value and a Type
19 */
20 Property::Value_bool::Ptr boo(const bool& v);
21 Property::Value_uint8::Ptr i8(const uint8_t& v);
22 Property::Value_uint16::Ptr i16(const uint16_t& v);
23 Property::Value_int32::Ptr i32(const int32_t& v);
24 Property::Value_int64::Ptr i64(const int64_t& v);
25 Property::Value_double::Ptr f64(const double& v);
26 Property::Value_string::Ptr str(std::string&& v);
30 
31 /* cfg methods for guarded types
32 * @param v The default Value and a Type
33 */
34 Property::Value_bool_g::Ptr g_boo(const bool& v);
35 Property::Value_uint8_g::Ptr g_i8(const uint8_t& v);
36 Property::Value_uint16_g::Ptr g_i16(const uint16_t& v);
37 Property::Value_int32_g::Ptr g_i32(const int32_t& v);
38 Property::Value_uint64_g::Ptr g_i64(const uint64_t& v);
41  const int32_t& v,
45 
46 /* cfg methods for types, a skippable option
47 * if no option parsed it is skipped
48 */
59 
60 
61 
62 
63 
64 class ParserConfig final {
65 
66  struct ParserOpt final {
69  std::string desc;
70 
71  ParserOpt() noexcept : value(nullptr), aliases(), desc() { }
72 
73  ParserOpt(ParserOpt&& other) noexcept
74  : value(other.value),
75  aliases(std::move(other.aliases)),
76  desc(std::move(other.desc)) {
77  other.value = nullptr;
78  }
79 
80  ParserOpt(const ParserOpt& other)
81  : value(other.value),
82  aliases(other.aliases),
83  desc(other.desc) {
84  }
85 
86  ParserOpt& operator=(const ParserOpt&) = delete;
87 
89  value = other.value;
90  other.value = nullptr;
91  aliases = std::move(other.aliases);
92  desc = std::move(other.desc);
93  return *this;
94  }
95 
96  ~ParserOpt() noexcept { }
97  };
98 
99  typedef std::map<std::string, ParserOpt> Map;
101 
102  public:
103 
104  std::string usage;
107  bool own;
108 
109  SWC_SHOULD_NOT_INLINE
110  explicit ParserConfig(bool own) noexcept;
111 
113  ParserConfig(const ParserConfig&) = delete;
116 
117  ~ParserConfig() noexcept;
118 
119  void free() noexcept;
120 
121  ParserConfig& definition(const char* u);
122 
123  ParserConfig& definition(std::string&& u);
124 
125  /* populate from other Parser Config */
126  ParserConfig& add(const ParserConfig& other_cfg);
127 
128  /* Method to add option */
129  ParserConfig& add(const char* names, Property::Value::Ptr vptr,
130  const char* description);
131 
132  ParserConfig& operator()(const char* name, Property::Value::Ptr vptr,
133  const char*description);
134 
136 
137  ParserConfig& add_options(const char* name, Property::Value::Ptr vptr,
138  const char* description);
139 
140  ParserConfig& add(const char* name, const char* description);
141 
142  ParserConfig& operator()(const char* name, const char* description);
143 
144  /* Method to add_pos option */
145  ParserConfig& add_pos(const char* s, int pos);
146 
147  ParserConfig& operator()(const char* s, int pos);
148 
149  std::string position_name(int n);
150 
151  bool SWC_PURE_FUNC has(const std::string& name) const noexcept;
152 
153  bool has(const std::string& name, std::string& alias_to) const noexcept;
154 
155  Property::Value::Ptr get_default(const std::string& name);
156 
157  void remove(const std::string& name);
158 
159  void print(std::ostream& os) const;
160 
161 };
162 
163 std::ostream& operator<<(std::ostream& os, const ParserConfig& cfg);
164 
165 
166 
167 
168 class Parser final {
169  public:
170 
172 
173  static Strings args_to_strings(int argc, char *argv[]);
174 
175  class Options final {
176  public:
177  std::map<std::string, Property::Value::Ptr> map;
178 
179  Options() noexcept;
180 
181  Options& operator=(Options&&) noexcept;
182 
183  Options(Options&&) = delete;
184  Options(const Options&) = delete;
185  Options& operator=(const Options&) = delete;
186 
187  ~Options() noexcept;
188 
189  };
190 
191  SWC_SHOULD_NOT_INLINE
192  explicit Parser(bool unregistered=false) noexcept;
193 
194  Parser(Parser&&) = delete;
195  Parser(const Parser&) = delete;
196  Parser& operator=(Parser&&) = delete;
197  Parser& operator=(const Parser&) = delete;
198 
199  ~Parser() noexcept;
200 
201  void free() noexcept;
202 
203  void parse_filedata(std::ifstream& in);
204 
205  void parse_cmdline(int argc, char *argv[]);
206 
207  void parse_cmdline(const Strings& raw_strings);
208 
209  void parse_line(const std::string& line);
210 
211  void set_pos_parse(const std::string& name, const std::string& value);
212 
213  bool parse_opt(const std::string& s);
214 
215  void make_options();
216 
217  // convert, validate and add property to options
218  void add_opt(const std::string& name, Property::Value::Ptr p,
219  const Strings& raw_opt);
220 
221  void own_options(Options& opts);
222 
223  const Options& SWC_CONST_FUNC get_options() const noexcept;
224 
225  void print(std::ostream& os) const;
226 
227  void print_options(std::ostream& os) const;
228 
229  private:
230  typedef std::pair<std::string, Strings> Pair;
231  typedef std::map<std::string, Strings> Map;
232 
233  Map raw_opts;
234  bool m_unregistered;
235  Options m_opts;
236 
237 };
238 
239 std::ostream& operator<<(std::ostream& os, const Parser& prs);
240 
241 
242 }} // namespace SWC::Config
243 
244 
245 #ifdef SWC_IMPL_SOURCE
247 #endif
248 
249 #endif // swcdb_core_config_PropertiesParser_h
SWC::Config::i32
Property::Value_int32::Ptr i32(const int32_t &v)
Definition: PropertiesParser.cc:33
SWC::Config::Parser
Definition: PropertiesParser.h:168
SWC::Config::ParserConfig::print
void print(std::ostream &os) const
Definition: PropertiesParser.cc:312
SWC::Config::ParserConfig::ParserOpt::desc
std::string desc
Definition: PropertiesParser.h:69
SWC::Config::g_i32
Property::Value_int32_g::Ptr g_i32(const int32_t &v)
Definition: PropertiesParser.cc:77
SWC::Config::ParserConfig::add
ParserConfig & add(const ParserConfig &other_cfg)
Definition: PropertiesParser.cc:175
SWC::Config::Property::Value_uint8_g::Ptr
Value_uint8_g * Ptr
Definition: Property.h:515
SWC::Config::ParserConfig::free
void free() noexcept
Definition: PropertiesParser.cc:155
SWC::Config::g_strs
Property::Value_strings_g::Ptr g_strs(Strings &&v)
Definition: PropertiesParser.cc:85
SWC::Config::g_enum
Property::Value_enum_g::Ptr g_enum(const int32_t &v, Property::Value_enum_g::OnChg_t &&cb, Property::Value_enum_g::FromString_t &&from_string, Property::Value_enum_g::Repr_t &&repr)
Definition: PropertiesParser.cc:89
SWC::Config::g_boo
Property::Value_bool_g::Ptr g_boo(const bool &v)
Definition: PropertiesParser.cc:65
SWC::Config::f64s
Property::Value_doubles::Ptr f64s(Doubles &&v)
Definition: PropertiesParser.cc:57
SWC::Config::i8
Property::Value_uint8::Ptr i8(const uint8_t &v)
Definition: PropertiesParser.cc:25
SWC::Config::Doubles
Core::Vector< double > Doubles
Definition: Property.h:19
SWC::Config::ParserConfig::ParserOpt::operator=
ParserOpt & operator=(const ParserOpt &)=delete
SWC::Config::ParserConfig::ParserOpt::ParserOpt
ParserOpt(ParserOpt &&other) noexcept
Definition: PropertiesParser.h:73
SWC::Config::Strings
Core::Vector< std::string > Strings
Definition: Property.h:17
SWC::Config::ParserConfig::has
bool SWC_PURE_FUNC has(const std::string &name) const noexcept
Definition: PropertiesParser.cc:261
SWC::Config::ParserConfig::Positions
Core::Vector< std::pair< int, std::string > > Positions
Definition: PropertiesParser.h:100
SWC::Config::Parser::Pair
std::pair< std::string, Strings > Pair
Definition: PropertiesParser.h:230
SWC::Config::Property::Value_uint16_g::Ptr
Value_uint16_g * Ptr
Definition: Property.h:552
SWC::Config::boo
Property::Value_bool::Ptr boo(const bool &v)
Definition: PropertiesParser.cc:21
SWC::Config::ParserConfig::positions
Positions positions
Definition: PropertiesParser.h:105
PropertiesParser.cc
SWC::Config::ParserConfig::position_name
std::string position_name(int n)
Definition: PropertiesParser.cc:252
SWC::Config::ParserConfig::remove
void remove(const std::string &name)
Definition: PropertiesParser.cc:303
SWC::Config::Parser::Map
std::map< std::string, Strings > Map
Definition: PropertiesParser.h:231
SWC::Config::Property::Value::Ptr
Value * Ptr
Definition: Property.h:68
SWC::Config::Parser::config
ParserConfig config
Definition: PropertiesParser.h:171
SWC::Config::i16
Property::Value_uint16::Ptr i16(const uint16_t &v)
Definition: PropertiesParser.cc:29
SWC::Config::Property::Value_uint64_g::Ptr
Value_uint64_g * Ptr
Definition: Property.h:628
SWC_CONST_FUNC
#define SWC_CONST_FUNC
Definition: Compat.h:107
SWC::Config::i64s
Property::Value_int64s::Ptr i64s(Int64s &&v)
Definition: PropertiesParser.cc:53
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
Property.h
SWC::Config::Property::Value_strings_g::Ptr
Value_strings_g * Ptr
Definition: Property.h:715
SWC::Config::ParserConfig::add_pos
ParserConfig & add_pos(const char *s, int pos)
Definition: PropertiesParser.cc:243
SWC_PURE_FUNC
#define SWC_PURE_FUNC
Definition: Compat.h:108
SWC::Config::ParserConfig::~ParserConfig
~ParserConfig() noexcept
Definition: PropertiesParser.cc:151
SWC::Config::Parser::Options::map
std::map< std::string, Property::Value::Ptr > map
Definition: PropertiesParser.h:177
SWC::Config::ParserConfig::ParserOpt::aliases
Strings aliases
Definition: PropertiesParser.h:68
SWC::Config::strs
Property::Value_strings::Ptr strs(Strings &&v)
Definition: PropertiesParser.cc:49
SWC::Config::ParserConfig::add_options
ParserConfig &SWC_CONST_FUNC add_options()
Definition: PropertiesParser.cc:223
SWC::Config::str
Property::Value_string::Ptr str(std::string &&v)
Definition: PropertiesParser.cc:45
SWC::Config::ParserConfig::ParserConfig
ParserConfig(const ParserConfig &)=delete
SWC::Config::Property::from_string
void from_string(const char *s, double *value)
Definition: Property.cc:109
SWC::Config::ParserConfig::usage
std::string usage
Definition: PropertiesParser.h:104
SWC::Config::ParserConfig
Definition: PropertiesParser.h:64
SWC::Config::Property::Value_bool_g::Ptr
Value_bool_g * Ptr
Definition: Property.h:476
SWC::Config::ParserConfig::Map
std::map< std::string, ParserOpt > Map
Definition: PropertiesParser.h:99
SWC::Config::ParserConfig::operator=
ParserConfig & operator=(ParserConfig &&)=delete
SWC::Config::Property::Value
Definition: Property.h:35
SWC::Config::Int64s
Core::Vector< int64_t > Int64s
Definition: Property.h:18
SWC::Config::Parser::Options
Definition: PropertiesParser.h:175
SWC::Config::ParserConfig::ParserOpt::~ParserOpt
~ParserOpt() noexcept
Definition: PropertiesParser.h:96
SWC::Config::ParserConfig::ParserOpt::value
Property::Value::Ptr value
Definition: PropertiesParser.h:67
SWC::Core::Vector< std::string >
SWC::Config::Property::Value_enum_g::Repr_t
Value_enum::Repr_t Repr_t
Definition: Property.h:669
SWC::Config::g_i16
Property::Value_uint16_g::Ptr g_i16(const uint16_t &v)
Definition: PropertiesParser.cc:73
SWC::Config::i64
Property::Value_int64::Ptr i64(const int64_t &v)
Definition: PropertiesParser.cc:37
SWC::Config::ParserConfig::ParserOpt::ParserOpt
ParserOpt() noexcept
Definition: PropertiesParser.h:71
SWC::Config::ParserConfig::ParserConfig
ParserConfig(ParserConfig &&)=delete
SWC::Config::ParserConfig::options
Map options
Definition: PropertiesParser.h:106
SWC::Config::ParserConfig::ParserOpt::ParserOpt
ParserOpt(const ParserOpt &other)
Definition: PropertiesParser.h:80
SWC::Config::Property::Value_enum_g::OnChg_t
std::function< void(int32_t)> OnChg_t
Definition: Property.h:667
SWC::Config::ParserConfig::ParserOpt
Definition: PropertiesParser.h:66
SWC::Config::ParserConfig::own
bool own
Definition: PropertiesParser.h:107
SWC::Config::g_i64
Property::Value_uint64_g::Ptr g_i64(const uint64_t &v)
Definition: PropertiesParser.cc:81
SWC::Config::g_i8
Property::Value_uint8_g::Ptr g_i8(const uint8_t &v)
Definition: PropertiesParser.cc:69
SWC::Config::Property::Value_int32_g::Ptr
Value_int32_g * Ptr
Definition: Property.h:590
SWC::Config::Property::Value_enum_g::FromString_t
Value_enum::FromString_t FromString_t
Definition: Property.h:668
SWC::Config::ParserConfig::get_default
Property::Value::Ptr get_default(const std::string &name)
Definition: PropertiesParser.cc:290
SWC::Config::ParserConfig::ParserConfig
SWC_SHOULD_NOT_INLINE ParserConfig(bool own) noexcept
Definition: PropertiesParser.cc:146
SWC::Config::ParserConfig::ParserOpt::operator=
ParserOpt & operator=(ParserOpt &&other)
Definition: PropertiesParser.h:88
SWC::Config::Property::Value_enum_g::Ptr
Value_enum_g * Ptr
Definition: Property.h:666
SWC::Config::f64
Property::Value_double::Ptr f64(const double &v)
Definition: PropertiesParser.cc:41
SWC::Config::ParserConfig::operator=
ParserConfig & operator=(const ParserConfig &)=delete
SWC::Config::ParserConfig::definition
ParserConfig & definition(const char *u)
Definition: PropertiesParser.cc:163