Skip to content

Commit 1a7b331

Browse files
committed
Take advantage of independent class_<> definitions everywhere.
[SVN r14976]
1 parent 3092e07 commit 1a7b331

15 files changed

+148
-212
lines changed

test/bienstman1.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,13 @@ BOOST_PYTHON_MODULE_INIT(bienstman1_ext)
2424
using boost::python::return_value_policy;
2525
using boost::python::reference_existing_object;
2626

27-
module m("bienstman1_ext");
28-
29-
m
30-
.add(class_<A, shared_ptr<A> >("A"))
27+
class_<A, shared_ptr<A> >("A");
3128

32-
.add(
33-
class_<V, boost::noncopyable>("V", no_init)
29+
class_<V, boost::noncopyable>("V", no_init)
3430
.def("inside", &V::inside,
3531
return_value_policy<reference_existing_object>())
3632
.def("outside", outside,
3733
return_value_policy<reference_existing_object>())
38-
)
39-
;
34+
;
4035
}
4136

test/bienstman2.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ BOOST_PYTHON_MODULE_INIT(bienstman2_ext)
1515
{
1616
using namespace boost::python;
1717

18-
module m("bienstman2_ext");
19-
20-
m
21-
.add(class_<C>("C"))
22-
.add(class_<D>("D"))
23-
.add(
24-
class_<E>("E")
18+
class_<C>("C");
19+
class_<D>("D");
20+
class_<E>("E")
2521
.def("fe", &E::fe) // this compiles.
26-
.def("fe2", &E::fe2) // this doesn't.
27-
)
28-
;
22+
.def("fe2", &E::fe2) // this doesn't... well, now it does ;-)
23+
;
2924
}

test/bienstman3.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ BOOST_PYTHON_MODULE_INIT(bienstman3_ext)
1515
{
1616
using namespace boost::python;
1717

18-
module m("bienstman3_ext");
19-
20-
m
21-
.add(
22-
class_<V, boost::noncopyable>("V", no_init)
23-
)
18+
class_<V, boost::noncopyable>("V", no_init);
19+
class_<B>("B", args<const V&>());
2420

25-
.add(
26-
class_<B>("B", args<const V&>())
27-
)
28-
;
2921
}

test/bienstman4.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ BOOST_PYTHON_MODULE_INIT(bienstman4_ext)
2121

2222
implicitly_convertible<Type1,Term>();
2323

