raw_ctor.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/class.hpp>
  5. #include <boost/python/raw_function.hpp>
  6. #include <boost/python/make_constructor.hpp>
  7. #include <boost/python/dict.hpp>
  8. #include <boost/python/tuple.hpp>
  9. #include <boost/python/module.hpp>
  10. using namespace boost::python;
  11. class Foo
  12. {
  13. public:
  14. Foo(tuple args, dict kw)
  15. : args(args), kw(kw) {}
  16. tuple args;
  17. dict kw;
  18. };
  19. object init_foo(tuple args, dict kw)
  20. {
  21. tuple rest(args.slice(1,_));
  22. return args[0].attr("__init__")(rest, kw);
  23. }
  24. BOOST_PYTHON_MODULE(raw_ctor_ext)
  25. {
  26. // using no_init postpones defining __init__ function until after
  27. // raw_function for proper overload resolution order, since later
  28. // defs get higher priority.
  29. class_<Foo>("Foo", no_init)
  30. .def("__init__", raw_function(&init_foo))
  31. .def(init<tuple, dict>())
  32. .def_readwrite("args", &Foo::args)
  33. .def_readwrite("kw", &Foo::kw)
  34. ;
  35. }
  36. #include "module_tail.cpp"