dmlite  0.6
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 <stdint.h>
8 #include <boost/any.hpp>
9 #include <boost/property_tree/ptree.hpp>
10 #include <dmlite/common/errno.h>
11 #include <dmlite/cpp/exceptions.h>
12 #include <map>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 namespace dmlite {
18 
19  /// Helpful typedef for KeyValue containers
20  struct Extensible {
21  private:
22  typedef std::pair<std::string, boost::any> EntryType_;
23  typedef std::vector<EntryType_> DictType_;
25 
26  void populate(const boost::property_tree::ptree& root);
27 
28  public:
29  /// Converts an any to a boolean, casting if needed.
30  static bool anyToBoolean (const boost::any& any);
31  /// Converts an any to an unsigned, casting if needed.
32  static unsigned anyToUnsigned(const boost::any& any);
33  /// Converts an any to a long, casting if needed.
34  static long anyToLong (const boost::any& any);
35  /// Converts an any to a double, casting if needed.
36  static double anyToDouble (const boost::any& any);
37  /// Converts an any to a string, casting if needed.
38  static std::string anyToString (const boost::any& any);
39  /// Converts an any to a int64_t
40  static int64_t anyToS64 (const boost::any& any);
41  /// Converts an any to a uint64_t
42  static uint64_t anyToU64 (const boost::any& any);
43 
44  /// Returns true if there is a field name "key".
45  bool hasField(const std::string& key) const;
46 
47  /// Returns a reference to the value associated with "key".
48  /// Will throw DmException(DM_INVALID_VALUE,...) when not found.
49  const boost::any& operator [] (const std::string& key) const throw (DmException);
50 
51  /// Returns a modifiable reference to the value associated with "key".
52  /// Will create the entry if it does not exist.
53  boost::any& operator [] (const std::string& key);
54 
55  // Comparison operators. Containers may need them.
56  bool operator == (const Extensible&) const;
57  bool operator != (const Extensible&) const;
58  bool operator > (const Extensible&) const;
59  bool operator < (const Extensible&) const;
60 
61  /// Number of elements inside this Extensible.
62  unsigned long size() const;
63 
64  /// Removes all the content.
65  void clear();
66 
67  /// Copies the content from another Extensible
68  void copy(const Extensible& s);
69 
70  /// Removes an entry.
71  void erase(const std::string&);
72 
73  /// Serializes to JSON. In principle, it only supports POD.
74  std::string serialize(void) const;
75 
76  /// Deserializes from a JSON string.
77  void deserialize(const std::string& serial) throw (DmException);
78 
79  /// Get all the keys available
80  std::vector<std::string> getKeys(void) const throw (DmException);
81 
82  /// Gets a boolean. May be able to perform some conversions.
83  bool getBool(const std::string& key, bool defaultValue = false) const throw (DmException);
84 
85  /// Gets an integer. May be able to perform some conversions.
86  long getLong(const std::string& key, long defaultValue = 0) const throw (DmException);
87 
88  /// Gets an unsigned integer. May be able to perform some conversions.
89  unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const throw (DmException);
90 
91  /// Gets a float. May be able to perform some conversions.
92  double getDouble(const std::string& key, double defaultValue = 0) const throw (DmException);
93 
94  /// Gets a signed 64 bits type
95  int64_t getS64(const std::string& key, int64_t defaultValue = 0) const throw (DmException);
96 
97  /// Gets an unsigned 64 bits type
98  uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const throw (DmException);
99 
100  /// Gets a string. May perform some conversions.
101  std::string getString(const std::string& key, const std::string& defaultValue = "") const throw (DmException);
102 
103  /// Gets a nested dictionary.
104  Extensible getExtensible(const std::string& key,
105  const Extensible& defaultValue = Extensible()) const throw (DmException);
106 
107  /// Gets an array.
108  std::vector<boost::any> getVector(const std::string& key,
109  const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const throw (DmException);
110 
111  /// Iterators
113 
114  const_iterator begin() const { return dictionary_.begin(); }
115  const_iterator end() const { return dictionary_.end(); }
116  };
117 
118 };
119 
120 #endif // DMLITE_CPP_UTILS_TYPES_H