Python2/Extensions.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_Extensions__h
00039 #define __CXX_Extensions__h
00040 
00041 
00042 #ifdef _MSC_VER
00043 // disable warning C4786: symbol greater than 255 character,
00044 // okay to ignore
00045 #pragma warning( disable: 4786 )
00046 #endif
00047 
00048 #include "CXX/WrapPython.h"
00049 #include "CXX/Version.hxx"
00050 #include "CXX/Python2/Config.hxx"
00051 #include "CXX/Python2/CxxDebug.hxx"
00052 #include "CXX/Python2/Objects.hxx"
00053 
00054 extern "C" { extern PyObject py_object_initializer; }
00055 
00056 #include <vector>
00057 #include <map>
00058 
00059 // ----------------------------------------------------------------------
00060 
00061 namespace Py
00062 {
00063     class ExtensionModuleBase;
00064 
00065     // Make an Exception Type for use in raising custom exceptions
00066     class PYCXX_EXPORT ExtensionExceptionType : public Object
00067     {
00068     public:
00069         ExtensionExceptionType();
00070         virtual ~ExtensionExceptionType();
00071 
00072         // call init to create the type
00073         void init( ExtensionModuleBase &module, const std::string &name, ExtensionExceptionType &parent );
00074         void init( ExtensionModuleBase &module, const std::string &name );
00075     };
00076 
00077     class PYCXX_EXPORT MethodTable
00078     {
00079     public:
00080         MethodTable();
00081         virtual ~MethodTable();
00082 
00083         void add( const char *method_name, PyCFunction f, const char *doc="", int flag=1 );
00084         PyMethodDef *table();
00085 
00086     protected:
00087         std::vector<PyMethodDef> t;    // accumulator of PyMethodDef's
00088         PyMethodDef *mt;        // Actual method table produced when full
00089 
00090         static PyMethodDef method( const char* method_name, PyCFunction f, int flags=1, const char* doc="" );
00091 
00092     private:
00093         //
00094         // prevent the compiler generating these unwanted functions
00095         //
00096         MethodTable( const MethodTable &m );    //unimplemented
00097         void operator=( const MethodTable &m );    //unimplemented
00098 
00099     }; // end class MethodTable
00100 
00101     // Note: Python calls noargs as varargs buts args==NULL
00102     extern "C" typedef PyObject *(*method_noargs_call_handler_t)( PyObject *_self, PyObject * );
00103     extern "C" typedef PyObject *(*method_varargs_call_handler_t)( PyObject *_self, PyObject *_args );
00104     extern "C" typedef PyObject *(*method_keyword_call_handler_t)( PyObject *_self, PyObject *_args, PyObject *_dict );
00105 
00106     template<class T>
00107     class MethodDefExt : public PyMethodDef
00108     {
00109     public:
00110         typedef Object (T::*method_noargs_function_t)();
00111         typedef Object (T::*method_varargs_function_t)( const Tuple &args );
00112         typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws );
00113 
00114         // NOARGS
00115         MethodDefExt
00116         (
00117             const char *_name,
00118             method_noargs_function_t _function,
00119             method_noargs_call_handler_t _handler,
00120             const char *_doc
00121         )
00122         {
00123             ext_meth_def.ml_name = const_cast<char *>( _name );
00124             ext_meth_def.ml_meth = reinterpret_cast<method_varargs_call_handler_t>( _handler );
00125             ext_meth_def.ml_flags = METH_NOARGS;
00126             ext_meth_def.ml_doc = const_cast<char *>( _doc );
00127 
00128             ext_noargs_function = _function;
00129             ext_varargs_function = NULL;
00130             ext_keyword_function = NULL;
00131         }
00132 
00133         // VARARGS
00134         MethodDefExt
00135         (
00136             const char *_name,
00137             method_varargs_function_t _function,
00138             method_varargs_call_handler_t _handler,
00139             const char *_doc
00140         )
00141         {
00142             ext_meth_def.ml_name = const_cast<char *>( _name );
00143             ext_meth_def.ml_meth = reinterpret_cast<method_varargs_call_handler_t>( _handler );
00144             ext_meth_def.ml_flags = METH_VARARGS;
00145             ext_meth_def.ml_doc = const_cast<char *>( _doc );
00146 
00147             ext_noargs_function = NULL;
00148             ext_varargs_function = _function;
00149             ext_keyword_function = NULL;
00150         }
00151 
00152         // VARARGS + KEYWORD
00153         MethodDefExt
00154         (
00155             const char *_name,
00156             method_keyword_function_t _function,
00157             method_keyword_call_handler_t _handler,
00158             const char *_doc
00159         )
00160         {
00161             ext_meth_def.ml_name = const_cast<char *>( _name );
00162             ext_meth_def.ml_meth = reinterpret_cast<method_varargs_call_handler_t>( _handler );
00163             ext_meth_def.ml_flags = METH_VARARGS|METH_KEYWORDS;
00164             ext_meth_def.ml_doc = const_cast<char *>( _doc );
00165 
00166             ext_noargs_function = NULL;
00167             ext_varargs_function = NULL;
00168             ext_keyword_function = _function;
00169         }
00170 
00171         ~MethodDefExt()
00172         {}
00173 
00174         PyMethodDef ext_meth_def;
00175         method_noargs_function_t ext_noargs_function;
00176         method_varargs_function_t ext_varargs_function;
00177         method_keyword_function_t ext_keyword_function;
00178         Object py_method;
00179     };
00180 } // Namespace Py
00181 
00182 #include "CXX/Python2/ExtensionModule.hxx"
00183 #include "CXX/Python2/PythonType.hxx"
00184 #include "CXX/Python2/ExtensionTypeBase.hxx"
00185 #include "CXX/Python2/ExtensionOldType.hxx"
00186 #include "CXX/Python2/ExtensionType.hxx"
00187 
00188 // End of CXX_Extensions.h
00189 #endif

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