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