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 (
const auto &opair : other._data) {
77 Pair *target =
nullptr;
78 for (
auto &dpair : _data) {
79 if (dpair.key == opair.key) {
87 _data.push_back(
Pair(opair.key));
101 if (_hasNonPodData) {
105 if (other._hasNonPodData) {
106 std::vector<Pair> data(other._data.size());
108 for (
size_t i = 0; i < _data.size(); ++i) {
109 _data[i].key = other._data[i].key;
115 _hasNonPodData = other._hasNonPodData;
120 if (
this == &other) {
123 if (_hasNonPodData) {
126 _hasNonPodData = other._hasNonPodData;
127 other._hasNonPodData =
false;
128 _data = std::move(other._data);
146 bool hasVal(
const std::string &what)
const {
147 for (
const auto &data : _data) {
148 if (data.key == what) {
162 res.reserve(_data.size());
163 for (
const auto &item : _data) {
164 res.push_back(item.key);
182 template <
typename T>
183 void getVal(
const std::string &what, T &res)
const {
184 res = getVal<T>(what);
188 template <
typename T>
189 T
getVal(
const std::string &what)
const {
190 for (
auto &data : _data) {
191 if (data.key == what) {
192 return from_rdvalue<T>(data.val);
199 void getVal(
const std::string &what, std::string &res)
const {
200 for (
const auto &i : _data) {
223 template <
typename T>
225 for (
const auto &data : _data) {
226 if (data.key == what) {
227 res = from_rdvalue<T>(data.val);
236 for (
const auto &i : _data) {
258 template <
typename T>
259 void setVal(
const std::string &what, T &val) {
260 static_assert(!std::is_same_v<T, std::string_view>,
261 "T cannot be string_view");
262 _hasNonPodData =
true;
263 for (
auto &&data : _data) {
264 if (data.key == what) {
265 RDValue::cleanup_rdvalue(data.val);
270 _data.push_back(
Pair(what, val));
273 template <
typename T>
275 static_assert(!std::is_same_v<T, std::string_view>,
276 "T cannot be string_view");
278 for (
auto &&data : _data) {
279 if (data.key == what) {
280 RDValue::cleanup_rdvalue(data.val);
285 _data.push_back(
Pair(what, val));
288 void setVal(
const std::string &what,
bool val) { setPODVal(what, val); }
290 void setVal(
const std::string &what,
double val) { setPODVal(what, val); }
292 void setVal(
const std::string &what,
float val) { setPODVal(what, val); }
294 void setVal(
const std::string &what,
int val) { setPODVal(what, val); }
296 void setVal(
const std::string &what,
unsigned int val) {
297 setPODVal(what, val);
301 void setVal(
const std::string &what,
const char *val) {
315 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
316 if (it->key == what) {
317 if (_hasNonPodData) {
318 RDValue::cleanup_rdvalue(it->val);
330 if (_hasNonPodData) {
331 for (
auto &&data : _data) {
332 RDValue::cleanup_rdvalue(data.val);
341 bool _hasNonPodData{
false};
346inline 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_is(const RDValue_cast_t)
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)