25 #include <boost/lexical_cast.hpp> 43 explicit Pair(std::string s) : key(
std::move(s)), val() {}
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;
119 const DataType &
getData()
const {
return _data; }
126 bool hasVal(
const std::string &what)
const {
127 for (
const auto &data : _data) {
128 if (data.key == what)
return true;
140 DataType::const_iterator item;
141 for (item = _data.begin(); item != _data.end(); item++) {
142 res.push_back(item->key);
160 template <
typename T>
161 void getVal(
const std::string &what, T &res)
const {
162 res = getVal<T>(what);
165 template <
typename T>
166 T
getVal(
const std::string &what)
const {
167 for (
auto &data : _data) {
168 if (data.key == what) {
169 return from_rdvalue<T>(data.val);
176 void getVal(
const std::string &what, std::string &res)
const;
193 template <
typename T>
195 for (
const auto &data : _data) {
196 if (data.key == what) {
197 res = from_rdvalue<T>(data.val);
205 bool getValIfPresent(
const std::string &what, std::string &res)
const;
220 template <
typename T>
221 void setVal(
const std::string &what, T &val) {
222 _hasNonPodData =
true;
223 for (
auto &&data : _data) {
224 if (data.key == what) {
230 _data.push_back(
Pair(what, val));
233 template <
typename T>
236 for (
auto &&data : _data) {
237 if (data.key == what) {
243 _data.push_back(
Pair(what, val));
246 void setVal(
const std::string &what,
bool val) { setPODVal(what, val); }
248 void setVal(
const std::string &what,
double val) { setPODVal(what, val); }
250 void setVal(
const std::string &what,
float val) { setPODVal(what, val); }
252 void setVal(
const std::string &what,
int val) { setPODVal(what, val); }
254 void setVal(
const std::string &what,
unsigned int val) {
255 setPODVal(what, val);
259 void setVal(
const std::string &what,
const char *val) {
276 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
277 if (it->key == what) {
278 if (_hasNonPodData) {
292 if (_hasNonPodData) {
293 for (
auto &&data : _data) {
335 const std::string &what)
const;
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
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)
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)
Pair(std::string s, const RDValue &v)
bool & getNonPODStatus()
Access to the underlying non-POD containment flag This is meant to be used only in bulk updates of _d...
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.