dmlite
0.6
|
00001 /// @file include/dmlite/cpp/utils/extensible.h 00002 /// @brief Extensible types (hold metadata). 00003 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch> 00004 #ifndef DMLITE_CPP_UTILS_EXTENSIBLE_H 00005 #define DMLITE_CPP_UTILS_EXTENSIBLE_H 00006 00007 #include <boost/any.hpp> 00008 #include <boost/property_tree/ptree.hpp> 00009 #include <dmlite/common/errno.h> 00010 #include <dmlite/cpp/exceptions.h> 00011 #include <map> 00012 #include <stdexcept> 00013 #include <string> 00014 #include <vector> 00015 00016 namespace dmlite { 00017 00018 /// Helpful typedef for KeyValue containers 00019 struct Extensible { 00020 private: 00021 typedef std::pair<std::string, boost::any> EntryType_; 00022 typedef std::vector<EntryType_> DictType_; 00023 DictType_ dictionary_; 00024 00025 void populate(const boost::property_tree::ptree& root); 00026 00027 public: 00028 /// Converts an any to a boolean, casting if needed. 00029 static bool anyToBoolean (const boost::any& any); 00030 /// Converts an any to an unsigned, casting if needed. 00031 static unsigned anyToUnsigned(const boost::any& any); 00032 /// Converts an any to a long, casting if needed. 00033 static long anyToLong (const boost::any& any); 00034 /// Converts an any to a double, casting if needed. 00035 static double anyToDouble (const boost::any& any); 00036 /// Converts an any to a string, casting if needed. 00037 static std::string anyToString (const boost::any& any); 00038 00039 /// Returns true if there is a field name "key". 00040 bool hasField(const std::string& key) const; 00041 00042 /// Returns a reference to the value associated with "key". 00043 /// Will throw DmException(DM_INVALID_VALUE,...) when not found. 00044 const boost::any& operator [] (const std::string& key) const throw (DmException); 00045 00046 /// Returns a modifiable reference to the value associated with "key". 00047 /// Will create the entry if it does not exist. 00048 boost::any& operator [] (const std::string& key); 00049 00050 // Comparison operators. Containers may need them. 00051 bool operator == (const Extensible&) const; 00052 bool operator != (const Extensible&) const; 00053 bool operator > (const Extensible&) const; 00054 bool operator < (const Extensible&) const; 00055 00056 /// Number of elements inside this Extensible. 00057 unsigned long size() const; 00058 00059 /// Removes all the content. 00060 void clear(); 00061 00062 /// Copies the content from another Extensible 00063 void copy(const Extensible& s); 00064 00065 /// Removes an entry. 00066 void erase(const std::string&); 00067 00068 /// Serializes to JSON. In principle, it only supports POD. 00069 std::string serialize(void) const; 00070 00071 /// Deserializes from a JSON string. 00072 void deserialize(const std::string& serial) throw (DmException); 00073 00074 /// Get all the keys available 00075 std::vector<std::string> getKeys(void) const throw (DmException); 00076 00077 /// Gets a boolean. May be able to perform some conversions. 00078 bool getBool(const std::string& key, bool defaultValue = false) const throw (DmException); 00079 00080 /// Gets an integer. May be able to perform some conversions. 00081 long getLong(const std::string& key, long defaultValue = 0) const throw (DmException); 00082 00083 /// Gets an unsigned integer. May be able to perform some conversions. 00084 unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const throw (DmException); 00085 00086 /// Gets a float. May be able to perform some conversions. 00087 double getDouble(const std::string& key, double defaultValue = 0) const throw (DmException); 00088 00089 /// Gets a string. May perform some conversions. 00090 std::string getString(const std::string& key, const std::string& defaultValue = "") const throw (DmException); 00091 00092 /// Gets a nested dictionary. 00093 Extensible getExtensible(const std::string& key, 00094 const Extensible& defaultValue = Extensible()) const throw (DmException); 00095 00096 /// Gets an array. 00097 std::vector<boost::any> getVector(const std::string& key, 00098 const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const throw (DmException); 00099 00100 /// Iterators 00101 typedef DictType_::const_iterator const_iterator; 00102 00103 const_iterator begin() const { return dictionary_.begin(); } 00104 const_iterator end() const { return dictionary_.end(); } 00105 }; 00106 00107 }; 00108 00109 #endif // DMLITE_CPP_UTILS_TYPES_H