Michele De Stefano's C++ Utilities
list.hpp
Go to the documentation of this file.
1 // list.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_LIST_HPP_INCLUDED
8 #define MDS_UTILS_PYTHON_LIST_HPP_INCLUDED
9 
11 
29 namespace mds_utils {
30  namespace python {
31 
47 class List : public Sequence_Base<List> {
48 
49  static PyObject* new_seq(size_t len) {
50  return PyList_New(len);
51  }
52 
53  static bool self_type_check(const Obj& o) {
54  return PyList_Check(o);
55  }
56 
57  template<class T>
58  bool set_item(size_t pos,T const& x) {
59  return PyList_SetItem(m_po,pos,to_python(x)) == 0;
60  }
61 
62 
63  void init_list() {
64  if ((m_po = new_seq(0)) == NULL) {
65  throw std::runtime_error("Could not create an empty list");
66  }
67  get_ownership();
68  }
69 
70  friend class Sequence_Base<List>;
71 
72 
73 public:
74 
76  List() {}
77 
78 
91  List(PyObject *po) : Sequence_Base<List>(po) {}
92 
93 
95  List(const List& rhs) : Sequence_Base<List>(rhs) {}
96 
97 
99  List(List&& rhs) : Sequence_Base<List>(std::move(rhs)) {}
100 
101 
103  List(ProxyAttr&& rhs) : Sequence_Base<List>(std::move(rhs)) {}
104 
105 
107  List& operator =(List&& rhs) {
108  Sequence_Base<List>::operator =(std::move(rhs));
109  return *this;
110  }
111 
113  List& operator =(const List& rhs) {
115  return *this;
116  }
117 
126  template<class T>
127  List& append(const T& val) {
128  if (m_po == NULL) init_list();
129  if (PyList_Append(m_po,to_python(val)) == -1) {
130  throw std::runtime_error("Append failed.");
131  }
132  ++m_len;
133  return *this;
134  }
135 
144  template<class T>
145  List& append(T&& val) {
146  if (m_po == NULL) init_list();
147  if (PyList_Append(m_po,to_python(val)) == -1) {
148  throw std::runtime_error("Append failed.");
149  }
150  ++m_len;
151  return *this;
152  }
153 
164  template<class T>
165  List& insert(long i,const T& val) {
166  if (PyList_Insert(*this,idx(i),to_python(val)) == -1) {
167  throw std::runtime_error("Insert failed.");
168  }
169  return *this;
170  }
171 
183  template<class T>
184  List& insert(long i,T&& val) {
185  if (PyList_Insert(*this,idx(i),to_python(val)) == -1) {
186  throw std::runtime_error("Insert failed.");
187  }
188  return *this;
189  }
190 
192  List& sort() {
193  if (PyList_Sort(*this) == -1) {
194  throw std::runtime_error("Cannot sort.");
195  }
196  return *this;
197  }
198 
199 
201  List& reverse() {
202  if (PyList_Reverse(*this) == -1) {
203  throw std::runtime_error("Cannot reverse.");
204  }
205  return *this;
206  }
207 };
208 
209  }
210 }
211 
212 #endif /* MDS_UTILS_PYTHON_LIST_HPP_INCLUDED */
Proxy class for managing attribute access.
Definition: obj.hpp:93
List(List &&rhs)
The move constructor.
Definition: list.hpp:99
PyObject * to_python(void)
Converts a value into a Python object.
Main namespace of all Michele De Stefano&#39;s C++ utilities.
Definition: endian.hpp:30
Wraps a Python list.
Definition: list.hpp:47
List()
The default constructor.
Definition: list.hpp:76
Contains a wrapper class for a generic Python sequence datatype.
PyObject * m_po
Underlying pointer to the wrapped Python object.
Definition: obj.hpp:165
List & insert(long i, T &&val)
Inserts an element in front of the specified element using move semantics.
Definition: list.hpp:184
size_t len() const
Returns the length of the sequence.
Definition: sequence.hpp:434
List & operator=(List &&rhs)
The move assignment.
Definition: list.hpp:107
List(PyObject *po)
Constructs a new List from a Python sequence.
Definition: list.hpp:91
void get_ownership()
Used in place of incref, when the wrapped PyObject* was increfed already.
Definition: obj.hpp:380
List & reverse()
Reverses the list.
Definition: list.hpp:201
List & sort()
Sorts the list.
Definition: list.hpp:192
List & append(const T &val)
Appends an element to the list.
Definition: list.hpp:127
List & append(T &&val)
Appends an element to the list using move semantics.
Definition: list.hpp:145
Sequence_Base< Derived > & operator=(Sequence_Base< Derived > &&rhs)
Move assignment.
Definition: sequence.hpp:264
size_t m_len
The length of the sequence.
Definition: sequence.hpp:170
List(ProxyAttr &&rhs)
The move constructor from ProxyAttr objects.
Definition: list.hpp:103
List(const List &rhs)
The copy constructor.
Definition: list.hpp:95
size_t idx(long i) const
Converts a Python index (that can also be negative) into a C index.
Definition: sequence.hpp:180
Base class for all generic sequence types.
Definition: sequence.hpp:94
List & insert(long i, const T &val)
Inserts an element in front of the specified element.
Definition: list.hpp:165
This is a simple wrapper around the PyObject* datatype.
Definition: obj.hpp:68