18#include <boost/shared_ptr.hpp>
121 Position(
const std::string& file,
const uint32_t line,
129 std::string
str()
const;
182 : type_(t), position_(pos) {
215 std::string
str()
const;
223 std::string
toWire()
const;
228 void toWire(std::ostream& out)
const;
234#define throwTypeError(error) \
236 std::string msg_ = error; \
237 if ((position_.file_ != "") || \
238 (position_.line_ != 0) || \
239 (position_.pos_ != 0)) { \
240 msg_ += " in (" + position_.str() + ")"; \
242 isc_throw(TypeError, msg_); \
281 throwTypeError(
"bigIntValue() called on non-big-integer Element");
300 virtual const std::vector<ElementPtr>&
listValue()
const {
306 virtual const std::map<std::string, ConstElementPtr>&
mapValue()
const {
325 virtual bool getValue(int64_t& t)
const;
331 virtual bool getValue(
double& t)
const;
337 virtual bool getValue(
bool& t)
const;
343 virtual bool getValue(std::string& t)
const;
349 virtual bool getValue(std::vector<ElementPtr>& t)
const;
355 virtual bool getValue(std::map<std::string, ConstElementPtr>& t)
const;
372 virtual bool setValue(
const long long int v);
384 virtual bool setValue(
const double v);
390 virtual bool setValue(
const bool t);
396 virtual bool setValue(
const std::string& v);
402 virtual bool setValue(
const std::vector<ElementPtr>& v);
408 virtual bool setValue(
const std::map<std::string, ConstElementPtr>& v);
416 return (
setValue(
static_cast<long long int>(i)));
425 return (
setValue(
static_cast<long long int>(i)));
461 virtual void remove(
const int i);
464 virtual size_t size()
const;
467 virtual bool empty()
const;
488 virtual void remove(
const std::string& name);
493 virtual bool contains(
const std::string& name)
const;
668 bool preproc =
false);
696 bool preproc =
false);
729 static void preprocess(std::istream& in, std::stringstream& out);
783 bool getValue(int64_t& t)
const { t = i;
return (
true); }
785 bool setValue(
long long int v) { i = v;
return (
true); }
786 void toJSON(std::ostream& ss,
824 void toJSON(std::ostream& ss,
849 bool getValue(
double& t)
const { t = d;
return (
true); }
851 bool setValue(
const double v) { d = v;
return (
true); }
852 void toJSON(std::ostream& ss,
866 bool getValue(
bool& t)
const { t = b;
return (
true); }
868 bool setValue(
const bool v) { b = v;
return (
true); }
869 void toJSON(std::ostream& ss,
879 void toJSON(std::ostream& ss,
893 bool getValue(std::string& t)
const { t = s;
return (
true); }
895 bool setValue(
const std::string& v) { s = v;
return (
true); }
896 void toJSON(std::ostream& ss,
903 std::vector<ElementPtr> l;
908 const std::vector<ElementPtr>&
listValue()
const {
return (l); }
928 void remove(
int i) { l.erase(l.begin() + i); }
929 void toJSON(std::ostream& ss,
931 size_t size()
const {
return (l.size()); }
932 bool empty()
const {
return (l.empty()); }
945 void sort(std::string
const& index = std::string());
949 std::map<std::string, ConstElementPtr> m;
955 const std::map<std::string, ConstElementPtr>&
mapValue()
const override {
959 bool getValue(std::map<std::string, ConstElementPtr>& t)
const override {
964 bool setValue(
const std::map<std::string, ConstElementPtr>& v)
override {
970 auto found = m.find(s);
989 void remove(
const std::string& s)
override { m.erase(s); }
1001 return (m.find(s) != m.end());
1003 void toJSON(std::ostream& ss,
1029 bool empty()
const override {
return (m.empty()); }
1185void extend(
const std::string& container,
const std::string& extension,
1242 unsigned indent = 0,
unsigned step = 2);
1255 unsigned indent = 0,
unsigned step = 2);
Element(types t, const Position &pos=ZERO_POSITION())
Constructor.
This is a base class for exceptions thrown from the DNS library module.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
bool setValue(const int128_t &v) override
Sets the underlying big integer value.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given stringstream.
BigIntElement(const int128_t &v, const Position &pos=ZERO_POSITION())
Constructor.
int128_t bigIntValue() const override
Retrieve the underlying big integer value.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Checks whether the other Element is equal.
bool getValue(bool &t) const
Get the boolean value.
bool setValue(const bool v)
Set the boolean value.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
BoolElement(const bool v, const Position &pos=ZERO_POSITION())
bool boolValue() const
Return the boolean value.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
bool setValue(const double v)
Set the double value.
DoubleElement(double v, const Position &pos=ZERO_POSITION())
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
double doubleValue() const
Return the double value.
bool getValue(double &t) const
Get the double value.
The Element class represents a piece of data, used by the command channel and configuration parts.
const Position & getPosition() const
Returns position where the data element's value starts in a configuration string.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
virtual bool getValue(int64_t &t) const
Get the integer value.
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
virtual int64_t intValue() const
Return the integer value.
static constexpr unsigned MAX_NESTING_LEVEL
Maximum nesting level of Element objects.
std::string str() const
Returns a string representing the Element and all its child elements.
virtual std::string stringValue() const
Return the string value.
std::string toWire() const
Returns the wireformat for the Element and all its child elements.
virtual void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const =0
Converts the Element to JSON format and appends it to the given output stream.
static ElementPtr fromWire(std::stringstream &in, int length)
These function parse the wireformat at the given stringstream (of the given length).
virtual bool setValue(const long long int v)
Set the integer value.
static ElementPtr fromJSONFile(const std::string &file_name, bool preproc=false)
Reads contents of specified file and interprets it as JSON.
virtual bool empty() const
Return true if there are no elements in the list.
virtual void remove(const int i)
Removes the element at the given position.
virtual bool contains(const std::string &name) const
Checks if there is data at the given key.
virtual ConstElementPtr find(const std::string &identifier) const
Recursively finds any data at the given identifier.
virtual size_t size() const
Returns the number of elements in the list.
Element(types t, const Position &pos=ZERO_POSITION())
Constructor.
static const Position & ZERO_POSITION()
Returns Position object with line_ and pos_ set to 0, and with an empty file name.
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
Return the map value.
virtual void add(ElementPtr element)
Adds an ElementPtr to the list.
virtual const std::vector< ElementPtr > & listValue() const
Return the list value.
bool setValue(const long int i)
Set the integer value (long int overload).
virtual bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const =0
Test equality.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
virtual ConstElementPtr get(const int i) const
Returns the ElementPtr at the given index.
types
The types that an Element can hold.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static Element::types nameToType(const std::string &type_name)
Converts the string to the corresponding type Throws a TypeError if the name is unknown.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
virtual double doubleValue() const
Return the double value.
bool setValue(const int i)
Set the integer value (int overload).
virtual isc::util::int128_t bigIntValue() const
Return the big integer value.
virtual bool boolValue() const
Return the boolean value.
static void preprocess(std::istream &in, std::stringstream &out)
input text preprocessor.
virtual ElementPtr getNonConst(const int i) const
returns element as non-const pointer.
void removeEmptyContainersRecursively(unsigned level=MAX_NESTING_LEVEL)
Remove all empty maps and lists from this Element and its descendants.
bool setValue(long long int v)
Set the integer value.
IntElement(int64_t v, const Position &pos=ZERO_POSITION())
int64_t intValue() const
Return the integer value.
bool getValue(int64_t &t) const
Get the integer value.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
JSONError(const char *file, size_t line, const char *what)
void sort(std::string const &index=std::string())
Sorts the elements inside the list.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
bool empty() const
Return true if there are no elements in the list.
const std::vector< ElementPtr > & listValue() const
Return the list value.
void remove(int i)
Removes the element at the given position.
bool getValue(std::vector< ElementPtr > &t) const
Get the list value.
void add(ElementPtr e)
Adds an ElementPtr to the list.
ElementPtr getNonConst(int i) const
returns element as non-const pointer.
void set(size_t i, ElementPtr e)
Sets the ElementPtr at the given index.
size_t size() const
Returns the number of elements in the list.
bool setValue(const std::vector< ElementPtr > &v)
Set the list value.
ListElement(const Position &pos=ZERO_POSITION())
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
ConstElementPtr get(int i) const
Returns the ElementPtr at the given index.
bool contains(const std::string &s) const override
Checks if there is data at the given key.
const std::map< std::string, ConstElementPtr > & mapValue() const override
Return the map value.
ConstElementPtr find(const std::string &id) const override
Recursively finds any data at the given identifier.
void set(const std::string &key, ConstElementPtr value) override
Sets the ElementPtr at the given key.
ConstElementPtr get(const std::string &s) const override
Returns the ElementPtr at the given key.
bool setValue(const std::map< std::string, ConstElementPtr > &v) override
Set the map value.
ConstElementPtr get(int const i) const override
Get the i-th element in the map.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Test equality.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given output stream.
bool empty() const override
Return true if there are no elements in the list.
void remove(int const i) override
Remove the i-th element from the map.
void remove(const std::string &s) override
Remove the ElementPtr at the given key.
bool getValue(std::map< std::string, ConstElementPtr > &t) const override
Get the map value.
MapElement(const Position &pos=ZERO_POSITION())
size_t size() const override
Returns number of stored elements.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
NullElement(const Position &pos=ZERO_POSITION())
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
std::string stringValue() const
Return the string value.
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
bool setValue(const std::string &v)
Set the string value.
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
bool getValue(std::string &t) const
Get the string value.
StringElement(std::string v, const Position &pos=ZERO_POSITION())
TypeError(const char *file, size_t line, const char *what)
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
boost::multiprecision::checked_int128_t int128_t
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError,...
bool operator==(const Element &a, const Element &b)
Test equality.
void extend(const std::string &container, const std::string &extension, ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, bool alter, unsigned level)
Extends data by adding the specified 'extension' elements from 'other' inside the 'container' element...
void removeIdentical(ElementPtr a, ConstElementPtr b)
Remove all values from the first ElementPtr that are equal in the second.
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element. (on the first level).
std::map< std::string, HierarchyTraversalTest > FunctionMap
Mapping between a container name and functions used to match elements inside the container.
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
bool operator<(Element const &a, Element const &b)
Test less than.
bool IsCircular(ConstElementPtr element)
Check if the data is circular.
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
std::function< bool(ElementPtr &)> NoDataTestFunc
Function used to check if the data provided for the element contains only information used for identi...
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
boost::shared_ptr< const Element > ConstElementPtr
void mergeDiffAdd(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by adding the missing elements from 'other' to 'element' (recursively).
unsigned getNestDepth(ConstElementPtr element, unsigned max_depth)
Compute the nesting depth.
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a null pointer.
std::function< bool(ElementPtr &, ElementPtr &)> MatchTestFunc
Function used to check if two MapElements refer to the same configuration data.
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
std::function< bool(const std::string &)> IsKeyTestFunc
Function used to check if the key is used for identification.
void mergeDiffDel(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by removing the data present in 'other' from 'element' (recursively).
bool operator!=(const Element &a, const Element &b)
Test inequality.
boost::shared_ptr< Element > ElementPtr
std::vector< FunctionMap > HierarchyDescriptor
Hierarchy descriptor of the containers in a specific Element hierarchy tree.
boost::multiprecision::checked_int128_t int128_t
Defines the logger used by the top-level component of kea-lfc.
Position()
Default constructor.
Represents the position of the data element within a configuration string.
Position(const std::string &file, const uint32_t line, const uint32_t pos)
Constructor.
uint32_t pos_
Position within the line.
std::string str() const
Returns the position in the textual format.
Position()
Default constructor.
uint32_t line_
Line number.
std::string file_
File name.
Structure holding the test functions used to traverse the element hierarchy.