7 #ifndef MDS_UTILS_PYTHON_LIST_HPP_INCLUDED 8 #define MDS_UTILS_PYTHON_LIST_HPP_INCLUDED 49 static PyObject* new_seq(
size_t len) {
50 return PyList_New(len);
53 static bool self_type_check(
const Obj& o) {
54 return PyList_Check(o);
58 bool set_item(
size_t pos,T
const& x) {
64 if ((
m_po = new_seq(0)) == NULL) {
65 throw std::runtime_error(
"Could not create an empty list");
128 if (
m_po == NULL) init_list();
130 throw std::runtime_error(
"Append failed.");
146 if (
m_po == NULL) init_list();
148 throw std::runtime_error(
"Append failed.");
167 throw std::runtime_error(
"Insert failed.");
186 throw std::runtime_error(
"Insert failed.");
193 if (PyList_Sort(*
this) == -1) {
194 throw std::runtime_error(
"Cannot sort.");
202 if (PyList_Reverse(*
this) == -1) {
203 throw std::runtime_error(
"Cannot reverse.");
Proxy class for managing attribute access.
List(List &&rhs)
The move constructor.
PyObject * to_python(void)
Converts a value into a Python object.
Main namespace of all Michele De Stefano's C++ utilities.
List()
The default constructor.
Contains a wrapper class for a generic Python sequence datatype.
PyObject * m_po
Underlying pointer to the wrapped Python object.
List & insert(long i, T &&val)
Inserts an element in front of the specified element using move semantics.
size_t len() const
Returns the length of the sequence.
List & operator=(List &&rhs)
The move assignment.
List(PyObject *po)
Constructs a new List from a Python sequence.
void get_ownership()
Used in place of incref, when the wrapped PyObject* was increfed already.
List & reverse()
Reverses the list.
List & sort()
Sorts the list.
List & append(const T &val)
Appends an element to the list.
List & append(T &&val)
Appends an element to the list using move semantics.
Sequence_Base< Derived > & operator=(Sequence_Base< Derived > &&rhs)
Move assignment.
size_t m_len
The length of the sequence.
List(ProxyAttr &&rhs)
The move constructor from ProxyAttr objects.
List(const List &rhs)
The copy constructor.
size_t idx(long i) const
Converts a Python index (that can also be negative) into a C index.
Base class for all generic sequence types.
List & insert(long i, const T &val)
Inserts an element in front of the specified element.
This is a simple wrapper around the PyObject* datatype.