From f2785302392eaf4e1b26c5fed8e3d52f9d48e052 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Tue, 22 Jan 2002 19:56:36 +0000 Subject: [PATCH] Nicer syntactic sugar [SVN r12448] --- test/m1.cpp | 93 +++++++++++++++++++++++++++++------------------------ test/m2.cpp | 28 ++++++++-------- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/test/m1.cpp b/test/m1.cpp index da3ca5d5..7f88af2b 100644 --- a/test/m1.cpp +++ b/test/m1.cpp @@ -269,7 +269,6 @@ BOOST_PYTHON_MODULE_INIT(m1) using boost::python::module; using boost::python::class_; - module m1("m1"); // Create the converters; they are self-registering/unregistering. static int_wrapper wrap_int; static simple_wrapper wrap_simple; @@ -280,56 +279,66 @@ BOOST_PYTHON_MODULE_INIT(m1) static simple_const_ref_unwrapper unwrap_simple_const_ref; static simple_ref_wrapper wrap_simple_ref; - // This unwrapper extracts pointers and references to the "complicated" class. - // static boost::python::converter::class_unwrapper unwrap_complicated; + module m1("m1"); + + m1 + // Insert the metaclass for all extension classes + .setattr("xclass", boost::python::objects::class_metatype()) - // Insert the extension metaclass object - m1.add( - boost::python::objects::class_metatype() - , "xclass"); - - // Insert the base class for all extension classes - m1.add(boost::python::objects::class_type() - , "xinst"); + // Insert the base class for all extension classes + .setattr("xinst", boost::python::objects::class_type()) - m1.def(new_noddy, "new_noddy"); - m1.def(new_simple, "new_simple"); - - // Expose f() - m1.def(f, "f"); + .def("new_noddy", new_noddy) + .def("new_simple", new_simple) - // Expose g() - m1.def(g, "g"); + // Expose f() + .def("f", f) - m1.def(take_a, "take_a"); - m1.def(take_b, "take_b"); - m1.def(take_c, "take_c"); - m1.def(take_d, "take_d"); + // Expose g() + .def("g", g) - class_(m1, "A") - .def_init() - .def(&A::name, "name") - ; - - class_ >(m1, "B") - .def_init() - .def(&B::name, "name") - ; - - class_ >(m1, "C") - .def_init() - .def(&C::name, "name") + .def("take_a", take_a) + .def("take_b", take_b) + .def("take_c", take_c) + .def("take_d", take_d) + + .add( + class_("A") + .def_init() + .def("name", &A::name) + ) + ; - class_ >(m1, "D") - .def_init() - .def(&D::name, "name") + // sequence points don't ensure that "A" is constructed before "B" + // or "C" below if we make them part of the same chain + m1 + .add( + class_ >("B") + .def_init() + .def("name", &B::name) + ) + + .add( + class_ >("C") + .def_init() + .def("name", &C::name) + ) ; - class_(m1, "complicated") - .def_init(args()) - .def_init(args()) - .def(&complicated::get_n, "get_n") + m1 + .add( + class_ >("D") + .def_init() + .def("name", &D::name) + ) + + .add( + class_("complicated") + .def_init(args()) + .def_init(args()) + .def("get_n", &complicated::get_n) + ) ; } diff --git a/test/m2.cpp b/test/m2.cpp index 7ea1f9ac..d70b0c13 100644 --- a/test/m2.cpp +++ b/test/m2.cpp @@ -66,21 +66,21 @@ struct rewrap BOOST_PYTHON_MODULE_INIT(m2) { - boost::python::module m2("m2"); - - m2.def(unwrap_int, "unwrap_int"); - m2.def(unwrap_int_ref, "unwrap_int_ref"); - m2.def(unwrap_int_const_ref, "unwrap_int_const_ref"); - m2.def(unwrap_simple, "unwrap_simple"); - m2.def(unwrap_simple_ref, "unwrap_simple_ref"); - m2.def(unwrap_simple_const_ref, "unwrap_simple_const_ref"); + boost::python::module("m2") + .def("unwrap_int", unwrap_int) + .def("unwrap_int_ref", unwrap_int_ref) + .def("unwrap_int_const_ref", unwrap_int_const_ref) + .def("unwrap_simple", unwrap_simple) + .def("unwrap_simple_ref", unwrap_simple_ref) + .def("unwrap_simple_const_ref", unwrap_simple_const_ref) - m2.def(&rewrap::f, "wrap_int"); - m2.def(&rewrap::f, "wrap_int_ref"); - m2.def(&rewrap::f, "wrap_int_const_ref"); - m2.def(&rewrap::f, "wrap_simple"); - m2.def(&rewrap::f, "wrap_simple_ref"); - m2.def(&rewrap::f, "wrap_simple_const_ref"); + .def("wrap_int", &rewrap::f) + .def("wrap_int_ref", &rewrap::f) + .def("wrap_int_const_ref", &rewrap::f) + .def("wrap_simple", &rewrap::f) + .def("wrap_simple_ref", &rewrap::f) + .def("wrap_simple_const_ref", &rewrap::f) + ; } #include "module_tail.cpp"