00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __RD_DICT_H__
00012 #define __RD_DICT_H__
00013
00014 #include <map>
00015 #include <string>
00016 #include <vector>
00017 #include <boost/any.hpp>
00018 #include <RDBoost/Exceptions.h>
00019 #include <boost/lexical_cast.hpp>
00020
00021 namespace RDKit{
00022 typedef std::vector<std::string> STR_VECT;
00023
00024
00025
00026
00027
00028
00029 class Dict {
00030
00031
00032
00033 friend void force_types();
00034 public:
00035 typedef std::map<const std::string, boost::any> DataType;
00036 Dict(){
00037 _data.clear();
00038 };
00039
00040 Dict(const Dict &other) {
00041 _data = other._data;
00042 };
00043
00044 Dict &operator=(const Dict &other) {
00045 _data = other._data;
00046 return *this;
00047 };
00048
00049
00050
00051
00052 bool hasVal(const char *what) const{
00053 std::string key(what);
00054 return hasVal(key);
00055 };
00056 bool hasVal(const std::string &what) const {
00057 return _data.find(what)!=_data.end();
00058 };
00059
00060
00061
00062
00063
00064
00065 STR_VECT keys() const {
00066 STR_VECT res;
00067 DataType::const_iterator item;
00068 for (item = _data.begin(); item != _data.end(); item++) {
00069 res.push_back(item->first);
00070 }
00071 return res;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 template <typename T>
00088 void getVal(const std::string &what,T &res) const {
00089 DataType::const_iterator pos=_data.find(what);
00090 if(pos==_data.end())
00091 throw KeyErrorException(what);
00092 const boost::any &val = pos->second;
00093 res = fromany<T>(val);
00094 };
00095
00096 template <typename T>
00097 T getVal(const std::string &what) const {
00098 T res;
00099 getVal(what,res);
00100 return res;
00101 }
00102
00103
00104 template <typename T>
00105 T getVal(const char *what,T &res) const {
00106 std::string key(what);
00107 getVal(key, res);
00108 return res;
00109 };
00110
00111 template <typename T>
00112 T getVal(const char *what) const {
00113 std::string key(what);
00114 return getVal<T>(key);
00115 };
00116
00117
00118 void getVal(const std::string &what, std::string &res) const;
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 template <typename T>
00134 void setVal(const std::string &what, T &val){
00135 std::string key = what;
00136 _data[key] = toany(val);
00137 };
00138
00139 template <typename T>
00140 void setVal(const char *what, T &val){
00141 std::string key = what;
00142 setVal(key,val);
00143 };
00144
00145 void setVal(const std::string &what, const char *val){
00146 std::string h(val);
00147 setVal(what,h);
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 void clearVal(const std::string &what) {
00164 if(! this->hasVal(what) ) throw KeyErrorException(what);
00165 _data.erase(what);
00166 };
00167
00168
00169 void clearVal(const char *what) {
00170 std::string key=what;
00171 clearVal(key);
00172 };
00173
00174
00175
00176
00177 void reset(){
00178 _data.clear();
00179 };
00180
00181
00182
00183
00184
00185
00186
00187
00188 template <typename T>
00189 T fromany(const boost::any &arg) const;
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 template <typename T>
00200 boost::any toany(T arg) const;
00201
00202 private:
00203 DataType _data;
00204 };
00205 }
00206 #endif