Michele De Stefano's C++ Utilities
mds_utils::python::Sequence_Base< Derived > Class Template Reference

Base class for all generic sequence types. More...

#include <mds_utils/python/sequence.hpp>

Inheritance diagram for mds_utils::python::Sequence_Base< Derived >:
mds_utils::python::Obj

Public Member Functions

 Sequence_Base ()
 Default constructor. More...
 
 Sequence_Base (size_t len)
 Creates a sequence of the desired length. More...
 
 Sequence_Base (PyObject *po)
 Constructs a new Sequence_Base from a Python sequence. More...
 
 Sequence_Base (Sequence_Base &&rhs)
 Move constructor.
 
 Sequence_Base (const Sequence_Base &rhs)
 The copy-constructor.
 
 Sequence_Base (ProxyAttr &&rhs)
 Move constructor from ProxyAttr object. More...
 
Sequence_Base< Derived > & operator= (Sequence_Base< Derived > &&rhs)
 Move assignment.
 
Sequence_Base< Derived > & operator= (const Sequence_Base< Derived > &rhs)
 The standard assignment.
 
template<class seq_T >
void set (const seq_T &seq)
 Sets a Python sequence from a Boost Fusion sequence. More...
 
template<class FwIt >
void set (FwIt b, FwIt e)
 Sets a Python sequence from a range of values. More...
 
void set (const Obj &o)
 Sets a Python sequence from another Python sequence. More...
 
void del (long i)
 Deletes the item in position i.
 
size_t len () const
 Returns the length of the sequence.
 
ProxyElem operator[] (long i)
 Element access. More...
 
- Public Member Functions inherited from mds_utils::python::Obj
 Obj ()
 Default constructor.
 
 Obj (const Obj &rhs)
 The copy-constructor. More...
 
 Obj (Obj &&rhs)
 Move constructor. More...
 
 Obj (ProxyAttr &&rhs)
 Move constructor from ProxyAttr objects. More...
 
 Obj (PyObject *po)
 Construct from a Python object. More...
 
Objoperator= (const Obj &rhs)
 Standard assignment.
 
Objoperator= (Obj &&rhs)
 Move assignment.
 
virtual Objoperator= (PyObject *po)
 Assignment from a Python object. More...
 
template<class T >
Objoperator= (const T &val)
 Assignment from a value. More...
 
virtual ~Obj ()
 Destructor.
 
PyObject * getPyObject () const
 Returns the underlying PyObject. More...
 
 operator PyObject * () const
 Automatic type conversion towards a Python object. More...
 
virtual PyObject * transfer ()
 Returns the Python object with transferred ownership. More...
 
virtual void incref ()
 Increments the reference count using Py_XINCREF. More...
 
virtual void decref ()
 Decrements the reference count using Py_XDECREF.
 
void reset ()
 Resets the object to the state given by the default constructor.
 
void get_ownership ()
 Used in place of incref, when the wrapped PyObject* was increfed already.
 
bool has_attr (const std::string &name) const
 Tests if the object has a particular attribute.
 
ProxyAttr attr (const std::string &name)
 Retrieves an attribute. More...
 
bool is_callable () const
 Checks if the object is callable. More...
 
Obj operator() ()
 Calling operator. More...
 
Obj operator() (const Obj &args)
 Calling operator, with positional arguments. More...
 
Obj operator() (const Obj &args, const Obj &kw)
 Calling operator, with positional and keyword arguments. More...
 

Protected Member Functions

size_t idx (long i) const
 Converts a Python index (that can also be negative) into a C index. More...
 
- Protected Member Functions inherited from mds_utils::python::Obj
void check_callable ()
 Checks if the object is callable. More...
 
void check_call_result (PyObject *result)
 Checks if the result of the object call was successful. More...
 

Protected Attributes

size_t m_len
 The length of the sequence.
 
- Protected Attributes inherited from mds_utils::python::Obj
size_t decref_on_destroy
 
PyObject * m_po
 Underlying pointer to the wrapped Python object.
 

Friends

class from_fusion_seq
 

Detailed Description

template<class Derived>
class mds_utils::python::Sequence_Base< Derived >

Base class for all generic sequence types.

Template Parameters
DerivedThe type of the derived class. It must provide the following member functions:
- <tt>static bool self_type_check(PyObject *o)</tt>:
  returns \b true if the Python object o has the
  same Python type as that of the Python object
  contained by the derived class. Returns
  \b false otherwise.
