|
Boost : |
From: Ullrich Koethe (koethe_at_[hidden])
Date: 2000-12-07 12:59:53
I strongly agree that this is a very important extension. Here is an
example that might be a little easier to explain:
Given two modules:
- uimodule.so: contains wrapped C++ user interface widgets
including, say, "Window"
- logicmodule.so: contains application logic and a new widget
"ResultsWindow"
derived from "Window"
Now the problem is: how can we use "ResultsWindow" like any window
defined in the UI module, *without* merging the two modules into one?
I think it can already be done with BPL. (At least, the following worked
in my tests.)
1. wrap the UI classes into uimodule.cpp as usual.
2. in uimodule.cpp: define functions to retrieve the class object for
any class needed in another module:
boost::python::detail::extension_class<Window> * get_window_class()
{
return
dynamic_cast<boost::python::detail::extension_class<Window> *>
(boost::python::detail::class_registry<Window>::class_object());
}
Declare these functions in a header "uicommon.hpp"
3. Wrap the application logic and "ResultWindow" into logicmodule.cpp
as usual:
boost::python::class_builder<ResultWindow>
reswindow_class(logicmodule, "ResultWindow");
4. in logicmodule.cpp:
- Include "uicommon.hpp"
- declare the wrapped "Window" as base of the wrapped
"ResultWindow"
reswindow_class.get_extension_class()->
declare_base(get_window_class());
5. *Dynamically* link logicmodule.so against uimodule.so
g++ -shared -o logicmodule.so logicmodule.o uimodule.so \
-lboostpython
6. Then in Python
>>> import ui
>>> import logic
>>> result_window = logic.ResultWindow()
>>> ui.show(result_window)
Function show() from module ui can be applied to an object from module
logic!
[Note: to make this work, line 33 in module_builder.cpp must be
commented out:
// assert(name_holder.get() == 0);
]
David, is there anything inherently wrong with this? It certainly works
in a simple test setting.
Regards
Ulli
-- ________________________________________________________________ | | | Ullrich Koethe Universität Hamburg / University of Hamburg | | FB Informatik / Dept. of Computer Science | | AB Kognitive Systeme / Cognitive Systems Group | | | | Phone: +49 (0)40 42883-2573 Vogt-Koelln-Str. 30 | | Fax: +49 (0)40 42883-2572 D - 22527 Hamburg | | Email: u.koethe_at_[hidden] Germany | | koethe_at_[hidden] | | WWW: http://kogs-www.informatik.uni-hamburg.de/~koethe/ | |________________________________________________________________|
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk