From 66cc1b3476c5eaa363603fc701b0e62efc350cc9 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 7 May 2008 11:23:56 +0100 Subject: [PATCH] From Mathieu Lacage: a couple more examples (nested C++ namespaces, nested classes) --- examples/g/g.cc | 19 +++++++++++++++++++ examples/g/g.h | 15 +++++++++++++++ examples/g/g_test.py | 12 ++++++++++++ examples/g/modulegen.py | 23 +++++++++++++++++++++++ examples/g/wscript | 30 ++++++++++++++++++++++++++++++ examples/h/h.cc | 19 +++++++++++++++++++ examples/h/h.h | 17 +++++++++++++++++ examples/h/h_test.py | 11 +++++++++++ examples/h/modulegen.py | 32 ++++++++++++++++++++++++++++++++ examples/h/wscript | 30 ++++++++++++++++++++++++++++++ examples/wscript | 2 +- 11 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 examples/g/g.cc create mode 100644 examples/g/g.h create mode 100644 examples/g/g_test.py create mode 100644 examples/g/modulegen.py create mode 100644 examples/g/wscript create mode 100644 examples/h/h.cc create mode 100644 examples/h/h.h create mode 100644 examples/h/h_test.py create mode 100644 examples/h/modulegen.py create mode 100644 examples/h/wscript diff --git a/examples/g/g.cc b/examples/g/g.cc new file mode 100644 index 0000000..b182d41 --- /dev/null +++ b/examples/g/g.cc @@ -0,0 +1,19 @@ +#include "g.h" + +#include + +void GDoA (void) +{ + std::cout << "GDoA" << std::endl; +} + +void G::GDoB (void) +{ + std::cout << "G::GDoB" << std::endl; +} + +void G::GInner::GDoC (void) +{ + std::cout << "G::Inner::GDoC" << std::endl; +} + diff --git a/examples/g/g.h b/examples/g/g.h new file mode 100644 index 0000000..051645a --- /dev/null +++ b/examples/g/g.h @@ -0,0 +1,15 @@ + +void GDoA (void); + +namespace G +{ + + void GDoB (void); + + namespace GInner { + + void GDoC (void); + + } + +} diff --git a/examples/g/g_test.py b/examples/g/g_test.py new file mode 100644 index 0000000..b326868 --- /dev/null +++ b/examples/g/g_test.py @@ -0,0 +1,12 @@ +import sys +sys.path.insert(0, "../../build/default/examples/g") +from g import * +from g.G import GDoB +from g.G.GInner import GDoC + +GDoA() +G.GDoB() +GDoB() +G.GInner.GDoC() +GDoC() + diff --git a/examples/g/modulegen.py b/examples/g/modulegen.py new file mode 100644 index 0000000..411d93e --- /dev/null +++ b/examples/g/modulegen.py @@ -0,0 +1,23 @@ +#! /usr/bin/env python + +import sys + +import pybindgen +from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) + +def my_module_gen(out_file): + pybindgen.write_preamble(FileCodeSink(out_file)) + + mod = Module('g') + mod.add_include('"g.h"') + + mod.add_function(Function(ReturnValue.new('void'), 'GDoA', [])) + G = mod.add_cpp_namespace("G") + G.add_function(Function(ReturnValue.new('void'), 'GDoB', [])) + GInner = G.add_cpp_namespace("GInner") + GInner.add_function(Function(ReturnValue.new('void'), 'GDoC', [])) + + mod.generate(FileCodeSink(out_file)) + +if __name__ == '__main__': + my_module_gen(sys.stdout) diff --git a/examples/g/wscript b/examples/g/wscript new file mode 100644 index 0000000..9b63c12 --- /dev/null +++ b/examples/g/wscript @@ -0,0 +1,30 @@ +## -*- python -*- +import Params + +def create_pyext_obj(bld, c_or_cpp_type): + waf_version = [int (s) for s in Params.g_version.split('.')] + if waf_version >= [1,4]: + return bld.create_obj(c_or_cpp_type, 'shlib', 'pyext') + else: + return bld.create_obj(c_or_cpp_type, 'plugin', 'pyext') + + +def build(bld): + ## manual code generation using simple pybindgen API calls + bindgen = bld.create_obj('command-output') + bindgen.stdout = 'gmodule.cc' + bindgen.argv = [bindgen.input_file('modulegen.py')] + bindgen.command = bindgen.env['PYTHON'] + bindgen.command_is_external = True + bindgen.prio = 55 # must run after the 'py' objects in ../pybindgen + + obj = create_pyext_obj(bld, 'cpp') + obj.source = [ + 'g.cc', + 'gmodule.cc' + ] + obj.target = 'g' + obj.inst_var = 0 # do not install + + ## Workaround a WAF bug (http://code.google.com/p/waf/issues/detail?id=37) + obj.env.append_value("CPPPATH", bld.m_curdirnode.srcpath(obj.env)) diff --git a/examples/h/h.cc b/examples/h/h.cc new file mode 100644 index 0000000..93c696d --- /dev/null +++ b/examples/h/h.cc @@ -0,0 +1,19 @@ +#include "h.h" + +#include + +void H::Do (void) +{ + std::cout << "H::Do" << std::endl; +} + +void H::Inner::Do (void) +{ + std::cout << "H::Inner::Do" << std::endl; +} + +void H::Inner::MostInner::Do (void) +{ + std::cout << "H::Inner::MostInner::Do" << std::endl; +} + diff --git a/examples/h/h.h b/examples/h/h.h new file mode 100644 index 0000000..660a3ff --- /dev/null +++ b/examples/h/h.h @@ -0,0 +1,17 @@ + +class H +{ + public: + void Do (void); + class Inner + { + public: + void Do (void); + + class MostInner + { + public: + void Do (void); + }; + }; +}; diff --git a/examples/h/h_test.py b/examples/h/h_test.py new file mode 100644 index 0000000..01f4346 --- /dev/null +++ b/examples/h/h_test.py @@ -0,0 +1,11 @@ +import sys +sys.path.insert(0, "../../build/default/examples/h") +from h import * + +h = H() +h.Do() +inner = H.Inner() +inner.Do() +most_inner = H.Inner.MostInner() +most_inner.Do() + diff --git a/examples/h/modulegen.py b/examples/h/modulegen.py new file mode 100644 index 0000000..53bc7a4 --- /dev/null +++ b/examples/h/modulegen.py @@ -0,0 +1,32 @@ +#! /usr/bin/env python + +import sys + +import pybindgen +from pybindgen import (ReturnValue, Parameter, Module, CppClass, CppMethod, CppConstructor, FileCodeSink) + +def my_module_gen(out_file): + pybindgen.write_preamble(FileCodeSink(out_file)) + + mod = Module('h') + mod.add_include('"h.h"') + + H = CppClass('H') + mod.add_class(H) + H.add_constructor(CppConstructor([])) + H.add_method(CppMethod(ReturnValue.new('void'), 'Do', [])) + + Inner = CppClass('Inner', outer_class=H) + mod.add_class(Inner) + Inner.add_constructor(CppConstructor([])) + Inner.add_method(CppMethod (ReturnValue.new('void'), 'Do', [])) + + MostInner = CppClass('MostInner', outer_class=Inner) + mod.add_class (MostInner) + MostInner.add_constructor (CppConstructor([])) + MostInner.add_method (CppMethod (ReturnValue.new('void'), 'Do', [])) + + mod.generate(FileCodeSink(out_file)) + +if __name__ == '__main__': + my_module_gen(sys.stdout) diff --git a/examples/h/wscript b/examples/h/wscript new file mode 100644 index 0000000..37ed8f5 --- /dev/null +++ b/examples/h/wscript @@ -0,0 +1,30 @@ +## -*- python -*- +import Params + +def create_pyext_obj(bld, c_or_cpp_type): + waf_version = [int (s) for s in Params.g_version.split('.')] + if waf_version >= [1,4]: + return bld.create_obj(c_or_cpp_type, 'shlib', 'pyext') + else: + return bld.create_obj(c_or_cpp_type, 'plugin', 'pyext') + + +def build(bld): + ## manual code generation using simple pybindgen API calls + bindgen = bld.create_obj('command-output') + bindgen.stdout = 'hmodule.cc' + bindgen.argv = [bindgen.input_file('modulegen.py')] + bindgen.command = bindgen.env['PYTHON'] + bindgen.command_is_external = True + bindgen.prio = 55 # must run after the 'py' objects in ../pybindgen + + obj = create_pyext_obj(bld, 'cpp') + obj.source = [ + 'h.cc', + 'hmodule.cc' + ] + obj.target = 'h' + obj.inst_var = 0 # do not install + + ## Workaround a WAF bug (http://code.google.com/p/waf/issues/detail?id=37) + obj.env.append_value("CPPPATH", bld.m_curdirnode.srcpath(obj.env)) diff --git a/examples/wscript b/examples/wscript index 5ba77cb..c1101e0 100644 --- a/examples/wscript +++ b/examples/wscript @@ -1,5 +1,5 @@ ## -*- python -*- def build(bld): - bld.add_subdirs('a b c d e f') + bld.add_subdirs('a b c d e f g h')