Skip to content

Commit 63eed89

Browse files
committed
class_<> is now derived from object
[SVN r14594]
1 parent f458dbd commit 63eed89

File tree

13 files changed

+115
-72
lines changed

13 files changed

+115
-72
lines changed

include/boost/python/class.hpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ namespace detail
4747
// to the type of holder that must be created. The 3rd argument is a
4848
// reference to the Python type object to be created.
4949
template <class T, class Holder>
50-
static inline void register_copy_constructor(mpl::bool_t<true> const&, Holder*, handle<> const& obj, T* = 0)
50+
static inline void register_copy_constructor(mpl::bool_t<true> const&, Holder*, object const& obj, T* = 0)
5151
{
5252
objects::class_wrapper<T,Holder> x(obj);
5353
}
5454

5555
// Tag dispatched to have no effect.
5656
template <class T, class Holder>
57-
static inline void register_copy_constructor(mpl::bool_t<false> const&, Holder*, handle<> const&, T* = 0)
57+
static inline void register_copy_constructor(mpl::bool_t<false> const&, Holder*, object const&, T* = 0)
5858
{
5959
}
6060
}
@@ -104,11 +104,13 @@ class class_ : public objects::class_base
104104
// Use function::add_to_namespace to achieve overloading if
105105
// appropriate.
106106
objects::function::add_to_namespace(
107-
this->object(), name,
108-
handle<>(detail::wrap_function(
107+
*this, name
108+
, object(
109+
detail::new_reference(
110+
detail::wrap_function(
109111
// This bit of nastiness casts F to a member function of T if possible.
110112
detail::member_function_cast<T,F>::stage1(f).stage2((T*)0).stage3(f)
111-
)));
113+
))));
112114
return *this;
113115
}
114116

@@ -132,8 +134,10 @@ class class_ : public objects::class_base
132134
// Use function::add_to_namespace to achieve overloading if
133135
// appropriate.
134136
objects::function::add_to_namespace(
135-
this->object(), op.name(),
136-
handle<>(detail::wrap_function(&op_t::template apply<T>::execute)));
137+
*this, op.name()
138+
, object(
139+
detail::new_reference(
140+
detail::wrap_function(&op_t::template apply<T>::execute))));
137141
return *this;
138142
}
139143

@@ -255,7 +259,7 @@ inline class_<T,X1,X2,X3>::class_()
255259
detail::register_copy_constructor<T>(
256260
mpl::bool_t<is_copyable>()
257261
, objects::select_holder<T,held_type>((held_type*)0).get()
258-
, this->object());
262+
, *this);
259263
}
260264

261265
template <class T, class X1, class X2, class X3>
@@ -268,7 +272,7 @@ inline class_<T,X1,X2,X3>::class_(char const* name)
268272
detail::register_copy_constructor<T>(
269273
mpl::bool_t<is_copyable>()
270274
, objects::select_holder<T,held_type>((held_type*)0).get()
271-
, this->object());
275+
, *this);
272276
}
273277

274278

include/boost/python/iterator.hpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# include <boost/python/detail/target.hpp>
1010
# include <boost/python/object/iterator.hpp>
11+
# include <boost/python/object_core.hpp>
1112
# include <boost/type_traits/cv_traits.hpp>
1213
# include <boost/type_traits/transform_traits.hpp>
1314

