libRoadRunner C++ API  1.3
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Dictionary.h
1 /*
2  * Dictionary.h
3  *
4  * Created on: Jul 14, 2014
5  * Author: andy
6  */
7 
8 #ifndef DICTIONARY_H_
9 #define DICTIONARY_H_
10 
11 #include "Variant.h"
12 #include <string>
13 #include <vector>
14 
15 #include "tr1proxy/rr_memory.h"
16 #include "tr1proxy/rr_unordered_map.h"
17 
18 #if defined(SWIG) || defined(SWIGPYTHON)
19 #include "PyUtils.h"
20 #endif
21 
22 
23 namespace rr {
24 
25 /**
26  * @brief This class is frozen, no new features
27  * A dictionary interface that objects can implement. These are automatically
28  * mapped to python dictionaries.
29  */
30 class RR_DECLSPEC Dictionary // Yes, I know this is a pure virtual *INTERFACE*
31 { // so it should NOT need to be exported, but MSVC
32 public: // *mistakenly* complains in ONE instance.
33  // add the declspec just to shut it up.
34 
35 #ifndef SWIG
36 
37  /**
38  * Set the value for an arbitrary key.
39  *
40  * The Variant object has a large number of creation operators so
41  * you only have to give the value as native C++ object i.e.
42  *
43  * @code
44  * Dictionary*d = (...);
45  * d->setItem("someIntKey", 1);
46  * d->setItem("someStrKey", string("some string"));
47  * d->setItem("someFloatVal, 0.123);
48  * @endcode
49  */
50  virtual void setItem(const std::string& key, const rr::Variant& value) = 0;
51 
52  /**
53  * Get a value. Variants are POD. If the key does not exist, an std::exception
54  * will be thrown.
55  *
56  * The Variant object has the assigment operator overloaded so
57  * it will automatically convert to any native C++ object. If the
58  * Variant can not be converted, it will raise an exception, i.e.
59  *
60  * @code
61  * Dictionary* d = ...;
62  * int a = d->getItem("someIntKey");
63  * string s = d->getItem("someStrVal");
64  * double val = d->getItem("someDoubleVal");
65  * @endcode
66  */
67  virtual Variant getItem(const std::string& key) const = 0;
68 
69  /**
70  * is there a key matching this name.
71  */
72  virtual bool hasKey(const std::string& key) const = 0;
73 
74  /**
75  * remove a value
76  */
77  virtual int deleteItem(const std::string& key) = 0;
78 
79  /**
80  * list of keys in this object.
81  */
82  virtual std::vector<std::string> getKeys() const = 0;
83 
84  /**
85  * Pure virtual interface, you should never have to delete
86  * an instance of this type directly.
87  */
88  virtual ~Dictionary() {};
89 
90 #endif
91 
92  /**
93  * implement the python method if we are being used in the python binding.
94  *
95  * Note, if these methods are added to an %extend section, derived classes
96  * will not see them, so as far as I can tell, this is the only way to
97  * add these methods.
98  */
99 #if defined(SWIGPYTHON)
100  PyObject *keys() {
101  return dictionary_keys(this);
102  }
103 
104  PyObject *values() {
105  return dictionary_values(this);
106  }
107 
108  PyObject *items() {
109  return dictionary_items(this);
110  }
111 
112  PyObject *__getitem__(const char* key) {
113  return dictionary_getitem(this, key);
114  }
115 
116  PyObject *__setitem__(const char* key, PyObject* value) {
117  return dictionary_setitem(this, key, value);
118  }
119 
120  void __delitem__(const char* key) {
121  return dictionary_delitem(this, key);
122  }
123 
124  PyObject *__contains__(const char* key) {
125  return dictionary_contains(this, key);
126  }
127 
128  std::string helloPython() {
129  return "hello python";
130  }
131 #endif
132 };
133 
134 
135 /**
136  * @brief This class is frozen, no new features
137  * Basic implementation of the Dictionary interface which uses a
138  * std unordered map to store the values.
139  */
140 class RR_DECLSPEC BasicDictionary : public Dictionary
141 {
142 public:
143  BasicDictionary() {};
144 
145  /**
146  * sets an item in the internal unordered map.
147  */
148  virtual void setItem(const std::string& key, const rr::Variant& value);
149 
150  /**
151  * gets an item from the internal unordered map.
152  */
153  virtual Variant getItem(const std::string& key) const;
154 
155  /**
156  * is there a key matching this name.
157  *
158  * @retruns true if this key exists, false otherwise.
159  */
160  virtual bool hasKey(const std::string& key) const;
161 
162  /**
163  * remove a value
164  */
165  virtual int deleteItem(const std::string& key);
166 
167  /**
168  * list of keys in this object.
169  */
170  virtual std::vector<std::string> getKeys() const;
171 
172  /**
173  * Pure virtual interface, you should never have to delete
174  * an instance of this type directly.
175  */
176  virtual ~BasicDictionary() {};
177 
178 protected:
179  typedef cxx11_ns::unordered_map<std::string, Variant> VariantMap;
180  VariantMap items;
181 };
182 
183 }
184 
185 
186 
187 
188 
189 #endif /* DICTIONARY_H_ */
Definition: CVODEIntegrator.cpp:26
virtual ~Dictionary()
Definition: Dictionary.h:88
This class is frozen, no new features A dictionary interface that objects can implement. These are automatically mapped to python dictionaries.
Definition: Dictionary.h:30
Definition: Variant.h:73
This class is frozen, no new features Basic implementation of the Dictionary interface which uses a s...
Definition: Dictionary.h:140
virtual ~BasicDictionary()
Definition: Dictionary.h:176