16#ifndef RD_DICT_H_012020
17#define RD_DICT_H_012020
25#include <boost/lexical_cast.hpp>
43 explicit Pair(std::string s) : key(std::move(s)), val() {}
44 Pair(std::string s,
const RDValue &v) : key(std::move(s)), val(v) {}
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;
69 void update(
const Dict &other,
bool preserveExisting =
false) {
70 if (!preserveExisting) {
73 if (other._hasNonPodData) {
74 _hasNonPodData =
true;
76 for (
size_t i = 0; i < other._data.size(); ++i) {
77 const Pair &pair = other._data[i];
78 Pair *target =
nullptr;
79 for (
size_t i = 0; i < _data.size(); ++i) {
80 if (_data[i].key == pair.
key) {
88 _data.push_back(
Pair(pair.
key));
102 if (_hasNonPodData) {
106 if (other._hasNonPodData) {
107 std::vector<Pair> data(other._data.size());
109 for (
size_t i = 0; i < _data.size(); ++i) {
110 _data[i].key = other._data[i].key;
116 _hasNonPodData = other._hasNonPodData;
121 if (
this == &other) {
124 if (_hasNonPodData) {
127 _hasNonPodData = other._hasNonPodData;
128 other._hasNonPodData =
false;
129 _data = std::move(other._data);
147 bool hasVal(
const std::string &what)
const {
148 for (
const auto &data : _data) {
149 if (data.key == what) {
163 res.reserve(_data.size());
164 for (
const auto &item : _data) {
165 res.push_back(item.key);
183 template <
typename T>
184 void getVal(
const std::string &what, T &res)
const {
185 res = getVal<T>(what);
189 template <
typename T>
190 T
getVal(
const std::string &what)
const {
191 for (
auto &data : _data) {
192 if (data.key == what) {
193 return from_rdvalue<T>(data.val);
200 void getVal(
const std::string &what, std::string &res)
const {
201 for (
const auto &i : _data) {
224 template <
typename T>
226 for (
const auto &data : _data) {
227 if (data.key == what) {
228 res = from_rdvalue<T>(data.val);
237 for (
const auto &i : _data) {
259 template <
typename T>
260 void setVal(
const std::string &what, T &val) {
261 static_assert(! std::is_same_v<T, std::string_view>,
262 "T cannot be string_view");
263 _hasNonPodData =
true;
264 for (
auto &&data : _data) {
265 if (data.key == what) {
271 _data.push_back(
Pair(what, val));
274 template <
typename T>
276 static_assert(! std::is_same_v<T, std::string_view>,
277 "T cannot be string_view");
279 for (
auto &&data : _data) {
280 if (data.key == what) {
286 _data.push_back(
Pair(what, val));
289 void setVal(
const std::string &what,
bool val) { setPODVal(what, val); }
291 void setVal(
const std::string &what,
double val) { setPODVal(what, val); }
293 void setVal(
const std::string &what,
float val) { setPODVal(what, val); }
295 void setVal(
const std::string &what,
int val) { setPODVal(what, val); }
297 void setVal(
const std::string &what,
unsigned int val) {
298 setPODVal(what, val);
302 void setVal(
const std::string &what,
const char *val) {
316 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
317 if (it->key == what) {
318 if (_hasNonPodData) {
331 if (_hasNonPodData) {
332 for (
auto &&data : _data) {
342 bool _hasNonPodData{
false};
347inline std::string Dict::getVal<std::string>(
const std::string &what)
const {
Class to allow us to throw a KeyError from C++ and have it make it back to Python.
The Dict class can be used to store objects of arbitrary type keyed by strings.
void setVal(const std::string &what, const char *val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const DataType & getData() const
Access to the underlying data.
T getVal(const std::string &what) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Dict(Dict &&other) noexcept=default
void reset()
Clears all keys (and values) from the dictionary.
void setPODVal(const std::string &what, T val)
Dict & operator=(const Dict &other)
STR_VECT keys() const
Returns the set of keys in the dictionary.
void setVal(const std::string &what, float val)
void setVal(const std::string &what, double val)
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
void setVal(const std::string &what, bool val)
void setVal(const std::string &what, unsigned int val)
void getVal(const std::string &what, std::string &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool & getNonPODStatus()
Access to the underlying non-POD containment flag This is meant to be used only in bulk updates of _d...
std::vector< Pair > DataType
Dict & operator=(Dict &&other) noexcept
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.
void update(const Dict &other, bool preserveExisting=false)
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
bool getValIfPresent(const std::string &what, std::string &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setVal(const std::string &what, int val)
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
#define RDKIT_RDGENERAL_EXPORT
std::vector< std::string > STR_VECT
bool rdvalue_tostring(RDValue_cast_t val, std::string &res)
void copy_rdvalue(RDValue &dest, const RDValue &src)
Pair(std::string s, const RDValue &v)
static void cleanup_rdvalue(RDValue v)