Michele De Stefano's C++ Utilities
dictionary.hpp
Go to the documentation of this file.
1 // dictionary.hpp
2 //
3 // Copyright (c) 2014 - Michele De Stefano (info.micheledestefano@gmail.com)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 #ifndef MDS_UTILS_PYTHON_DICTIONARY_HPP_INCLUDED
8 #define MDS_UTILS_PYTHON_DICTIONARY_HPP_INCLUDED
9 
10 #include <mds_utils/python/obj.hpp>
12 
30 namespace mds_utils {
31  namespace python {
32 
50 class Dictionary : public Obj {
51 
52  static bool self_type_check(const Obj& o) {
53  return PyDict_Check(o);
54  }
55 
56 public:
57 
60  m_po = PyDict_New();
61  get_ownership();
62  }
63 
76  Dictionary(PyObject *po) : Obj(po) {
77  assert(self_type_check(o));
78  }
79 
80 
82  Dictionary(const Dictionary& rhs) : Obj(rhs) {}
83 
84 
86  Dictionary(Dictionary&& rhs) : Obj(std::move(rhs)) {}
87 
88 
90  void copy(const Dictionary& rhs) {
91  reset();
92  m_po = PyDict_Copy(rhs);
93  get_ownership();
94  }
95 
96 
98  void clear() {
99  PyDict_Clear(m_po);
100  }
101 
102 
105  Obj::operator =(std::move(rhs));
106  return *this;
107  }
108 
111  Obj::operator =(rhs);
112  return *this;
113  }
114 
129  template<class Key_T>
130  bool contains(const Key_T& key) const {
131  int retval(PyDict_Contains(m_po,to_python(key)));
132  if (retval == -1) {
133  throw std::runtime_error("Error while searching for the "
134  "provided key.");
135  }
136  return (retval == 1);
137  }
138 
139 
151  template<class Key_T,class Val_T>
152  void set_item(const Key_T& key,const Val_T& val) {
153  Obj
154  pykey(to_python(key)),
155  pyval(to_python(val));
156 
157  pykey.get_ownership();
158  pyval.get_ownership();
159 
160  if (PyDict_SetItem(m_po,pykey,pyval) == -1) {
161  throw std::runtime_error("Could not set item into dictionary.");
162  }
163  }
164 
165 
175  template<class Key_T>
176  void del_item(const Key_T& key) {
177  Obj
178  pykey(to_python(key));
179  pykey.get_ownership();
180 
181  if (PyDict_DelItem(m_po,key) == -1) {
182  throw std::runtime_error("Could not remove item from dictionary.");
183  }
184  }
185 
186 
198  template<class Key_T>
199  Obj get_item(const Key_T& key) const {
200  PyObject *pout(PyDict_GetItem(m_po,to_python(key)));
201  if (pout == NULL) {
202  throw std::runtime_error("Could not get item for the provided key");
203  }
204  Obj out(pout);
205  out.incref();
206  return out;
207  }
208 
209 
211  List items() const {
212  List
213  out(PyDict_Items(m_po));
214  out.get_ownership();
215  return out;
216  }
217 
219  List keys() const {
220  List
221  out(PyDict_Keys(m_po));
222  out.get_ownership();
223  return out;
224  }
225 
227  List values() const {
228  List
229  out(PyDict_Values(m_po));
230  out.get_ownership();
231  return out;
232  }
233 
235  size_t len() const {
236  return PyDict_Size(m_po);
237  }
238 
239 
249  void update(const Dictionary& b) {
250  if (PyDict_Update(m_po,b) == -1) {
251  throw std::runtime_error("Cannot update dictionary.");
252  }
253  }
254 };
255 
256  }
257 }
258 
259 #endif /* MDS_UTILS_PYTHON_DICTIONARY_HPP_INCLUDED */
260 
void reset()
Resets the object to the state given by the default constructor.
Definition: obj.hpp:377
Contains a wrapper class for the Python list datatype.
virtual void incref()
Increments the reference count using Py_XINCREF.
Definition: obj.hpp:363
PyObject * to_python(void)
Converts a value into a Python object.
Dictionary(PyObject *po)
Construct from a Python object.
Definition: dictionary.hpp:76
void del_item(const Key_T &key)
Removes an item from the dictionary.
Definition: dictionary.hpp:176
void set_item(const Key_T &key, const Val_T &val)
Inserts a key-value pair into the dictionary.
Definition: dictionary.hpp:152
Dictionary()
Default constructor.
Definition: dictionary.hpp:59
void clear()
Empty an existing dictionary of all key-value pairs.
Definition: dictionary.hpp:98
Obj get_item(const Key_T &key) const
Returns the item corresponding to a key.
Definition: dictionary.hpp:199
Main namespace of all Michele De Stefano&#39;s C++ utilities.
Definition: endian.hpp:30
Dictionary & operator=(Dictionary &&rhs)
Move assignment.
Definition: dictionary.hpp:104
Dictionary(Dictionary &&rhs)
Move constructor.
Definition: dictionary.hpp:86
Wraps a Python list.
Definition: list.hpp:47
PyObject * m_po
Underlying pointer to the wrapped Python object.
Definition: obj.hpp:165
Contains a wrapper class for the PyObject* datatype.
List values() const
Returns a list of all the values of the dictionary.
Definition: dictionary.hpp:227
void get_ownership()
Used in place of incref, when the wrapped PyObject* was increfed already.
Definition: obj.hpp:380
void update(const Dictionary &b)
Updates the current dictionary with the key-value pairs from another dictionary.
Definition: dictionary.hpp:249
Obj & operator=(const Obj &rhs)
Standard assignment.
Definition: obj.hpp:257
bool contains(const Key_T &key) const
Determine if the dictionary contains key.
Definition: dictionary.hpp:130
void copy(const Dictionary &rhs)
copies a dictionary into the current one.
Definition: dictionary.hpp:90
Dictionary(const Dictionary &rhs)
Copy constructor.
Definition: dictionary.hpp:82
size_t len() const
Returns the length of the dictionary.
Definition: dictionary.hpp:235
List keys() const
Returns a list of all the keys of the dictionary.
Definition: dictionary.hpp:219
Wraps a Python dictionary.
Definition: dictionary.hpp:50
List items() const
Returns a list of all the items of the dictionary.
Definition: dictionary.hpp:211
This is a simple wrapper around the PyObject* datatype.
Definition: obj.hpp:68