data.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // Copyright (C) 2010, 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef ISC_DATA_H
  15. #define ISC_DATA_H 1
  16. #include <stdint.h>
  17. #include <string>
  18. #include <vector>
  19. #include <map>
  20. #include <boost/shared_ptr.hpp>
  21. #include <stdexcept>
  22. #include <exceptions/exceptions.h>
  23. namespace isc { namespace data {
  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. ///
  29. /// \brief A standard Data module exception that is thrown if a function
  30. /// is called for an Element that has a wrong type (e.g. int_value on a
  31. /// ListElement)
  32. ///
  33. class TypeError : public isc::Exception {
  34. public:
  35. TypeError(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) {}
  37. };
  38. ///
  39. /// \brief A standard Data module exception that is thrown if a parse
  40. /// error is encountered when constructing an Element from a string
  41. ///
  42. // i'd like to use Exception here but we need one that is derived from
  43. // runtime_error (as this one is directly based on external data, and
  44. // i want to add some values to any static data string that is provided)
  45. class JSONError : public isc::Exception {
  46. public:
  47. JSONError(const char* file, size_t line, const char* what) :
  48. isc::Exception(file, line, what) {}
  49. };
  50. ///
  51. /// \brief The \c Element class represents a piece of data, used by
  52. /// the command channel and configuration parts.
  53. ///
  54. /// An \c Element can contain simple types (int, real, string, bool and
  55. /// None), and composite types (list and string->element maps)
  56. ///
  57. /// Elements should in calling functions usually be referenced through
  58. /// an \c ElementPtr, which can be created using the factory functions
  59. /// \c Element::create() and \c Element::fromJSON()
  60. ///
  61. /// Notes to developers: Element is a base class, implemented by a
  62. /// specific subclass for each type (IntElement, BoolElement, etc).
  63. /// Element does define all functions for all types, and defaults to
  64. /// raising a \c TypeError for functions that are not supported for
  65. /// the type in question.
  66. ///
  67. class Element {
  68. public:
  69. /// \brief Represents the position of the data element within a
  70. /// configuration string.
  71. ///
  72. /// Position comprises a file name, line number and an offset within this
  73. /// line where the element value starts. For example, if the JSON string is
  74. ///
  75. /// \code
  76. /// { "foo": "some string",
  77. /// "bar": 123 }
  78. /// \endcode
  79. ///
  80. /// the position of the element "bar" is: line_ = 2; pos_ = 9, because
  81. /// begining of the value "123" is at offset 9 from the beginning of
  82. /// the second line, including whitespaces.
  83. ///
  84. /// Note that the @c Position structure is used as an argument to @c Element
  85. /// constructors and factory functions to avoid ambiguity and so that the
  86. /// uint32_t arguments holding line number and position within the line are
  87. /// not confused with the @c Element values passed to these functions.
  88. struct Position {
  89. std::string file_; ///< File name.
  90. uint32_t line_; ///< Line number.
  91. uint32_t pos_; ///< Position within the line.
  92. /// \brief Default constructor.
  93. Position() : file_(""), line_(0), pos_(0) {
  94. }
  95. /// \brief Constructor.
  96. ///
  97. /// \param file File name.
  98. /// \param line Line number.
  99. /// \param pos Position within the line.
  100. Position(const std::string& file, const uint32_t line,
  101. const uint32_t pos)
  102. : file_(file), line_(line), pos_(pos) {
  103. }
  104. /// \brief Returns the position in the textual format.
  105. ///
  106. /// The returned position has the following format: file:line:pos.
  107. std::string str() const;
  108. };
  109. /// \brief Returns @c Position object with line_ and pos_ set to 0, and
  110. /// with an empty file name.
  111. ///
  112. /// The object containing two zeros is a default for most of the
  113. /// methods creating @c Element objects. The returned value is static
  114. /// so as it is not created everytime the function with the default
  115. /// position argument is called.
  116. static const Position& ZERO_POSITION() {
  117. static Position position("", 0, 0);
  118. return (position);
  119. }
  120. private:
  121. // technically the type could be omitted; is it useful?
  122. // should we remove it or replace it with a pure virtual
  123. // function getType?
  124. int type_;
  125. /// \brief Position of the element in the configuration string.
  126. Position position_;
  127. protected:
  128. /// \brief Constructor.
  129. ///
  130. /// \param t Element type.
  131. /// \param pos Structure holding position of the value of the data element.
  132. /// It comprises the line number and the position within this line. The values
  133. /// held in this structure are used for error logging purposes.
  134. Element(int t, const Position& pos = ZERO_POSITION())
  135. : type_(t), position_(pos) {
  136. }
  137. public:
  138. // any is a special type used in list specifications, specifying
  139. // that the elements can be of any type
  140. enum types { integer, real, boolean, null, string, list, map, any };
  141. // base class; make dtor virtual
  142. virtual ~Element() {};
  143. /// \return the type of this element
  144. int getType() const { return (type_); }
  145. /// \brief Returns position where the data element's value starts in a
  146. /// configuration string.
  147. ///
  148. /// @warning The returned reference is valid as long as the object which
  149. /// created it lives.
  150. const Position& getPosition() const { return (position_); }
  151. /// Returns a string representing the Element and all its
  152. /// child elements; note that this is different from stringValue(),
  153. /// which only returns the single value of a StringElement
  154. ///
  155. /// The resulting string will contain the Element in JSON format.
  156. ///
  157. /// \return std::string containing the string representation
  158. std::string str() const;
  159. /// Returns the wireformat for the Element and all its child
  160. /// elements.
  161. ///
  162. /// \return std::string containing the element in wire format
  163. std::string toWire() const;
  164. void toWire(std::ostream& out) const;
  165. /// \name pure virtuals, every derived class must implement these
  166. /// \return true if the other ElementPtr has the same type and value
  167. virtual bool equals(const Element& other) const = 0;
  168. /// Converts the Element to JSON format and appends it to
  169. /// the given stringstream.
  170. virtual void toJSON(std::ostream& ss) const = 0;
  171. /// \name Type-specific getters
  172. ///
  173. /// \brief These functions only
  174. /// work on their corresponding Element type. For all other
  175. /// types, a TypeError is thrown.
  176. /// If you want an exception-safe getter method, use
  177. /// getValue() below
  178. //@{
  179. virtual int64_t intValue() const
  180. { isc_throw(TypeError, "intValue() called on non-integer Element"); };
  181. virtual double doubleValue() const
  182. { isc_throw(TypeError, "doubleValue() called on non-double Element"); };
  183. virtual bool boolValue() const
  184. { isc_throw(TypeError, "boolValue() called on non-Bool Element"); };
  185. virtual std::string stringValue() const
  186. { isc_throw(TypeError, "stringValue() called on non-string Element"); };
  187. virtual const std::vector<ConstElementPtr>& listValue() const {
  188. // replace with real exception or empty vector?
  189. isc_throw(TypeError, "listValue() called on non-list Element");
  190. };
  191. virtual const std::map<std::string, ConstElementPtr>& mapValue() const {
  192. // replace with real exception or empty map?
  193. isc_throw(TypeError, "mapValue() called on non-map Element");
  194. };
  195. //@}
  196. /// \name Exception-safe getters
  197. ///
  198. /// \brief The getValue() functions return false if the given reference
  199. /// is of another type than the element contains
  200. /// By default it always returns false; the derived classes
  201. /// override the function for their type, copying their
  202. /// data to the given reference and returning true
  203. ///
  204. //@{
  205. virtual bool getValue(int64_t& t) const;
  206. virtual bool getValue(double& t) const;
  207. virtual bool getValue(bool& t) const;
  208. virtual bool getValue(std::string& t) const;
  209. virtual bool getValue(std::vector<ConstElementPtr>& t) const;
  210. virtual bool getValue(std::map<std::string, ConstElementPtr>& t) const;
  211. //@}
  212. ///
  213. /// \name Exception-safe setters.
  214. ///
  215. /// \brief Return false if the Element is not
  216. /// the right type. Set the value and return true if the Elements
  217. /// is of the correct type
  218. ///
  219. /// Notes: Read notes of IntElement definition about the use of
  220. /// long long int, long int and int.
  221. //@{
  222. virtual bool setValue(const long long int v);
  223. bool setValue(const long int i) { return (setValue(static_cast<long long int>(i))); };
  224. bool setValue(const int i) { return (setValue(static_cast<long long int>(i))); };
  225. virtual bool setValue(const double v);
  226. virtual bool setValue(const bool t);
  227. virtual bool setValue(const std::string& v);
  228. virtual bool setValue(const std::vector<ConstElementPtr>& v);
  229. virtual bool setValue(const std::map<std::string, ConstElementPtr>& v);
  230. //@}
  231. // Other functions for specific subtypes
  232. /// \name ListElement functions
  233. ///
  234. /// \brief If the Element on which these functions are called are not
  235. /// an instance of ListElement, a TypeError exception is thrown.
  236. //@{
  237. /// Returns the ElementPtr at the given index. If the index is out
  238. /// of bounds, this function throws an std::out_of_range exception.
  239. /// \param i The position of the ElementPtr to return
  240. virtual ConstElementPtr get(const int i) const;
  241. /// Sets the ElementPtr at the given index. If the index is out
  242. /// of bounds, this function throws an std::out_of_range exception.
  243. /// \param i The position of the ElementPtr to set
  244. /// \param element The ElementPtr to set at the position
  245. virtual void set(const size_t i, ConstElementPtr element);
  246. /// Adds an ElementPtr to the list
  247. /// \param element The ElementPtr to add
  248. virtual void add(ConstElementPtr element);
  249. /// Removes the element at the given position. If the index is out
  250. /// of nothing happens.
  251. /// \param i The index of the element to remove.
  252. virtual void remove(const int i);
  253. /// Returns the number of elements in the list.
  254. virtual size_t size() const;
  255. /// Return true if there are no elements in the list.
  256. virtual bool empty() const;
  257. //@}
  258. /// \name MapElement functions
  259. ///
  260. /// \brief If the Element on which these functions are called are not
  261. /// an instance of MapElement, a TypeError exception is thrown.
  262. //@{
  263. /// Returns the ElementPtr at the given key
  264. /// \param name The key of the Element to return
  265. /// \return The ElementPtr at the given key, or null if not present
  266. virtual ConstElementPtr get(const std::string& name) const;
  267. /// Sets the ElementPtr at the given key
  268. /// \param name The key of the Element to set
  269. /// \param element The ElementPtr to set at the given key.
  270. virtual void set(const std::string& name, ConstElementPtr element);
  271. /// Remove the ElementPtr at the given key
  272. /// \param name The key of the Element to remove
  273. virtual void remove(const std::string& name);
  274. /// Checks if there is data at the given key
  275. /// \param name The key of the Element to remove
  276. /// \return true if there is data at the key, false if not.
  277. virtual bool contains(const std::string& name) const;
  278. /// Recursively finds any data at the given identifier. The
  279. /// identifier is a /-separated list of names of nested maps, with
  280. /// the last name being the leaf that is returned.
  281. ///
  282. /// For instance, if you have a MapElement that contains another
  283. /// MapElement at the key "foo", and that second MapElement contains
  284. /// Another Element at key "bar", the identifier for that last
  285. /// element from the first is "foo/bar".
  286. ///
  287. /// \param identifier The identifier of the element to find
  288. /// \return The ElementPtr at the given identifier. Returns a
  289. /// null ElementPtr if it is not found, which can be checked with
  290. /// Element::is_null(ElementPtr e).
  291. virtual ConstElementPtr find(const std::string& identifier) const;
  292. /// See \c Element::find()
  293. /// \param identifier The identifier of the element to find
  294. /// \param t Reference to store the resulting ElementPtr, if found.
  295. /// \return true if the element was found, false if not.
  296. virtual bool find(const std::string& identifier, ConstElementPtr& t) const;
  297. //@}
  298. /// \name Factory functions
  299. // TODO: should we move all factory functions to a different class
  300. // so as not to burden the Element base with too many functions?
  301. // and/or perhaps even to a separate header?
  302. /// \name Direct factory functions
  303. /// \brief These functions simply wrap the given data directly
  304. /// in an Element object, and return a reference to it, in the form
  305. /// of an \c ElementPtr.
  306. /// These factory functions are exception-free (unless there is
  307. /// no memory available, in which case bad_alloc is raised by the
  308. /// underlying system).
  309. /// (Note that that is different from an NullElement, which
  310. /// represents an empty value, and is created with Element::create())
  311. ///
  312. /// Notes: Read notes of IntElement definition about the use of
  313. /// long long int, long int and int.
  314. //@{
  315. static ElementPtr create(const Position& pos = ZERO_POSITION());
  316. static ElementPtr create(const long long int i,
  317. const Position& pos = ZERO_POSITION());
  318. static ElementPtr create(const int i,
  319. const Position& pos = ZERO_POSITION());
  320. static ElementPtr create(const long int i,
  321. const Position& pos = ZERO_POSITION());
  322. static ElementPtr create(const double d,
  323. const Position& pos = ZERO_POSITION());
  324. static ElementPtr create(const bool b,
  325. const Position& pos = ZERO_POSITION());
  326. static ElementPtr create(const std::string& s,
  327. const Position& pos = ZERO_POSITION());
  328. // need both std:string and char *, since c++ will match
  329. // bool before std::string when you pass it a char *
  330. static ElementPtr create(const char *s,
  331. const Position& pos = ZERO_POSITION());
  332. /// \brief Creates an empty ListElement type ElementPtr.
  333. ///
  334. /// \param pos A structure holding position of the data element value
  335. /// in the configuration string. It is used for error logging purposes.
  336. static ElementPtr createList(const Position& pos = ZERO_POSITION());
  337. /// \brief Creates an empty MapElement type ElementPtr.
  338. ///
  339. /// \param pos A structure holding position of the data element value
  340. /// in the configuration string. It is used for error logging purposes.
  341. static ElementPtr createMap(const Position& pos = ZERO_POSITION());
  342. //@}
  343. /// \name Compound factory functions
  344. /// \brief These functions will parse the given string (JSON)
  345. /// representation of a compound element. If there is a parse
  346. /// error, an exception of the type isc::data::JSONError is thrown.
  347. //@{
  348. /// Creates an Element from the given JSON string
  349. /// \param in The string to parse the element from
  350. /// \param preproc specified whether preprocessing (e.g. comment removal)
  351. /// should be performed
  352. /// \return An ElementPtr that contains the element(s) specified
  353. /// in the given string.
  354. static ElementPtr fromJSON(const std::string& in, bool preproc = false);
  355. /// Creates an Element from the given input stream containing JSON
  356. /// formatted data.
  357. ///
  358. /// \param in The string to parse the element from
  359. /// \param preproc specified whether preprocessing (e.g. comment removal)
  360. /// should be performed
  361. /// \return An ElementPtr that contains the element(s) specified
  362. /// in the given input stream.
  363. static ElementPtr fromJSON(std::istream& in, bool preproc = false)
  364. throw(JSONError);
  365. /// Creates an Element from the given input stream containing JSON
  366. /// formatted data.
  367. ///
  368. /// \param in The string to parse the element from
  369. /// \param file_name specified input file name (used in error reporting)
  370. /// \param preproc specified whether preprocessing (e.g. comment removal)
  371. /// should be performed
  372. /// \return An ElementPtr that contains the element(s) specified
  373. /// in the given input stream.
  374. static ElementPtr fromJSON(std::istream& in, const std::string& file_name,
  375. bool preproc = false)
  376. throw(JSONError);
  377. /// Creates an Element from the given input stream, where we keep
  378. /// track of the location in the stream for error reporting.
  379. ///
  380. /// \param in The string to parse the element from.
  381. /// \param file The input file name.
  382. /// \param line A reference to the int where the function keeps
  383. /// track of the current line.
  384. /// \param pos A reference to the int where the function keeps
  385. /// track of the current position within the current line.
  386. /// \return An ElementPtr that contains the element(s) specified
  387. /// in the given input stream.
  388. // make this one private?
  389. static ElementPtr fromJSON(std::istream& in, const std::string& file,
  390. int& line, int &pos)
  391. throw(JSONError);
  392. /// Reads contents of specified file and interprets it as JSON.
  393. ///
  394. /// @param file_name name of the file to read
  395. /// @param preproc specified whether preprocessing (e.g. comment removal)
  396. /// should be performed
  397. /// @return An ElementPtr that contains the element(s) specified
  398. /// if the given file.
  399. static ElementPtr fromJSONFile(const std::string& file_name,
  400. bool preproc = false);
  401. //@}
  402. /// \name Type name conversion functions
  403. /// Returns the name of the given type as a string
  404. ///
  405. /// \param type The type to return the name of
  406. /// \return The name of the type, or "unknown" if the type
  407. /// is not known.
  408. static std::string typeToName(Element::types type);
  409. /// Converts the string to the corresponding type
  410. /// Throws a TypeError if the name is unknown.
  411. ///
  412. /// \param type_name The name to get the type of
  413. /// \return the corresponding type value
  414. static Element::types nameToType(const std::string& type_name);
  415. /// \brief input text preprocessor
  416. ///
  417. /// This method performs preprocessing of the input stream (which is
  418. /// expected to contain a text version of to be parsed JSON). For now the
  419. /// sole supported operation is bash-style (line starting with #) comment
  420. /// removal, but it will be extended later to cover more cases (C, C++ style
  421. /// comments, file inclusions, maybe macro replacements?).
  422. ///
  423. /// This method processes the whole input stream. It reads all contents of
  424. /// the input stream, filters the content and returns the result in a
  425. /// different stream.
  426. ///
  427. /// @param in input stream to be preprocessed
  428. /// @param out output stream (filtered content will be written here)
  429. static void preprocess(std::istream& in, std::stringstream& out);
  430. /// \name Wire format factory functions
  431. /// These function pparse the wireformat at the given stringstream
  432. /// (of the given length). If there is a parse error an exception
  433. /// of the type isc::cc::DecodeError is raised.
  434. //@{
  435. /// Creates an Element from the wire format in the given
  436. /// stringstream of the given length.
  437. /// Since the wire format is JSON, thise is the same as
  438. /// fromJSON, and could be removed.
  439. ///
  440. /// \param in The input stringstream.
  441. /// \param length The length of the wireformat data in the stream
  442. /// \return ElementPtr with the data that is parsed.
  443. static ElementPtr fromWire(std::stringstream& in, int length);
  444. /// Creates an Element from the wire format in the given string
  445. /// Since the wire format is JSON, thise is the same as
  446. /// fromJSON, and could be removed.
  447. ///
  448. /// \param s The input string
  449. /// \return ElementPtr with the data that is parsed.
  450. static ElementPtr fromWire(const std::string& s);
  451. //@}
  452. };
  453. /// Notes: IntElement type is changed to int64_t.
  454. /// Due to C++ problems on overloading and automatic type conversion,
  455. /// (C++ tries to convert integer type values and reference/pointer
  456. /// if value types do not match exactly)
  457. /// We decided the storage as int64_t,
  458. /// three (long long, long, int) override function definitions
  459. /// and cast int/long/long long to int64_t via long long.
  460. /// Therefore, call by value methods (create, setValue) have three
  461. /// (int,long,long long) definitions. Others use int64_t.
  462. ///
  463. class IntElement : public Element {
  464. int64_t i;
  465. private:
  466. public:
  467. IntElement(int64_t v, const Position& pos = ZERO_POSITION())
  468. : Element(integer, pos), i(v) { }
  469. int64_t intValue() const { return (i); }
  470. using Element::getValue;
  471. bool getValue(int64_t& t) const { t = i; return (true); }
  472. using Element::setValue;
  473. bool setValue(long long int v) { i = v; return (true); }
  474. void toJSON(std::ostream& ss) const;
  475. bool equals(const Element& other) const;
  476. };
  477. class DoubleElement : public Element {
  478. double d;
  479. public:
  480. DoubleElement(double v, const Position& pos = ZERO_POSITION())
  481. : Element(real, pos), d(v) {};
  482. double doubleValue() const { return (d); }
  483. using Element::getValue;
  484. bool getValue(double& t) const { t = d; return (true); }
  485. using Element::setValue;
  486. bool setValue(const double v) { d = v; return (true); }
  487. void toJSON(std::ostream& ss) const;
  488. bool equals(const Element& other) const;
  489. };
  490. class BoolElement : public Element {
  491. bool b;
  492. public:
  493. BoolElement(const bool v, const Position& pos = ZERO_POSITION())
  494. : Element(boolean, pos), b(v) {};
  495. bool boolValue() const { return (b); }
  496. using Element::getValue;
  497. bool getValue(bool& t) const { t = b; return (true); }
  498. using Element::setValue;
  499. bool setValue(const bool v) { b = v; return (true); }
  500. void toJSON(std::ostream& ss) const;
  501. bool equals(const Element& other) const;
  502. };
  503. class NullElement : public Element {
  504. public:
  505. NullElement(const Position& pos = ZERO_POSITION())
  506. : Element(null, pos) {};
  507. void toJSON(std::ostream& ss) const;
  508. bool equals(const Element& other) const;
  509. };
  510. class StringElement : public Element {
  511. std::string s;
  512. public:
  513. StringElement(std::string v, const Position& pos = ZERO_POSITION())
  514. : Element(string, pos), s(v) {};
  515. std::string stringValue() const { return (s); }
  516. using Element::getValue;
  517. bool getValue(std::string& t) const { t = s; return (true); }
  518. using Element::setValue;
  519. bool setValue(const std::string& v) { s = v; return (true); }
  520. void toJSON(std::ostream& ss) const;
  521. bool equals(const Element& other) const;
  522. };
  523. class ListElement : public Element {
  524. std::vector<ConstElementPtr> l;
  525. public:
  526. ListElement(const Position& pos = ZERO_POSITION())
  527. : Element(list, pos) {}
  528. const std::vector<ConstElementPtr>& listValue() const { return (l); }
  529. using Element::getValue;
  530. bool getValue(std::vector<ConstElementPtr>& t) const {
  531. t = l;
  532. return (true);
  533. }
  534. using Element::setValue;
  535. bool setValue(const std::vector<ConstElementPtr>& v) {
  536. l = v;
  537. return (true);
  538. }
  539. using Element::get;
  540. ConstElementPtr get(int i) const { return (l.at(i)); }
  541. using Element::set;
  542. void set(size_t i, ConstElementPtr e) {
  543. l.at(i) = e;
  544. }
  545. void add(ConstElementPtr e) { l.push_back(e); };
  546. using Element::remove;
  547. void remove(int i) { l.erase(l.begin() + i); };
  548. void toJSON(std::ostream& ss) const;
  549. size_t size() const { return (l.size()); }
  550. bool empty() const { return (l.empty()); }
  551. bool equals(const Element& other) const;
  552. };
  553. class MapElement : public Element {
  554. std::map<std::string, ConstElementPtr> m;
  555. public:
  556. MapElement(const Position& pos = ZERO_POSITION()) : Element(map, pos) {}
  557. // @todo should we have direct iterators instead of exposing the std::map
  558. // here?
  559. const std::map<std::string, ConstElementPtr>& mapValue() const {
  560. return (m);
  561. }
  562. using Element::getValue;
  563. bool getValue(std::map<std::string, ConstElementPtr>& t) const {
  564. t = m;
  565. return (true);
  566. }
  567. using Element::setValue;
  568. bool setValue(const std::map<std::string, ConstElementPtr>& v) {
  569. m = v;
  570. return (true);
  571. }
  572. using Element::get;
  573. ConstElementPtr get(const std::string& s) const {
  574. return (contains(s) ? m.find(s)->second : ConstElementPtr());
  575. }
  576. using Element::set;
  577. void set(const std::string& key, ConstElementPtr value);
  578. using Element::remove;
  579. void remove(const std::string& s) { m.erase(s); }
  580. bool contains(const std::string& s) const {
  581. return (m.find(s) != m.end());
  582. }
  583. void toJSON(std::ostream& ss) const;
  584. // we should name the two finds better...
  585. // find the element at id; raises TypeError if one of the
  586. // elements at path except the one we're looking for is not a
  587. // mapelement.
  588. // returns an empty element if the item could not be found
  589. ConstElementPtr find(const std::string& id) const;
  590. // find the Element at 'id', and store the element pointer in t
  591. // returns true if found, or false if not found (either because
  592. // it doesn't exist or one of the elements in the path is not
  593. // a MapElement)
  594. bool find(const std::string& id, ConstElementPtr& t) const;
  595. /// @brief Returns number of stored elements
  596. ///
  597. /// @return number of elements.
  598. size_t size() const {
  599. return (m.size());
  600. }
  601. bool equals(const Element& other) const;
  602. };
  603. /// Checks whether the given ElementPtr is a NULL pointer
  604. /// \param p The ElementPtr to check
  605. /// \return true if it is NULL, false if not.
  606. bool isNull(ConstElementPtr p);
  607. ///
  608. /// \brief Remove all values from the first ElementPtr that are
  609. /// equal in the second. Both ElementPtrs MUST be MapElements
  610. /// The use for this function is to end up with a MapElement that
  611. /// only contains new and changed values (for ModuleCCSession and
  612. /// configuration update handlers)
  613. /// Raises a TypeError if a or b are not MapElements
  614. void removeIdentical(ElementPtr a, ConstElementPtr b);
  615. /// \brief Create a new ElementPtr from the first ElementPtr, removing all
  616. /// values that are equal in the second. Both ElementPtrs MUST be MapElements.
  617. /// The returned ElementPtr will be a MapElement that only contains new and
  618. /// changed values (for ModuleCCSession and configuration update handlers).
  619. /// Raises a TypeError if a or b are not MapElements
  620. ConstElementPtr removeIdentical(ConstElementPtr a, ConstElementPtr b);
  621. /// \brief Merges the data from other into element.
  622. /// (on the first level). Both elements must be
  623. /// MapElements.
  624. /// Every string,value pair in other is copied into element
  625. /// (the ElementPtr of value is copied, this is not a new object)
  626. /// Unless the value is a NullElement, in which case the
  627. /// key is removed from element, rather than setting the value to
  628. /// the given NullElement.
  629. /// This way, we can remove values from for instance maps with
  630. /// configuration data (which would then result in reverting back
  631. /// to the default).
  632. /// Raises a TypeError if either ElementPtr is not a MapElement
  633. void merge(ElementPtr element, ConstElementPtr other);
  634. ///
  635. /// \brief Insert Element::Position as a string into stream.
  636. ///
  637. /// This operator converts the \c Element::Position into a string and
  638. /// inserts it into the output stream \c out.
  639. ///
  640. /// \param out A \c std::ostream object on which the insertion operation is
  641. /// performed.
  642. /// \param pos The \c Element::Position structure to insert.
  643. /// \return A reference to the same \c std::ostream object referenced by
  644. /// parameter \c out after the insertion operation.
  645. std::ostream& operator<<(std::ostream& out, const Element::Position& pos);
  646. ///
  647. /// \brief Insert the Element as a string into stream.
  648. ///
  649. /// This method converts the \c ElementPtr into a string with
  650. /// \c Element::str() and inserts it into the
  651. /// output stream \c out.
  652. ///
  653. /// This function overloads the global operator<< to behave as described in
  654. /// ostream::operator<< but applied to \c ElementPtr objects.
  655. ///
  656. /// \param out A \c std::ostream object on which the insertion operation is
  657. /// performed.
  658. /// \param e The \c ElementPtr object to insert.
  659. /// \return A reference to the same \c std::ostream object referenced by
  660. /// parameter \c out after the insertion operation.
  661. std::ostream& operator<<(std::ostream& out, const Element& e);
  662. bool operator==(const Element& a, const Element& b);
  663. bool operator!=(const Element& a, const Element& b);
  664. } }
  665. #endif // ISC_DATA_H
  666. // Local Variables:
  667. // mode: c++
  668. // End: