Skip to content
Snippets Groups Projects
Commit 394cc15a authored by Andreas Dedner's avatar Andreas Dedner
Browse files

added a function to import a c++ class similar to the 'algorithm'.

This makes it possible to import "statefull" C++ objects to Python.
At the moment only the constructor is available on the Python side
so the object can only be used as argument for another C++ function/method.
Exporting methods will be made available as well in a future version.
parent 51d0d290
Branches
Tags
1 merge request!790Feature/add python bindings
add_python_targets(generator
__init__
algorithm
importclass
builder
exceptions
generator
......
......@@ -51,7 +51,7 @@ class Constructor(object):
def register(self, cls="cls"):
if self.body is None:
return cls + ".def( pybind11::init( " + args + " )" + "".join(", " + e for e in self.extra) + " );\n"
return cls + ".def( pybind11::init( " + self.args + " )" + "".join(", " + e for e in self.extra) + " );\n"
if self.args:
source = cls + ".def( pybind11::init( [] ( " + ", ".join(self.args) + " ) {"
else:
......
......@@ -11,6 +11,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import logging
from . import builder
from dune.common.hashit import hashIt
logger = logging.getLogger(__name__)
......@@ -146,7 +147,6 @@ class SimpleGenerator(object):
baseClasses=bc)
return self.post(moduleName, source)
from dune.common.hashit import hashIt
def simpleGenerator(inc, baseType, namespace, pythonname=None, filename=None):
generator = SimpleGenerator(baseType, namespace, pythonname, filename)
def load(includes, typeName, *args):
......
from __future__ import absolute_import, division, print_function, unicode_literals
import os
from dune.common.hashit import hashIt
from . import builder
from dune.common.compatibility import isString
from dune.generator.algorithm import cppType
def load(className, includeFiles, *args):
source = '#include <config.h>\n\n'
source += '#define USING_DUNE_PYTHON 1\n\n'
includes = []
if isString(includeFiles):
if not os.path.dirname(includeFiles):
with open(includeFiles, "r") as include:
source += include.read()
source += "\n"
else:
source += "#include <"+includeFiles+">\n"
includes += [includeFiles]
elif isinstance(includeFiles, list):
for includefile in includeFiles:
if not os.path.dirname(includefile):
with open(includefile, "r") as include:
source += include.read()
source += "\n"
else:
source += "#include <"+includefile+">\n"
includes += [includefile]
argTypes = []
for arg in args:
t,i = cppType(arg)
argTypes.append(t)
includes += i
signature = className + "( " + ", ".join(argTypes) + " )"
moduleName = "class_" + hashIt(signature) + "_" + hashIt(source)
includes = sorted(set(includes))
source += "".join(["#include <" + i + ">\n" for i in includes])
source += "\n"
source += '#include <dune/python/common/typeregistry.hh>\n'
source += '#include <dune/python/pybind11/pybind11.h>\n'
source += '\n'
source += "PYBIND11_MODULE( " + moduleName + ", module )\n"
source += "{\n"
source += " auto cls = Dune::Python::insertClass< "+className+\
" >( module, \"cls\","+\
"Dune::Python::GenerateTypeName(\""+className+"\"),"+\
"Dune::Python::IncludeFiles{"+",".join(["\""+f+"\"" for f in includes])+"}).first;\n"
ctorArgs = ", ".join([argTypes[i] + " arg" + str(i) for i in range(len(argTypes))])
source += "cls.def( pybind11::init( [] ( "+ctorArgs+" ) {\n"
source += "return new "+className+"( "+",".join(["arg"+str(i) for i in range(len(argTypes))]) +"); \n"
source += " }));\n"
source += "}"
return builder.load(moduleName, source, signature).cls(*args)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment