00001
00002
00003
00004 #ifndef DMLITE_CPP_UTILS_EXTENSIBLE_H
00005 #define DMLITE_CPP_UTILS_EXTENSIBLE_H
00006
00007 #include <stdint.h>
00008 #include <boost/any.hpp>
00009 #include <boost/property_tree/ptree.hpp>
00010 #include <dmlite/common/errno.h>
00011 #include <dmlite/cpp/exceptions.h>
00012 #include <map>
00013 #include <stdexcept>
00014 #include <string>
00015 #include <vector>
00016
00017 namespace dmlite {
00018
00019
00020 struct Extensible {
00021 private:
00022 typedef std::pair<std::string, boost::any> EntryType_;
00023 typedef std::vector<EntryType_> DictType_;
00024 DictType_ dictionary_;
00025
00026 void populate(const boost::property_tree::ptree& root);
00027
00028 public:
00029
00030 static bool anyToBoolean (const boost::any& any);
00031
00032 static unsigned anyToUnsigned(const boost::any& any);
00033
00034 static long anyToLong (const boost::any& any);
00035
00036 static double anyToDouble (const boost::any& any);
00037
00038 static std::string anyToString (const boost::any& any);
00039
00040 static int64_t anyToS64 (const boost::any& any);
00041
00042 static uint64_t anyToU64 (const boost::any& any);
00043
00044
00045 bool hasField(const std::string& key) const;
00046
00047
00048
00049 const boost::any& operator [] (const std::string& key) const ;
00050
00051
00052
00053 boost::any& operator [] (const std::string& key);
00054
00055
00056 bool operator == (const Extensible&) const;
00057 bool operator != (const Extensible&) const;
00058 bool operator > (const Extensible&) const;
00059 bool operator < (const Extensible&) const;
00060
00061
00062 unsigned long size() const;
00063
00064
00065 void clear();
00066
00067
00068
00069 void copy(const Extensible& s);
00070
00071
00072 void erase(const std::string&);
00073
00074
00075 std::string serialize(void) const;
00076
00077
00078 void deserialize(const std::string& serial) ;
00079
00080
00081 std::vector<std::string> getKeys(void) const ;
00082
00083
00084 bool getBool(const std::string& key, bool defaultValue = false) const ;
00085
00086
00087 long getLong(const std::string& key, long defaultValue = 0) const ;
00088
00089
00090 unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const ;
00091
00092
00093 double getDouble(const std::string& key, double defaultValue = 0) const ;
00094
00095
00096 int64_t getS64(const std::string& key, int64_t defaultValue = 0) const ;
00097
00098
00099 uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const ;
00100
00101
00102 std::string getString(const std::string& key, const std::string& defaultValue = "") const ;
00103
00104
00105 Extensible getExtensible(const std::string& key,
00106 const Extensible& defaultValue = Extensible()) const ;
00107
00108
00109 std::vector<boost::any> getVector(const std::string& key,
00110 const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const ;
00111
00112
00113 typedef DictType_::const_iterator const_iterator;
00114
00115 const_iterator begin() const { return dictionary_.begin(); }
00116 const_iterator end() const { return dictionary_.end(); }
00117
00118 };
00119
00120 };
00121
00122 #endif // DMLITE_CPP_UTILS_TYPES_H