From 37dcfcb514abae199cb26f29c698eabbbeefa24e Mon Sep 17 00:00:00 2001 From: Mike Owen Date: Mon, 10 Dec 2012 23:26:25 -0800 Subject: [PATCH] Adding the beginnings of an example for cross module inheritance. Not building yet 'cause I don't understand waf. Why does every project I look at use a different build system! --- examples/cross_module_inheritance/Agen.py | 18 +++++++ examples/cross_module_inheritance/Bgen.py | 20 ++++++++ .../ModuleGenerationFactory.py | 48 +++++++++++++++++++ examples/cross_module_inheritance/classes.hh | 20 ++++++++ examples/cross_module_inheritance/wscript | 19 ++++++++ examples/wscript | 1 + 6 files changed, 126 insertions(+) create mode 100644 examples/cross_module_inheritance/Agen.py create mode 100644 examples/cross_module_inheritance/Bgen.py create mode 100644 examples/cross_module_inheritance/ModuleGenerationFactory.py create mode 100644 examples/cross_module_inheritance/classes.hh create mode 100644 examples/cross_module_inheritance/wscript diff --git a/examples/cross_module_inheritance/Agen.py b/examples/cross_module_inheritance/Agen.py new file mode 100644 index 0000000..1fcad42 --- /dev/null +++ b/examples/cross_module_inheritance/Agen.py @@ -0,0 +1,18 @@ +from pybindgen import * +from ModuleGenerationFactory import * + +# Module +mod = Module("A") +mod.add_include('"classes.hh"') + +# Base +mod.begin_section("A_BaseBindings") +Base = mod.add_class("Base", allow_subclassing = True) +Base.add_constructor([]) +Base.add_method("do_something", None, [], is_virtual=True) +mod.end_section("A_BaseBindings") + +# Generate the code. +out = ModuleGenerationFactory("A.C", ["BaseBindings"]) +mod.generate(out, "A") +out.close() diff --git a/examples/cross_module_inheritance/Bgen.py b/examples/cross_module_inheritance/Bgen.py new file mode 100644 index 0000000..aeb9c1a --- /dev/null +++ b/examples/cross_module_inheritance/Bgen.py @@ -0,0 +1,20 @@ +from pybindgen import * +from ModuleGenerationFactory import * +import Agen + +# Module +mod = Module("B") +mod.add_include('"classes.hh"') +mod.add_include('"A.hh"') + +# Derived +mod.begin_section("B_DerivedBindings") +Derived = mod.add_class("Derived", allow_subclassing = True, parent = Agen.Base) +Derived.add_constructor([]) +Derived.add_method("do_something", None, [], is_virtual=True) +mod.end_section("B_DerivedBindings") + +# Generate the code. +out = ModuleGenerationFactory("B.C", ["DerivedBindings"]) +mod.generate(out, "B") +out.close() diff --git a/examples/cross_module_inheritance/ModuleGenerationFactory.py b/examples/cross_module_inheritance/ModuleGenerationFactory.py new file mode 100644 index 0000000..b4a1615 --- /dev/null +++ b/examples/cross_module_inheritance/ModuleGenerationFactory.py @@ -0,0 +1,48 @@ +import os +from pybindgen import * + +#------------------------------------------------------------------------------- +# This is the implementation of the multi-section factory that allows us to +# write multiple source files for compiling. +# +# This is directly cribbed from Gustavo's example from the ns3 package at +# http://code.nsnam.org/ns-3-dev/file/e15adc7172f1/bindings/python/ns3modulegen.py +#------------------------------------------------------------------------------- +class ModuleGenerationFactory(module.MultiSectionFactory): + + def __init__(self, main_file_name, modules): + print "main file name: ", main_file_name + print "modules: ", modules + self.basename, ext = os.path.splitext(main_file_name) + super(ModuleGenerationFactory, self).__init__() + self.main_file_name = main_file_name + self.main_sink = FileCodeSink(open(main_file_name, "wt")) + self.header_name = self.basename + ".hh" + header_file_name = os.path.join(os.path.dirname(self.main_file_name), + '.', + self.header_name) + self.header_sink = FileCodeSink(open(header_file_name, "wt")) + self.section_sinks = {'__main__': self.main_sink} + for module in modules: + section_name = '%s_%s' % (self.basename, module.replace('-', '_')) + file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.C" % section_name) + sink = FileCodeSink(open(file_name, "wt")) + self.section_sinks[section_name] = sink + + def get_section_code_sink(self, section_name): + return self.section_sinks[section_name] + + def get_main_code_sink(self): + return self.main_sink + + def get_common_header_code_sink(self): + return self.header_sink + + def get_common_header_include(self): + return '"%s"' % self.header_name + + def close(self): + self.header_sink.file.close() + self.main_sink.file.close() + for sink in self.section_sinks.itervalues(): + sink.file.close() diff --git a/examples/cross_module_inheritance/classes.hh b/examples/cross_module_inheritance/classes.hh new file mode 100644 index 0000000..7b5b182 --- /dev/null +++ b/examples/cross_module_inheritance/classes.hh @@ -0,0 +1,20 @@ +#ifndef __PYBINDGEN_CROSS_MODULE_EXAMPLE_CLASSES_HH__ +#define __PYBINDGEN_CROSS_MODULE_EXAMPLE_CLASSES_HH__ + +#include + +class Base { +public: + Base() { std::cerr << "Base::Base()" << std::endl; } + virtual ~Base() { std::cerr << "Base::~Base()" << std::endl; } + virtual void do_something() const { std::cerr << "Base::do_something()" << std::endl; } +}; + +class Derived: public Base { +public: + Derived(): Base() { std::cerr << "Derived::Derived()" << std::endl; } + virtual ~Derived() { std::cerr << "Derived::~Derived()" << std::endl; } + virtual void do_something() const { std::cerr << "Derived::do_something()" << std::endl; } +}; + +#endif diff --git a/examples/cross_module_inheritance/wscript b/examples/cross_module_inheritance/wscript new file mode 100644 index 0000000..89108a0 --- /dev/null +++ b/examples/cross_module_inheritance/wscript @@ -0,0 +1,19 @@ +## -*- python -*- + +def build(bld): + + gen = bld.new_task_gen( + features='command', + source='Agen.py', + target='A.C', + command='${PYTHON} ${SRC[0]} > ${TGT[0]}') + + if bld.env['CXX']: + obj = bld.new_task_gen(features=['cxx', 'cxxshlib', 'pyext']) + obj.source = [ + 'A.C', + ] + obj.target = 'A' + obj.install_path = None # do not install + obj.includes = '.' + diff --git a/examples/wscript b/examples/wscript index d10e90c..d7105f7 100644 --- a/examples/wscript +++ b/examples/wscript @@ -9,6 +9,7 @@ def build(bld): bld.add_subdirs('buffer') if bld.env['ENABLE_BOOST_SHARED_PTR']: bld.add_subdirs('boost_shared_ptr') + bld.add_subdirs('cross_module_inheritance') def configure(conf): pass