2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

Take advantage of independent class_<> definitions everywhere.

[SVN r14976]
This commit is contained in:
Dave Abrahams
2002-08-19 22:21:03 +00:00
parent 3092e07281
commit 1a7b331a4b
15 changed files with 141 additions and 205 deletions

View File

@@ -24,18 +24,13 @@ BOOST_PYTHON_MODULE_INIT(bienstman1_ext)
using boost::python::return_value_policy;
using boost::python::reference_existing_object;
module m("bienstman1_ext");
m
.add(class_<A, shared_ptr<A> >("A"))
class_<A, shared_ptr<A> >("A");
.add(
class_<V, boost::noncopyable>("V", no_init)
class_<V, boost::noncopyable>("V", no_init)
.def("inside", &V::inside,
return_value_policy<reference_existing_object>())
.def("outside", outside,
return_value_policy<reference_existing_object>())
)
;
;
}

View File

@@ -15,15 +15,10 @@ BOOST_PYTHON_MODULE_INIT(bienstman2_ext)
{
using namespace boost::python;
module m("bienstman2_ext");
m
.add(class_<C>("C"))
.add(class_<D>("D"))
.add(
class_<E>("E")
class_<C>("C");
class_<D>("D");
class_<E>("E")
.def("fe", &E::fe) // this compiles.
.def("fe2", &E::fe2) // this doesn't.
)
;
.def("fe2", &E::fe2) // this doesn't... well, now it does ;-)
;
}

View File

@@ -15,15 +15,7 @@ BOOST_PYTHON_MODULE_INIT(bienstman3_ext)
{
using namespace boost::python;
module m("bienstman3_ext");
m
.add(
class_<V, boost::noncopyable>("V", no_init)
)
class_<V, boost::noncopyable>("V", no_init);
class_<B>("B", args<const V&>());
.add(
class_<B>("B", args<const V&>())
)
;
}

View File

