Kea  2.1.7-git
lib/cc/simple_parser.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef SIMPLE_PARSER_H
8 #define SIMPLE_PARSER_H
9 
10 #include <asiolink/io_address.h>
11 #include <cc/data.h>
12 #include <cc/dhcp_config_error.h>
13 #include <util/triplet.h>
14 #include <map>
15 #include <vector>
16 #include <string>
17 #include <stdint.h>
18 #include <limits>
19 
20 namespace isc {
21 namespace data {
22 
24 struct SimpleDefault {
25  SimpleDefault(const char* name, isc::data::Element::types type, const char* value)
26  :name_(name), type_(type), value_(value) {}
27  std::string name_;
29  const char* value_;
30 };
31 
33 typedef std::vector<std::string> SimpleRequiredKeywords;
34 
36 typedef std::map<std::string, isc::data::Element::types> SimpleKeywords;
37 
39 typedef std::vector<SimpleDefault> SimpleDefaults;
40 
43 typedef std::vector<std::string> ParamsList;
44 
45 
68 class SimpleParser {
69 public:
70 
79  static void checkRequired(const SimpleRequiredKeywords& required,
81 
92  static void checkKeywords(const SimpleKeywords& keywords,
94 
108  static size_t deriveParams(isc::data::ConstElementPtr parent,
109  isc::data::ElementPtr child,
110  const ParamsList& params);
111 
123  static size_t setDefaults(isc::data::ElementPtr scope,
124  const SimpleDefaults& default_values);
125 
135  static size_t setListDefaults(isc::data::ConstElementPtr list,
136  const SimpleDefaults& default_values);
137 
147  static const data::Element::Position&
148  getPosition(const std::string& name, const data::ConstElementPtr parent);
149 
159  static std::string getString(isc::data::ConstElementPtr scope,
160  const std::string& name);
161 
171  static int64_t getInteger(isc::data::ConstElementPtr scope,
172  const std::string& name);
173 
187  static int64_t getInteger(isc::data::ConstElementPtr scope,
188  const std::string& name,
189  int64_t min, int64_t max);
190 
200  static bool getBoolean(isc::data::ConstElementPtr scope,
201  const std::string& name);
202 
203 
215  getAddress(const ConstElementPtr& scope, const std::string& name);
216 
226  static double getDouble(const ConstElementPtr& scope,
227  const std::string& name);
228 
229 protected:
230 
241  template <typename int_type> int_type
243  const std::string& name) {
244  int64_t val_int = getInteger(scope, name);
245  if ((val_int < std::numeric_limits<int_type>::min()) ||
246  (val_int > std::numeric_limits<int_type>::max())) {
248  "out of range value (" << val_int
249  << ") specified for parameter '" << name
250  << "' (" << getPosition(name, scope) << ")");
251  }
252  return (static_cast<int_type>(val_int));
253  }
254 
267  template <typename target_type,
268  target_type convert(const std::string&)> target_type
270  const std::string& name,
271  const std::string& type_name) {
272  std::string str = getString(scope, name);
273  try {
274  return (convert(str));
275  } catch (const std::exception&) {
277  "invalid " << type_name << " (" << str
278  << ") specified for parameter '" << name
279  << "' (" << getPosition(name, scope) << ")");
280  }
281  }
282 
283 public:
293  const std::string& name) {
294  return (getIntType<uint32_t>(scope, name));
295  }
296 
306  const std::string& name) {
307  return (getIntType<uint16_t>(scope, name));
308  }
309 
318  uint8_t getUint8(ConstElementPtr scope, const std::string& name) {
319  return (getIntType<uint8_t>(scope, name));
320  }
321 
332  const isc::util::Triplet<uint32_t> parseIntTriplet(const data::ConstElementPtr& scope,
333  const std::string& name);
334 };
335 
336 }
337 }
338 
339 #endif
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
const isc::data::Element::types type_
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
To be removed. Please use ConfigError instead.
SimpleDefault(const char *name, isc::data::Element::types type, const char *value)
uint8_t getUint8(ConstElementPtr scope, const std::string &name)
Get an uint8_t value.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::vector< std::string > SimpleRequiredKeywords
This specifies all required keywords.
This array defines a single entry of default values.
uint32_t getUint32(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint32_t.
Represents the position of the data element within a configuration string.
Definition: data.h:92
Defines the logger used by the top-level component of kea-lfc.
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
target_type getAndConvert(isc::data::ConstElementPtr scope, const std::string &name, const std::string &type_name)
Returns a converted value from a scope.
int_type getIntType(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer value with range checking from a scope.
uint16_t getUint16(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint16_t.