Add a boost::shared_ptr example

This commit is contained in:
Gustavo J. A. M. Carneiro
2011-02-23 15:16:27 +00:00
parent 88203d7ff9
commit 2cbd986fbc
7 changed files with 115 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
#include "c.h"
TestClass::Ptr someFct()
{
TestClass::Ptr p(new TestClass);
return p;
}
+13
View File
@@ -0,0 +1,13 @@
// -*- C++ -*-
#include <boost/smart_ptr/shared_ptr.hpp>
class TestClass {
public:
typedef boost::shared_ptr< TestClass > Ptr;
public:
};
TestClass::Ptr someFct();
+9
View File
@@ -0,0 +1,9 @@
import sys
sys.path.insert(0, "../../build/default/examples/callback")
import c
def visitor(value):
print value
c.visit(visitor)
+53
View File
@@ -0,0 +1,53 @@
#! /usr/bin/env python
import sys
import pybindgen
from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
from pybindgen import CppMethod, CppConstructor, CppClass, Enum
from pybindgen.typehandlers.base import ForwardWrapperBase
from pybindgen.typehandlers import base as typehandlers
class SharedPtrTransformation(typehandlers.TypeTransformation):
def get_untransformed_name(self, name):
if name == 'TestClass::Ptr':
return 'TestClass *'
return name
def create_type_handler(self, type_handler, *args, **kwargs):
if issubclass(type_handler, Parameter):
kwargs['transfer_ownership'] = False
elif issubclass(type_handler, ReturnValue):
kwargs['caller_owns_return'] = False
else:
raise AssertionError
handler = type_handler(*args, **kwargs)
handler.set_transformation(self, self.get_untransformed_name(args[0]))
return handler
def untransform(self, type_handler, declarations, code_block, expression):
return '(%s).get()' % (expression,)
def transform(self, type_handler, declarations, code_block, expression):
assert type_handler.untransformed_ctype[-1] == '*' # make sure it's a pointer
return 'TestClass::Ptr (expression)'
transf = SharedPtrTransformation()
typehandlers.return_type_matcher.register_transformation(transf)
typehandlers.param_type_matcher.register_transformation(transf)
del transf
def my_module_gen(out_file):
mod = Module('c')
mod.add_include('"c.h"')
mod.add_class('TestClass')
mod.add_function("someFct", 'TestClass::Ptr', [])
mod.generate(FileCodeSink(out_file))
if __name__ == '__main__':
my_module_gen(sys.stdout)
+22
View File
@@ -0,0 +1,22 @@
## -*- python -*-
def build(bld):
gen = bld.new_task_gen(
features='command',
source='modulegen.py',
target='cmodule.cc',
command='${PYTHON} ${SRC[0]} > ${TGT[0]}')
if bld.env['CXX']:
obj = bld.new_task_gen('cxx', 'shlib', 'pyext')
obj.source = [
'c.cc',
'cmodule.cc'
]
obj.target = 'c'
obj.install_path = None # do not install
obj.includes = '.'
obj.uselib = 'BOOST'
+7
View File
@@ -1,6 +1,13 @@
## -*- python -*-
def set_options(opt):
opt.tool_options('boost')
def build(bld):
bld.add_subdirs('a b c d e f g h')
bld.add_subdirs('callback')
if bld.env['HAVE_BOOST']:
bld.add_subdirs('shared_ptr')
def configure(conf):
conf.env['HAVE_BOOST'] = conf.check_boost()
+3
View File
@@ -202,6 +202,8 @@ def set_options(opt):
opt.tool_options('compiler_cc')
opt.tool_options('compiler_cxx')
opt.tool_options('cflags')
opt.sub_options('examples')
optgrp = opt.add_option_group("PyBindGen Options")
@@ -291,6 +293,7 @@ def configure(conf):
conf.env.append_value('CPPPATH', os.path.join(conf.curdir, 'include'))
conf.sub_config('benchmarks')
conf.sub_config('examples')
def build(bld):