Kea  2.1.7-git
data.h
Go to the documentation of this file.
1 // Copyright (C) 2010-2021 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 ISC_DATA_H
8 #define ISC_DATA_H 1
9 
10 #include <iostream>
11 #include <map>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15 
16 #include <boost/shared_ptr.hpp>
17 
18 #include <stdint.h>
19 
20 #include <exceptions/exceptions.h>
21 
22 namespace isc { namespace data {
23 
24 class Element;
25 // todo: describe the rationale behind ElementPtr?
26 typedef boost::shared_ptr<Element> ElementPtr;
27 typedef boost::shared_ptr<const Element> ConstElementPtr;
28 
34 class TypeError : public isc::Exception {
35 public:
36  TypeError(const char* file, size_t line, const char* what) :
37  isc::Exception(file, line, what) {}
38 };
39 
44 // i'd like to use Exception here but we need one that is derived from
45 // runtime_error (as this one is directly based on external data, and
46 // i want to add some values to any static data string that is provided)
47 class JSONError : public isc::Exception {
48 public:
49  JSONError(const char* file, size_t line, const char* what) :
50  isc::Exception(file, line, what) {}
51 };
52 
70 class Element {
71 
72 public:
92  struct Position {
93  std::string file_;
94  uint32_t line_;
95  uint32_t pos_;
96 
98  Position() : file_(""), line_(0), pos_(0) {
99  }
100 
106  Position(const std::string& file, const uint32_t line,
107  const uint32_t pos)
108  : file_(file), line_(line), pos_(pos) {
109  }
110 
114  std::string str() const;
115  };
116 
124  static const Position& ZERO_POSITION() {
125  static Position position("", 0, 0);
126  return (position);
127  }
128 
129 private:
130  // technically the type could be omitted; is it useful?
131  // should we remove it or replace it with a pure virtual
132  // function getType?
133  int type_;
134 
136  Position position_;
137 
138 protected:
139 
146  Element(int t, const Position& pos = ZERO_POSITION())
147  : type_(t), position_(pos) {
148  }
149 
150 
151 public:
152 
153  // any is a special type used in list specifications, specifying
154  // that the elements can be of any type
155  enum types { integer, real, boolean, null, string, list, map, any };
156  // base class; make dtor virtual
157  virtual ~Element() {};
158 
160  int getType() const { return (type_); }
161 
167  const Position& getPosition() const { return (position_); }
168 
176  std::string str() const;
177 
182  std::string toWire() const;
183  void toWire(std::ostream& out) const;
184 
187 #define throwTypeError(error) \
188  { \
189  std::string msg_ = error; \
190  if ((position_.file_ != "") || \
191  (position_.line_ != 0) || \
192  (position_.pos_ != 0)) { \
193  msg_ += " in (" + position_.str() + ")"; \
194  } \
195  isc_throw(TypeError, msg_); \
196  }
197 
199 
201  virtual bool equals(const Element& other) const = 0;
202 
205  virtual void toJSON(std::ostream& ss) const = 0;
206 
214 
215  virtual int64_t intValue() const
216  { throwTypeError("intValue() called on non-integer Element"); };
217  virtual double doubleValue() const
218  { throwTypeError("doubleValue() called on non-double Element"); };
219  virtual bool boolValue() const
220  { throwTypeError("boolValue() called on non-Bool Element"); };
221  virtual std::string stringValue() const
222  { throwTypeError("stringValue() called on non-string Element"); };
223  virtual const std::vector<ElementPtr>& listValue() const {
224  // replace with real exception or empty vector?
225  throwTypeError("listValue() called on non-list Element");
226  };
227  virtual const std::map<std::string, ConstElementPtr>& mapValue() const {
228  // replace with real exception or empty map?
229  throwTypeError("mapValue() called on non-map Element");
230  };
232 
241 
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;
249 
259 
260  virtual bool setValue(const long long int v);
261  bool setValue(const long int i) { return (setValue(static_cast<long long int>(i))); };
262  bool setValue(const int i) { return (setValue(static_cast<long long int>(i))); };
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);
269 
270  // Other functions for specific subtypes
271 
276 
277  virtual ConstElementPtr get(const int i) const;
281 
286  virtual ElementPtr getNonConst(const int i) const;
287 
292  virtual void set(const size_t i, ElementPtr element);
293 
296  virtual void add(ElementPtr element);
297 
301  virtual void remove(const int i);
302 
304  virtual size_t size() const;
305 
307  virtual bool empty() const;
309 
310 
315 
316  virtual ConstElementPtr get(const std::string& name) const;
320 
324  virtual void set(const std::string& name, ConstElementPtr element);
325 
328  virtual void remove(const std::string& name);
329 
333  virtual bool contains(const std::string& name) const;
334 
348  virtual ConstElementPtr find(const std::string& identifier) const;
349 
354  virtual bool find(const std::string& identifier, ConstElementPtr& t) const;
356 
358 
359  // TODO: should we move all factory functions to a different class
360  // so as not to burden the Element base with too many functions?
361  // and/or perhaps even to a separate header?
362 
375 
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 double d,
384  const Position& pos = ZERO_POSITION());
385  static ElementPtr create(const bool b,
386  const Position& pos = ZERO_POSITION());
387  static ElementPtr create(const std::string& s,
388  const Position& pos = ZERO_POSITION());
389  // need both std:string and char *, since c++ will match
390  // bool before std::string when you pass it a char *
391  static ElementPtr create(const char *s,
392  const Position& pos = ZERO_POSITION());
393 
398  static ElementPtr createList(const Position& pos = ZERO_POSITION());
399 
404  static ElementPtr createMap(const Position& pos = ZERO_POSITION());
406 
408 
412 
414  static ElementPtr fromJSON(const std::string& in, bool preproc = false);
421 
431  static ElementPtr fromJSON(std::istream& in, bool preproc = false);
432 
444  static ElementPtr fromJSON(std::istream& in, const std::string& file_name,
445  bool preproc = false);
446 
459  // make this one private?
461  static ElementPtr fromJSON(std::istream& in, const std::string& file,
462  int& line, int &pos);
463 
471  static ElementPtr fromJSONFile(const std::string& file_name,
472  bool preproc = false);
474 
476 
482  static std::string typeToName(Element::types type);
483 
489  static Element::types nameToType(const std::string& type_name);
490 
505  static void preprocess(std::istream& in, std::stringstream& out);
506 
508 
512 
514  static ElementPtr fromWire(std::stringstream& in, int length);
523 
530  static ElementPtr fromWire(const std::string& s);
532 
536  if (type_ == list || type_ == map) {
537  size_t s(size());
538  for (size_t i = 0; i < s; ++i) {
539  // Get child.
540  ElementPtr child;
541  if (type_ == list) {
542  child = getNonConst(i);
543  } else if (type_ == map) {
544  std::string const key(get(i)->stringValue());
545  // The ElementPtr - ConstElementPtr disparity between
546  // ListElement and MapElement is forcing a const cast here.
547  // It's undefined behavior to modify it after const casting.
548  // The options are limited. I've tried templating, moving
549  // this function from a member function to free-standing and
550  // taking the Element template as argument. I've tried
551  // making it a virtual function with overridden
552  // implementations in ListElement and MapElement. Nothing
553  // works.
554  child = boost::const_pointer_cast<Element>(get(key));
555  }
556 
557  // Makes no sense to continue for non-container children.
558  if (child->getType() != list && child->getType() != map) {
559  continue;
560  }
561 
562  // Recurse if not empty.
563  if (!child->empty()){
564  child->removeEmptyContainersRecursively();
565  }
566 
567  // When returning from recursion, remove if empty.
568  if (child->empty()) {
569  remove(i);
570  --i;
571  --s;
572  }
573  }
574  }
575  }
576 };
577 
588 class IntElement : public Element {
589  int64_t i;
590 public:
591  IntElement(int64_t v, const Position& pos = ZERO_POSITION())
592  : Element(integer, pos), i(v) { }
593  int64_t intValue() const { return (i); }
594  using Element::getValue;
595  bool getValue(int64_t& t) const { t = i; return (true); }
596  using Element::setValue;
597  bool setValue(long long int v) { i = v; return (true); }
598  void toJSON(std::ostream& ss) const;
599  bool equals(const Element& other) const;
600 };
601 
602 class DoubleElement : public Element {
603  double d;
604 
605 public:
606  DoubleElement(double v, const Position& pos = ZERO_POSITION())
607  : Element(real, pos), d(v) {};
608  double doubleValue() const { return (d); }
609  using Element::getValue;
610  bool getValue(double& t) const { t = d; return (true); }
611  using Element::setValue;
612  bool setValue(const double v) { d = v; return (true); }
613  void toJSON(std::ostream& ss) const;
614  bool equals(const Element& other) const;
615 };
616 
617 class BoolElement : public Element {
618  bool b;
619 
620 public:
621  BoolElement(const bool v, const Position& pos = ZERO_POSITION())
622  : Element(boolean, pos), b(v) {};
623  bool boolValue() const { return (b); }
624  using Element::getValue;
625  bool getValue(bool& t) const { t = b; return (true); }
626  using Element::setValue;
627  bool setValue(const bool v) { b = v; return (true); }
628  void toJSON(std::ostream& ss) const;
629  bool equals(const Element& other) const;
630 };
631 
632 class NullElement : public Element {
633 public:
634  NullElement(const Position& pos = ZERO_POSITION())
635  : Element(null, pos) {};
636  void toJSON(std::ostream& ss) const;
637  bool equals(const Element& other) const;
638 };
639 
640 class StringElement : public Element {
641  std::string s;
642 
643 public:
644  StringElement(std::string v, const Position& pos = ZERO_POSITION())
645  : Element(string, pos), s(v) {};
646  std::string stringValue() const { return (s); }
647  using Element::getValue;
648  bool getValue(std::string& t) const { t = s; return (true); }
649  using Element::setValue;
650  bool setValue(const std::string& v) { s = v; return (true); }
651  void toJSON(std::ostream& ss) const;
652  bool equals(const Element& other) const;
653 };
654 
655 class ListElement : public Element {
656  std::vector<ElementPtr> l;
657 
658 public:
659  ListElement(const Position& pos = ZERO_POSITION())
660  : Element(list, pos) {}
661  const std::vector<ElementPtr>& listValue() const { return (l); }
662  using Element::getValue;
663  bool getValue(std::vector<ElementPtr>& t) const {
664  t = l;
665  return (true);
666  }
667  using Element::setValue;
668  bool setValue(const std::vector<ElementPtr>& v) {
669  l = v;
670  return (true);
671  }
672  using Element::get;
673  ConstElementPtr get(int i) const { return (l.at(i)); }
674  ElementPtr getNonConst(int i) const { return (l.at(i)); }
675  using Element::set;
676  void set(size_t i, ElementPtr e) {
677  l.at(i) = e;
678  }
679  void add(ElementPtr e) { l.push_back(e); };
680  using Element::remove;
681  void remove(int i) { l.erase(l.begin() + i); };
682  void toJSON(std::ostream& ss) const;
683  size_t size() const { return (l.size()); }
684  bool empty() const { return (l.empty()); }
685  bool equals(const Element& other) const;
686 
696  void sort(std::string const& index = std::string());
697 };
698 
699 class MapElement : public Element {
700  std::map<std::string, ConstElementPtr> m;
701 
702 public:
703  MapElement(const Position& pos = ZERO_POSITION()) : Element(map, pos) {}
704  // @todo should we have direct iterators instead of exposing the std::map
705  // here?
706  const std::map<std::string, ConstElementPtr>& mapValue() const override {
707  return (m);
708  }
709  using Element::getValue;
710  bool getValue(std::map<std::string, ConstElementPtr>& t) const override {
711  t = m;
712  return (true);
713  }
714  using Element::setValue;
715  bool setValue(const std::map<std::string, ConstElementPtr>& v) override {
716  m = v;
717  return (true);
718  }
719  using Element::get;
720  ConstElementPtr get(const std::string& s) const override {
721  auto found = m.find(s);
722  return (found != m.end() ? found->second : ConstElementPtr());
723  }
724 
731  ConstElementPtr get(int const i) const override {
732  auto it(m.begin());
733  std::advance(it, i);
734  return create(it->first);
735  }
736 
737  using Element::set;
738  void set(const std::string& key, ConstElementPtr value) override;
739  using Element::remove;
740  void remove(const std::string& s) override { m.erase(s); }
741 
745  void remove(int const i) override {
746  auto it(m.begin());
747  std::advance(it, i);
748  m.erase(it);
749  }
750 
751  bool contains(const std::string& s) const override {
752  return (m.find(s) != m.end());
753  }
754  void toJSON(std::ostream& ss) const override;
755 
756  // we should name the two finds better...
757  // find the element at id; raises TypeError if one of the
758  // elements at path except the one we're looking for is not a
759  // mapelement.
760  // returns an empty element if the item could not be found
761  ConstElementPtr find(const std::string& id) const override;
762 
763  // find the Element at 'id', and store the element pointer in t
764  // returns true if found, or false if not found (either because
765  // it doesn't exist or one of the elements in the path is not
766  // a MapElement)
767  bool find(const std::string& id, ConstElementPtr& t) const override;
768 
772  size_t size() const override {
773  return (m.size());
774  }
775 
776  bool equals(const Element& other) const override;
777 
778  bool empty() const override { return (m.empty()); }
779 };
780 
784 bool isNull(ConstElementPtr p);
785 
793 void removeIdentical(ElementPtr a, ConstElementPtr b);
794 
800 ConstElementPtr removeIdentical(ConstElementPtr a, ConstElementPtr b);
801 
814 void merge(ElementPtr element, ConstElementPtr other);
815 
826 ElementPtr copy(ConstElementPtr from, int level = 100);
827 
833 bool isEquivalent(ConstElementPtr a, ConstElementPtr b);
834 
846 void prettyPrint(ConstElementPtr element, std::ostream& out,
847  unsigned indent = 0, unsigned step = 2);
848 
859 std::string prettyPrint(ConstElementPtr element,
860  unsigned indent = 0, unsigned step = 2);
861 
872 std::ostream& operator<<(std::ostream& out, const Element::Position& pos);
873 
888 std::ostream& operator<<(std::ostream& out, const Element& e);
889 
890 bool operator==(const Element& a, const Element& b);
891 bool operator!=(const Element& a, const Element& b);
892 bool operator<(const Element& a, const Element& b);
893 
894 } // namespace data
895 } // namespace isc
896 
897 #endif // ISC_DATA_H
898 
899 // Local Variables:
900 // mode: c++
901 // End:
virtual std::string stringValue() const
Definition: data.h:221
int64_t intValue() const
Definition: data.h:593
virtual ~Element()
Definition: data.h:157
bool getValue(double &t) const
Definition: data.h:610
bool operator<(Element const &a, Element const &b)
Definition: data.cc:219
bool setValue(const int i)
Definition: data.h:262
uint32_t line_
Line number.
Definition: data.h:94
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
Definition: data.cc:140
bool empty() const
Return true if there are no elements in the list.
Definition: data.h:684
static const Position & ZERO_POSITION()
Returns Position object with line_ and pos_ set to 0, and with an empty file name.
Definition: data.h:124
const Position & getPosition() const
Returns position where the data element&#39;s value starts in a configuration string. ...
Definition: data.h:167
bool setValue(const std::vector< ElementPtr > &v)
Definition: data.h:668
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
Definition: data.h:47
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition: data.cc:1279
const std::map< std::string, ConstElementPtr > & mapValue() const override
Definition: data.h:706
bool operator!=(const Element &a, const Element &b)
Definition: data.cc:214
virtual int64_t intValue() const
Definition: data.h:215
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition: data.cc:1274
bool setValue(long long int v)
Definition: data.h:597
virtual bool getValue(int64_t &t) const
Definition: data.cc:70
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
ListElement(const Position &pos=ZERO_POSITION())
Definition: data.h:659
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError, error)
Definition: data.h:187
void removeIdentical(ElementPtr a, ConstElementPtr b)
Remove all values from the first ElementPtr that are equal in the second.
Definition: data.cc:1088
StringElement(std::string v, const Position &pos=ZERO_POSITION())
Definition: data.h:644
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
Definition: data.cc:1083
double doubleValue() const
Definition: data.h:608
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
Definition: data.h:710
Position()
Default constructor.
Definition: data.h:98
Position(const std::string &file, const uint32_t line, const uint32_t pos)
Constructor.
Definition: data.h:106
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
bool getValue(bool &t) const
Definition: data.h:625
bool setValue(const std::map< std::string, ConstElementPtr > &v) override
Definition: data.h:715
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1152
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
Definition: data.cc:45
size_t size() const override
Returns number of stored elements.
Definition: data.h:772
bool setValue(const bool v)
Definition: data.h:627
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
Definition: data.h:227
Notes: IntElement type is changed to int64_t.
Definition: data.h:588
TypeError(const char *file, size_t line, const char *what)
Definition: data.h:36
IntElement(int64_t v, const Position &pos=ZERO_POSITION())
Definition: data.h:591
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
virtual const std::vector< ElementPtr > & listValue() const
Definition: data.h:223
bool getValue(int64_t &t) const
Definition: data.h:595
void removeEmptyContainersRecursively()
Remove all empty maps and lists from this Element and its descendants.
Definition: data.h:535
NullElement(const Position &pos=ZERO_POSITION())
Definition: data.h:634
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition: data.h:34
DoubleElement(double v, const Position &pos=ZERO_POSITION())
Definition: data.h:606
MapElement(const Position &pos=ZERO_POSITION())
Definition: data.h:703
Represents the position of the data element within a configuration string.
Definition: data.h:92
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.
bool boolValue() const
Definition: data.h:623
uint32_t pos_
Position within the line.
Definition: data.h:95
bool contains(const std::string &s) const override
Checks if there is data at the given key.
Definition: data.h:751
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
Definition: data.cc:1134
bool setValue(const long int i)
Definition: data.h:261
virtual double doubleValue() const
Definition: data.h:217
bool getValue(std::string &t) const
Definition: data.h:648
int getType() const
Definition: data.h:160
virtual ConstElementPtr get(const int i) const
Returns the ElementPtr at the given index.
Definition: data.cc:130
The Element class represents a piece of data, used by the command channel and configuration parts...
Definition: data.h:70
bool empty() const override
Return true if there are no elements in the list.
Definition: data.h:778
bool setValue(const std::string &v)
Definition: data.h:650
ElementPtr getNonConst(int i) const
returns element as non-const pointer
Definition: data.h:674
bool getValue(std::vector< ElementPtr > &t) const
Definition: data.h:663
virtual bool setValue(const long long int v)
Definition: data.cc:100
bool setValue(const double v)
Definition: data.h:612
BoolElement(const bool v, const Position &pos=ZERO_POSITION())
Definition: data.h:621
size_t size() const
Returns the number of elements in the list.
Definition: data.h:683
virtual void remove(const int i)
Removes the element at the given position.
Definition: data.cc:150
std::string file_
File name.
Definition: data.h:93
void add(ElementPtr e)
Adds an ElementPtr to the list.
Definition: data.h:679
virtual bool boolValue() const
Definition: data.h:219
JSONError(const char *file, size_t line, const char *what)
Definition: data.h:49
Element(int t, const Position &pos=ZERO_POSITION())
Constructor.
Definition: data.h:146
const std::vector< ElementPtr > & listValue() const
Definition: data.h:661
std::string stringValue() const
Definition: data.h:646