ExtensionTypeBase.hxx

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // Copyright (c) 1998 - 2007, The Regents of the University of California
00004 // Produced at the Lawrence Livermore National Laboratory
00005 // All rights reserved.
00006 //
00007 // This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
00008 // full copyright notice is contained in the file COPYRIGHT located at the root
00009 // of the PyCXX distribution.
00010 //
00011 // Redistribution  and  use  in  source  and  binary  forms,  with  or  without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 //  - Redistributions of  source code must  retain the above  copyright notice,
00015 //    this list of conditions and the disclaimer below.
00016 //  - Redistributions in binary form must reproduce the above copyright notice,
00017 //    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
00018 //    documentation and/or materials provided with the distribution.
00019 //  - Neither the name of the UC/LLNL nor  the names of its contributors may be
00020 //    used to  endorse or  promote products derived from  this software without
00021 //    specific prior written permission.
00022 //
00023 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
00024 // AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
00025 // IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
00026 // ARE  DISCLAIMED.  IN  NO  EVENT  SHALL  THE  REGENTS  OF  THE  UNIVERSITY OF
00027 // CALIFORNIA, THE U.S.  DEPARTMENT  OF  ENERGY OR CONTRIBUTORS BE  LIABLE  FOR
00028 // ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR CONSEQUENTIAL
00029 // DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
00030 // SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
00031 // CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
00032 // LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
00033 // OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00034 // DAMAGE.
00035 //
00036 //-----------------------------------------------------------------------------
00037 
00038 #ifndef __CXX_ExtensionTypeBase__h
00039 #define __CXX_ExtensionTypeBase__h
00040 
00041 namespace Py
00042 {
00043     // Class PythonExtension is what you inherit from to create
00044     // a new Python extension type. You give your class itself
00045     // as the template paramter.
00046 
00047     // There are two ways that extension objects can get destroyed.
00048     // 1. Their reference count goes to zero
00049     // 2. Someone does an explicit delete on a pointer.
00050     // In(1) the problem is to get the destructor called 
00051     //      We register a special deallocator in the Python type object
00052     //      (see behaviors()) to do this.
00053     // In(2) there is no problem, the dtor gets called.
00054 
00055     // PythonExtension does not use the usual Python heap allocator, 
00056     // instead using new/delete. We do the setting of the type object
00057     // and reference count, usually done by PyObject_New, in the 
00058     // base class ctor.
00059 
00060     // This special deallocator does a delete on the pointer.
00061 
00062     class PYCXX_EXPORT PythonExtensionBase : public PyObject
00063     {
00064     public:
00065         PythonExtensionBase();
00066         virtual ~PythonExtensionBase();
00067 
00068     public:
00069         // object 
00070         virtual void reinit( Tuple &args, Dict &kwds );
00071 
00072         // object basics
00073 #if defined( PYCXX_PYTHON_2TO3 ) || !defined( PY3 )
00074         virtual int print( FILE *, int );
00075 #endif
00076         virtual Object getattr( const char * );
00077         virtual int setattr( const char *, const Object & );
00078         virtual Object getattro( const String & );
00079         Object genericGetAttro( const String & );
00080         virtual int setattro( const String &, const Object & );
00081         int genericSetAttro( const String &, const Object & );
00082         virtual int compare( const Object & );
00083         virtual Object rich_compare( const Object &, int );
00084         virtual Object repr();
00085         virtual Object str();
00086         virtual long hash();
00087         virtual Object call( const Object &, const Object & );
00088         virtual Object iter();
00089         virtual PyObject *iternext();
00090 
00091         // Sequence methods
00092         virtual int sequence_length();
00093         virtual Object sequence_concat( const Object & );
00094         virtual Object sequence_repeat( Py_ssize_t );
00095         virtual Object sequence_item( Py_ssize_t );
00096         virtual Object sequence_slice( Py_ssize_t, Py_ssize_t );
00097         virtual int sequence_ass_item( Py_ssize_t, const Object & );
00098         virtual int sequence_ass_slice( Py_ssize_t, Py_ssize_t, const Object & );
00099 
00100         // Mapping
00101         virtual int mapping_length();
00102         virtual Object mapping_subscript( const Object & );
00103         virtual int mapping_ass_subscript( const Object &, const Object & );
00104 
00105         // Number
00106         virtual int number_nonzero();
00107         virtual Object number_negative();
00108         virtual Object number_positive();
00109         virtual Object number_absolute();
00110         virtual Object number_invert();
00111         virtual Object number_int();
00112         virtual Object number_float();
00113         virtual Object number_long();
00114         virtual Object number_oct();
00115         virtual Object number_hex();
00116         virtual Object number_add( const Object & );
00117         virtual Object number_subtract( const Object & );
00118         virtual Object number_multiply( const Object & );
00119         virtual Object number_divide( const Object & );
00120         virtual Object number_remainder( const Object & );
00121         virtual Object number_divmod( const Object & );
00122         virtual Object number_lshift( const Object & );
00123         virtual Object number_rshift( const Object & );
00124         virtual Object number_and( const Object & );
00125         virtual Object number_xor( const Object & );
00126         virtual Object number_or( const Object & );
00127         virtual Object number_power( const Object &, const Object & );
00128 
00129         // Buffer
00130         virtual Py_ssize_t buffer_getreadbuffer( Py_ssize_t, void** );
00131         virtual Py_ssize_t buffer_getwritebuffer( Py_ssize_t, void** );
00132         virtual Py_ssize_t buffer_getsegcount( Py_ssize_t* );
00133 
00134     public:
00135         // helper functions to call function fn_name with 0 to 9 args
00136         Object callOnSelf( const std::string &fn_name );
00137         Object callOnSelf( const std::string &fn_name,
00138                                 const Object &arg1 );
00139         Object callOnSelf( const std::string &fn_name,
00140                                 const Object &arg1, const Object &arg2 );
00141         Object callOnSelf( const std::string &fn_name,
00142                                 const Object &arg1, const Object &arg2, const Object &arg3 );
00143         Object callOnSelf( const std::string &fn_name,
00144                                 const Object &arg1, const Object &arg2, const Object &arg3,
00145                                 const Object &arg4 );
00146         Object callOnSelf( const std::string &fn_name,
00147                                 const Object &arg1, const Object &arg2, const Object &arg3,
00148                                 const Object &arg4, const Object &arg5 );
00149         Object callOnSelf( const std::string &fn_name,
00150                                 const Object &arg1, const Object &arg2, const Object &arg3,
00151                                 const Object &arg4, const Object &arg5, const Object &arg6 );
00152         Object callOnSelf( const std::string &fn_name,
00153                                 const Object &arg1, const Object &arg2, const Object &arg3,
00154                                 const Object &arg4, const Object &arg5, const Object &arg6,
00155                                 const Object &arg7 );
00156         Object callOnSelf( const std::string &fn_name,
00157                                 const Object &arg1, const Object &arg2, const Object &arg3,
00158                                 const Object &arg4, const Object &arg5, const Object &arg6,
00159                                 const Object &arg7, const Object &arg8 );
00160         Object callOnSelf( const std::string &fn_name,
00161                                 const Object &arg1, const Object &arg2, const Object &arg3,
00162                                 const Object &arg4, const Object &arg5, const Object &arg6,
00163                                 const Object &arg7, const Object &arg8, const Object &arg9 );
00164 
00165     public:
00166         virtual PyObject *selfPtr() = 0;
00167         virtual Object self() = 0;
00168 
00169     private:
00170         void missing_method( void );
00171         static PyObject *method_call_handler( PyObject *self, PyObject *args );
00172     };
00173 
00174 } // Namespace Py
00175 
00176 // End of __CXX_ExtensionTypeBase__h
00177 #endif

Generated on Wed Nov 23 19:00:10 2011 for FreeCAD by  doxygen 1.6.1