Skip to content

Commit 97afc4b

Browse files
committed
operator support
[SVN r14068]
1 parent a67b29a commit 97afc4b

13 files changed

Lines changed: 759 additions & 37 deletions

File tree

include/boost/python/class.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# include <boost/python/object/class_wrapper.hpp>
2525
# include <boost/python/data_members.hpp>
2626
# include <boost/utility.hpp>
27+
# include <boost/python/detail/operator_id.hpp>
2728

2829
namespace boost { namespace python {
2930

@@ -37,6 +38,9 @@ namespace detail
3738
template <class T1, class T2, class T3>
3839
struct has_noncopyable;
3940

41+
template <detail::operator_id, class L, class R>
42+
struct operator_;
43+
4044
// Register a to_python converter for a class T, depending on the
4145
// type of the first (tag) argument. The 2nd argument is a pointer
4246
// to the type of holder that must be created. The 3rd argument is a
@@ -117,6 +121,18 @@ class class_ : public objects::class_base
117121

118122
return *this;
119123
}
124+
125+
template <detail::operator_id id, class L, class R>
126+
self& def(detail::operator_<id,L,R> const& op)
127+
{
128+
typedef detail::operator_<id,L,R> op_t;
129+
// Use function::add_to_namespace to achieve overloading if
130+
// appropriate.
131+
objects::function::add_to_namespace(
132+
this->object(), op.name(),
133+
ref(detail::wrap_function(&op_t::template apply<T>::execute)));
134+
return *this;
135+
}
120136

121137
// Define the constructor with the given Args, which should be an
122138
// MPL sequence of types.

include/boost/python/converter/builtin_converters.hpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# include <boost/python/detail/wrap_python.hpp>
99
# include <boost/python/detail/none.hpp>
1010
# include <boost/python/reference.hpp>
11+
# include <boost/python/converter/callback_to_python_base.hpp>
1112
# include <string>
1213
# include <complex>
1314

@@ -33,40 +34,37 @@ namespace converter
3334
BOOST_PYTHON_DECL PyObject* do_callback_to_python(PyObject*);
3435
}
3536

36-
# define BOOST_PYTHON_CALL_TO_PYTHON_BY_VALUE(T, expr) \
37-
template <> struct to_python_value<T&> \
38-
: detail::builtin_to_python \
39-
{ \
40-
PyObject* operator()(T const& x) const \
41-
{ \
42-
return (expr); \
43-
} \
44-
}; \
45-
template <> struct to_python_value<T const&> \
46-
: detail::builtin_to_python \
47-
{ \
48-
PyObject* operator()(T const& x) const \
49-
{ \
50-
return (expr); \
51-
} \
37+
# define BOOST_PYTHON_CALL_TO_PYTHON_BY_VALUE(T, expr) \
38+
template <> struct to_python_value<T&> \
39+
: detail::builtin_to_python \
40+
{ \
41+
PyObject* operator()(T const& x) const \
42+
{ \
43+
return (expr); \
44+
} \
45+
}; \
46+
template <> struct to_python_value<T const&> \
47+
: detail::builtin_to_python \
48+
{ \
49+
PyObject* operator()(T const& x) const \
50+
{ \
51+
return (expr); \
52+
} \
5253
};
5354

54-
# define BOOST_PYTHON_CALLBACK_TO_PYTHON_BY_VALUE(T, expr) \
55-
namespace converter \
56-
{ \
57-
template <> struct callback_to_python< T > \
58-
{ \
59-
callback_to_python(T const& x) \
60-
: m_held(expr) {} \
61-
PyObject* get() const \
62-
{ return m_held.get(); } \
63-
private: \
64-
ref m_held; \
65-
}; \
55+
# define BOOST_PYTHON_CALLBACK_TO_PYTHON_BY_VALUE(T, expr) \
56+
namespace converter \
57+
{ \
58+
template <> struct callback_to_python< T > \
59+
: detail::callback_to_python_holder \
60+
{ \
61+
callback_to_python(T const& x) \
62+
: detail::callback_to_python_holder(expr) {} \
63+
}; \
6664
}
6765

68-
# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr) \
69-
BOOST_PYTHON_CALL_TO_PYTHON_BY_VALUE(T,expr) \
66+
# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr) \
67+
BOOST_PYTHON_CALL_TO_PYTHON_BY_VALUE(T,expr) \
7068
BOOST_PYTHON_CALLBACK_TO_PYTHON_BY_VALUE(T,expr)
7169

7270
# define BOOST_PYTHON_TO_INT(T) \

include/boost/python/converter/callback_to_python_base.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace detail
1616
struct callback_to_python_holder
1717
{
1818
callback_to_python_holder(PyObject* obj);
19-
inline PyObject* get() const;
19+
PyObject* get() const;
20+
PyObject* get_incref() const;
2021
private:
2122
ref m_held;
2223
};
@@ -38,6 +39,13 @@ namespace detail
3839
{
3940
return m_held.get();
4041
}
42+
43+
inline PyObject* callback_to_python_holder::get_incref() const
44+
{
45+
PyObject* result = m_held.get();
46+
Py_XINCREF(result);
47+
return result;
48+
}
4149
}
4250

4351
}}} // namespace boost::python::converter

include/boost/python/detail/msvc_typeinfo.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ template <bool> struct bool_t{};
5757
template <class T>
5858
inline typeinfo typeid_nonref(boost::type<T>* = 0)
5959
{
60-
BOOST_STATIC_CONSTANT(bool, c = is_const<T>::value);
61-
BOOST_STATIC_CONSTANT(bool, v = is_volatile<T>::value);
60+
bool const c = is_const<T>::value;
61+
bool const v = is_volatile<T>::value;
6262
return value_id_accessor<(2 * v + c)>::get((T*)0);
6363
}
6464

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 OPERATOR_ID_DWA2002531_HPP
7+
# define OPERATOR_ID_DWA2002531_HPP
8+
9+
namespace boost { namespace python { namespace detail {
10+
11+
enum operator_id
12+
{
13+
op_add,
14+
op_sub,
15+
op_mul,
16+
op_div,
17+
op_mod,
18+
op_divmod,
19+
op_pow,
20+
op_lshift,
21+
op_rshift,
22+
op_and,
23+
op_xor,
24+
op_or,
25+
op_neg,
26+
op_pos,
27+
op_abs,
28+
op_invert,
29+
op_int,
30+
op_long,
31+
op_float,
32+
op_str,
33+
op_cmp,
34+
op_gt,
35+
op_ge,
36+
op_lt,
37+
op_le,
38+
op_eq,
39+
op_ne,
40+
op_iadd,
41+
op_isub,
42+
op_imul,
43+
op_idiv,
44+
op_imod,
45+
op_ilshift,
46+
op_irshift,
47+
op_iand,
48+
op_ixor,
49+
op_ior
50+
};
51+
52+
}}} // namespace boost::python::detail
53+
54+
#endif // OPERATOR_ID_DWA2002531_HPP

0 commit comments

Comments
 (0)