AppSandbox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer@users.sourceforge.net>        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <Python.h>
00027 #endif
00028 
00029 #include <Base/Console.h>
00030 #include <App/DocumentPy.h>
00031 #include <App/DocumentObjectPy.h>
00032 #include <CXX/Extensions.hxx>
00033 #include <CXX/Objects.hxx>
00034 
00035 #include "DocumentThread.h"
00036 #include "DocumentProtector.h"
00037 #include "DocumentProtectorPy.h"
00038 
00039 namespace Sandbox {
00040 class PythonBaseClass : public Py::PythonClass< PythonBaseClass >
00041 {
00042 public:
00043     PythonBaseClass( Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds )
00044     : Py::PythonClass< PythonBaseClass >::PythonClass( self, args, kwds )
00045     , m_value( "default value" )
00046     {
00047         std::cout << "PythonBaseClass c'tor Called with " << args.length() << " normal arguments." << std::endl;
00048         Py::List names( kwds.keys() );
00049         std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
00050         for( Py::List::size_type i=0; i< names.length(); i++ )
00051         {
00052             Py::String name( names[i] );
00053             std::cout << "    " << name << std::endl;
00054         }
00055     }
00056 
00057     virtual ~PythonBaseClass()
00058     {
00059         std::cout << "~PythonBaseClass." << std::endl;
00060     }
00061 
00062     static void init_type(void)
00063     {
00064         behaviors().name( "PythonBaseClass" );
00065         behaviors().doc( "documentation for PythonBaseClass class" );
00066         behaviors().supportGetattro();
00067         behaviors().supportSetattro();
00068 
00069         PYCXX_ADD_NOARGS_METHOD( func_noargs, PythonBaseClass_func_noargs, "docs for PythonBaseClass_func_noargs" );
00070         PYCXX_ADD_VARARGS_METHOD( func_varargs, PythonBaseClass_func_varargs, "docs for PythonBaseClass_func_varargs" );
00071         PYCXX_ADD_KEYWORDS_METHOD( func_keyword, PythonBaseClass_func_keyword, "docs for PythonBaseClass_func_keyword" );
00072 
00073         PYCXX_ADD_NOARGS_METHOD( func_noargs_raise_exception, PythonBaseClass_func_noargs_raise_exception,
00074           "docs for PythonBaseClass_func_noargs_raise_exception" );
00075 
00076         // Call to make the type ready for use
00077         behaviors().readyType();
00078     }
00079 
00080     Py::Object PythonBaseClass_func_noargs( void )
00081     {
00082         std::cout << "PythonBaseClass_func_noargs Called." << std::endl;
00083         std::cout << "value ref count " << m_value.reference_count() << std::endl;
00084         return Py::None();
00085     }
00086     PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs )
00087 
00088     Py::Object PythonBaseClass_func_varargs( const Py::Tuple &args )
00089     {
00090         std::cout << "PythonBaseClass_func_varargs Called with " << args.length() << " normal arguments." << std::endl;
00091         return Py::None();
00092     }
00093     PYCXX_VARARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_varargs )
00094 
00095     Py::Object PythonBaseClass_func_keyword( const Py::Tuple &args, const Py::Dict &kwds )
00096     {
00097         std::cout << "PythonBaseClass_func_keyword Called with " << args.length() << " normal arguments." << std::endl;
00098         Py::List names( kwds.keys() );
00099         std::cout << "and with " << names.length() << " keyword arguments:" << std::endl;
00100         for( Py::List::size_type i=0; i< names.length(); i++ )
00101         {
00102             Py::String name( names[i] );
00103             std::cout << "    " << name << std::endl;
00104         }
00105         return Py::None();
00106     }
00107     PYCXX_KEYWORDS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_keyword )
00108 
00109     Py::Object PythonBaseClass_func_noargs_raise_exception( void )
00110     {
00111         std::cout << "PythonBaseClass_func_noargs_raise_exception Called." << std::endl;
00112         throw Py::RuntimeError( "its an error" );
00113         return Py::None();
00114     }
00115     PYCXX_NOARGS_METHOD_DECL( PythonBaseClass, PythonBaseClass_func_noargs_raise_exception )
00116 
00117     Py::Object getattro( const Py::String &name_ )
00118     {
00119         std::string name( name_.as_std_string( "utf-8" ) );
00120 
00121         if( name == "value" )
00122         {
00123             return m_value;
00124         }
00125         else
00126         {
00127             return genericGetAttro( name_ );
00128         }
00129     }
00130 
00131     int setattro( const Py::String &name_, const Py::Object &value )
00132     {
00133         std::string name( name_.as_std_string( "utf-8" ) );
00134 
00135         if( name == "value" )
00136         {
00137             m_value = value;
00138             return 0;
00139         }
00140         else
00141         {
00142             return genericSetAttro( name_, value );
00143         }
00144     }
00145 
00146     Py::String m_value;
00147 };
00148 
00149 } // namespace Sandbox
00150 
00151 /* module functions */
00152 
00153 class SandboxModule : public Py::ExtensionModule<SandboxModule>
00154 {
00155 
00156 public:
00157     SandboxModule() : Py::ExtensionModule<SandboxModule>("Sandbox")
00158     {
00159         Sandbox::PythonBaseClass::init_type();
00160         Sandbox::DocumentProtectorPy::init_type();
00161         add_varargs_method("DocumentProtector",
00162             &SandboxModule::new_DocumentProtector,
00163             "DocumentProtector(Document)");
00164         Sandbox::DocumentObjectProtectorPy::init_type();
00165         add_varargs_method("DocumentObjectProtector",
00166             &SandboxModule::new_DocumentObjectProtector,
00167             "DocumentObjectProtector(DocumentObject)");
00168         initialize("This module is the Sandbox module"); // register with Python
00169         
00170         Py::Dict d( moduleDictionary() );
00171         Py::Object x( Sandbox::PythonBaseClass::type() );
00172         d["PythonBaseClass"] = x;
00173     }
00174     
00175     virtual ~SandboxModule() {}
00176 
00177 private:
00178     Py::Object new_DocumentProtector(const Py::Tuple& args)
00179     {
00180         PyObject* o;
00181         if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentPy::Type), &o))
00182             throw Py::Exception();
00183         App::DocumentPy* doc = static_cast<App::DocumentPy*>(o);
00184         return Py::asObject(new Sandbox::DocumentProtectorPy(doc));
00185     }
00186     Py::Object new_DocumentObjectProtector(const Py::Tuple& args)
00187     {
00188         PyObject* o;
00189         if (!PyArg_ParseTuple(args.ptr(), "O!",&(App::DocumentObjectPy::Type), &o))
00190             throw Py::Exception();
00191         App::DocumentObjectPy* obj = static_cast<App::DocumentObjectPy*>(o);
00192         return Py::asObject(new Sandbox::DocumentObjectProtectorPy(obj));
00193     }
00194 };
00195 
00196 
00197 /* Python entry */
00198 extern "C" {
00199 void SandboxAppExport initSandbox() {
00200 
00201     Sandbox::DocumentProtector  ::init();
00202     Sandbox::SandboxObject      ::init();
00203 
00204     // the following constructor call registers our extension module
00205     // with the Python runtime system
00206     (void)new SandboxModule;
00207     Base::Console().Log("Loading Sandbox module... done\n");
00208 }
00209 
00210 } // extern "C"

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1