24 #include <boost/lexical_cast.hpp> 42 Pair(
const std::string &s) : key(s), val() {}
43 Pair(
const std::string &s,
const RDValue &v) : key(s), val(v) {}
48 Dict() : _data(), _hasNonPodData(false){};
51 _hasNonPodData = other._hasNonPodData;
53 std::vector<Pair> data(other._data.size());
55 for (
size_t i = 0; i < _data.size(); ++i) {
56 _data[i].key = other._data[i].key;
66 void update(
const Dict &other,
bool preserveExisting =
false) {
67 if (!preserveExisting) {
70 if (other._hasNonPodData) _hasNonPodData =
true;
71 for (
size_t i = 0; i < other._data.size(); ++i) {
72 const Pair &pair = other._data[i];
74 for (
size_t i = 0; i < _data.size(); ++i) {
75 if (_data[i].
key == pair.
key) {
83 _data.push_back(
Pair(pair.
key));
94 _hasNonPodData = other._hasNonPodData;
96 std::vector<Pair> data(other._data.size());
98 for (
size_t i = 0; i < _data.size(); ++i) {
99 _data[i].key = other._data[i].key;
110 const DataType &
getData()
const {
return _data; }
117 bool hasVal(
const std::string &what)
const {
118 for (
size_t i = 0; i < _data.size(); ++i) {
119 if (_data[i].
key == what)
return true;
131 DataType::const_iterator item;
132 for (item = _data.begin(); item != _data.end(); item++) {
133 res.push_back(item->key);
151 template <
typename T>
152 void getVal(
const std::string &what, T &res)
const {
153 res = getVal<T>(what);
156 template <
typename T>
157 T
getVal(
const std::string &what)
const {
158 for (
size_t i = 0; i < _data.size(); ++i) {
159 if (_data[i].
key == what) {
160 return from_rdvalue<T>(_data[i].val);
167 void getVal(
const std::string &what, std::string &res)
const;
184 template <
typename T>
186 for (
size_t i = 0; i < _data.size(); ++i) {
187 if (_data[i].
key == what) {
188 res = from_rdvalue<T>(_data[i].val);
211 template <
typename T>
213 _hasNonPodData =
true;
214 for (
size_t i = 0; i < _data.size(); ++i) {
215 if (_data[i].
key == what) {
221 _data.push_back(
Pair(what, val));
224 template <
typename T>
227 for (
size_t i = 0; i < _data.size(); ++i) {
228 if (_data[i].
key == what) {
234 _data.push_back(
Pair(what, val));
245 void setVal(
const std::string &what,
unsigned int val) {
267 for (DataType::iterator it = _data.begin(); it < _data.end(); ++it) {
268 if (it->key == what) {
269 if (_hasNonPodData) {
283 if (_hasNonPodData) {
284 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)
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.