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  /// Note: This will call clear first!
69  void copy(const Extensible& s);
70 
71  /// Removes an entry.
72  void erase(const std::string&);
73 
74  /// Serializes to JSON. In principle, it only supports POD.
75  std::string serialize(void) const;
76 
77  /// Deserializes from a JSON string.
78  void deserialize(const std::string& serial) throw (DmException);
79 
80  /// Get all the keys available
81  std::vector<std::string> getKeys(void) const throw (DmException);
82 
83  /// Gets a boolean. May be able to perform some conversions.
84  bool getBool(const std::string& key, bool defaultValue = false) const throw (DmException);
85 
86  /// Gets an integer. May be able to perform some conversions.
87  long getLong(const std::string& key, long defaultValue = 0) const throw (DmException);
88 
89  /// Gets an unsigned integer. May be able to perform some conversions.
90  unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const throw (DmException);
91 
92  /// Gets a float. May be able to perform some conversions.
93  double getDouble(const std::string& key, double defaultValue = 0) const throw (DmException);
94 
95  /// Gets a signed 64 bits type
96  int64_t getS64(const std::string& key, int64_t defaultValue = 0) const throw (DmException);
97 
98  /// Gets an unsigned 64 bits type
99  uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const throw (DmException);
100 
101  /// Gets a string. May perform some conversions.
102  std::string getString(const std::string& key, const std::string& defaultValue = "") const throw (DmException);
103 
104  /// Gets a nested dictionary.
105  Extensible getExtensible(const std::string& key,
106  const Extensible& defaultValue = Extensible()) const throw (DmException);
107 
108  /// Gets an array.
109  std::vector<boost::any> getVector(const std::string& key,
110  const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const throw (DmException);
111 
112  /// Iterators
114 
115  const_iterator begin() const { return dictionary_.begin(); }
116  const_iterator end() const { return dictionary_.end(); }
117  };
118 
119 };
120 
121 #endif // DMLITE_CPP_UTILS_TYPES_H
static std::string anyToString(const boost::any &any)
Converts an any to a string, casting if needed.
uint64_t getU64(const std::string &key, uint64_t defaultValue=0) const
Gets an unsigned 64 bits type.
static double anyToDouble(const boost::any &any)
Converts an any to a double, casting if needed.
Error codes.
DictType_::const_iterator const_iterator
Iterators.
Definition: extensible.h:113
static bool anyToBoolean(const boost::any &any)
Converts an any to a boolean, casting if needed.
const_iterator begin() const
Definition: extensible.h:115
void erase(const std::string &)
Removes an entry.
Base exception class.
Definition: exceptions.h:17
std::vector< boost::any > getVector(const std::string &key, const std::vector< boost::any > &defaultValue=std::vector< boost::any >()) const
Gets an array.
double getDouble(const std::string &key, double defaultValue=0) const
Gets a float. May be able to perform some conversions.
std::vector< std::string > getKeys(void) const
Get all the keys available.
std::vector< EntryType_ > DictType_
Definition: extensible.h:23
bool getBool(const std::string &key, bool defaultValue=false) const
Gets a boolean. May be able to perform some conversions.
std::string serialize(void) const
Serializes to JSON. In principle, it only supports POD.
const_iterator end() const
Definition: extensible.h:116
void clear()
Removes all the content.
std::string getString(const std::string &key, const std::string &defaultValue="") const
Gets a string. May perform some conversions.
Exceptions used by the API.
bool operator<(const Extensible &) const
DictType_ dictionary_
Definition: extensible.h:24
Helpful typedef for KeyValue containers.
Definition: extensible.h:20
Extensible getExtensible(const std::string &key, const Extensible &defaultValue=Extensible()) const
Gets a nested dictionary.
unsigned long getUnsigned(const std::string &key, unsigned long defaultValue=0) const
Gets an unsigned integer. May be able to perform some conversions.
void populate(const boost::property_tree::ptree &root)
static uint64_t anyToU64(const boost::any &any)
Converts an any to a uint64_t.
std::pair< std::string, boost::any > EntryType_
Definition: extensible.h:22
void copy(const Extensible &s)
bool hasField(const std::string &key) const
Returns true if there is a field name &quot;key&quot;.
bool operator==(const Extensible &) const
unsigned long size() const
Number of elements inside this Extensible.
int64_t getS64(const std::string &key, int64_t defaultValue=0) const
Gets a signed 64 bits type.
void deserialize(const std::string &serial)
Deserializes from a JSON string.
static long anyToLong(const boost::any &any)
Converts an any to a long, casting if needed.
bool operator!=(const Extensible &) const
long getLong(const std::string &key, long defaultValue=0) const
Gets an integer. May be able to perform some conversions.
bool operator>(const Extensible &) const
static int64_t anyToS64(const boost::any &any)
Converts an any to a int64_t.
static unsigned anyToUnsigned(const boost::any &any)
Converts an any to an unsigned, casting if needed.
const boost::any & operator[](const std::string &key) const