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>
13#include <vector>
14#include <string>
15#include <stdint.h>
16#include <limits>
17
18namespace isc {
19namespace data {
20
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
31typedef std::vector<SimpleDefault> SimpleDefaults;
32
35typedef std::vector<std::string> ParamsList;
36
37
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
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
168protected:
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
222public:
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
target_type getAndConvert(isc::data::ConstElementPtr scope, const std::string &name, const std::string &type_name)
Returns a converted value from a scope.
static size_t setListDefaults(isc::data::ConstElementPtr list, const SimpleDefaults &default_values)
Sets the default values for all entries in a list.
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
uint8_t getUint8(ConstElementPtr scope, const std::string &name)
Get an uint8_t value.
static isc::asiolink::IOAddress getAddress(const ConstElementPtr &scope, const std::string &name)
Returns a IOAddress parameter from a scope.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
uint32_t getUint32(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint32_t.
int_type getIntType(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer value with range checking from a scope.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
uint16_t getUint16(isc::data::ConstElementPtr scope, const std::string &name)
Returns a value converted to uint16_t.
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
To be removed. Please use ConfigError instead.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
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:22
Defines the logger used by the top-level component of kea-dhcp-ddns.
Represents the position of the data element within a configuration string.
Definition: data.h:88
This array defines a single entry of default values.
const isc::data::Element::types type_
SimpleDefault(const char *name, isc::data::Element::types type, const char *value)