dmlite  0.4
extensible.h
Go to the documentation of this file.
1 /// @file include/dmlite/cpp/utils/extensible.h
2 /// @brief Extensible types (hold metadata).
3 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
4 #ifndef DMLITE_CPP_UTILS_EXTENSIBLE_H
5 #define DMLITE_CPP_UTILS_EXTENSIBLE_H
6 
7 #include <boost/any.hpp>
8 #include <boost/property_tree/ptree.hpp>
9 #include <dmlite/common/errno.h>
10 #include <dmlite/cpp/exceptions.h>
11 #include <map>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15 
16 namespace dmlite {
17 
18  /// Helpful typedef for KeyValue containers
19  struct Extensible {
20  private:
21  typedef std::map<std::string, boost::any> DictType_;
23 
24  void populate(const boost::property_tree::ptree& root);
25 
26  public:
27  /// Converts an any to a boolean, casting if needed.
28  static bool anyToBoolean (const boost::any& any);
29  /// Converts an any to an unsigned, casting if needed.
30  static unsigned anyToUnsigned(const boost::any& any);
31  /// Converts an any to a long, casting if needed.
32  static long anyToLong (const boost::any& any);
33  /// Converts an any to a double, casting if needed.
34  static double anyToDouble (const boost::any& any);
35  /// Converts an any to a string, casting if needed.
36  static std::string anyToString (const boost::any& any);
37 
38  /// Returns true if there is a field name "key".
39  bool hasField(const std::string& key) const;
40 
41  /// Returns a reference to the value associated with "key".
42  /// Will throw DmException(DM_INVALID_VALUE,...) when not found.
43  const boost::any& operator [] (const std::string& key) const throw (DmException);
44 
45  /// Returns a modifiable reference to the value associated with "key".
46  /// Will create the entry if it does not exist.
47  boost::any& operator [] (const std::string& key);
48 
49  // Comparison operators. Containers may need them.
50  bool operator == (const Extensible&) const;
51  bool operator != (const Extensible&) const;
52  bool operator > (const Extensible&) const;
53  bool operator < (const Extensible&) const;
54 
55  /// Number of elements inside this Extensible.
56  unsigned long size() const;
57 
58  /// Removes all the content.
59  void clear();
60 
61  /// Copies the content from another Extensible
62  void copy(const Extensible& s);
63 
64  /// Removes an entry.
65  void erase(const std::string&);
66 
67  /// Serializes to JSON. In principle, it only supports POD.
68  std::string serialize(void) const;
69 
70  /// Deserializes from a JSON string.
71  void deserialize(const std::string& serial) throw (DmException);
72 
73  /// Get all the keys available
74  std::vector<std::string> getKeys(void) const throw (DmException);
75 
76  /// Gets a boolean. May be able to perform some conversions.
77  bool getBool(const std::string& key) const throw (DmException);
78 
79  /// Gets an integer. May be able to perform some conversions.
80  long getLong(const std::string& key) const throw (DmException);
81 
82  /// Gets an unsigned integer. May be able to perform some conversions.
83  unsigned long getUnsigned(const std::string& key) const throw (DmException);
84 
85  /// Gets a float. May be able to perform some conversions.
86  double getDouble(const std::string& key) const throw (DmException);
87 
88  /// Gets a string. May perform some conversions.
89  std::string getString(const std::string& key) const throw (DmException);
90 
91  /// Gets a nested dictionary.
92  Extensible getExtensible(const std::string& key) const throw (DmException);
93 
94  /// Gets an array.
95  std::vector<boost::any> getVector(const std::string& key) const throw (DmException);
96  };
97 
98 };
99 
100 #endif // DMLITE_CPP_UTILS_TYPES_H