1 // Copyright David Abrahams 2004. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 #include <boost/python.hpp>
5 #include <iostream>
6 using namespace boost::python;
7 using namespace boost;
8
9 struct Product {};
10 typedef shared_ptr<Product> ProductPtr;
11
12
13 struct Creator
14 {
~CreatorCreator15 virtual ~Creator() {}
16 virtual ProductPtr create() = 0;
17 };
18
19
20 struct Factory
21 {
regFactory22 void reg(Creator* c) { mC = c; }
createFactory23 ProductPtr create()
24 {
25 std::cout << "Name: " << (typeid(*mC)).name() << std::endl;
26 return mC->create();
27 }
28
29 private:
30 Creator* mC;
31 };
32
33 struct CreatorWrap : public Creator
34 {
CreatorWrapCreatorWrap35 CreatorWrap(PyObject* self) : mSelf(self) {}
createCreatorWrap36 ProductPtr create() { return call_method<ProductPtr>(mSelf, "create"); }
37 PyObject* mSelf;
38 };
39
BOOST_PYTHON_MODULE(ben_scott1_ext)40 BOOST_PYTHON_MODULE(ben_scott1_ext)
41 {
42 class_<Product, ProductPtr>("Product");
43
44 class_<Creator, CreatorWrap, noncopyable>("Creator")
45 .def("create", &CreatorWrap::create)
46 ;
47
48 class_<Factory>("Factory")
49 .def("reg", &Factory::reg, with_custodian_and_ward<1,2>())
50 .def("create", &Factory::create)
51 ;
52 }
53
54 #include "../test/module_tail.cpp"
55