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 throw (DmException);
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 void copy(const Extensible& s);
00069
00070
00071 void erase(const std::string&);
00072
00073
00074 std::string serialize(void) const;
00075
00076
00077 void deserialize(const std::string& serial) throw (DmException);
00078
00079
00080 std::vector<std::string> getKeys(void) const throw (DmException);
00081
00082
00083 bool getBool(const std::string& key, bool defaultValue = false) const throw (DmException);
00084
00085
00086 long getLong(const std::string& key, long defaultValue = 0) const throw (DmException);
00087
00088
00089 unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const throw (DmException);
00090
00091
00092 double getDouble(const std::string& key, double defaultValue = 0) const throw (DmException);
00093
00094
00095 int64_t getS64(const std::string& key, int64_t defaultValue = 0) const throw (DmException);
00096
00097
00098 uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const throw (DmException);
00099
00100
00101 std::string getString(const std::string& key, const std::string& defaultValue = "") const throw (DmException);
00102
00103
00104 Extensible getExtensible(const std::string& key,
00105 const Extensible& defaultValue = Extensible()) const throw (DmException);
00106
00107
00108 std::vector<boost::any> getVector(const std::string& key,
00109 const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const throw (DmException);
00110
00111
00112 typedef DictType_::const_iterator const_iterator;
00113
00114 const_iterator begin() const { return dictionary_.begin(); }
00115 const_iterator end() const { return dictionary_.end(); }
00116 };
00117
00118 };
00119
00120 #endif // DMLITE_CPP_UTILS_TYPES_H