14 #include <boost/shared_ptr.hpp>
18 namespace isc {
namespace data {
102 Position(
const std::string& file,
const uint32_t line,
104 : file_(file), line_(line), pos_(pos) {
110 std::string
str()
const;
143 : type_(t), position_(pos) {
172 std::string
str()
const;
178 std::string
toWire()
const;
179 void toWire(std::ostream& out)
const;
183 #define throwTypeError(error) \
185 std::string msg_ = error; \
186 if ((position_.file_ != "") || \
187 (position_.line_ != 0) || \
188 (position_.pos_ != 0)) { \
189 msg_ += " in (" + position_.str() + ")"; \
191 isc_throw(TypeError, msg_); \
201 virtual void toJSON(std::ostream& ss)
const = 0;
214 {
throwTypeError(
"doubleValue() called on non-double Element"); };
218 {
throwTypeError(
"stringValue() called on non-string Element"); };
219 virtual const std::vector<ElementPtr>&
listValue()
const {
223 virtual const std::map<std::string, ConstElementPtr>&
mapValue()
const {
238 virtual bool getValue(int64_t& t)
const;
239 virtual bool getValue(
double& t)
const;
240 virtual bool getValue(
bool& t)
const;
241 virtual bool getValue(std::string& t)
const;
242 virtual bool getValue(std::vector<ElementPtr>& t)
const;
243 virtual bool getValue(std::map<std::string, ConstElementPtr>& t)
const;
256 virtual bool setValue(
const long long int v);
259 virtual bool setValue(
const double v);
260 virtual bool setValue(
const bool t);
261 virtual bool setValue(
const std::string& v);
262 virtual bool setValue(
const std::vector<ElementPtr>& v);
263 virtual bool setValue(
const std::map<std::string, ConstElementPtr>& v);
275 virtual ConstElementPtr
get(
const int i)
const;
290 virtual void set(
const size_t i, ElementPtr element);
294 virtual void add(ElementPtr element);
299 virtual void remove(
const int i);
302 virtual size_t size()
const;
305 virtual bool empty()
const;
314 virtual ConstElementPtr
get(
const std::string& name)
const;
322 virtual void set(
const std::string& name, ConstElementPtr element);
326 virtual void remove(
const std::string& name);
331 virtual bool contains(
const std::string& name)
const;
346 virtual ConstElementPtr
find(
const std::string& identifier)
const;
352 virtual bool find(
const std::string& identifier, ConstElementPtr& t)
const;
376 static ElementPtr
create(
const long long int i,
378 static ElementPtr
create(
const int i,
380 static ElementPtr
create(
const long int i,
382 static ElementPtr
create(
const double d,
384 static ElementPtr
create(
const bool b,
386 static ElementPtr
create(
const std::string& s,
390 static ElementPtr
create(
const char *s,
414 static ElementPtr
fromJSON(
const std::string& in,
bool preproc =
false);
431 static ElementPtr
fromJSON(std::istream& in,
bool preproc =
false);
444 static ElementPtr
fromJSON(std::istream& in,
const std::string& file_name,
445 bool preproc =
false);
461 static ElementPtr
fromJSON(std::istream& in,
const std::string& file,
462 int& line,
int &pos);
471 static ElementPtr
fromJSONFile(
const std::string& file_name,
472 bool preproc =
false);
505 static void preprocess(std::istream& in, std::stringstream& out);
514 static ElementPtr
fromWire(std::stringstream& in,
int length);
530 static ElementPtr
fromWire(
const std::string& s);
553 bool getValue(int64_t& t)
const { t = i;
return (
true); }
555 bool setValue(
long long int v) { i = v;
return (
true); }
556 void toJSON(std::ostream& ss)
const;
568 bool getValue(
double& t)
const { t = d;
return (
true); }
570 bool setValue(
const double v) { d = v;
return (
true); }
571 void toJSON(std::ostream& ss)
const;
583 bool getValue(
bool& t)
const { t = b;
return (
true); }
585 bool setValue(
const bool v) { b = v;
return (
true); }
586 void toJSON(std::ostream& ss)
const;
594 void toJSON(std::ostream& ss)
const;
606 bool getValue(std::string& t)
const { t = s;
return (
true); }
608 bool setValue(
const std::string& v) { s = v;
return (
true); }
609 void toJSON(std::ostream& ss)
const;
614 std::vector<ElementPtr> l;
619 const std::vector<ElementPtr>&
listValue()
const {
return (l); }
631 ConstElementPtr
get(
int i)
const {
return (l.at(i)); }
634 void set(
size_t i, ElementPtr e) {
637 void add(ElementPtr e) { l.push_back(e); };
639 void remove(
int i) { l.erase(l.begin() + i); };
640 void toJSON(std::ostream& ss)
const;
641 size_t size()
const {
return (l.size()); }
642 bool empty()
const {
return (l.empty()); }
647 std::map<std::string, ConstElementPtr> m;
653 const std::map<std::string, ConstElementPtr>&
mapValue()
const {
657 bool getValue(std::map<std::string, ConstElementPtr>& t)
const {
662 bool setValue(
const std::map<std::string, ConstElementPtr>& v) {
667 ConstElementPtr
get(
const std::string& s)
const {
668 auto found = m.find(s);
672 void set(
const std::string& key, ConstElementPtr value);
674 void remove(
const std::string& s) { m.erase(s); }
676 return (m.find(s) != m.end());
678 void toJSON(std::ostream& ss)
const;
685 ConstElementPtr
find(
const std::string&
id)
const;
691 bool find(
const std::string&
id, ConstElementPtr& t)
const;
702 bool empty()
const {
return (m.empty()); }
708 bool isNull(ConstElementPtr p);
738 void merge(ElementPtr element, ConstElementPtr other);
750 ElementPtr
copy(ConstElementPtr from,
int level = 100);
757 bool isEquivalent(ConstElementPtr a, ConstElementPtr b);
770 void prettyPrint(ConstElementPtr element, std::ostream& out,
771 unsigned indent = 0,
unsigned step = 2);
784 unsigned indent = 0,
unsigned step = 2);
797 std::ostream&
operator<<(std::ostream& out,
const Element::Position& pos);
814 std::ostream&
operator<<(std::ostream& out,
const Element& e);
816 bool operator==(
const Element& a,
const Element& b);
817 bool operator!=(
const Element& a,
const Element& b);
virtual double doubleValue() const
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
bool getValue(bool &t) const
static ElementPtr fromJSONFile(const std::string &file_name, bool preproc=false)
Reads contents of specified file and interprets it as JSON.
bool contains(const std::string &s) const
Checks if there is data at the given key.
bool setValue(const int i)
uint32_t line_
Line number.
bool equals(const Element &other) const
std::string toWire() const
Returns the wireformat for the Element and all its child elements.
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
bool equals(const Element &other) const
bool equals(const Element &other) const
static const Position & ZERO_POSITION()
Returns Position object with line_ and pos_ set to 0, and with an empty file name.
const std::vector< ElementPtr > & listValue() const
std::string stringValue() const
bool setValue(const std::vector< ElementPtr > &v)
bool getValue(std::string &t) const
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
bool getValue(int64_t &t) const
std::string str() const
Returns the position in the textual format.
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
bool operator!=(const Element &a, const Element &b)
bool equals(const Element &other) const
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
double doubleValue() const
bool equals(const Element &other) const
bool setValue(long long int v)
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
boost::shared_ptr< Element > ElementPtr
ListElement(const Position &pos=ZERO_POSITION())
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
virtual bool contains(const std::string &name) const
Checks if there is data at the given key.
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError, error)
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
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())
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
bool empty() const
Return true if there are no elements in the list.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
virtual size_t size() const
Returns the number of elements in the list.
virtual bool equals(const Element &other) const =0
Position()
Default constructor.
Position(const std::string &file, const uint32_t line, const uint32_t pos)
Constructor.
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
bool getValue(std::vector< ElementPtr > &t) const
bool operator==(const Element &a, const Element &b)
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
static ElementPtr fromWire(std::stringstream &in, int length)
These function pparse the wireformat at the given stringstream (of the given length).
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
bool setValue(const bool v)
virtual bool boolValue() const
Notes: IntElement type is changed to int64_t.
virtual ConstElementPtr find(const std::string &identifier) const
Recursively finds any data at the given identifier.
TypeError(const char *file, size_t line, const char *what)
bool equals(const Element &other) const
const Position & getPosition() const
Returns position where the data element's value starts in a configuration string. ...
IntElement(int64_t v, const Position &pos=ZERO_POSITION())
boost::shared_ptr< const Element > ConstElementPtr
size_t size() const
Returns the number of elements in the list.
std::string str() const
Returns a string representing the Element and all its child elements; note that this is different fro...
NullElement(const Position &pos=ZERO_POSITION())
bool getValue(std::map< std::string, ConstElementPtr > &t) const
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())
virtual void add(ElementPtr element)
Adds an ElementPtr to the list.
bool equals(const Element &other) const
MapElement(const Position &pos=ZERO_POSITION())
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual ElementPtr getNonConst(const int i) const
returns element as non-const pointer
virtual void toJSON(std::ostream &ss) const =0
Converts the Element to JSON format and appends it to the given stringstream.
virtual bool getValue(int64_t &t) const
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-dhcp-ddns.
static void preprocess(std::istream &in, std::stringstream &out)
input text preprocessor
uint32_t pos_
Position within the line.
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
bool setValue(const long int i)
static Element::types nameToType(const std::string &type_name)
Converts the string to the corresponding type Throws a TypeError if the name is unknown.
void toJSON(std::ostream &ss) const
Converts the Element to JSON format and appends it to the given stringstream.
static ElementPtr create(const Position &pos=ZERO_POSITION())
const std::map< std::string, ConstElementPtr > & mapValue() const
virtual bool empty() const
Return true if there are no elements in the list.
void set(const std::string &key, ConstElementPtr value)
Sets the ElementPtr at the given key.
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...
ElementPtr getNonConst(int i) const
returns element as non-const pointer
size_t size() const
Returns number of stored elements.
virtual int64_t intValue() const
void set(size_t i, ElementPtr e)
Sets the ElementPtr at the given index.
bool setValue(const std::map< std::string, ConstElementPtr > &v)
bool setValue(const std::string &v)
virtual bool setValue(const long long int v)
bool setValue(const double v)
BoolElement(const bool v, const Position &pos=ZERO_POSITION())
virtual void remove(const int i)
Removes the element at the given position.
std::string file_
File name.
virtual std::string stringValue() const
void add(ElementPtr e)
Adds an ElementPtr to the list.
virtual const std::vector< ElementPtr > & listValue() const
bool empty() const
Return true if there are no elements in the list.
JSONError(const char *file, size_t line, const char *what)
ConstElementPtr find(const std::string &id) const
Recursively finds any data at the given identifier.
bool getValue(double &t) const
Element(int t, const Position &pos=ZERO_POSITION())
Constructor.