| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef __CXX_Objects__h |
| #define __CXX_Objects__h |
|
|
| #include "CXX/WrapPython.h" |
| #include "CXX/Version.hxx" |
| #include "CXX/Python3/Config.hxx" |
| #include "CXX/Python3/CxxDebug.hxx" |
| #include "CXX/Python3/Exception.hxx" |
|
|
| #include <iostream> |
| #include STR_STREAM |
| #include <string> |
| #include <iterator> |
| #include <utility> |
| #include <typeinfo> |
| #include <algorithm> |
| #include <cstring> |
|
|
| namespace Py |
| { |
| PYCXX_EXPORT void ifPyErrorThrowCxxException(); |
|
|
| typedef Py_ssize_t sequence_index_type; |
| PYCXX_EXPORT Py_ssize_t numeric_limits_max(); |
|
|
| |
| class Object; |
| class Type; |
| template<TEMPLATE_TYPENAME T> class SeqBase; |
| class Bytes; |
| class String; |
| class List; |
| template<TEMPLATE_TYPENAME T> class MapBase; |
| class Tuple; |
| class Dict; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| class PYCXX_EXPORT Object |
| { |
| private: |
| |
| |
| |
| |
| |
| PyObject *p; |
|
|
| protected: |
| void set( PyObject *pyob, bool owned = false ) |
| { |
| release(); |
| p = pyob; |
| if( !owned ) |
| { |
| Py::_XINCREF( p ); |
| } |
| validate(); |
| } |
|
|
| void release() |
| { |
| Py::_XDECREF( p ); |
| p = NULL; |
| } |
|
|
| void validate(); |
|
|
| public: |
| |
| explicit Object( PyObject *pyob=Py::_None(), bool owned = false ) |
| : p( pyob ) |
| { |
| if( !owned ) |
| { |
| Py::_XINCREF( p ); |
| } |
| validate(); |
| } |
|
|
| |
| Object( const Object &ob ) |
| : p( ob.p ) |
| { |
| Py::_XINCREF( p ); |
| validate(); |
| } |
|
|
| |
| Object &operator=( const Object &rhs ) |
| { |
| set( rhs.p ); |
| return *this; |
| } |
|
|
| Object &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
|
|
| return *this; |
| } |
|
|
| |
| virtual ~Object() |
| { |
| release(); |
| } |
|
|
| |
| PyObject *operator*() const |
| { |
| return p; |
| } |
|
|
| |
| void increment_reference_count() |
| { |
| Py::_XINCREF( p ); |
| } |
|
|
| void decrement_reference_count() |
| { |
| |
| if( reference_count() == 1 ) |
| { |
| throw RuntimeError( "Object::decrement_reference_count error." ); |
| } |
| Py::_XDECREF( p ); |
| } |
|
|
| |
| PyObject *ptr() const |
| { |
| return p; |
| } |
|
|
| |
| |
| |
|
|
| |
| virtual bool accepts( PyObject * ) const |
| { |
| |
| return true; |
| } |
|
|
| Py_ssize_t reference_count() const |
| { |
| return p ? p->ob_refcnt : 0; |
| } |
|
|
| Type type() const; |
|
|
| String str() const; |
|
|
| std::string as_string() const; |
|
|
| String repr() const; |
|
|
| List dir() const; |
|
|
| bool hasAttr( const std::string &s ) const |
| { |
| return PyObject_HasAttrString( p, const_cast<char*>( s.c_str() ) ) ? true: false; |
| } |
|
|
| Object getAttr( const std::string &s ) const |
| { |
| return Object( PyObject_GetAttrString( p, const_cast<char*>( s.c_str() ) ), true ); |
| } |
|
|
| Object callMemberFunction( const std::string &function_name ) const; |
| Object callMemberFunction( const std::string &function_name, const Tuple &args ) const; |
| Object callMemberFunction( const std::string &function_name, const Tuple &args, const Dict &kw ) const; |
|
|
| Object getItem( const Object &key ) const |
| { |
| return Object( PyObject_GetItem( p, *key ), true ); |
| } |
|
|
| Py_hash_t hashValue() const |
| { |
| return PyObject_Hash( p ); |
| } |
|
|
| |
| bool as_bool() const |
| { |
| return PyObject_IsTrue( ptr() ) != 0; |
| } |
|
|
| bool is( PyObject *pother ) const |
| { |
| return p == pother; |
| } |
|
|
| bool is( const Object &other ) const |
| { |
| return p == other.p; |
| } |
|
|
| bool isNull() const |
| { |
| return p == NULL; |
| } |
|
|
| bool isNone() const |
| { |
| return p == _None(); |
| } |
|
|
| bool isCallable() const |
| { |
| return PyCallable_Check( p ) != 0; |
| } |
|
|
| bool isDict() const |
| { |
| return Py::_Dict_Check( p ); |
| } |
|
|
| bool isList() const |
| { |
| return Py::_List_Check( p ); |
| } |
|
|
| bool isMapping() const |
| { |
| return PyMapping_Check( p ) != 0; |
| } |
|
|
| bool isNumeric() const |
| { |
| return PyNumber_Check( p ) != 0; |
| } |
|
|
| bool isSequence() const |
| { |
| return PySequence_Check( p ) != 0; |
| } |
|
|
| bool isTrue() const |
| { |
| return PyObject_IsTrue( p ) != 0; |
| } |
|
|
| bool isType( const Type &t ) const; |
|
|
| bool isTuple() const |
| { |
| return Py::_Tuple_Check( p ); |
| } |
|
|
| bool isString() const |
| { |
| return Py::_Unicode_Check( p ); |
| } |
|
|
| bool isBytes() const |
| { |
| return Py::_Bytes_Check( p ); |
| } |
|
|
| bool isBoolean() const |
| { |
| return Py::_Boolean_Check( p ); |
| } |
|
|
| |
| void setAttr( const std::string &s, const Object &value ) |
| { |
| if( PyObject_SetAttrString( p, const_cast<char*>( s.c_str() ), *value ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void delAttr( const std::string &s ) |
| { |
| if( PyObject_DelAttrString( p, const_cast<char*>( s.c_str() ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| |
| |
|
|
| void delItem( const Object &key ) |
| { |
| if( PyObject_DelItem( p, *key ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| |
|
|
| }; |
| |
|
|
| |
| class PYCXX_EXPORT Null: public Object |
| { |
| public: |
| Null() |
| : Object( NULL ) |
| { |
| } |
| virtual ~Null() |
| { |
| } |
|
|
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob == NULL; |
| } |
| }; |
|
|
| |
| PYCXX_EXPORT bool operator==( const Object &o1, const Object &o2 ); |
| PYCXX_EXPORT bool operator!=( const Object &o1, const Object &o2 ); |
| PYCXX_EXPORT bool operator>=( const Object &o1, const Object &o2 ); |
| PYCXX_EXPORT bool operator<=( const Object &o1, const Object &o2 ); |
| PYCXX_EXPORT bool operator<( const Object &o1, const Object &o2 ); |
| PYCXX_EXPORT bool operator>( const Object &o1, const Object &o2 ); |
|
|
| |
|
|
|
|
| |
| |
| |
| inline Object asObject( PyObject *p ) |
| { |
| return Object( p, true ); |
| } |
|
|
| |
| inline PyObject *new_reference_to( PyObject *p ) |
| { |
| Py::_XINCREF( p ); |
| return p; |
| } |
|
|
| inline PyObject *new_reference_to( const Object &g ) |
| { |
| PyObject *p = g.ptr(); |
| Py::_XINCREF( p ); |
| return p; |
| } |
|
|
| |
| inline Object None() |
| { |
| return Object( Py::_None() ); |
| } |
|
|
| |
| inline Object False() |
| { |
| return Object( Py::_False() ); |
| } |
|
|
| inline Object True() |
| { |
| return Object( Py::_True() ); |
| } |
|
|
| |
| #ifndef CXX_NO_IOSTREAMS |
| PYCXX_EXPORT std::ostream &operator<<( std::ostream &os, const Object &ob ); |
| #endif |
|
|
| |
| class PYCXX_EXPORT Type: public Object |
| { |
| public: |
| explicit Type( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Type( const Object &ob ) |
| : Object( *ob ) |
| { |
| validate(); |
| } |
|
|
| Type( const Type &t ) |
| : Object( t ) |
| { |
| validate(); |
| } |
|
|
| Type &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Type &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Type_Check( pyob ); |
| } |
| }; |
|
|
| |
| |
| class PYCXX_EXPORT Boolean: public Object |
| { |
| public: |
| |
| Boolean( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Boolean( const Boolean &ob ) |
| : Object( *ob ) |
| { |
| validate(); |
| } |
|
|
| |
| Boolean( bool v=false ) |
| { |
| set( PyBool_FromLong( v ? 1 : 0 ), true ); |
| validate(); |
| } |
|
|
| explicit Boolean( const Object &ob ) |
| : Object( *ob ) |
| { |
| validate(); |
| } |
|
|
| |
| Boolean &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Boolean &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| |
| return pyob && PyObject_IsTrue( pyob ) != -1; |
| } |
|
|
| Boolean &operator=( bool v ) |
| { |
| set( PyBool_FromLong( v ? 1 : 0 ), true ); |
| return *this; |
| } |
|
|
| operator bool() const |
| { |
| return as_bool(); |
| } |
| }; |
|
|
| |
| |
| class PYCXX_EXPORT Long: public Object |
| { |
| public: |
| |
| explicit Long( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Long( const Long &ob ) |
| : Object( ob.ptr() ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Long( const Object &ob ) |
| : Object( PyNumber_Long( *ob ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Long( long v = 0L ) |
| : Object( PyLong_FromLong( v ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Long( unsigned long v ) |
| : Object( PyLong_FromUnsignedLong( v ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Long( int v ) |
| : Object( PyLong_FromLong( static_cast<long>( v ) ), true ) |
| { |
| validate(); |
| } |
|
|
| #ifdef HAVE_LONG_LONG |
| |
| explicit Long( PY_LONG_LONG v ) |
| : Object( PyLong_FromLongLong( v ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Long( unsigned PY_LONG_LONG v ) |
| : Object( PyLong_FromUnsignedLongLong( v ), true ) |
| { |
| validate(); |
| } |
| #endif |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Long_Check( pyob ); |
| } |
|
|
| |
| Long &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Long &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( PyNumber_Long( rhsp ), true ); |
| return *this; |
| } |
|
|
| |
| Long &operator=( int v ) |
| { |
| set( PyLong_FromLong( long( v ) ), true ); |
| return *this; |
| } |
|
|
| |
| Long &operator=( long v ) |
| { |
| set( PyLong_FromLong( v ), true ); |
| return *this; |
| } |
|
|
| |
| Long &operator=( unsigned long v ) |
| { |
| set( PyLong_FromUnsignedLong( v ), true ); |
| return *this; |
| } |
|
|
| #ifdef HAVE_LONG_LONG |
| Long &operator=( PY_LONG_LONG v ) |
| { |
| set( PyLong_FromLongLong( v ), true ); |
| return *this; |
| } |
|
|
| Long &operator=( unsigned PY_LONG_LONG v ) |
| { |
| set( PyLong_FromUnsignedLongLong( v ), true ); |
| return *this; |
| } |
| #endif |
|
|
| |
| long as_long() const |
| { |
| return PyLong_AsLong( ptr() ); |
| } |
|
|
| operator long() const |
| { |
| return as_long(); |
| } |
|
|
| operator int() const |
| { |
| return static_cast<int>( as_long() ); |
| } |
|
|
| |
| long as_unsigned_long() const |
| { |
| return PyLong_AsUnsignedLong( ptr() ); |
| } |
|
|
| |
| operator unsigned long() const |
| { |
| return as_unsigned_long(); |
| } |
|
|
| double as_double() const |
| { |
| return PyLong_AsDouble( ptr() ); |
| } |
|
|
| operator double() const |
| { |
| return as_double(); |
| } |
|
|
| #ifdef HAVE_LONG_LONG |
| PY_LONG_LONG as_long_long() const |
| { |
| return PyLong_AsLongLong( ptr() ); |
| } |
|
|
| operator PY_LONG_LONG() const |
| { |
| return as_long_long(); |
| } |
|
|
| unsigned PY_LONG_LONG as_unsigned_long_long() const |
| { |
| return PyLong_AsUnsignedLongLong( ptr() ); |
| } |
|
|
| operator unsigned PY_LONG_LONG() const |
| { |
| return as_unsigned_long_long(); |
| } |
| #endif |
|
|
| |
| Long operator++() |
| { |
| set( PyNumber_Add( ptr(), *Long( 1 ) ) ); |
| return *this; |
| } |
|
|
| |
| Long operator++( int ) |
| { |
| Long a = *this; |
| set( PyNumber_Add( ptr(), *Long( 1 ) ) ); |
| return a; |
| } |
|
|
| |
| Long operator--() |
| { |
| set( PyNumber_Subtract( ptr(), *Long( 1 ) ) ); |
| return *this; |
| } |
|
|
| |
| Long operator--( int ) |
| { |
| Long a = *this; |
| set( PyNumber_Subtract( ptr(), *Long( 1 ) ) ); |
| return a; |
| } |
| }; |
|
|
| #ifdef PYCXX_PYTHON_2TO3 |
| |
| typedef Long Int; |
| #ifdef HAVE_LONG_LONG |
| typedef Long LongLong; |
| #endif |
| #endif |
|
|
| #if 1 |
| |
| |
| bool operator!=( const Long &a, const Long &b ); |
| bool operator!=( const Long &a, int b ); |
| bool operator!=( const Long &a, long b ); |
| bool operator!=( int a, const Long &b ); |
| bool operator!=( long a, const Long &b ); |
| |
| bool operator==( const Long &a, const Long &b ); |
| bool operator==( const Long &a, int b ); |
| bool operator==( const Long &a, long b ); |
| bool operator==( int a, const Long &b ); |
| bool operator==( long a, const Long &b ); |
| |
| bool operator>( const Long &a, const Long &b ); |
| bool operator>( const Long &a, int b ); |
| bool operator>( const Long &a, long b ); |
| bool operator>( int a, const Long &b ); |
| bool operator>( long a, const Long &b ); |
| |
| bool operator>=( const Long &a, const Long &b ); |
| bool operator>=( const Long &a, int b ); |
| bool operator>=( const Long &a, long b ); |
| bool operator>=( int a, const Long &b ); |
| bool operator>=( long a, const Long &b ); |
| |
| bool operator<( const Long &a, const Long &b ); |
| bool operator<( const Long &a, int b ); |
| bool operator<( const Long &a, long b ); |
| bool operator<( int a, const Long &b ); |
| bool operator<( long a, const Long &b ); |
| |
| bool operator<=( const Long &a, const Long &b ); |
| bool operator<=( int a, const Long &b ); |
| bool operator<=( long a, const Long &b ); |
| bool operator<=( const Long &a, int b ); |
| bool operator<=( const Long &a, long b ); |
|
|
| #ifdef HAVE_LONG_LONG |
| |
| bool operator!=( const Long &a, PY_LONG_LONG b ); |
| bool operator!=( PY_LONG_LONG a, const Long &b ); |
| |
| bool operator==( const Long &a, PY_LONG_LONG b ); |
| bool operator==( PY_LONG_LONG a, const Long &b ); |
| |
| bool operator>( const Long &a, PY_LONG_LONG b ); |
| bool operator>( PY_LONG_LONG a, const Long &b ); |
| |
| bool operator>=( const Long &a, PY_LONG_LONG b ); |
| bool operator>=( PY_LONG_LONG a, const Long &b ); |
| |
| bool operator<( const Long &a, PY_LONG_LONG b ); |
| bool operator<( PY_LONG_LONG a, const Long &b ); |
| |
| bool operator<=( const Long &a, PY_LONG_LONG b ); |
| bool operator<=( PY_LONG_LONG a, const Long &b ); |
| #endif |
| #endif |
|
|
| |
| |
| |
| class PYCXX_EXPORT Float: public Object |
| { |
| public: |
| |
| explicit Float( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Float( const Float &f ) |
| : Object( f ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Float( double v=0.0 ) |
| : Object( PyFloat_FromDouble( v ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| Float( const Object &ob ) |
| : Object( PyNumber_Float( *ob ), true ) |
| { |
| validate(); |
| } |
|
|
| Float &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Float &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( PyNumber_Float( rhsp ), true ); |
| return *this; |
| } |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Float_Check( pyob ); |
| } |
|
|
| double as_double() const |
| { |
| return PyFloat_AsDouble( ptr() ); |
| } |
|
|
| |
| operator double() const |
| { |
| return as_double(); |
| } |
|
|
| |
| Float &operator=( double v ) |
| { |
| set( PyFloat_FromDouble( v ), true ); |
| return *this; |
| } |
| |
| Float &operator=( int v ) |
| { |
| set( PyFloat_FromDouble( double( v ) ), true ); |
| return *this; |
| } |
| |
| Float &operator=( long v ) |
| { |
| set( PyFloat_FromDouble( double( v ) ), true ); |
| return *this; |
| } |
| |
| Float &operator=( const Long &iob ) |
| { |
| set( PyFloat_FromDouble( double( iob.as_long() ) ), true ); |
| return *this; |
| } |
| }; |
|
|
| |
| |
| bool operator!=( const Float &a, const Float &b ); |
| bool operator!=( const Float &a, double b ); |
| bool operator!=( double a, const Float &b ); |
| |
| bool operator==( const Float &a, const Float &b ); |
| bool operator==( const Float &a, double b ); |
| bool operator==( double a, const Float &b ); |
| |
| bool operator>( const Float &a, const Float &b ); |
| bool operator>( const Float &a, double b ); |
| bool operator>( double a, const Float &b ); |
| |
| bool operator>=( const Float &a, const Float &b ); |
| bool operator>=( const Float &a, double b ); |
| bool operator>=( double a, const Float &b ); |
| |
| bool operator<( const Float &a, const Float &b ); |
| bool operator<( const Float &a, double b ); |
| bool operator<( double a, const Float &b ); |
| |
| bool operator<=( const Float &a, const Float &b ); |
| bool operator<=( double a, const Float &b ); |
| bool operator<=( const Float &a, double b ); |
|
|
| |
| |
| class PYCXX_EXPORT Complex: public Object |
| { |
| public: |
| |
| explicit Complex( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Complex( const Complex &f ) |
| : Object( f ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Complex( double v=0.0, double w=0.0 ) |
| :Object( PyComplex_FromDoubles( v, w ), true ) |
| { |
| validate(); |
| } |
|
|
| Complex &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Complex &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Complex_Check( pyob ); |
| } |
|
|
| #if !defined( Py_LIMITED_API ) |
| |
| operator Py_complex() const |
| { |
| return PyComplex_AsCComplex( ptr() ); |
| } |
| |
| Complex &operator=( const Py_complex &v ) |
| { |
| set( PyComplex_FromCComplex( v ), true ); |
| return *this; |
| } |
| #endif |
|
|
| |
| Complex &operator=( double v ) |
| { |
| set( PyComplex_FromDoubles( v, 0.0 ), true ); |
| return *this; |
| } |
| |
| Complex &operator=( int v ) |
| { |
| set( PyComplex_FromDoubles( double( v ), 0.0 ), true ); |
| return *this; |
| } |
| |
| Complex &operator=( long v ) |
| { |
| set( PyComplex_FromDoubles( double( v ), 0.0 ), true ); |
| return *this; |
| } |
| |
| Complex &operator=( const Long &iob ) |
| { |
| set( PyComplex_FromDoubles( double( iob.as_long() ), 0.0 ), true ); |
| return *this; |
| } |
|
|
| double real() const |
| { |
| return PyComplex_RealAsDouble( ptr() ); |
| } |
|
|
| double imag() const |
| { |
| return PyComplex_ImagAsDouble( ptr() ); |
| } |
| }; |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| template<TEMPLATE_TYPENAME T> |
| class seqref |
| { |
| protected: |
| SeqBase<T> &s; |
| sequence_index_type offset; |
| T the_item; |
|
|
| public: |
| seqref( SeqBase<T> &seq, sequence_index_type j ) |
| : s( seq ) |
| , offset( j ) |
| , the_item( s.getItem( j ) ) |
| {} |
|
|
| seqref( const seqref<T> &range ) |
| : s( range.s ) |
| , offset( range.offset ) |
| , the_item( range.the_item ) |
| {} |
|
|
| |
| seqref( Object &obj ) |
| : s( dynamic_cast< SeqBase<T>&>( obj ) ) |
| , offset( 0 ) |
| , the_item( s.getItem( offset ) ) |
| {} |
|
|
| ~seqref() |
| {} |
|
|
| operator T() const |
| { |
| return the_item; |
| } |
|
|
| seqref<T> &operator=( const seqref<T> &rhs ) |
| { |
| the_item = rhs.the_item; |
| s.setItem( offset, the_item ); |
| return *this; |
| } |
|
|
| seqref<T> &operator=( const T &ob ) |
| { |
| the_item = ob; |
| s.setItem( offset, ob ); |
| return *this; |
| } |
|
|
| |
| PyObject *ptr() const |
| { |
| return the_item.ptr(); |
| } |
|
|
| int reference_count() const |
| { |
| return the_item.reference_count(); |
| } |
|
|
| Type type() const |
| { |
| return the_item.type(); |
| } |
|
|
| String str() const; |
| String repr() const; |
|
|
| bool hasAttr( const std::string &attr_name ) const |
| { |
| return the_item.hasAttr( attr_name ); |
| } |
|
|
| Object getAttr( const std::string &attr_name ) const |
| { |
| return the_item.getAttr( attr_name ); |
| } |
|
|
| Object getItem( const Object &key ) const |
| { |
| return the_item.getItem( key ); |
| } |
|
|
| long hashValue() const |
| { |
| return the_item.hashValue(); |
| } |
|
|
| bool isCallable() const |
| { |
| return the_item.isCallable(); |
| } |
|
|
| bool isInstance() const |
| { |
| return the_item.isInstance(); |
| } |
|
|
| bool isDict() const |
| { |
| return the_item.isDict(); |
| } |
|
|
| bool isList() const |
| { |
| return the_item.isList(); |
| } |
|
|
| bool isMapping() const |
| { |
| return the_item.isMapping(); |
| } |
|
|
| bool isNumeric() const |
| { |
| return the_item.isNumeric(); |
| } |
|
|
| bool isSequence() const |
| { |
| return the_item.isSequence(); |
| } |
|
|
| bool isTrue() const |
| { |
| return the_item.isTrue(); |
| } |
|
|
| bool isType( const Type &t ) const |
| { |
| return the_item.isType( t ); |
| } |
|
|
| bool isTuple() const |
| { |
| return the_item.isTuple(); |
| } |
|
|
| bool isString() const |
| { |
| return the_item.isString(); |
| } |
| |
| void setAttr( const std::string &attr_name, const Object &value ) |
| { |
| the_item.setAttr( attr_name, value ); |
| } |
|
|
| void delAttr( const std::string &attr_name ) |
| { |
| the_item.delAttr( attr_name ); |
| } |
|
|
| void delItem( const Object &key ) |
| { |
| the_item.delItem( key ); |
| } |
|
|
| bool operator==( const Object &o2 ) const |
| { |
| return the_item == o2; |
| } |
|
|
| bool operator!=( const Object &o2 ) const |
| { |
| return the_item != o2; |
| } |
|
|
| bool operator>=( const Object &o2 ) const |
| { |
| return the_item >= o2; |
| } |
|
|
| bool operator<=( const Object &o2 ) const |
| { |
| return the_item <= o2; |
| } |
|
|
| bool operator<( const Object &o2 ) const |
| { |
| return the_item < o2; |
| } |
|
|
| bool operator>( const Object &o2 ) const |
| { |
| return the_item > o2; |
| } |
| }; |
|
|
|
|
| |
| |
|
|
| template<TEMPLATE_TYPENAME T> |
| class SeqBase: public Object |
| { |
| public: |
| |
| typedef PyCxx_ssize_t size_type; |
| typedef seqref<T> reference; |
| typedef T const_reference; |
| typedef seqref<T> *pointer; |
| typedef int difference_type; |
| typedef T value_type; |
|
|
| virtual size_type max_size() const |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| return numeric_limits_max(); |
| } |
|
|
| virtual size_type capacity() const |
| { |
| return size(); |
| } |
|
|
| virtual void swap( SeqBase<T> &c ) |
| { |
| SeqBase<T> temp = c; |
| c = ptr(); |
| set( temp.ptr() ); |
| } |
|
|
| virtual size_type size() const |
| { |
| return PySequence_Length( ptr() ); |
| } |
|
|
| explicit SeqBase() |
| :Object( PyTuple_New( 0 ), true ) |
| { |
| validate(); |
| } |
|
|
| explicit SeqBase( PyObject *pyob, bool owned=false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| SeqBase( const Object &ob ) |
| : Object( ob ) |
| { |
| validate(); |
| } |
|
|
| |
| SeqBase<T> &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| SeqBase<T> &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && PySequence_Check( pyob ); |
| } |
|
|
| size_type length() const |
| { |
| return PySequence_Length( ptr() ); |
| } |
|
|
| |
| const T operator[]( sequence_index_type index ) const |
| { |
| return getItem( index ); |
| } |
|
|
| seqref<T> operator[]( sequence_index_type index ) |
| { |
| return seqref<T>( *this, index ); |
| } |
|
|
| virtual T getItem( sequence_index_type i ) const |
| { |
| return T( asObject( PySequence_GetItem( ptr(), i ) ) ); |
| } |
|
|
| virtual void setItem( sequence_index_type i, const T &ob ) |
| { |
| if( PySequence_SetItem( ptr(), i, *ob ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| SeqBase<T> repeat( int count ) const |
| { |
| return SeqBase<T>( PySequence_Repeat( ptr(), count ), true ); |
| } |
|
|
| SeqBase<T> concat( const SeqBase<T> &other ) const |
| { |
| return SeqBase<T>( PySequence_Concat( ptr(), *other ), true ); |
| } |
|
|
| |
| const T front() const |
| { |
| return getItem( 0 ); |
| } |
|
|
| seqref<T> front() |
| { |
| return seqref<T>( *this, 0 ); |
| } |
|
|
| const T back() const |
| { |
| return getItem( size()-1 ); |
| } |
|
|
| seqref<T> back() |
| { |
| return seqref<T>( *this, size()-1 ); |
| } |
|
|
| void verify_length( size_type required_size ) const |
| { |
| if( size() != required_size ) |
| { |
| throw IndexError( "Unexpected SeqBase<T> length." ); |
| } |
| } |
|
|
| void verify_length( size_type min_size, size_type max_size ) const |
| { |
| size_type n = size(); |
| if( n < min_size || n > max_size ) |
| { |
| throw IndexError( "Unexpected SeqBase<T> length." ); |
| } |
| } |
|
|
| class iterator |
| { |
| public: |
| using iterator_category = std::random_access_iterator_tag; |
| using value_type = seqref<T>; |
| using difference_type = int; |
| using pointer = value_type*; |
| using reference = value_type&; |
|
|
| protected: |
| friend class SeqBase<T>; |
| SeqBase<T> *seq; |
| sequence_index_type count; |
|
|
| public: |
| ~iterator() |
| {} |
|
|
| iterator() |
| : seq( 0 ) |
| , count( 0 ) |
| {} |
|
|
| iterator( SeqBase<T> *s, Py_ssize_t where ) |
| : seq( s ) |
| , count( where ) |
| {} |
|
|
| iterator( const iterator &other ) |
| : seq( other.seq ) |
| , count( other.count ) |
| {} |
|
|
| bool eql( const iterator &other ) const |
| { |
| return seq->ptr() == other.seq->ptr() && count == other.count; |
| } |
|
|
| bool neq( const iterator &other ) const |
| { |
| return seq->ptr() != other.seq->ptr() || count != other.count; |
| } |
|
|
| bool lss( const iterator &other ) const |
| { |
| return count < other.count; |
| } |
|
|
| bool gtr( const iterator &other ) const |
| { |
| return count > other.count; |
| } |
|
|
| bool leq( const iterator &other ) const |
| { |
| return count <= other.count; |
| } |
|
|
| bool geq( const iterator &other ) const |
| { |
| return count >= other.count; |
| } |
|
|
| seqref<T> operator*() |
| { |
| return seqref<T>( *seq, count ); |
| } |
|
|
| seqref<T> operator[]( sequence_index_type i ) |
| { |
| return seqref<T>( *seq, count + i ); |
| } |
|
|
| iterator &operator=( const iterator &other ) |
| { |
| if( this != &other ) |
| { |
| seq = other.seq; |
| count = other.count; |
| } |
| return *this; |
| } |
|
|
| iterator operator+( sequence_index_type n ) const |
| { |
| return iterator( seq, count + n ); |
| } |
|
|
| iterator operator-( sequence_index_type n ) const |
| { |
| return iterator( seq, count - n ); |
| } |
|
|
| iterator &operator+=( sequence_index_type n ) |
| { |
| count = count + n; |
| return *this; |
| } |
|
|
| iterator &operator-=( sequence_index_type n ) |
| { |
| count = count - n; |
| return *this; |
| } |
|
|
| sequence_index_type operator-( const iterator &other ) const |
| { |
| if( seq->ptr() != other.seq->ptr() ) |
| { |
| throw RuntimeError( "SeqBase<T>::iterator comparison error" ); |
| } |
| return count - other.count; |
| } |
|
|
| |
| iterator &operator++() |
| { |
| count++; |
| return *this; |
| } |
|
|
| |
| iterator operator++( int ) |
| { |
| return iterator( seq, count++ ); |
| } |
|
|
| |
| iterator &operator--() |
| { |
| count--; |
| return *this; |
| } |
|
|
| |
| iterator operator--( int ) |
| { |
| return iterator( seq, count-- ); |
| } |
|
|
| std::string diagnose() const |
| { |
| std::OSTRSTREAM oss; |
| oss << "iterator diagnosis " << seq << ", " << count << std::ends; |
| return std::string( oss.str() ); |
| } |
|
|
| }; |
|
|
| iterator begin() |
| { |
| return iterator( this, 0 ); |
| } |
|
|
| iterator end() |
| { |
| return iterator( this, length() ); |
| } |
|
|
| class const_iterator |
| { |
| public: |
| using iterator_category = std::random_access_iterator_tag; |
| using value_type = const Object; |
| using difference_type = int; |
| using pointer = value_type*; |
| using reference = value_type&; |
|
|
| protected: |
| friend class SeqBase<T>; |
| const SeqBase<T> *seq; |
| sequence_index_type count; |
|
|
| public: |
| ~const_iterator() |
| {} |
|
|
| const_iterator() |
| : seq( 0 ) |
| , count( 0 ) |
| {} |
|
|
| const_iterator( const SeqBase<T> *s, sequence_index_type where ) |
| : seq( s ) |
| , count( where ) |
| {} |
|
|
| const_iterator( const const_iterator &other ) |
| : seq( other.seq ) |
| , count( other.count ) |
| {} |
|
|
| const T operator*() const |
| { |
| return seq->getItem( count ); |
| } |
|
|
| const T operator[]( sequence_index_type i ) const |
| { |
| return seq->getItem( count + i ); |
| } |
|
|
| const_iterator &operator=( const const_iterator &other ) |
| { |
| if( this != &other ) |
| { |
| seq = other.seq; |
| count = other.count; |
| } |
| return *this; |
| } |
|
|
| const_iterator operator+( sequence_index_type n ) const |
| { |
| return const_iterator( seq, count + n ); |
| } |
|
|
| bool eql( const const_iterator &other ) const |
| { |
| return seq->ptr() == other.seq->ptr() && count == other.count; |
| } |
|
|
| bool neq( const const_iterator &other ) const |
| { |
| return seq->ptr() != other.seq->ptr() || count != other.count; |
| } |
|
|
| bool lss( const const_iterator &other ) const |
| { |
| return count < other.count; |
| } |
|
|
| bool gtr( const const_iterator &other ) const |
| { |
| return count > other.count; |
| } |
|
|
| bool leq( const const_iterator &other ) const |
| { |
| return count <= other.count; |
| } |
|
|
| bool geq( const const_iterator &other ) const |
| { |
| return count >= other.count; |
| } |
|
|
| const_iterator operator-( sequence_index_type n ) |
| { |
| return const_iterator( seq, count - n ); |
| } |
|
|
| const_iterator &operator+=( sequence_index_type n ) |
| { |
| count = count + n; |
| return *this; |
| } |
|
|
| const_iterator &operator-=( sequence_index_type n ) |
| { |
| count = count - n; |
| return *this; |
| } |
|
|
| int operator-( const const_iterator &other ) const |
| { |
| if( *seq != *other.seq ) |
| { |
| throw RuntimeError( "SeqBase<T>::const_iterator::- error" ); |
| } |
| return count - other.count; |
| } |
|
|
| |
| const_iterator &operator++() |
| { |
| count++; |
| return *this; |
| } |
|
|
| |
| const_iterator operator++( int ) |
| { |
| return const_iterator( seq, count++ ); |
| } |
|
|
| |
| const_iterator &operator--() |
| { |
| count--; |
| return *this; |
| } |
|
|
| |
| const_iterator operator--( int ) |
| { |
| return const_iterator( seq, count-- ); |
| } |
|
|
| }; |
|
|
| const_iterator begin() const |
| { |
| return const_iterator( this, 0 ); |
| } |
|
|
| const_iterator end() const |
| { |
| return const_iterator( this, length() ); |
| } |
| }; |
|
|
| |
| typedef SeqBase<Object> Sequence; |
|
|
| template <TEMPLATE_TYPENAME T> bool operator==( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator!=( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator< ( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator> ( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator<=( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator>=( const EXPLICIT_TYPENAME SeqBase<T>::iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::iterator &right ); |
|
|
| template <TEMPLATE_TYPENAME T> bool operator==( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator!=( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator< ( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator> ( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator<=( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator>=( const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator &right ); |
|
|
|
|
| PYCXX_EXPORT extern bool operator==( const Sequence::iterator &left, const Sequence::iterator &right ); |
| PYCXX_EXPORT extern bool operator!=( const Sequence::iterator &left, const Sequence::iterator &right ); |
| PYCXX_EXPORT extern bool operator< ( const Sequence::iterator &left, const Sequence::iterator &right ); |
| PYCXX_EXPORT extern bool operator> ( const Sequence::iterator &left, const Sequence::iterator &right ); |
| PYCXX_EXPORT extern bool operator<=( const Sequence::iterator &left, const Sequence::iterator &right ); |
| PYCXX_EXPORT extern bool operator>=( const Sequence::iterator &left, const Sequence::iterator &right ); |
|
|
| PYCXX_EXPORT extern bool operator==( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator!=( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator< ( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator> ( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator<=( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator>=( const Sequence::const_iterator &left, const Sequence::const_iterator &right ); |
|
|
| |
| |
| |
| |
| |
| #if !defined(Py_LIMITED_API) && !defined(Py_UNICODE_DEPRECATED) |
| typedef std::basic_string<Py_UNICODE> unicodestring; |
| extern Py_UNICODE unicode_null_string[1]; |
| #endif |
| typedef std::basic_string<char32_t> ucs4string; |
| extern char32_t ucs4_null_string[1]; |
|
|
| class PYCXX_EXPORT Byte: public Object |
| { |
| public: |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob != NULL |
| && Py::_Bytes_Check( pyob ) |
| && PySequence_Length( pyob ) == 1; |
| } |
|
|
| explicit Byte( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Byte( const Object &ob ) |
| : Object( ob ) |
| { |
| validate(); |
| } |
|
|
| Byte( const std::string &v = "" ) |
| : Object( PyBytes_FromStringAndSize( const_cast<char*>( v.c_str() ), 1 ), true ) |
| { |
| validate(); |
| } |
|
|
| Byte( char v ) |
| : Object( PyBytes_FromStringAndSize( &v, 1 ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| Byte &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Byte &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| |
| Byte &operator=( const std::string &v ) |
| { |
| set( PyBytes_FromStringAndSize( const_cast<char*>( v.c_str() ), 1 ), true ); |
| return *this; |
| } |
|
|
| Byte &operator=( char v ) |
| { |
| set( PyUnicode_FromStringAndSize( &v, 1 ), true ); |
| return *this; |
| } |
|
|
| |
| operator Bytes() const; |
| }; |
|
|
| class PYCXX_EXPORT Bytes: public SeqBase<Byte> |
| { |
| public: |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob != NULL && Py::_Bytes_Check( pyob ); |
| } |
|
|
| virtual size_type capacity() const |
| { |
| return max_size(); |
| } |
|
|
| explicit Bytes( PyObject *pyob, bool owned = false ) |
| : SeqBase<Byte>( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Bytes( const Object &ob ) |
| : SeqBase<Byte>( ob ) |
| { |
| validate(); |
| } |
|
|
| Bytes() |
| : SeqBase<Byte>( PyBytes_FromStringAndSize( "", 0 ), true ) |
| { |
| validate(); |
| } |
|
|
| Bytes( const std::string &v ) |
| : SeqBase<Byte>( PyBytes_FromStringAndSize( const_cast<char*>( v.data() ), v.length() ), true ) |
| { |
| validate(); |
| } |
|
|
| Bytes( const std::string &v, Py_ssize_t vsize ) |
| : SeqBase<Byte>( PyBytes_FromStringAndSize( const_cast<char*>( v.data() ), vsize ), true ) |
| { |
| validate(); |
| } |
|
|
| Bytes( const char *v ) |
| : SeqBase<Byte>( PyBytes_FromString( v ), true ) |
| { |
| validate(); |
| } |
|
|
| Bytes( const char *v, Py_ssize_t vsize ) |
| : SeqBase<Byte>( PyBytes_FromStringAndSize( const_cast<char*>( v ), vsize ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| Bytes &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Bytes &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| |
| Bytes &operator=( const std::string &v ) |
| { |
| set( PyBytes_FromStringAndSize( const_cast<char*>( v.data() ), v.length() ), true ); |
| return *this; |
| } |
|
|
| String decode( const char *encoding, const char *error="strict" ); |
|
|
| |
| virtual size_type size() const |
| { |
| return PyBytes_Size( ptr() ); |
| } |
|
|
| operator std::string() const |
| { |
| return as_std_string(); |
| } |
|
|
| std::string as_std_string() const |
| { |
| return std::string( PyBytes_AsString( ptr() ), static_cast<size_t>( PyBytes_Size( ptr() ) ) ); |
| } |
| }; |
|
|
| class PYCXX_EXPORT Char: public Object |
| { |
| public: |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return (pyob != 0 && |
| Py::_Unicode_Check( pyob ) && |
| PySequence_Length( pyob ) == 1); |
| } |
|
|
| explicit Char( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Char( const Object &ob ) |
| : Object( ob ) |
| { |
| validate(); |
| } |
|
|
| Char( int v ) |
| : Object( PyUnicode_FromOrdinal( v ), true ) |
| { |
| validate(); |
| } |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| Char( Py_UNICODE v ) |
| : Object( PyUnicode_FromOrdinal( v ), true ) |
| { |
| validate(); |
| } |
| #endif |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| Char( const unicodestring &v ) |
| : Object( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, const_cast<Py_UNICODE*>( v.data() ),1 ), true ) |
| { |
| validate(); |
| } |
| #endif |
|
|
| |
| Char &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Char &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| Char &operator=( const unicodestring &v ) |
| { |
| set( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, const_cast<Py_UNICODE*>( v.data() ), 1 ), true ); |
| return *this; |
| } |
| #endif |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| Char &operator=( int v_ ) |
| { |
| Py_UNICODE v( v_ ); |
| set( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, &v, 1 ), true ); |
| return *this; |
| } |
| #endif |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| Char &operator=( Py_UNICODE v ) |
| { |
| set( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, &v, 1 ), true ); |
| return *this; |
| } |
| #endif |
|
|
| long ord() |
| { |
| #if !defined( Py_LIMITED_API ) |
| return static_cast<long>( PyUnicode_ReadChar( ptr(), 0 ) ); |
| #else |
| |
| |
| wchar_t buf[2]; |
| Py_ssize_t num_elements = PyUnicode_AsWideChar( ptr(), buf, 2 ); |
|
|
| |
| if( num_elements == 1 ) |
| { |
| return static_cast<long>( buf[0] ); |
| } |
| |
| if( num_elements == 2 ) |
| { |
| |
| return static_cast<long>( ((buf[0]-0xd800)*0x400) + (buf[1]-0xdc00) + 0x10000); |
| } |
| return 0; |
| #endif |
|
|
| } |
|
|
| |
| operator String() const; |
| }; |
|
|
| class PYCXX_EXPORT String: public SeqBase<Char> |
| { |
| public: |
| virtual size_type capacity() const |
| { |
| return max_size(); |
| } |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob != NULL && Py::_Unicode_Check( pyob ); |
| } |
|
|
| explicit String( PyObject *pyob, bool owned = false ) |
| : SeqBase<Char>( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| String( const Object &ob ) |
| : SeqBase<Char>( ob ) |
| { |
| validate(); |
| } |
|
|
| String() |
| : SeqBase<Char>( PyUnicode_FromString( "" ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const char *latin1 ) |
| : SeqBase<Char>( PyUnicode_FromString( latin1 ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const std::string &latin1 ) |
| : SeqBase<Char>( PyUnicode_FromStringAndSize( latin1.c_str(), latin1.size() ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const char *latin1, Py_ssize_t size ) |
| : SeqBase<Char>( PyUnicode_FromStringAndSize( latin1, size ), true ) |
| { |
| validate(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| String( const std::string &s, const char *encoding, const char *errors=NULL ) |
| : SeqBase<Char>( PyUnicode_Decode( s.c_str(), s.size(), encoding, errors ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const char *s, const char *encoding, const char *errors=NULL ) |
| : SeqBase<Char>( PyUnicode_Decode( s, strlen(s), encoding, errors ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const char *s, Py_ssize_t size, const char *encoding, const char *errors=NULL ) |
| : SeqBase<Char>( PyUnicode_Decode( s, size, encoding, errors ), true ) |
| { |
| validate(); |
| } |
|
|
| #if !defined( Py_LIMITED_API ) && !defined( Py_UNICODE_WIDE ) |
| |
| |
| String( const unsigned int *s, int length ) |
| : SeqBase<Char>( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, reinterpret_cast<const char32_t *>( s ), length ), true ) |
| { |
| validate(); |
| } |
|
|
| String( const int *s, int length ) |
| : SeqBase<Char>( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, reinterpret_cast<const char32_t *>( s ), length ), true ) |
| { |
| validate(); |
| } |
| #endif |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| String( const Py_UNICODE *s, int length ) |
| : SeqBase<Char>( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, s, length ), true ) |
| { |
| validate(); |
| } |
| #endif |
|
|
| |
| String &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| String &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| #if !defined( Py_LIMITED_API ) && !defined(Py_UNICODE_DEPRECATED) |
| String &operator=( const unicodestring &v ) |
| { |
| set( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, const_cast<Py_UNICODE *>( v.data() ), v.length() ), true ); |
| return *this; |
| } |
| #endif |
|
|
| #if !defined( Py_UNICODE_WIDE ) && !defined( Py_LIMITED_API ) |
| String &operator=( const ucs4string &v ) |
| { |
| set( PyUnicode_FromKindAndData( PyUnicode_4BYTE_KIND, reinterpret_cast<const char32_t *>( v.data() ), v.length() ), true ); |
| return *this; |
| } |
| #endif |
| |
| Bytes encode( const char *encoding, const char *error="strict" ) const |
| { |
| return Bytes( PyUnicode_AsEncodedString( ptr(), encoding, error ), true ); |
| } |
|
|
| #if !defined( Py_LIMITED_API ) |
| |
| virtual size_type size() const |
| { |
| return PyUnicode_GetLength( ptr() ); |
| } |
| #endif |
|
|
| #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9 |
| #if !defined( Py_LIMITED_API ) |
| const Py_UNICODE *unicode_data() const |
| { |
| return PyUnicode_AS_UNICODE( ptr() ); |
| } |
| #endif |
|
|
| #if !defined( Py_LIMITED_API ) |
| unicodestring as_unicodestring() const |
| { |
| return unicodestring( unicode_data(), PyUnicode_GetLength( ptr() ) ); |
| } |
| #endif |
| #endif |
|
|
| ucs4string as_ucs4string() const |
| { |
| Py_UCS4 *buf = new Py_UCS4[ size() ]; |
| if( PyUnicode_AsUCS4( ptr(), buf, size(), 0 ) == NULL ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| ucs4string ucs4( reinterpret_cast<char32_t *>(buf), size() ); |
| delete[] buf; |
|
|
| return ucs4; |
| } |
|
|
| operator std::string() const |
| { |
| |
| return as_std_string( NULL ); |
| } |
|
|
| std::string as_std_string( const char *encoding=NULL, const char *error="strict" ) const |
| { |
| Bytes b( encode( encoding, error ) ); |
| return b.as_std_string(); |
| } |
| }; |
|
|
| |
| |
| class PYCXX_EXPORT Tuple: public Sequence |
| { |
| public: |
| virtual void setItem( sequence_index_type offset, const Object&ob ) |
| { |
| |
| if( PyTuple_SetItem( ptr(), offset, new_reference_to( ob ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| |
| explicit Tuple( PyObject *pyob, bool owned = false ) |
| : Sequence( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Tuple( const Object &ob ) |
| : Sequence( ob ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Tuple( size_type size=0 ) |
| { |
| set( PyTuple_New( size ), true ); |
| validate(); |
| for( sequence_index_type i=0; i < size; i++ ) |
| { |
| if( PyTuple_SetItem( ptr(), i, new_reference_to( Py::_None() ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| } |
| |
| explicit Tuple( const Sequence &s ) |
| { |
| sequence_index_type limit( sequence_index_type( s.length() ) ); |
|
|
| set( PyTuple_New( limit ), true ); |
| validate(); |
|
|
| for( sequence_index_type i=0; i < limit; i++ ) |
| { |
| if( PyTuple_SetItem( ptr(), i, new_reference_to( s[i] ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| } |
|
|
| |
| Tuple &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Tuple &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Tuple_Check( pyob ); |
| } |
|
|
| Tuple getSlice( int i, int j ) const |
| { |
| return Tuple( PySequence_GetSlice( ptr(), i, j ), true ); |
| } |
|
|
| }; |
|
|
| class PYCXX_EXPORT TupleN: public Tuple |
| { |
| public: |
| TupleN() |
| : Tuple( (size_type)0 ) |
| { |
| } |
|
|
| TupleN( const Object &obj1 ) |
| : Tuple( 1 ) |
| { |
| setItem( 0, obj1 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2 ) |
| : Tuple( 2 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3 ) |
| : Tuple( 3 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4 ) |
| : Tuple( 4 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4, const Object &obj5 ) |
| : Tuple( 5 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| setItem( 4, obj5 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4, const Object &obj5, const Object &obj6 ) |
| : Tuple( 6 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| setItem( 4, obj5 ); |
| setItem( 5, obj6 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4, const Object &obj5, const Object &obj6, |
| const Object &obj7 ) |
| : Tuple( 7 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| setItem( 4, obj5 ); |
| setItem( 5, obj6 ); |
| setItem( 6, obj7 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4, const Object &obj5, const Object &obj6, |
| const Object &obj7, const Object &obj8 ) |
| : Tuple( 8 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| setItem( 4, obj5 ); |
| setItem( 5, obj6 ); |
| setItem( 6, obj7 ); |
| setItem( 7, obj8 ); |
| } |
|
|
| TupleN( const Object &obj1, const Object &obj2, const Object &obj3, |
| const Object &obj4, const Object &obj5, const Object &obj6, |
| const Object &obj7, const Object &obj8, const Object &obj9 ) |
| : Tuple( 9 ) |
| { |
| setItem( 0, obj1 ); |
| setItem( 1, obj2 ); |
| setItem( 2, obj3 ); |
| setItem( 3, obj4 ); |
| setItem( 4, obj5 ); |
| setItem( 5, obj6 ); |
| setItem( 6, obj7 ); |
| setItem( 7, obj8 ); |
| setItem( 8, obj9 ); |
| } |
|
|
| virtual ~TupleN() |
| { } |
| }; |
|
|
| |
| |
|
|
| class PYCXX_EXPORT List: public Sequence |
| { |
| public: |
| |
| explicit List( PyObject *pyob, bool owned = false ) |
| : Sequence( pyob, owned ) |
| { |
| validate(); |
| } |
| List( const Object &ob ) |
| : Sequence( ob ) |
| { |
| validate(); |
| } |
| |
| List( size_type size = 0 ) |
| { |
| set( PyList_New( size ), true ); |
| validate(); |
| for( sequence_index_type i=0; i < size; i++ ) |
| { |
| if( PyList_SetItem( ptr(), i, new_reference_to( Py::_None() ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| } |
|
|
| |
| List( const Sequence &s ) |
| : Sequence() |
| { |
| size_type n = s.length(); |
| set( PyList_New( n ), true ); |
| validate(); |
| for( sequence_index_type i=0; i < n; i++ ) |
| { |
| if( PyList_SetItem( ptr(), i, new_reference_to( s[i] ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| } |
|
|
| virtual size_type capacity() const |
| { |
| return max_size(); |
| } |
|
|
| |
| List &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| List &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_List_Check( pyob ); |
| } |
|
|
| List getSlice( Py_ssize_t i, Py_ssize_t j ) const |
| { |
| return List( PyList_GetSlice( ptr(), i, j ), true ); |
| } |
|
|
| void setSlice( Py_ssize_t i, Py_ssize_t j, const Object &v ) |
| { |
| if( PyList_SetSlice( ptr(), i, j, *v ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void append( const Object &ob ) |
| { |
| if( PyList_Append( ptr(), *ob ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void extend( const Object &ob ) |
| { |
| setSlice( size(), size(), ob ); |
| } |
|
|
| void insert( int i, const Object &ob ) |
| { |
| if( PyList_Insert( ptr(), i, *ob ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void sort() |
| { |
| if( PyList_Sort( ptr() ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void reverse() |
| { |
| if( PyList_Reverse( ptr() ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
| }; |
|
|
|
|
| |
| |
| template<TEMPLATE_TYPENAME T> |
| class mapref |
| { |
| protected: |
| MapBase<T> &s; |
| Object key; |
| T the_item; |
|
|
| public: |
| mapref( MapBase<T> &map, const std::string &k ) |
| : s( map ), the_item() |
| { |
| key = String( k ); |
| if( map.hasKey( key ) ) the_item = map.getItem( key ); |
| } |
|
|
| mapref( MapBase<T> &map, const Object &k ) |
| : s( map ), key( k ), the_item() |
| { |
| if( map.hasKey( key ) ) the_item = map.getItem( key ); |
| } |
|
|
| virtual ~mapref() |
| {} |
|
|
| |
| |
| mapref<T> &operator=( const mapref<T> &other ) |
| { |
| if( this != &other ) |
| { |
| the_item = other.the_item; |
| s.setItem( key, other.the_item ); |
| } |
| return *this; |
| } |
|
|
| mapref<T> &operator=( const T &ob ) |
| { |
| the_item = ob; |
| s.setItem( key, ob ); |
| return *this; |
| } |
|
|
| |
| operator T() const |
| { |
| return the_item; |
| } |
|
|
| |
| PyObject *ptr() const |
| { |
| return the_item.ptr(); |
| } |
|
|
| int reference_count() const |
| { |
| return the_item.reference_count(); |
| } |
|
|
| Type type() const |
| { |
| return the_item.type(); |
| } |
|
|
| String str() const |
| { |
| return the_item.str(); |
| } |
|
|
| String repr() const |
| { |
| return the_item.repr(); |
| } |
|
|
| bool hasAttr( const std::string &attr_name ) const |
| { |
| return the_item.hasAttr( attr_name ); |
| } |
|
|
| Object getAttr( const std::string &attr_name ) const |
| { |
| return the_item.getAttr( attr_name ); |
| } |
|
|
| Object getItem( const Object &k ) const |
| { |
| return the_item.getItem( k ); |
| } |
|
|
| long hashValue() const |
| { |
| return the_item.hashValue(); |
| } |
|
|
| bool isCallable() const |
| { |
| return the_item.isCallable(); |
| } |
|
|
| bool isInstance() const |
| { |
| return the_item.isInstance(); |
| } |
|
|
| bool isList() const |
| { |
| return the_item.isList(); |
| } |
|
|
| bool isMapping() const |
| { |
| return the_item.isMapping(); |
| } |
|
|
| bool isNumeric() const |
| { |
| return the_item.isNumeric(); |
| } |
|
|
| bool isSequence() const |
| { |
| return the_item.isSequence(); |
| } |
|
|
| bool isTrue() const |
| { |
| return the_item.isTrue(); |
| } |
|
|
| bool isType( const Type &t ) const |
| { |
| return the_item.isType( t ); |
| } |
|
|
| bool isTuple() const |
| { |
| return the_item.isTuple(); |
| } |
|
|
| bool isString() const |
| { |
| return the_item.isString(); |
| } |
|
|
| |
| void setAttr( const std::string &attr_name, const Object &value ) |
| { |
| the_item.setAttr( attr_name, value ); |
| } |
|
|
| void delAttr( const std::string &attr_name ) |
| { |
| the_item.delAttr( attr_name ); |
| } |
|
|
| void delItem( const Object &k ) |
| { |
| the_item.delItem( k ); |
| } |
| }; |
|
|
| #if 0 |
| |
| template< class T > |
| bool operator==( const mapref<T> &left, const mapref<T> &right ) |
| { |
| return true; |
| } |
|
|
| template< class T > |
| bool operator!=( const mapref<T> &left, const mapref<T> &right ) |
| { |
| return true; |
| } |
| #endif |
|
|
| template<TEMPLATE_TYPENAME T> |
| class MapBase: public Object |
| { |
| protected: |
| explicit MapBase() |
| {} |
| public: |
| |
| |
| |
| |
| |
| typedef PyCxx_ssize_t size_type; |
| typedef Object key_type; |
| typedef mapref<T> data_type; |
| typedef std::pair< const T, T > value_type; |
| typedef std::pair< const T, mapref<T> > reference; |
| typedef const std::pair< const T, const T > const_reference; |
| typedef std::pair< const T, mapref<T> > pointer; |
|
|
| |
| explicit MapBase( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| |
| MapBase( const Object &ob ) |
| : Object( ob ) |
| { |
| validate(); |
| } |
|
|
| |
| MapBase<T> &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| MapBase<T> &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && PyMapping_Check( pyob ); |
| } |
|
|
| |
| |
|
|
| void clear() |
| { |
| List k = keys(); |
| for( List::iterator i = k.begin(); i != k.end(); i++ ) |
| { |
| delItem( *i ); |
| } |
| } |
|
|
| virtual Py_ssize_t size() const |
| { |
| return PyMapping_Length( ptr() ); |
| } |
|
|
| |
| T operator[]( const std::string &key ) const |
| { |
| return getItem( key ); |
| } |
|
|
| T operator[]( const Object &key ) const |
| { |
| return getItem( key ); |
| } |
|
|
| mapref<T> operator[]( const char *key ) |
| { |
| return mapref<T>( *this, key ); |
| } |
|
|
| mapref<T> operator[]( const std::string &key ) |
| { |
| return mapref<T>( *this, key ); |
| } |
|
|
| mapref<T> operator[]( const Object &key ) |
| { |
| return mapref<T>( *this, key ); |
| } |
|
|
| Py_ssize_t length() const |
| { |
| return PyMapping_Length( ptr() ); |
| } |
|
|
| bool hasKey( const std::string &s ) const |
| { |
| return PyMapping_HasKeyString( ptr(),const_cast<char*>( s.c_str() ) ) != 0; |
| } |
|
|
| bool hasKey( const Object &s ) const |
| { |
| return PyMapping_HasKey( ptr(), s.ptr() ) != 0; |
| } |
|
|
| T getItem( const std::string &s ) const |
| { |
| return T( asObject( PyMapping_GetItemString( ptr(), const_cast<char*>( s.c_str() ) ) ) ); |
| } |
|
|
| T getItem( const Object &s ) const |
| { |
| return T( asObject( PyObject_GetItem( ptr(), s.ptr() ) ) ); |
| } |
|
|
| virtual void setItem( const char *s, const Object &ob ) |
| { |
| if( PyMapping_SetItemString( ptr(), const_cast<char*>( s ), *ob ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| virtual void setItem( const std::string &s, const Object &ob ) |
| { |
| if( PyMapping_SetItemString( ptr(), const_cast<char*>( s.c_str() ), *ob ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| virtual void setItem( const Object &s, const Object &ob ) |
| { |
| if( PyObject_SetItem( ptr(), s.ptr(), ob.ptr() ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void delItem( const std::string &s ) |
| { |
| if( PyMapping_DelItemString( ptr(), const_cast<char*>( s.c_str() ) ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| void delItem( const Object &s ) |
| { |
| if( PyMapping_DelItem( ptr(), *s ) == -1 ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| } |
|
|
| |
| List keys() const |
| { |
| return List( PyMapping_Keys( ptr() ), true ); |
| } |
|
|
| List values() const |
| { |
| |
| return List( PyMapping_Values( ptr() ), true ); |
| } |
|
|
| List items() const |
| { |
| return List( PyMapping_Items( ptr() ), true ); |
| } |
|
|
| class iterator |
| { |
| |
| protected: |
| typedef std::forward_iterator_tag iterator_category; |
| typedef std::pair< const T, T > value_type; |
| typedef int difference_type; |
| typedef std::pair< const T, mapref<T> > pointer; |
| typedef std::pair< const T, mapref<T> > reference; |
|
|
| friend class MapBase<T>; |
| |
| MapBase<T> *map; |
| List keys; |
| sequence_index_type pos; |
|
|
| public: |
| ~iterator() |
| {} |
|
|
| iterator() |
| : map( 0 ) |
| , keys() |
| , pos( 0 ) |
| {} |
|
|
| iterator( MapBase<T> *m, bool end = false ) |
| : map( m ) |
| , keys( m->keys() ) |
| , pos( end ? keys.length() : 0 ) |
| {} |
|
|
| iterator( const iterator &other ) |
| : map( other.map ) |
| , keys( other.keys ) |
| , pos( other.pos ) |
| {} |
|
|
| iterator( MapBase<T> *map_, List keys_, size_type pos_ ) |
| : map( map_ ) |
| , keys( keys_ ) |
| , pos( pos_ ) |
| {} |
|
|
| reference operator*() |
| { |
| Object key = keys[ pos ]; |
| return std::make_pair( key, mapref<T>( *map, key ) ); |
| } |
|
|
| iterator &operator=( const iterator &other ) |
| { |
| if( this != &other ) |
| { |
| map = other.map; |
| keys = other.keys; |
| pos = other.pos; |
| } |
| return *this; |
| } |
|
|
| bool eql( const iterator &other ) const |
| { |
| return map->ptr() == other.map->ptr() && pos == other.pos; |
| } |
|
|
| bool neq( const iterator &other ) const |
| { |
| return map->ptr() != other.map->ptr() || pos != other.pos; |
| } |
|
|
| |
| |
| |
|
|
| |
| iterator &operator++() |
| { |
| pos++; |
| return *this; |
| } |
|
|
| |
| iterator operator++( int ) |
| { |
| return iterator( map, keys, pos++ ); |
| } |
|
|
| |
| iterator &operator--() |
| { |
| pos--; |
| return *this; |
| } |
|
|
| |
| iterator operator--( int ) |
| { |
| return iterator( map, keys, pos-- ); |
| } |
|
|
| std::string diagnose() const |
| { |
| std::OSTRSTREAM oss; |
| oss << "iterator diagnosis " << map << ", " << pos << std::ends; |
| return std::string( oss.str() ); |
| } |
| }; |
|
|
| iterator begin() |
| { |
| return iterator( this, false ); |
| } |
|
|
| iterator end() |
| { |
| return iterator( this, true ); |
| } |
|
|
| class const_iterator |
| { |
| protected: |
| typedef std::forward_iterator_tag iterator_category; |
| typedef const std::pair< const T, T > value_type; |
| typedef int difference_type; |
| typedef const std::pair< const T, T > pointer; |
| typedef const std::pair< const T, T > reference; |
|
|
| friend class MapBase<T>; |
| const MapBase<T> *map; |
| List keys; |
| Py_ssize_t pos; |
|
|
| public: |
| ~const_iterator() |
| {} |
|
|
| const_iterator() |
| : map( 0 ) |
| , keys() |
| , pos() |
| {} |
|
|
| const_iterator( const MapBase<T> *m, List k, Py_ssize_t p ) |
| : map( m ) |
| , keys( k ) |
| , pos( p ) |
| {} |
|
|
| const_iterator( const const_iterator &other ) |
| : map( other.map ) |
| , keys( other.keys ) |
| , pos( other.pos ) |
| {} |
|
|
| bool eql( const const_iterator &other ) const |
| { |
| return map->ptr() == other.map->ptr() && pos == other.pos; |
| } |
|
|
| bool neq( const const_iterator &other ) const |
| { |
| return map->ptr() != other.map->ptr() || pos != other.pos; |
| } |
|
|
|
|
| |
| |
| |
| |
| |
|
|
| const_reference operator*() |
| { |
| Object key = keys[ pos ]; |
| return std::make_pair( key, mapref<T>( *map, key ) ); |
| } |
|
|
| const_iterator &operator=( const const_iterator &other ) |
| { |
| if( this != &other ) |
| { |
| map = other.map; |
| keys = other.keys; |
| pos = other.pos; |
| } |
| return *this; |
| } |
|
|
| |
| const_iterator &operator++() |
| { |
| pos++; |
| return *this; |
| } |
|
|
| |
| const_iterator operator++( int ) |
| { |
| return const_iterator( map, keys, pos++ ); |
| } |
|
|
| |
| const_iterator &operator--() |
| { |
| pos--; |
| return *this; |
| } |
|
|
| |
| const_iterator operator--( int ) |
| { |
| return const_iterator( map, keys, pos-- ); |
| } |
| }; |
|
|
| const_iterator begin() const |
| { |
| return const_iterator( this, keys(), 0 ); |
| } |
|
|
| const_iterator end() const |
| { |
| return const_iterator( this, keys(), length() ); |
| } |
|
|
| }; |
|
|
| typedef MapBase<Object> Mapping; |
|
|
| template <TEMPLATE_TYPENAME T> bool operator==( const EXPLICIT_TYPENAME MapBase<T>::iterator &left, const EXPLICIT_TYPENAME MapBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator!=( const EXPLICIT_TYPENAME MapBase<T>::iterator &left, const EXPLICIT_TYPENAME MapBase<T>::iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator==( const EXPLICIT_TYPENAME MapBase<T>::const_iterator &left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator &right ); |
| template <TEMPLATE_TYPENAME T> bool operator!=( const EXPLICIT_TYPENAME MapBase<T>::const_iterator &left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator &right ); |
|
|
| PYCXX_EXPORT extern bool operator==( const Mapping::iterator &left, const Mapping::iterator &right ); |
| PYCXX_EXPORT extern bool operator!=( const Mapping::iterator &left, const Mapping::iterator &right ); |
| PYCXX_EXPORT extern bool operator==( const Mapping::const_iterator &left, const Mapping::const_iterator &right ); |
| PYCXX_EXPORT extern bool operator!=( const Mapping::const_iterator &left, const Mapping::const_iterator &right ); |
|
|
|
|
| |
| |
| class PYCXX_EXPORT Dict: public Mapping |
| { |
| public: |
| |
| explicit Dict( PyObject *pyob, bool owned=false ) |
| : Mapping( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Dict( const Object &ob ) |
| : Mapping( ob ) |
| { |
| validate(); |
| } |
|
|
| |
| Dict() |
| { |
| set( PyDict_New(), true ); |
| validate(); |
| } |
|
|
| |
| Dict &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Dict &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && Py::_Dict_Check( pyob ); |
| } |
| }; |
|
|
| class PYCXX_EXPORT Callable: public Object |
| { |
| public: |
| |
| explicit Callable() |
| : Object() |
| {} |
|
|
| explicit Callable( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| Callable( const Object &ob ) |
| : Object( ob ) |
| { |
| validate(); |
| } |
|
|
| |
| Callable &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Callable &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| { |
| set( rhsp ); |
| } |
| return *this; |
| } |
|
|
| |
| virtual bool accepts( PyObject *pyob ) const |
| { |
| return pyob && PyCallable_Check( pyob ); |
| } |
|
|
| |
| Object apply( const Tuple &args ) const |
| { |
| PyObject *result = PyObject_CallObject( ptr(), args.ptr() ); |
| if( result == NULL ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| return asObject( result ); |
| } |
|
|
| |
| Object apply( const Tuple &args, const Dict &kw ) const |
| { |
| #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 9 |
| PyObject *result = PyObject_Call( ptr(), args.ptr(), kw.ptr() ); |
| #else |
| PyObject *result = PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ); |
| #endif |
| if( result == NULL ) |
| { |
| ifPyErrorThrowCxxException(); |
| } |
| return asObject( result ); |
| } |
| #if (!defined( Py_LIMITED_API ) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 9) || (defined( Py_LIMITED_API ) && Py_LIMITED_API+0 >= 0x03090000) |
| Object apply() const |
| { |
| PyObject *result = PyObject_CallNoArgs( ptr() ); |
| return asObject( result ); |
| } |
|
|
| Object apply( PyObject *pargs ) const |
| { |
| if( pargs == 0 ) |
| { |
| return apply( Tuple() ); |
| } |
| else |
| { |
| return apply( Tuple( pargs ) ); |
| } |
| } |
| #else |
| Object apply( PyObject *pargs = 0 ) const |
| { |
| if( pargs == 0 ) |
| { |
| return apply( Tuple() ); |
| } |
| else |
| { |
| return apply( Tuple( pargs ) ); |
| } |
| } |
| #endif |
| }; |
|
|
| class PYCXX_EXPORT Module: public Object |
| { |
| public: |
| explicit Module( PyObject *pyob, bool owned = false ) |
| : Object( pyob, owned ) |
| { |
| validate(); |
| } |
|
|
| |
| explicit Module( const std::string &s ) |
| : Object() |
| { |
| PyObject *m = PyImport_AddModule( const_cast<char *>( s.c_str() ) ); |
| set( m, false ); |
| validate(); |
| } |
|
|
| |
| Module( const Module &ob ) |
| : Object( *ob ) |
| { |
| validate(); |
| } |
|
|
| Module &operator=( const Object &rhs ) |
| { |
| return *this = *rhs; |
| } |
|
|
| Module &operator=( PyObject *rhsp ) |
| { |
| if( ptr() != rhsp ) |
| set( rhsp ); |
| return *this; |
| } |
|
|
| Dict getDict() const |
| { |
| return Dict( PyModule_GetDict( ptr() ) ); |
| |
| } |
| }; |
|
|
| |
| inline Object Object::callMemberFunction( const std::string &function_name ) const |
| { |
| Callable target( getAttr( function_name ) ); |
| return target.apply(); |
| } |
|
|
| inline Object Object::callMemberFunction( const std::string &function_name, const Tuple &args ) const |
| { |
| Callable target( getAttr( function_name ) ); |
| return target.apply( args ); |
| } |
|
|
| inline Object Object::callMemberFunction( const std::string &function_name, const Tuple &args, const Dict &kw ) const |
| { |
| Callable target( getAttr( function_name ) ); |
| return target.apply( args, kw ); |
| } |
|
|
| |
| inline Object operator+( const Object &a ) |
| { |
| return asObject( PyNumber_Positive( *a ) ); |
| } |
|
|
| inline Object operator-( const Object &a ) |
| { |
| return asObject( PyNumber_Negative( *a ) ); |
| } |
|
|
| inline Object abs( const Object &a ) |
| { |
| return asObject( PyNumber_Absolute( *a ) ); |
| } |
|
|
| |
| |
| inline Object operator+( const Object &a, const Object &b ) |
| { |
| return asObject( PyNumber_Add( *a, *b ) ); |
| } |
|
|
| inline Object operator+( const Object &a, int j ) |
| { |
| return asObject( PyNumber_Add( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator+( const Object &a, long j ) |
| { |
| return asObject( PyNumber_Add( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator+( const Object &a, double v ) |
| { |
| return asObject( PyNumber_Add( *a, *Float( v ) ) ); |
| } |
|
|
| inline Object operator+( int j, const Object &b ) |
| { |
| return asObject( PyNumber_Add( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator+( long j, const Object &b ) |
| { |
| return asObject( PyNumber_Add( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator+( double v, const Object &b ) |
| { |
| return asObject( PyNumber_Add( *Float( v ), *b ) ); |
| } |
|
|
| |
| |
| inline Object operator-( const Object &a, const Object &b ) |
| { |
| return asObject( PyNumber_Subtract( *a, *b ) ); |
| } |
|
|
| inline Object operator-( const Object &a, int j ) |
| { |
| return asObject( PyNumber_Subtract( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator-( const Object &a, double v ) |
| { |
| return asObject( PyNumber_Subtract( *a, *Float( v ) ) ); |
| } |
|
|
| inline Object operator-( int j, const Object &b ) |
| { |
| return asObject( PyNumber_Subtract( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator-( double v, const Object &b ) |
| { |
| return asObject( PyNumber_Subtract( *Float( v ), *b ) ); |
| } |
|
|
| |
| |
| inline Object operator*( const Object &a, const Object &b ) |
| { |
| return asObject( PyNumber_Multiply( *a, *b ) ); |
| } |
|
|
| inline Object operator*( const Object &a, int j ) |
| { |
| return asObject( PyNumber_Multiply( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator*( const Object &a, double v ) |
| { |
| return asObject( PyNumber_Multiply( *a, *Float( v ) ) ); |
| } |
|
|
| inline Object operator*( int j, const Object &b ) |
| { |
| return asObject( PyNumber_Multiply( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator*( double v, const Object &b ) |
| { |
| return asObject( PyNumber_Multiply( *Float( v ), *b ) ); |
| } |
|
|
| |
| |
| inline Object operator/( const Object &a, const Object &b ) |
| { |
| return asObject( PyNumber_TrueDivide( *a, *b ) ); |
| } |
|
|
| inline Object operator/( const Object &a, int j ) |
| { |
| return asObject( PyNumber_TrueDivide( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator/( const Object &a, double v ) |
| { |
| return asObject( PyNumber_TrueDivide( *a, *Float( v ) ) ); |
| } |
|
|
| inline Object operator/( int j, const Object &b ) |
| { |
| return asObject( PyNumber_TrueDivide( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator/( double v, const Object &b ) |
| { |
| return asObject( PyNumber_TrueDivide( *Float( v ), *b ) ); |
| } |
|
|
| |
| |
| inline Object operator%( const Object &a, const Object &b ) |
| { |
| return asObject( PyNumber_Remainder( *a, *b ) ); |
| } |
|
|
| inline Object operator%( const Object &a, int j ) |
| { |
| return asObject( PyNumber_Remainder( *a, *Long( j ) ) ); |
| } |
|
|
| inline Object operator%( const Object &a, double v ) |
| { |
| return asObject( PyNumber_Remainder( *a, *Float( v ) ) ); |
| } |
|
|
| inline Object operator%( int j, const Object &b ) |
| { |
| return asObject( PyNumber_Remainder( *Long( j ), *b ) ); |
| } |
|
|
| inline Object operator%( double v, const Object &b ) |
| { |
| return asObject( PyNumber_Remainder( *Float( v ), *b ) ); |
| } |
|
|
| |
| |
| inline Object type( const BaseException & ) |
| { |
| PyObject *ptype, *pvalue, *ptrace; |
| PyErr_Fetch( &ptype, &pvalue, &ptrace ); |
| Object result; |
| if( ptype ) |
| result = ptype; |
| PyErr_Restore( ptype, pvalue, ptrace ); |
| return result; |
| } |
|
|
| inline Object value( const BaseException & ) |
| { |
| PyObject *ptype, *pvalue, *ptrace; |
| PyErr_Fetch( &ptype, &pvalue, &ptrace ); |
| Object result; |
| if( pvalue ) |
| result = pvalue; |
| PyErr_Restore( ptype, pvalue, ptrace ); |
| return result; |
| } |
|
|
| inline Object trace( const BaseException & ) |
| { |
| PyObject *ptype, *pvalue, *ptrace; |
| PyErr_Fetch( &ptype, &pvalue, &ptrace ); |
| Object result; |
| if( ptrace ) |
| result = ptrace; |
| PyErr_Restore( ptype, pvalue, ptrace ); |
| return result; |
| } |
|
|
| template<TEMPLATE_TYPENAME T> |
| String seqref<T>::str() const |
| { |
| return the_item.str(); |
| } |
|
|
| template<TEMPLATE_TYPENAME T> |
| String seqref<T>::repr() const |
| { |
| return the_item.repr(); |
| } |
|
|
| } |
| #endif |
|
|