25 #include <boost/lexical_cast.hpp> 43 Pair(
const std::string &s) : key(s), val() {}
44 Pair(
const std::string &s,
const RDValue &v) : key(s), val(v) {}
49 Dict() : _data(), _hasNonPodData(false){};
52 _hasNonPodData = other._hasNonPodData;
53 if (other._hasNonPodData) {
54 std::vector<Pair> data(other._data.size());
56 for (
size_t i = 0; i < _data.size(); ++i) {
57 _data[i].key = other._data[i].key;
67 void update(
const Dict &other,
bool preserveExisting =
false) {
68 if (!preserveExisting) {
71 if (other._hasNonPodData) _hasNonPodData =
true;
72 for (
size_t i = 0; i < other._data.size(); ++i) {
73 const Pair &pair = other._data[i];
75 for (
size_t i = 0; i < _data.size(); ++i) {
76 if (_data[i].key == pair.
key) {
84 _data.push_back(
Pair(pair.
key));
95 if (
this == &other)
return *
this;
96 if (_hasNonPodData) reset();
98 if (other._hasNonPodData) {
99 std::vector<Pair> data(other._data.size());
101 for (
size_t i = 0; i < _data.size(); ++i) {
102 _data[i].key = other._data[i].key;
108 _hasNonPodData = other._hasNonPodData;
114 const DataType &
getData()
const {
return _data; }
121 bool hasVal(
const std::string &what)
const {
122 for (
size_t i = 0; i < _data.size(); ++i) {
123 if (_data[i].key == what)
return true;
135 DataType::const_iterator item;
136 for (item = _data.begin(); item != _data.end(); item++) {
137 res.push_back(item->key);
155 template <
typename T>
156 void getVal(
const std::string &what, T &res)
const {
157 res = getVal<T>(what);
160 template <
typename T>
161 T
getVal(
const std::string &what)
const {
162 for (
size_t i = 0; i < _data.size(); ++i) {
163 if (_data[i].key == what) {
164 return from_rdvalue<T>(_data[i].val);
171 void getVal(
const std::string &what, std::string &res)
const;
188 template <
typename T>
190 for (
size_t i = 0; i < _data.size(); ++i) {
191 if (_data[i].key == what) {
192 res = from_rdvalue<T>(_data[i].val);
200 bool getValIfPresent(
const std::string &what, std::string &res)
const;
215 template <
typename T>
216 void setVal(
const std::string &what, T &val) {
217 _hasNonPodData =
true;
218 for (
size_t i = 0; i < _data.size(); ++i) {
219 if (_data[i].key == what) {
225 _data.push_back(
Pair(what, val));
228 template <
typename T>
231 for (
size_t i = 0; i < _data.size(); ++i) {
232 if (_data[i].key == what) {
238 _data.push_back(
Pair(what, val));
241 void setVal(
const std::string &what,
bool val) { setPODVal(what, val); }
243 void setVal(
const std::string &what,
double val) { setPODVal(what, val); }
245 void setVal(
const std::string &what,
float val) { setPODVal(what, val); }
247 void setVal(
const std::string &what,
int val) { setPODVal(what, val); }
249 void setVal(
const std::string &what,
unsigned int val) {
250 setPODVal(what, val);
254 void setVal(
const std::string &what,
const char *val) {
271 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
272 if (it->key == what) {
273 if (_hasNonPodData) {
287 if (_hasNonPodData) {
288 for (
size_t i = 0; i < _data.size(); ++i) {
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
void copy_rdvalue(RDValue &dest, const RDValue &src)
#define RDKIT_RDGENERAL_EXPORT
void setVal(const std::string &what, int val)
std::vector< Pair > DataType
Pair(const std::string &s)
void setVal(const std::string &what, float val)
const DataType & getData() const
Access to the underlying data.
void setVal(const std::string &what, bool val)
void setVal(const std::string &what, double val)
Pair(const std::string &s, const RDValue &v)
static void cleanup_rdvalue(RDValue v)
void setVal(const std::string &what, const char *val)
void setVal(const std::string &what, unsigned int val)
bool getValIfPresent(const std::string &what, T &res) const
Potentially gets the value associated with a particular key returns true on success/false on failure...
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
STR_VECT keys() const
Returns the set of keys in the dictionary.
Dict & operator=(const Dict &other)
T getVal(const std::string &what) const
void setPODVal(const std::string &what, T val)
void update(const Dict &other, bool preserveExisting=false)
void reset()
Clears all keys (and values) from the dictionary.
The Dict class can be used to store objects of arbitrary type keyed by strings.
std::vector< std::string > STR_VECT
Class to allow us to throw a KeyError from C++ and have it make it back to Python.