16 #include <boost/shared_ptr.hpp> 22 namespace isc {
namespace data {
106 Position(
const std::string& file,
const uint32_t line,
108 : file_(file), line_(line), pos_(pos) {
114 std::string str()
const;
147 : type_(t), position_(pos) {
155 enum types { integer, real, boolean, null, string, list, map, any };
176 std::string str()
const;
182 std::string toWire()
const;
183 void toWire(std::ostream& out)
const;
187 #define throwTypeError(error) \ 189 std::string msg_ = error; \ 190 if ((position_.file_ != "") || \ 191 (position_.line_ != 0) || \ 192 (position_.pos_ != 0)) { \ 193 msg_ += " in (" + position_.str() + ")"; \ 195 isc_throw(TypeError, msg_); \ 201 virtual bool equals(
const Element& other)
const = 0;
205 virtual void toJSON(std::ostream& ss)
const = 0;
218 {
throwTypeError(
"doubleValue() called on non-double Element"); };
222 {
throwTypeError(
"stringValue() called on non-string Element"); };
223 virtual const std::vector<ElementPtr>&
listValue()
const {
227 virtual const std::map<std::string, ConstElementPtr>&
mapValue()
const {
242 virtual bool getValue(int64_t& t)
const;
243 virtual bool getValue(
double& t)
const;
244 virtual bool getValue(
bool& t)
const;
245 virtual bool getValue(std::string& t)
const;
246 virtual bool getValue(std::vector<ElementPtr>& t)
const;
247 virtual bool getValue(std::map<std::string, ConstElementPtr>& t)
const;
260 virtual bool setValue(
const long long int v);
263 virtual bool setValue(
const double v);
264 virtual bool setValue(
const bool t);
265 virtual bool setValue(
const std::string& v);
266 virtual bool setValue(
const std::vector<ElementPtr>& v);
267 virtual bool setValue(
const std::map<std::string, ConstElementPtr>& v);
277 virtual ConstElementPtr
get(
const int i)
const;
286 virtual ElementPtr getNonConst(
const int i)
const;
292 virtual void set(
const size_t i, ElementPtr element);
296 virtual void add(ElementPtr element);
301 virtual void remove(
const int i);
304 virtual size_t size()
const;
307 virtual bool empty()
const;
316 virtual ConstElementPtr
get(
const std::string& name)
const;
324 virtual void set(
const std::string& name, ConstElementPtr element);
328 virtual void remove(
const std::string& name);
333 virtual bool contains(
const std::string& name)
const;
348 virtual ConstElementPtr find(
const std::string& identifier)
const;
354 virtual bool find(
const std::string& identifier, ConstElementPtr& t)
const;
376 static ElementPtr create(
const Position& pos = ZERO_POSITION());
377 static ElementPtr create(
const long long int i,
378 const Position& pos = ZERO_POSITION());
379 static ElementPtr create(
const int i,
380 const Position& pos = ZERO_POSITION());
381 static ElementPtr create(
const long int i,
382 const Position& pos = ZERO_POSITION());
383 static ElementPtr create(
const uint32_t i,
384 const Position& pos = ZERO_POSITION());
385 static ElementPtr create(
const double d,
386 const Position& pos = ZERO_POSITION());
387 static ElementPtr create(
const bool b,
388 const Position& pos = ZERO_POSITION());
389 static ElementPtr create(
const std::string& s,
390 const Position& pos = ZERO_POSITION());
393 static ElementPtr create(
const char *s,
394 const Position& pos = ZERO_POSITION());
400 static ElementPtr createList(
const Position& pos = ZERO_POSITION());
406 static ElementPtr createMap(
const Position& pos = ZERO_POSITION());
416 static ElementPtr fromJSON(
const std::string& in,
bool preproc =
false);
433 static ElementPtr fromJSON(std::istream& in,
bool preproc =
false);
446 static ElementPtr fromJSON(std::istream& in,
const std::string& file_name,
447 bool preproc =
false);
463 static ElementPtr fromJSON(std::istream& in,
const std::string& file,
464 int& line,
int &pos);
473 static ElementPtr fromJSONFile(
const std::string& file_name,
474 bool preproc =
false);
507 static void preprocess(std::istream& in, std::stringstream& out);
516 static ElementPtr fromWire(std::stringstream& in,
int length);
532 static ElementPtr fromWire(
const std::string& s);
538 if (type_ == list || type_ == map) {
540 for (
size_t i = 0; i < s; ++i) {
544 child = getNonConst(i);
545 }
else if (type_ == map) {
546 std::string
const key(
get(i)->stringValue());
556 child = boost::const_pointer_cast<
Element>(
get(key));
560 if (child->getType() != list && child->getType() != map) {
565 if (!child->empty()){
566 child->removeEmptyContainersRecursively();
570 if (child->empty()) {
594 :
Element(integer, pos), i(v) { }
597 bool getValue(int64_t& t)
const { t = i;
return (
true); }
599 bool setValue(
long long int v) { i = v;
return (
true); }
600 void toJSON(std::ostream& ss)
const;
601 bool equals(
const Element& other)
const;
612 bool getValue(
double& t)
const { t = d;
return (
true); }
614 bool setValue(
const double v) { d = v;
return (
true); }
615 void toJSON(std::ostream& ss)
const;
616 bool equals(
const Element& other)
const;
624 :
Element(boolean, pos), b(v) {};
627 bool getValue(
bool& t)
const { t = b;
return (
true); }
629 bool setValue(
const bool v) { b = v;
return (
true); }
630 void toJSON(std::ostream& ss)
const;
631 bool equals(
const Element& other)
const;
638 void toJSON(std::ostream& ss)
const;
639 bool equals(
const Element& other)
const;
647 :
Element(string, pos), s(v) {};
650 bool getValue(std::string& t)
const { t = s;
return (
true); }
652 bool setValue(
const std::string& v) { s = v;
return (
true); }
653 void toJSON(std::ostream& ss)
const;
654 bool equals(
const Element& other)
const;
658 std::vector<ElementPtr> l;
663 const std::vector<ElementPtr>&
listValue()
const {
return (l); }
675 ConstElementPtr
get(
int i)
const {
return (l.at(i)); }
678 void set(
size_t i, ElementPtr e) {
681 void add(ElementPtr e) { l.push_back(e); };
683 void remove(
int i) { l.erase(l.begin() + i); };
684 void toJSON(std::ostream& ss)
const;
685 size_t size()
const {
return (l.size()); }
686 bool empty()
const {
return (l.empty()); }
687 bool equals(
const Element& other)
const;
698 void sort(std::string
const& index = std::string());
702 std::map<std::string, ConstElementPtr> m;
708 const std::map<std::string, ConstElementPtr>&
mapValue()
const override {
712 bool getValue(std::map<std::string, ConstElementPtr>& t)
const override {
717 bool setValue(
const std::map<std::string, ConstElementPtr>& v)
override {
722 ConstElementPtr
get(
const std::string& s)
const override {
723 auto found = m.find(s);
733 ConstElementPtr
get(
int const i)
const override {
736 return create(it->first);
740 void set(
const std::string& key, ConstElementPtr value)
override;
742 void remove(
const std::string& s)
override { m.erase(s); }
747 void remove(
int const i)
override {
753 bool contains(
const std::string& s)
const override {
754 return (m.find(s) != m.end());
756 void toJSON(std::ostream& ss)
const override;
763 ConstElementPtr find(
const std::string&
id)
const override;
769 bool find(
const std::string&
id, ConstElementPtr& t)
const override;
778 bool equals(
const Element& other)
const override;
780 bool empty()
const override {
return (m.empty()); }
786 bool isNull(ConstElementPtr p);
812 void merge(ElementPtr element, ConstElementPtr other);
842 typedef std::map<std::string, HierarchyTraversalTest>
FunctionMap;
877 void mergeDiffAdd(ElementPtr& element, ElementPtr& other,
878 HierarchyDescriptor& hierarchy, std::string key,
898 void mergeDiffDel(ElementPtr& element, ElementPtr& other,
899 HierarchyDescriptor& hierarchy, std::string key,
918 void extend(
const std::string& container,
const std::string& extension,
919 ElementPtr& element, ElementPtr& other,
920 HierarchyDescriptor& hierarchy, std::string key,
size_t idx = 0,
933 ElementPtr
copy(ConstElementPtr from,
int level = 100);
940 bool isEquivalent(ConstElementPtr a, ConstElementPtr b);
953 void prettyPrint(ConstElementPtr element, std::ostream& out,
954 unsigned indent = 0,
unsigned step = 2);
967 unsigned indent = 0,
unsigned step = 2);
1004 #endif // ISC_DATA_H virtual std::string stringValue() const
bool getValue(double &t) const
bool operator<(Element const &a, Element const &b)
bool setValue(const int i)
uint32_t line_
Line number.
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
bool empty() const
Return true if there are no elements in the list.
static const Position & ZERO_POSITION()
Returns Position object with line_ and pos_ set to 0, and with an empty file name.
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...
const Position & getPosition() const
Returns position where the data element's value starts in a configuration string. ...
bool setValue(const std::vector< ElementPtr > &v)
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
const std::map< std::string, ConstElementPtr > & mapValue() const override
bool operator!=(const Element &a, const Element &b)
virtual int64_t intValue() const
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
bool setValue(long long int v)
virtual bool getValue(int64_t &t) const
boost::shared_ptr< Element > ElementPtr
ListElement(const Position &pos=ZERO_POSITION())
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError, error)
void removeIdentical(ElementPtr a, ConstElementPtr b)
Remove all values from the first ElementPtr that are equal in the second.
StringElement(std::string v, const Position &pos=ZERO_POSITION())
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
double doubleValue() const
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
bool getValue(std::map< std::string, ConstElementPtr > &t) const override
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).
Position()
Default constructor.
Position(const std::string &file, const uint32_t line, const uint32_t pos)
Constructor.
std::vector< FunctionMap > HierarchyDescriptor
Hierarchy descriptor of the containers in a specific Element hierarchy tree.
bool operator==(const Element &a, const Element &b)
bool getValue(bool &t) const
bool setValue(const std::map< std::string, ConstElementPtr > &v) override
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
std::function< bool(ElementPtr &)> NoDataTestFunc
Function used to check if the data provided for the element contains only information used for identi...
size_t size() const override
Returns number of stored elements.
bool setValue(const bool v)
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
Notes: IntElement type is changed to int64_t.
TypeError(const char *file, size_t line, const char *what)
IntElement(int64_t v, const Position &pos=ZERO_POSITION())
boost::shared_ptr< const Element > ConstElementPtr
virtual const std::vector< ElementPtr > & listValue() const
bool getValue(int64_t &t) const
void removeEmptyContainersRecursively()
Remove all empty maps and lists from this Element and its descendants.
NullElement(const Position &pos=ZERO_POSITION())
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
DoubleElement(double v, const Position &pos=ZERO_POSITION())
MapElement(const Position &pos=ZERO_POSITION())
Represents the position of the data element within a configuration string.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::function< bool(const std::string &)> IsKeyTestFunc
Function used to check if the key is used for identification.
uint32_t pos_
Position within the line.
bool contains(const std::string &s) const override
Checks if there is data at the given key.
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
bool setValue(const long int i)
Structure holding the test functions used to traverse the element hierarchy.
virtual double doubleValue() const
bool getValue(std::string &t) const
std::map< std::string, HierarchyTraversalTest > FunctionMap
Mapping between a container name and functions used to match elements inside the container.
std::function< bool(ElementPtr &, ElementPtr &)> MatchTestFunc
Function used to check if two MapElements refer to the same configuration data.
virtual ConstElementPtr get(const int i) const
Returns the ElementPtr at the given index.
The Element class represents a piece of data, used by the command channel and configuration parts...
bool empty() const override
Return true if there are no elements in the list.
bool setValue(const std::string &v)
ElementPtr getNonConst(int i) const
returns element as non-const pointer
bool getValue(std::vector< ElementPtr > &t) const
virtual bool setValue(const long long int v)
bool setValue(const double v)
BoolElement(const bool v, const Position &pos=ZERO_POSITION())
size_t size() const
Returns the number of elements in the list.
virtual void remove(const int i)
Removes the element at the given position.
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).
std::string file_
File name.
void add(ElementPtr e)
Adds an ElementPtr to the list.
virtual bool boolValue() const
JSONError(const char *file, size_t line, const char *what)
Element(int t, const Position &pos=ZERO_POSITION())
Constructor.
const std::vector< ElementPtr > & listValue() const
std::string stringValue() const