Kea 3.2.0-git
lib/cc/simple_parser.h
Go to the documentation of this file.
1// Copyright (C) 2016-2026 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 <util/triplet.h>
14#include <map>
15#include <vector>
16#include <string>
17#include <stdint.h>
18#include <limits>
19
20namespace isc {
21namespace data {
22
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
33typedef std::vector<std::string> SimpleRequiredKeywords;
34
36typedef std::map<std::string, isc::data::Element::types> SimpleKeywords;
37
39typedef std::vector<SimpleDefault> SimpleDefaults;
40
43typedef std::vector<std::string> ParamsList;
44
45
69public:
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,
110 const ParamsList& params);
111
123 static size_t setDefaults(isc::data::ElementPtr scope,
124 const SimpleDefaults& default_values);
125
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
184 template<typename T>
186 const std::string& name, T& output_value) {
187 auto min = std::numeric_limits<T>::min();
188 auto max = std::numeric_limits<T>::max();
189 int64_t ivalue = getInteger(scope, name);
190
191 if (ivalue < min || ivalue > max) {
193 "'" << name << "' : " << ivalue << " is out of range,"
194 << " must be >= " << min << " and <= " << max);
195 }
196
197 output_value = static_cast<T>(ivalue);
198 }
199
213 static int64_t getInteger(isc::data::ConstElementPtr scope,
214 const std::string& name,
215 int64_t min, int64_t max);
216
226 static bool getBoolean(isc::data::ConstElementPtr scope,
227 const std::string& name);
228
229
241 getAddress(const ConstElementPtr& scope, const std::string& name);
242
252 static double getDouble(const ConstElementPtr& scope,
253 const std::string& name);
254
255protected:
256
267 template <typename int_type> int_type
269 const std::string& name) {
270 int64_t val_int = getInteger(scope, name);
271 if ((val_int < std::numeric_limits<int_type>::min()) ||
272 (val_int > std::numeric_limits<int_type>::max())) {
274 "out of range value (" << val_int
275 << ") specified for parameter '" << name
276 << "' (" << getPosition(name, scope) << ")");
277 }
278 return (static_cast<int_type>(val_int));
279 }
280
293 template <typename target_type,
294 target_type convert(const std::string&)> target_type
296 const std::string& name,
297 const std::string& type_name) {
298 std::string str = getString(scope, name);
299 try {
300 return (convert(str));
301 } catch (const std::exception&) {
303 "invalid " << type_name << " (" << str
304 << ") specified for parameter '" << name
305 << "' (" << getPosition(name, scope) << ")");
306 }
307 }
308
309public:
319 const std::string& name) {
320 return (getIntType<uint32_t>(scope, name));
321 }
322
332 const std::string& name) {
333 return (getIntType<uint16_t>(scope, name));
334 }
335
344 uint8_t getUint8(ConstElementPtr scope, const std::string& name) {
345 return (getIntType<uint8_t>(scope, name));
346 }
347
359 const std::string& name);
360};
361
362}
363}
364
365#endif
types
The types that an Element can hold.
Definition data.h:152
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
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 double getDouble(const ConstElementPtr &scope, const std::string &name)
Returns a floating point parameter from a scope.
static void checkRequired(const SimpleRequiredKeywords &required, isc::data::ConstElementPtr scope)
Checks that all required keywords are present.
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.
static void rangeCheckedInteger(isc::data::ConstElementPtr scope, const std::string &name, T &output_value)
Fetches a range-checked integer parameter from a scope.
const isc::util::Triplet< uint32_t > parseIntTriplet(const data::ConstElementPtr &scope, const std::string &name)
Parses an integer triplet.
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.
This template specifies a parameter value.
Definition triplet.h:37
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::vector< std::string > SimpleRequiredKeywords
This specifies all required keywords.
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:30
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:29
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
Defines the logger used by the top-level component of kea-lfc.
Represents the position of the data element within a configuration string.
Definition data.h:107
const isc::data::Element::types type_
SimpleDefault(const char *name, isc::data::Element::types type, const char *value)