ModuleScope

This commit is contained in:
Christoph Wintersteiger 2014-10-08 15:48:48 +00:00
parent 4bef2d7617
commit 17fe06468d
3 changed files with 6 additions and 94 deletions

View File

@ -106,19 +106,7 @@ namespace netgen
void ExportCSG()
{
std::string nested_name = "csg";
if( bp::scope() )
nested_name = bp::extract<std::string>(bp::scope().attr("__name__") + ".csg");
bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule(nested_name.c_str()))));
cout << "exporting csg " << nested_name << endl;
bp::object parent = bp::scope() ? bp::scope() : bp::import("__main__");
parent.attr("csg") = module ;
bp::scope local_scope(module);
ModuleScope module("csg");
bp::class_<Point<2>> ("Point2d", bp::init<double,double>())
.def(bp::self+Vec<2>())

View File

@ -1,6 +1,8 @@
#ifdef NG_PYTHON
#include <boost/python.hpp>
#include <../general/ngpython.hpp>
#include <meshing.hpp>
#include <geometry2d.hpp>
@ -8,49 +10,9 @@ using namespace netgen;
namespace bp = boost::python;
//////////////////////////////////////////////////////////////////////
// Lambda to function pointer conversion
template <typename Function>
struct function_traits
: public function_traits<decltype(&Function::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
typedef ReturnType (*pointer)(Args...);
typedef ReturnType return_type;
};
template <typename Function>
typename function_traits<Function>::pointer
FunctionPointer (const Function& lambda) {
return static_cast<typename function_traits<Function>::pointer>(lambda);
}
template <class T>
inline string ToString (const T& t)
{
stringstream ss;
ss << t;
return ss.str();
}
void ExportGeom2d()
{
std::string nested_name = "geom2d";
if( bp::scope() )
nested_name = bp::extract<std::string>(bp::scope().attr("__name__") + ".geom2d");
bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule(nested_name.c_str()))));
cout << "exporting geom2d " << nested_name << endl;
bp::object parent = bp::scope() ? bp::scope() : bp::import("__main__");
parent.attr("geom2d") = module ;
bp::scope local_scope(module);
ModuleScope module("geom2d");
bp::class_<SplineGeometry2d, boost::noncopyable>("SplineGeometry")
.def("Load",&SplineGeometry2d::Load)

View File

@ -2,6 +2,7 @@
#include <boost/python.hpp>
#include <boost/python/slice.hpp>
#include <../general/ngpython.hpp>
#include <mystdlib.h>
#include "meshing.hpp"
@ -11,35 +12,6 @@ using namespace netgen;
namespace bp = boost::python;
//////////////////////////////////////////////////////////////////////
// Lambda to function pointer conversion
template <typename Function>
struct function_traits
: public function_traits<decltype(&Function::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
typedef ReturnType (*pointer)(Args...);
typedef ReturnType return_type;
};
template <typename Function>
typename function_traits<Function>::pointer
FunctionPointer (const Function& lambda) {
return static_cast<typename function_traits<Function>::pointer>(lambda);
}
template <class T>
inline string ToString (const T& t)
{
stringstream ss;
ss << t;
return ss.str();
}
template <typename T, int BASE = 0, typename TIND = int>
void ExportArray ()
{
@ -67,17 +39,7 @@ void ExportArray ()
void ExportNetgenMeshing()
{
std::string nested_name = "meshing";
if( bp::scope() )
nested_name = bp::extract<std::string>(bp::scope().attr("__name__") + ".meshing");
bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule(nested_name.c_str()))));
cout << "exporting meshing " << nested_name << endl;
bp::object parent = bp::scope() ? bp::scope() : bp::import("__main__");
parent.attr("meshing") = module ;
bp::scope local_scope(module);
ModuleScope module("meshing");
bp::class_<PointIndex>("PointId", bp::init<int>())
.def("__repr__", &ToString<PointIndex>)