@@ -19,7 +20,7 @@ namespace detail
1920
// objects::make_iterator(...), which allows us to pass member
2021
// function and member data pointers.
2122
template <class NextPolicies, class Target, class Accessor1, class Accessor2>
22-
inline handle<> make_iterator(
23+
inline object make_iterator(
2324
Accessor1 get_start, Accessor2 get_finish, boost::type<Target>* target = 0, NextPolicies* = 0)
2425
{
2526
return objects::make_iterator_function<NextPolicies,Target>(
@@ -70,17 +71,27 @@ struct iterators
7071
template <class Accessor1, class Accessor2>
7172
handle<> range(Accessor1 start, Accessor2 finish)
7273
{
73-
return detail::make_iterator<objects::default_iterator_call_policies>(
74-
start, finish
75-
, detail::target(start));
74+
return handle<>(
75+
borrowed(allow_null(
76+
detail::make_iterator<objects::default_iterator_call_policies>(
77+
start, finish
78+
, detail::target(start))
79+
.ptr()
80+
))
81+
);
7682
}
7783

7884
// Create an iterator-building function which uses the given accessors
7985
// and next() policies. Deduce the Target type.
8086
template <class NextPolicies, class Accessor1, class Accessor2>
8187
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
8288
{
83-
return detail::make_iterator<NextPolicies>(start, finish, detail::target(start));
89+
return handle<>(
90+
borrowed(
91+
allow_null(
92+
detail::make_iterator<NextPolicies>(start, finish, detail::target(start))
93+
.ptr()
94+
)));
8495
}
8596

8697
// Create an iterator-building function which uses the given accessors
@@ -89,7 +100,12 @@ template <class NextPolicies, class Target, class Accessor1, class Accessor2>
89100
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
90101
{
91102
typedef typename add_reference<Target>::type target;
92-
return detail::make_iterator<NextPolicies, target>(start, finish);
103+
return handle<>(
104+
borrowed(
105+
allow_null(
106+
detail::make_iterator<NextPolicies, target>(start, finish)
107+
.ptr()
108+
)));
93109
}
94110

95111
// A Python callable object which produces an iterator traversing

include/boost/python/module.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class module : public detail::module_base
3232
template <class T1, class T2 , class T3, class T4>
3333
module& add(class_<T1,T2,T3,T4> const& c)
3434
{
35-
this->add_class(c.object());
35+
this->add_class(type_handle(borrowed((PyTypeObject*)c.ptr())));
3636
return *this;
3737
}
3838

include/boost/python/object/class.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# include <boost/python/type_id.hpp>
1313
# include <boost/python/handle.hpp>
1414
# include <boost/python/instance_holder.hpp>
15+
# include <boost/python/object_core.hpp>
1516
# include <cstddef>
1617

1718
namespace boost { namespace python {
@@ -23,7 +24,7 @@ namespace objects {
2324
// To identify a class, we don't need cv/reference decorations
2425
typedef type_info class_id;
2526

26-
struct BOOST_PYTHON_DECL class_base : private noncopyable
27+
struct BOOST_PYTHON_DECL class_base : python::api::object
2728
{
2829
// constructor
2930
class_base(
@@ -35,13 +36,10 @@ struct BOOST_PYTHON_DECL class_base : private noncopyable
3536
);
3637

3738
// Retrieve the underlying object
38-
type_handle object() const { return m_object; }
3939
void add_property(char const* name, handle<> const& fget);
4040
void add_property(char const* name, handle<> const& fget, handle<> const& fset);
4141
void setattr(char const* name, handle<> const&);
4242
void enable_pickling(bool getstate_manages_dict);
43-
private:
44-
type_handle m_object;
4543
};
4644

4745
BOOST_PYTHON_DECL type_handle registered_class_object(class_id id);

include/boost/python/object/class_wrapper.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ template <class T, class Holder>
1515
struct class_wrapper
1616
: to_python_converter<T,class_wrapper<T,Holder> >
1717
{
18-
class_wrapper(handle<> const& type_)
18+
class_wrapper(object const& type_)
1919
: m_class_object_keeper(type_)
2020
{
21-
assert(type_->ob_type == (PyTypeObject*)class_metatype().get());
22-
m_class_object = (PyTypeObject*)type_.get();
21+
assert(type_.ptr()->ob_type == (PyTypeObject*)class_metatype().get());
22+
m_class_object = (PyTypeObject*)type_.ptr();
2323
}
2424

2525
static PyObject* convert(T const& x)
@@ -48,7 +48,7 @@ struct class_wrapper
4848
}
4949

5050
private:
51-
handle<> m_class_object_keeper;
51+
object m_class_object_keeper;
5252
static PyTypeObject* m_class_object;
5353
};
5454

include/boost/python/object/function.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# include <boost/python/detail/config.hpp>
1111
# include <boost/python/handle.hpp>
1212
# include <boost/function/function2.hpp>
13+
# include <boost/python/object_fwd.hpp>
1314

1415
namespace boost { namespace python { namespace objects {
1516

@@ -27,7 +28,7 @@ struct BOOST_PYTHON_DECL function : PyObject
2728
// a function object (this class), and an existing function is
2829
// already there, add it as an overload.
2930
static void add_to_namespace(
30-
handle<> const& name_space, char const* name, handle<> const& attribute);
31+
object const& name_space, char const* name, object const& attribute);
3132

3233
private: // helper functions
3334
void argument_error(PyObject* args, PyObject* keywords) const;

include/boost/python/object/iterator.hpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# include <boost/mpl/apply.hpp>
1818
# include <boost/bind.hpp>
1919
# include <boost/bind/protect.hpp>
20+
# include <boost/python/detail/raw_pyobject.hpp>
2021

2122
namespace boost { namespace python { namespace objects {
2223

@@ -113,30 +114,28 @@ namespace detail
113114
// policies, creating it if neccessary. Requires: NextPolicies is
114115
// default-constructible.
115116
template <class Iterator, class NextPolicies>
116-
handle<> demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
117+
object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
117118
{
118119
typedef iterator_range<NextPolicies,Iterator> range_;
119120

120121
// Check the registry. If one is already registered, return it.
121-
handle<> result(
122+
handle<> class_obj(
122123
objects::registered_class_object(python::type_id<range_>()));
123124

124-
if (result.get() == 0)
125-
{
126-
// Make a callable object which can be used as the iterator's next() function.
127-
handle<> next_function(
128-
new objects::function(
129-
objects::py_function(
130-
bind(&detail::iterator_next<Iterator,NextPolicies>::execute, _1, _2, policies))
131-
, 1));
125+
if (class_obj.get() != 0)
126+
return object(class_obj);
127+
128+
// Make a callable object which can be used as the iterator's next() function.
129+
handle<> next_function(
130+
new objects::function(
131+
objects::py_function(
132+
bind(&detail::iterator_next<Iterator,NextPolicies>::execute, _1, _2, policies))
133+
, 1));
132134

133-
result = class_<range_>(name)
134-
.def("__iter__", identity_function())
135-
.setattr("next", next_function)
136-
.object();
137-
138-
}
139-
return result;
135+
return class_<range_>(name)
136+
.def("__iter__", identity_function())
137+
.setattr("next", next_function)
138+
;
140139
}
141140

142141
// This class template acts as a generator for an ordinary function
@@ -187,22 +186,23 @@ namespace detail
187186
// iterators for the range, and an instance of NextPolicies is used as
188187
// CallPolicies for the Python iterator's next() function.
189188
template <class NextPolicies, class Target, class Accessor1, class Accessor2>
190-
inline handle<> make_iterator_function(
189+
inline object make_iterator_function(
191190
Accessor1 const& get_start, Accessor2 const& get_finish
192191
, boost::type<Target>* = 0, NextPolicies* = 0)
193192
{
194193
typedef typename Accessor1::result_type result_type;
195194

196-
return handle<>(
197-
new objects::function(
198-
objects::py_function(
199-
boost::bind(
200-
&detail::make_iterator_help<
201-
Target,result_type,Accessor1,Accessor2,NextPolicies
202-
>::create
203-
, get_start, get_finish, _1, _2)
204-
)
205-
,1 ));
195+
return object(
196+
python::detail::new_non_null_reference(
197+
new objects::function(
198+
objects::py_function(
199+
boost::bind(
200+
&detail::make_iterator_help<
201+
Target,result_type,Accessor1,Accessor2,NextPolicies
202+
>::create
203+
, get_start, get_finish, _1, _2))
204+
,1 ))
205+
);
206206
}
207207

208208
//

include/boost/python/object/pointer_holder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# include <boost/type.hpp>
1313

14-
# include <boost/python/object/class.hpp>
14+
# include <boost/python/instance_holder.hpp>
1515
# include <boost/python/type_id.hpp>
1616
# include <boost/python/object/inheritance.hpp>
1717
# include <boost/python/object/find_instance.hpp>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef OBJECT_FWD_DWA2002724_HPP
7+
# define OBJECT_FWD_DWA2002724_HPP
8+
9+
namespace boost { namespace python {
10+
namespace api
11+
{
12+
class object;
13+
}
14+
using api::object;
15+
}} // namespace boost::python
16+
17+
#endif // OBJECT_FWD_DWA2002724_HPP

src/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void module_base::setattr(char const* name, handle<> const& x)
3737
{
3838
// Use function::add_to_namespace to achieve overloading if
3939
// appropriate.
40-
objects::function::add_to_namespace(m_module, name, x);
40+
objects::function::add_to_namespace(python::object(m_module), name, python::object(x));
4141
}
4242

4343
void module_base::add(type_handle const& x)

0 commit comments

Comments
 (0)