Kea  1.5.0
lib/cc/simple_parser.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2017 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 <vector>
14 #include <string>
15 #include <stdint.h>
16 #include <limits>
17 
18 namespace isc {
19 namespace data {
20 
22 struct SimpleDefault {
23  SimpleDefault(const char* name, isc::data::Element::types type, const char* value)
24  :name_(name), type_(type), value_(value) {}
25  std::string name_;
27  const char* value_;
28 };
29 
31 typedef std::vector<SimpleDefault> SimpleDefaults;
32 
35 typedef std::vector<std::string> ParamsList;
36 
37 
60 class SimpleParser {
61  public:
62 
76  static size_t deriveParams(isc::data::ConstElementPtr parent,
78  const ParamsList& params);
79 
91  static size_t setDefaults(isc::data::ElementPtr scope,
92  const SimpleDefaults& default_values);
93 
103  static size_t setListDefaults(isc::data::ConstElementPtr list,
104  const SimpleDefaults& default_values);
105 
115  static const data::Element::Position&
116  getPosition(const std::string& name, const data::ConstElementPtr parent);
117 
127  static std::string getString(isc::data::ConstElementPtr scope,
128  const std::string& name);
129 
139  static int64_t getInteger(isc::data::ConstElementPtr scope,
140  const std::string& name);
141 
151  static bool getBoolean(isc::data::ConstElementPtr scope,
152  const std::string& name);
153 
154 
166  getAddress(const ConstElementPtr& scope, const std::string& name);
167 
168 protected:
169 
180  template <typename int_type> int_type
182  const std::string& name) {
183  int64_t val_int = getInteger(scope, name);
184  if ((val_int < std::numeric_limits<int_type>::min()) ||
185  (val_int > std::numeric_limits<int_type>::max())) {
187  "out of range value (" << val_int
188  << ") specified for parameter '" << name
189  << "' (" << getPosition(name, scope) << ")");
190  }
191  return (static_cast<int_type>(val_int));
192  }
193 
206  template <typename target_type,
207  target_type convert(const std::string&)> target_type
209  const std::string& name,
210  const std::string& type_name) {
211  std::string str = getString(scope, name);
212  try {
213  return (convert(str));
214  } catch (const std::exception&) {
216  "invalid " << type_name << " (" << str
217  << ") specified for parameter '" << name
218  << "' (" << getPosition(name, scope) << ")");
219  }
220  }
221 
222 public:
232  const std::string& name) {
233  return (getIntType<uint32_t>(scope, name));
234  }
235 
245  const std::string& name) {
246  return (getIntType<uint16_t>(scope, name));
247  }
248 
257  uint8_t getUint8(ConstElementPtr scope, const std::string& name) {
258  return (getIntType<uint8_t>(scope, name));
259  }
260 };
261 
262 };
263 };
264 
265 #endif
isc::data::SimpleDefault::value_
const char * value_
Definition: lib/cc/simple_parser.h:27
isc::dhcp::DhcpConfigError
To be removed. Please use ConfigError instead.
Definition: dhcp_config_error.h:58
isc::data::SimpleParser::getAndConvert
target_type getAndConvert(isc::data::ConstElementPtr scope, const std::string &name, const std::string &type_name)
Returns a converted value from a scope.
Definition: lib/cc/simple_parser.h:208
isc::data::SimpleParser::getIntType
int_type getIntType(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer value with range checking from a scope.
Definition: lib/cc/simple_parser.h:181
isc::data::SimpleParser::setListDefaults
static size_t setListDefaults(isc::data::ConstElementPtr list, const SimpleDefaults &default_values)
Sets the default values for all entries in a list.
Definition: lib/cc/simple_parser.cc:173
isc::data::SimpleParser::getInteger
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
Definition: lib/cc/simple_parser.cc:41
io_address.h
isc::data::SimpleParser::getAddress
static isc::asiolink::IOAddress getAddress(const ConstElementPtr &scope, const std::string &name)
Returns a IOAddress parameter from a scope.
Definition: lib/cc/simple_parser.cc:75
dhcp_config_error.h
isc::data::SimpleParser::getUint16
uint16_t getUint16(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint16_t.
Definition: lib/cc/simple_parser.h:244
isc::data::SimpleDefaults
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet)
Definition: lib/cc/simple_parser.h:31
isc::data::SimpleDefault::type_
const isc::data::Element::types type_
Definition: lib/cc/simple_parser.h:26
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::data::SimpleDefault::SimpleDefault
SimpleDefault(const char *name, isc::data::Element::types type, const char *value)
Definition: lib/cc/simple_parser.h:23
isc::data::ParamsList
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
Definition: lib/cc/simple_parser.h:35
isc::data::SimpleParser::getUint8
uint8_t getUint8(ConstElementPtr scope, const std::string &name)
Get an uint8_t value.
Definition: lib/cc/simple_parser.h:257
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:88
isc::data::SimpleParser
A simple parser.
Definition: lib/cc/simple_parser.h:60
isc::data::SimpleDefault
This array defines a single entry of default values.
Definition: lib/cc/simple_parser.h:22
isc::data::SimpleParser::deriveParams
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
Definition: lib/cc/simple_parser.cc:183
isc::data::SimpleParser::getUint32
uint32_t getUint32(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint32_t.
Definition: lib/cc/simple_parser.h:231
isc::data::SimpleParser::getBoolean
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
Definition: lib/cc/simple_parser.cc:58
isc::data::SimpleDefault::name_
std::string name_
Definition: lib/cc/simple_parser.h:25
isc::data::Element::types
types
Definition: data.h:151
data.h
isc::data::SimpleParser::getPosition
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
Definition: lib/cc/simple_parser.cc:88
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::data::SimpleParser::getString
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
Definition: lib/cc/simple_parser.cc:24
isc::data::SimpleParser::setDefaults
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
Definition: lib/cc/simple_parser.cc:99