24-
module("bienstman4_ext")
25-
.add(class_<Expression>("Expression")
26-
.def("add", &Expression::add))
27-
.add(class_<Type1>("T1")
28-
.add(class_<Term>("Term"
29-
, args<Type1&>()))
30-
;
24+
class_<Expression>("Expression")
25+
.def("add", &Expression::add)
26+
;
27+
28+
class_<Type1>("T1")
29+
;
30+
31+
class_<Term>("Term", args<Type1&>())
32+
;
3133

32-
3334
Type1 t1;
3435
Expression e;
3536
e.add(t1);

test/bienstman5.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@ struct M {M(const std::complex<double>&) {} };
1515
BOOST_PYTHON_MODULE_INIT(bienstman5_ext)
1616
{
1717
using namespace boost::python;
18-
using boost::mpl::type_list;
1918

20-
module m("bienstman5_ext");
21-
22-
m
23-
.add(class_<M>("M")
24-
.def_init(args<std::complex<double> const&>()))
25-
;
26-
19+
class_<M>("M", args<std::complex<double> const&>())
20+
;
2721
}
2822

2923

test/cltree.cpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,21 @@ class variable_wrapper: public variable {
4848

4949
};
5050

51-
BOOST_PYTHON_MODULE_INIT(cltree) {
52-
53-
boost::python::module m("cltree");
54-
m
55-
.add(
56-
boost::python::class_<basic>("basic")
57-
.def_init(boost::python::args<>())
51+
BOOST_PYTHON_MODULE_INIT(cltree)
52+
{
53+
boost::python::class_<basic>("basic")
5854
.def("__repr__",&basic::repr)
59-
)
60-
;
61-
62-
m
63-
.add(boost::python::class_<constant
64-
,boost::python::bases<basic>
65-
,boost::noncopyable
66-
>("constant")
67-
.def_init(boost::python::args<>())
68-
)
69-
70-
.add(boost::python::class_<symbol
71-
,symbol_wrapper
72-
,boost::noncopyable
73-
>("symbol")
74-
.def_init(boost::python::args<>())
75-
)
76-
77-
.add(boost::python::class_<variable
78-
,boost::python::bases<basic>
79-
,variable_wrapper
80-
//,boost::noncopyable // leads to compiler failure?!
81-
>("variable")
82-
.def_init(boost::python::args<>())
83-
)
84-
;
55+
;
56+
57+
boost::python::class_<constant, boost::python::bases<basic>, boost::noncopyable>("constant")
58+
;
59+
60+
61+
boost::python::class_<symbol, symbol_wrapper, boost::noncopyable>("symbol")
62+
;
63+
64+
boost::python::class_<variable, boost::python::bases<basic>, variable_wrapper>("variable")
65+
;
8566
}
8667

8768
#include "module_tail.cpp"

test/data_members.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,17 @@ double get_fair_value(X const& x) { return x.value(); }
2222

2323
BOOST_PYTHON_MODULE_INIT(data_members_ext)
2424
{
25-
module("data_members_ext")
26-
.add(
27-
class_<X>("X", args<int>())
28-
.def("value", &X::value)
29-
.def("set", &X::set)
30-
.def_readonly("x", &X::x)
31-
.add_property("get_fair_value", object(&get_fair_value))
32-
)
33-
.add(
34-
class_<Y>("Y", args<int>())
35-
.def("value", &Y::value)
36-
.def("set", &Y::set)
37-
.def_readwrite("x", &Y::x)
38-
)
25+
class_<X>("X", args<int>())
26+
.def("value", &X::value)
27+
.def("set", &X::set)
28+
.def_readonly("x", &X::x)
29+
.add_property("get_fair_value", object(&get_fair_value))
30+
;
31+
32+
class_<Y>("Y", args<int>())
33+
.def("value", &Y::value)
34+
.def("set", &Y::set)
35+
.def_readwrite("x", &Y::x)
3936
;
4037
}
4138

test/extract.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ BOOST_PYTHON_MODULE_INIT(extract_ext)
9999
{
100100
implicitly_convertible<int, X>();
101101

102-
class_<X> x_class("X", args<int>());
103-
104-
x_class
105-
.def( "__repr__", x_rep)
106-
;
107-
108102
module("extract_ext")
109103
.def("extract_bool", extract_bool)
110104
.def("extract_list", extract_list)
@@ -124,12 +118,15 @@ BOOST_PYTHON_MODULE_INIT(extract_ext)
124118
.def("check_X_ptr", check_X_ptr)
125119
.def("check_X_ref", check_X_ref)
126120

127-
.add(x_class)
128121
.def("double_X", double_X)
129122

130123
.def("count_Xs", &X::count)
131124
;
132125

126+
object x_class(
127+
class_<X>("X", args<int>())
128+
.def( "__repr__", x_rep));
129+
133130
// Instantiate an X object through the Python interface
134131
object x_obj = x_class(3);
135132

test/iterator.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,51 +74,51 @@ BOOST_PYTHON_MODULE_INIT(iterator_ext)
7474
{
7575
module("iterator_ext")
7676
.def("range", &::range)
77-
.add(
78-
class_<list_int>("list_int")
79-
.def("push_back", push_back)
80-
.def("back", back)
81-
.def("__iter__", iterator<list_int>())
82-
)
83-
.add(
84-
class_<list_range>("list_range")
85-
86-
// We can specify data members
87-
.def("__iter__"
88-
, range(&list_range::first, &list_range::second))
89-
)
90-
.add(
91-
class_<two_lists>("two_lists")
92-
93-
// We can spcify member functions
94-
.add_property(
95-
"primes"
96-
, range(&two_lists::one_begin, &two_lists::one_end))
97-
98-
// Prove that we can explicitly specify call policies
99-
.add_property(
100-
"evens"
101-
, range<return_value_policy<copy_non_const_reference> >(
102-
&two_lists::two_begin, &two_lists::two_end))
103-
104-
// Prove that we can specify call policies and target
105-
.add_property(
106-
"twosies"
107-
, range<return_value_policy<copy_non_const_reference>, two_lists>(
108-
// And we can use adaptable function objects when
109-
// partial specialization is available.
77+
;
78+
79+
class_<list_int>("list_int")
80+
.def("push_back", push_back)
81+
.def("back", back)
82+
.def("__iter__", iterator<list_int>())
83+
;
84+
85+
class_<list_range>("list_range")
86+
87+
// We can specify data members
88+
.def("__iter__"
89+
, range(&list_range::first, &list_range::second))
90+
;
91+
92+
class_<two_lists>("two_lists")
93+
94+
// We can spcify member functions
95+
.add_property(
96+
"primes"
97+
, range(&two_lists::one_begin, &two_lists::one_end))
98+
99+
// Prove that we can explicitly specify call policies
100+
.add_property(
101+
"evens"
102+
, range<return_value_policy<copy_non_const_reference> >(
103+
&two_lists::two_begin, &two_lists::two_end))
104+
105+
// Prove that we can specify call policies and target
106+
.add_property(
107+
"twosies"
108+
, range<return_value_policy<copy_non_const_reference>, two_lists>(
109+
// And we can use adaptable function objects when
110+
// partial specialization is available.
110111
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
111-
two_lists::two_start()
112+
two_lists::two_start()
112113
# else
113-
&two_lists::two_begin
114+
&two_lists::two_begin
114115
# endif
115-
, &two_lists::two_end))
116-
)
117-
.add(
118-
class_<list_list>("list_list")
119-
.def("push_back", push_list_back)
120-
.def("__iter__", iterator<list_list,return_internal_reference<> >())
121-
)
116+
, &two_lists::two_end))
117+
;
118+
119+
class_<list_list>("list_list")
120+
.def("push_back", push_list_back)
121+
.def("__iter__", iterator<list_list,return_internal_reference<> >())
122122
;
123123
}
124124

test/multi_arg_constructor.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ BOOST_PYTHON_MODULE_INIT(multi_arg_constructor_ext)
1414
using namespace boost::python;
1515
using boost::shared_ptr;
1616

17-
module("multi_arg_constructor_ext")
18-
19-
.add(class_<A, shared_ptr<A> >(
20-
"A"
21-
, args<double, double, double, double, double, double, double, double, double>()
22-
)
23-
)
17+
class_<A, shared_ptr<A> >(
18+
"A"
19+
, args<double, double, double, double, double, double, double, double, double>()
20+
)
2421
;
2522

2623
}

0 commit comments

Comments
 (0)