ben_scott1.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. struct Product {};
  9. typedef shared_ptr<Product> ProductPtr;
  10. struct Creator
  11. {
  12. virtual ~Creator() {}
  13. virtual ProductPtr create() = 0;
  14. };
  15. struct Factory
  16. {
  17. void reg(Creator* c) { mC = c; }
  18. ProductPtr create()
  19. {
  20. std::cout << "Name: " << (typeid(*mC)).name() << std::endl;
  21. return mC->create();
  22. }
  23. private:
  24. Creator* mC;
  25. };
  26. struct CreatorWrap : public Creator
  27. {
  28. CreatorWrap(PyObject* self) : mSelf(self) {}
  29. ProductPtr create() { return call_method<ProductPtr>(mSelf, "create"); }
  30. PyObject* mSelf;
  31. };
  32. BOOST_PYTHON_MODULE(ben_scott1_ext)
  33. {
  34. class_<Product, ProductPtr>("Product");
  35. class_<Creator, CreatorWrap, noncopyable>("Creator")
  36. .def("create", &CreatorWrap::create)
  37. ;
  38. class_<Factory>("Factory")
  39. .def("reg", &Factory::reg, with_custodian_and_ward<1,2>())
  40. .def("create", &Factory::create)
  41. ;
  42. }
  43. #include "../test/module_tail.cpp"