@@ -21,15 +21,16 @@ BOOST_PYTHON_MODULE_INIT(bienstman4_ext)
implicitly_convertible<Type1,Term>();
module("bienstman4_ext")
.add(class_<Expression>("Expression")
.def("add", &Expression::add))
.add(class_<Type1>("T1")
.add(class_<Term>("Term"
, args<Type1&>()))
;
class_<Expression>("Expression")
.def("add", &Expression::add)
;
class_<Type1>("T1")
;
class_<Term>("Term", args<Type1&>())
;
Type1 t1;
Expression e;
e.add(t1);

View File

@@ -15,15 +15,9 @@ struct M {M(const std::complex<double>&) {} };
BOOST_PYTHON_MODULE_INIT(bienstman5_ext)
{
using namespace boost::python;
using boost::mpl::type_list;
module m("bienstman5_ext");
m
.add(class_<M>("M")
.def_init(args<std::complex<double> const&>()))
;
class_<M>("M", args<std::complex<double> const&>())
;
}

View File

@@ -48,40 +48,21 @@ public:
};
BOOST_PYTHON_MODULE_INIT(cltree) {
boost::python::module m("cltree");
m
.add(
boost::python::class_<basic>("basic")
.def_init(boost::python::args<>())
BOOST_PYTHON_MODULE_INIT(cltree)
{
boost::python::class_<basic>("basic")
.def("__repr__",&basic::repr)
)
;
;
m
.add(boost::python::class_<constant
,boost::python::bases<basic>
,boost::noncopyable
>("constant")
.def_init(boost::python::args<>())
)
boost::python::class_<constant, boost::python::bases<basic>, boost::noncopyable>("constant")
;
.add(boost::python::class_<symbol
,symbol_wrapper
,boost::noncopyable
>("symbol")
.def_init(boost::python::args<>())
)
.add(boost::python::class_<variable
,boost::python::bases<basic>
,variable_wrapper
//,boost::noncopyable // leads to compiler failure?!
>("variable")
.def_init(boost::python::args<>())
)
;
boost::python::class_<symbol, symbol_wrapper, boost::noncopyable>("symbol")
;
boost::python::class_<variable, boost::python::bases<basic>, variable_wrapper>("variable")
;
}
#include "module_tail.cpp"

View File

@@ -22,20 +22,17 @@ double get_fair_value(X const& x) { return x.value(); }
BOOST_PYTHON_MODULE_INIT(data_members_ext)
{
module("data_members_ext")
.add(
class_<X>("X", args<int>())
.def("value", &X::value)
.def("set", &X::set)
.def_readonly("x", &X::x)
.add_property("get_fair_value", object(&get_fair_value))
)
.add(
class_<Y>("Y", args<int>())
.def("value", &Y::value)
.def("set", &Y::set)
.def_readwrite("x", &Y::x)
)
class_<X>("X", args<int>())
.def("value", &X::value)
.def("set", &X::set)
.def_readonly("x", &X::x)
.add_property("get_fair_value", object(&get_fair_value))
;
class_<Y>("Y", args<int>())
.def("value", &Y::value)
.def("set", &Y::set)
.def_readwrite("x", &Y::x)
;
}

View File

@@ -99,12 +99,6 @@ BOOST_PYTHON_MODULE_INIT(extract_ext)
{
implicitly_convertible<int, X>();
class_<X> x_class("X", args<int>());
x_class
.def( "__repr__", x_rep)
;
module("extract_ext")
.def("extract_bool", extract_bool)
.def("extract_list", extract_list)
@@ -124,12 +118,15 @@ BOOST_PYTHON_MODULE_INIT(extract_ext)
.def("check_X_ptr", check_X_ptr)
.def("check_X_ref", check_X_ref)
.add(x_class)
.def("double_X", double_X)
.def("count_Xs", &X::count)
;
object x_class(
class_<X>("X", args<int>())
.def( "__repr__", x_rep));
// Instantiate an X object through the Python interface
object x_obj = x_class(3);

View File

@@ -74,51 +74,51 @@ BOOST_PYTHON_MODULE_INIT(iterator_ext)
{
module("iterator_ext")
.def("range", &::range)
.add(
class_<list_int>("list_int")
.def("push_back", push_back)
.def("back", back)
.def("__iter__", iterator<list_int>())
)
.add(
class_<list_range>("list_range")
;
// We can specify data members
.def("__iter__"
, range(&list_range::first, &list_range::second))
)
.add(
class_<two_lists>("two_lists")
class_<list_int>("list_int")
.def("push_back", push_back)
.def("back", back)
.def("__iter__", iterator<list_int>())
;
// We can spcify member functions
.add_property(
"primes"
, range(&two_lists::one_begin, &two_lists::one_end))
class_<list_range>("list_range")
// Prove that we can explicitly specify call policies
.add_property(
"evens"
, range<return_value_policy<copy_non_const_reference> >(
&two_lists::two_begin, &two_lists::two_end))
// We can specify data members
.def("__iter__"
, range(&list_range::first, &list_range::second))
;
// Prove that we can specify call policies and target
.add_property(
"twosies"
, range<return_value_policy<copy_non_const_reference>, two_lists>(
// And we can use adaptable function objects when
// partial specialization is available.
class_<two_lists>("two_lists")
// We can spcify member functions
.add_property(
"primes"
, range(&two_lists::one_begin, &two_lists::one_end))
// Prove that we can explicitly specify call policies
.add_property(
"evens"
, range<return_value_policy<copy_non_const_reference> >(
&two_lists::two_begin, &two_lists::two_end))
// Prove that we can specify call policies and target
.add_property(
"twosies"
, range<return_value_policy<copy_non_const_reference>, two_lists>(
// And we can use adaptable function objects when
// partial specialization is available.
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
two_lists::two_start()
two_lists::two_start()
# else
&two_lists::two_begin
&two_lists::two_begin
# endif
, &two_lists::two_end))
)
.add(
class_<list_list>("list_list")
.def("push_back", push_list_back)
.def("__iter__", iterator<list_list,return_internal_reference<> >())
)
, &two_lists::two_end))
;
class_<list_list>("list_list")
.def("push_back", push_list_back)
.def("__iter__", iterator<list_list,return_internal_reference<> >())
;
}

View File

@@ -14,13 +14,10 @@ BOOST_PYTHON_MODULE_INIT(multi_arg_constructor_ext)
using namespace boost::python;
using boost::shared_ptr;
module("multi_arg_constructor_ext")
.add(class_<A, shared_ptr<A> >(
"A"
, args<double, double, double, double, double, double, double, double, double>()
)
)
class_<A, shared_ptr<A> >(
"A"
, args<double, double, double, double, double, double, double, double, double>()
)
;
}

View File

@@ -58,31 +58,28 @@ std::ostream& operator<<(std::ostream& s, X const& x)
BOOST_PYTHON_MODULE_INIT(operators_ext)
{
module("operators_ext")
.add(
class_<X>("X", args<int>())
.def("value", &X::value)
.def(self - self)
.def(self - int())
.def(other<int>() - self)
.def(-self)
.def(self < other<int>())
.def(self < self)
.def(1 < self)
.def(self -= self)
.def(abs(self))
.def(str(self))
class_<X>("X", args<int>())
.def("value", &X::value)
.def(self - self)
.def(self - int())
.def(other<int>() - self)
.def(-self)
.def(self < other<int>())
.def(self < self)
.def(1 < self)
.def(self -= self)
.def(abs(self))
.def(str(self))
.def(pow(self,self))
.def(pow(self,int()))
.def(pow(int(),self))
)
.add(
class_<test_class<1> >("Z", args<int>())
.def(int_(self))
.def(float_(self))
.def(complex_(self))
)
.def(pow(self,self))
.def(pow(self,int()))
.def(pow(int(),self))
;
class_<test_class<1> >("Z", args<int>())
.def(int_(self))
.def(float_(self))
.def(complex_(self))
;
}

View File

@@ -50,11 +50,8 @@ namespace {
BOOST_PYTHON_MODULE_INIT(pickle1_ext)
{
using namespace boost::python;
module("pickle1_ext")
.add(class_<world>("world"
, args<const std::string&>())
class_<world>("world", args<const std::string&>())
.def("greet", &world::greet)
.def_pickle(world_pickle_suite())
)
;
;
}

View File

@@ -89,13 +89,11 @@ namespace { // Avoid cluttering the global namespace.
BOOST_PYTHON_MODULE_INIT(pickle2_ext)
{
boost::python::module("pickle2_ext")
.add(boost::python::class_<world>("world"
, boost::python::args<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
)
;
boost::python::class_<world>(
"world", boost::python::args<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
;
}

View File

@@ -95,13 +95,11 @@ namespace { // Avoid cluttering the global namespace.
BOOST_PYTHON_MODULE_INIT(pickle3_ext)
{
boost::python::module("pickle3_ext")
.add(boost::python::class_<world>("world"
, boost::python::args<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
)
;
boost::python::class_<world>(
"world", boost::python::args<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
;
}

View File

@@ -88,27 +88,24 @@ int X::counter;
BOOST_PYTHON_MODULE_INIT(virtual_functions_ext)
{
module("virtual_functions_ext")
.add(
class_<concrete, concrete_callback>("concrete", args<int>())
.def("value", &concrete::value)
.def("set", &concrete::set)
.def("call_f", &concrete::call_f)
.def("f", &concrete_callback::f_impl))
class_<concrete, concrete_callback>("concrete", args<int>())
.def("value", &concrete::value)
.def("set", &concrete::set)
.def("call_f", &concrete::call_f)
.def("f", &concrete_callback::f_impl)
;
.add(
class_<abstract, boost::noncopyable, boost::shared_ptr<abstract_callback>
>("abstract", args<int>())
class_<abstract, boost::noncopyable, boost::shared_ptr<abstract_callback>
>("abstract", args<int>())
.def("value", &abstract::value)
.def("call_f", &abstract::call_f)
.def("set", &abstract::set))
.def("value", &abstract::value)
.def("call_f", &abstract::call_f)
.def("set", &abstract::set)
;
.add(
class_<Y>("Y", args<int>())
.def("value", &Y::value)
.def("set", &Y::set)
)
class_<Y>("Y", args<int>())
.def("value", &Y::value)
.def("set", &Y::set)
;
}