- <tt>static PyObject* new_seq(size_t len)</tt>:
  provides a new sequence, given its length <tt>len</tt>.
  It must return a new reference or NULL on error.
- <tt>template<class T> bool set_item(size_t pos,T const& x)</tt>:
  Inserts the object \p x into the position
  <tt>pos</tt>. Returns \b true on success
  and \b false on error. The proper "to_python" converter
  must be defined.
Warning
Remember that the copy/move constructors and the assignement operators do not duplicate the underlying Python object. They only increment its reference count.
Author
Michele De Stefano
Date
05/08/2014

Definition at line 94 of file sequence.hpp.

Constructor & Destructor Documentation

template<class Derived>
mds_utils::python::Sequence_Base< Derived >::Sequence_Base ( )
inline

Default constructor.

Author
Michele De Stefano
Date
14/04/2014

Definition at line 199 of file sequence.hpp.

template<class Derived>
mds_utils::python::Sequence_Base< Derived >::Sequence_Base ( size_t  len)
inline

Creates a sequence of the desired length.

Parameters
lenThe length of the sequence.
Remarks
It gets ownership of the constructed Python object.
Author
Michele De Stefano
Date
27/05/2014

Definition at line 212 of file sequence.hpp.

template<class Derived>
mds_utils::python::Sequence_Base< Derived >::Sequence_Base ( PyObject *  po)
inline

Constructs a new Sequence_Base from a Python sequence.

It throws an exception if the parameter is not a sequence of the same type as the derived class.

Parameters
poPointer to a Pyton sequence object.
Remarks
It does not incref the pointer to the Python object.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 230 of file sequence.hpp.

template<class Derived>
mds_utils::python::Sequence_Base< Derived >::Sequence_Base ( ProxyAttr &&  rhs)
inline

Move constructor from ProxyAttr object.

It increfs the current object.

Author
Michele De Stefano
Date
29/05/2014

Definition at line 258 of file sequence.hpp.

Member Function Documentation

template<class Derived>
size_t mds_utils::python::Sequence_Base< Derived >::idx ( long  i) const
inlineprotected

Converts a Python index (that can also be negative) into a C index.

Parameters
iIndex in Python style (i.e. it can be negative).
Author
Michele De Stefano
Date
19/04/2014

Definition at line 180 of file sequence.hpp.

Referenced by mds_utils::python::Sequence_Base< List >::del(), and mds_utils::python::Sequence_Base< List >::operator[]().

template<class Derived>
ProxyElem mds_utils::python::Sequence_Base< Derived >::operator[] ( long  i)
inline

Element access.

Parameters
iThe index of the element. It can be negative, with the same meaning it has in Python code.
Author
Michele De Stefano
Date
19/04/2014

Definition at line 446 of file sequence.hpp.

template<class Derived>
template<class seq_T >
void mds_utils::python::Sequence_Base< Derived >::set ( const seq_T &  seq)
inline

Sets a Python sequence from a Boost Fusion sequence.

Template Parameters
seq_TAny Boost Fusion sequence type.
Parameters
seqA Boost Fusion sequence.
Precondition
The Derived class must provide the static member function\ \ static PyObject* new_seq(size_t len)\ \ where len is the length of the new sequence, which will be returned.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 296 of file sequence.hpp.

Referenced by mds_utils::python::ublas::to_python().

template<class Derived>
template<class FwIt >
void mds_utils::python::Sequence_Base< Derived >::set ( FwIt  b,
FwIt  e 
)
inline

Sets a Python sequence from a range of values.

Template Parameters
FwItModels the Forward Iterator Concept.
Parameters
bStart iterator of the sequence.
ePast-the-end iterator of the sequence.
Precondition
The Derived class must provide the static member function\ \ static PyObject* new_seq(size_t len)\ \ where len is the length of the new sequence, which will be returned.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 330 of file sequence.hpp.

template<class Derived>
void mds_utils::python::Sequence_Base< Derived >::set ( const Obj o)
inline

Sets a Python sequence from another Python sequence.

Parameters
oAnother Python sequence.
Precondition
The Derived class must provide the static member function\ \ static PyObject* new_seq(size_t len)\ \ where len is the length of the new sequence, which will be returned.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 375 of file sequence.hpp.


The documentation for this class was generated from the following file: