18#include <boost/shared_ptr.hpp>
24namespace isc {
namespace data {
108 Position(
const std::string& file,
const uint32_t line,
116 std::string
str()
const;
169 : type_(t), position_(pos) {
194 std::string
str()
const;
200 std::string
toWire()
const;
201 void toWire(std::ostream& out)
const;
205#define throwTypeError(error) \
207 std::string msg_ = error; \
208 if ((position_.file_ != "") || \
209 (position_.line_ != 0) || \
210 (position_.pos_ != 0)) { \
211 msg_ += " in (" + position_.str() + ")"; \
213 isc_throw(TypeError, msg_); \
224 virtual void toJSON(std::ostream& ss)
const = 0;
237 throwTypeError(
"bigIntValue() called on non-big-integer Element");
245 virtual const std::vector<ElementPtr>&
listValue()
const {
249 virtual const std::map<std::string, ConstElementPtr>&
mapValue()
const {
264 virtual bool getValue(int64_t& t)
const;
265 virtual bool getValue(
double& t)
const;
266 virtual bool getValue(
bool& t)
const;
267 virtual bool getValue(std::string& t)
const;
268 virtual bool getValue(std::vector<ElementPtr>& t)
const;
269 virtual bool getValue(std::map<std::string, ConstElementPtr>& t)
const;
282 virtual bool setValue(
const long long int v);
286 virtual bool setValue(
const double v);
287 virtual bool setValue(
const bool t);
288 virtual bool setValue(
const std::string& v);
289 virtual bool setValue(
const std::vector<ElementPtr>& v);
290 virtual bool setValue(
const std::map<std::string, ConstElementPtr>& v);
324 virtual void remove(
const int i);
327 virtual size_t size()
const;
330 virtual bool empty()
const;
351 virtual void remove(
const std::string& name);
356 virtual bool contains(
const std::string& name)
const;
472 bool preproc =
false);
489 int& line,
int &pos);
499 bool preproc =
false);
532 static void preprocess(std::istream& in, std::stringstream& out);
563 if (type_ ==
list || type_ ==
map) {
565 for (
size_t i = 0; i < s; ++i) {
570 }
else if (type_ ==
map) {
581 child = boost::const_pointer_cast<Element>(
get(key));
585 if (child->getType() !=
list && child->getType() !=
map) {
590 if (!child->empty()){
591 child->removeEmptyContainersRecursively();
595 if (child->empty()) {
622 bool getValue(int64_t& t)
const { t = i;
return (
true); }
624 bool setValue(
long long int v) { i = v;
return (
true); }
625 void toJSON(std::ostream& ss)
const;
658 void toJSON(std::ostream& ss)
const override;
679 bool getValue(
double& t)
const { t = d;
return (
true); }
681 bool setValue(
const double v) { d = v;
return (
true); }
682 void toJSON(std::ostream& ss)
const;
694 bool getValue(
bool& t)
const { t = b;
return (
true); }
696 bool setValue(
const bool v) { b = v;
return (
true); }
697 void toJSON(std::ostream& ss)
const;
705 void toJSON(std::ostream& ss)
const;
717 bool getValue(std::string& t)
const { t = s;
return (
true); }
719 bool setValue(
const std::string& v) { s = v;
return (
true); }
720 void toJSON(std::ostream& ss)
const;
725 std::vector<ElementPtr> l;
730 const std::vector<ElementPtr>&
listValue()
const {
return (l); }
750 void remove(
int i) { l.erase(l.begin() + i); }
751 void toJSON(std::ostream& ss)
const;
752 size_t size()
const {
return (l.size()); }
753 bool empty()
const {
return (l.empty()); }
765 void sort(std::string
const& index = std::string());
769 std::map<std::string, ConstElementPtr> m;
775 const std::map<std::string, ConstElementPtr>&
mapValue()
const override {
779 bool getValue(std::map<std::string, ConstElementPtr>& t)
const override {
784 bool setValue(
const std::map<std::string, ConstElementPtr>& v)
override {
790 auto found = m.find(s);
809 void remove(
const std::string& s)
override { m.erase(s); }
820 bool contains(
const std::string& s)
const override {
821 return (m.find(s) != m.end());
823 void toJSON(std::ostream& ss)
const override;
847 bool empty()
const override {
return (m.empty()); }
985void extend(
const std::string& container,
const std::string& extension,
1021 unsigned indent = 0,
unsigned step = 2);
1034 unsigned indent = 0,
unsigned step = 2);
This is a base class for exceptions thrown from the DNS library module.
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.
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) const override
Checks whether the other Element is equal.
void toJSON(std::ostream &ss) const override
Converts the Element to JSON format and appends it to the given stringstream.
bool getValue(bool &t) const
bool setValue(const bool v)
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
BoolElement(const bool v, const Position &pos=ZERO_POSITION())
bool equals(const Element &other) const
bool setValue(const double v)
DoubleElement(double v, const Position &pos=ZERO_POSITION())
bool equals(const Element &other) const
double doubleValue() const
bool getValue(double &t) const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
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())
virtual bool equals(const Element &other) const =0
virtual bool getValue(int64_t &t) const
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
virtual int64_t intValue() const
std::string str() const
Returns a string representing the Element and all its child elements; note that this is different fro...
virtual std::string stringValue() const
std::string toWire() const
Returns the wireformat for the Element and all its child elements.
static ElementPtr fromWire(std::stringstream &in, int length)
These function pparse the wireformat at the given stringstream (of the given length).
virtual bool setValue(const long long int v)
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.
void removeEmptyContainersRecursively()
Remove all empty maps and lists from this Element and its descendants.
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
virtual void add(ElementPtr element)
Adds an ElementPtr to the list.
virtual const std::vector< ElementPtr > & listValue() const
bool setValue(const long int i)
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 toJSON(std::ostream &ss) const =0
Converts the Element to JSON format and appends it to the given stringstream.
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
virtual double doubleValue() const
bool setValue(const int i)
virtual isc::util::int128_t bigIntValue() const
virtual bool boolValue() const
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
Notes: IntElement type is changed to int64_t.
bool setValue(long long int v)
IntElement(int64_t v, const Position &pos=ZERO_POSITION())
bool equals(const Element &other) const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
bool getValue(int64_t &t) const
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
JSONError(const char *file, size_t line, const char *what)
void sort(std::string const &index=std::string())
Sorts the elements inside the list.
bool empty() const
Return true if there are no elements in the list.
const std::vector< ElementPtr > & listValue() const
void remove(int i)
Removes the element at the given position.
bool getValue(std::vector< ElementPtr > &t) const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
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)
bool equals(const Element &other) const
ListElement(const Position &pos=ZERO_POSITION())
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
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 equals(const Element &other) const override
bool setValue(const std::map< std::string, ConstElementPtr > &v) override
void toJSON(std::ostream &ss) const override
Converts the Element to JSON format and appends it to the given stringstream.
ConstElementPtr get(int const i) const override
Get the i-th element in the map.
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
MapElement(const Position &pos=ZERO_POSITION())
size_t size() const override
Returns number of stored elements.
bool equals(const Element &other) const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
NullElement(const Position &pos=ZERO_POSITION())
std::string stringValue() const
bool setValue(const std::string &v)
bool getValue(std::string &t) const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
StringElement(std::string v, const Position &pos=ZERO_POSITION())
bool equals(const Element &other) const
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
TypeError(const char *file, size_t line, const char *what)
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError,...
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
bool operator==(const Element &a, const Element &b)
void mergeDiffAdd(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx)
Merges the diff data by adding the missing elements from 'other' to 'element' (recursively).
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.
void mergeDiffDel(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx)
Merges the diff data by removing the data present in 'other' from 'element' (recursively).
std::map< std::string, HierarchyTraversalTest > FunctionMap
Mapping between a container name and functions used to match elements inside the container.
bool operator<(Element const &a, Element const &b)
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
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...
boost::shared_ptr< const Element > ConstElementPtr
void extend(const std::string &container, const std::string &extension, ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, bool alter)
Extends data by adding the specified 'extension' elements from 'other' inside the 'container' element...
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.
bool operator!=(const Element &a, const Element &b)
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.
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.