Skip to content

Commit 6a75fa8

Browse files
committed
New conversion methods, builtin converters
---------------------------------------------------------------------- Committing in . Modified Files: boost/python/reference_from_python.hpp boost/python/value_from_python.hpp boost/python/converter/body.hpp boost/python/converter/handle.hpp libs/python/src/converter/builtin_converters.cpp libs/python/test/m1.cpp libs/python/test/m2.cpp Added Files: boost/python/converter/builtin_converters.hpp boost/python/converter/builtin_to_python_converters.hpp boost/python/converter/from_python.hpp boost/python/converter/from_python_data.hpp boost/python/converter/from_python_function.hpp boost/python/converter/to_python.hpp boost/python/converter/to_python_function.hpp boost/python/object/auto_ptr_generator.hpp boost/python/object/pointer_holder.hpp libs/python/src/converter/from_python.cpp libs/python/src/converter/to_python.cpp libs/python/test/test_builtin_converters.cpp libs/python/test/test_builtin_converters.py Removed Files: boost/python/convert.hpp boost/python/converter/unwrap.hpp boost/python/converter/unwrapper.hpp boost/python/converter/wrap.hpp boost/python/converter/wrapper.hpp boost/python/object/class_unwrapper.hpp ---------------------------------------------------------------------- [SVN r12596]
1 parent 88a8721 commit 6a75fa8

26 files changed

+1255
-657
lines changed

include/boost/python/convert.hpp

Lines changed: 0 additions & 84 deletions
This file was deleted.

include/boost/python/converter/body.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace boost { namespace python { namespace converter {
1212

13-
struct BOOST_PYTHON_DECL handle;
14-
1513
namespace registry
1614
{
1715
class entry;
@@ -23,9 +21,6 @@ struct BOOST_PYTHON_DECL body
2321
body(type_id_t key);
2422
virtual ~body() {}
2523

26-
// default implementation is a no-op
27-
virtual void destroy_handle(handle*) const;
28-
2924
type_id_t key() const;
3025

3126
protected:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 BUILTIN_CONVERTERS_DWA2002124_HPP
7+
# define BUILTIN_CONVERTERS_DWA2002124_HPP
8+
9+
namespace boost { namespace python { namespace converter {
10+
11+
void initialize_builtin_converters();
12+
13+
}}} // namespace boost::python::converter
14+
15+
#endif // BUILTIN_CONVERTERS_DWA2002124_HPP
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 BUILTIN_TO_PYTHON_CONVERTERS_DWA2002129_HPP
7+
# define BUILTIN_TO_PYTHON_CONVERTERS_DWA2002129_HPP
8+
# include <string>
9+
# include <boost/python/detail/wrap_python.hpp>
10+
11+
namespace boost { namespace python { namespace converter {
12+
13+
template <class T> struct to_python_lookup;
14+
15+
template <class T>
16+
struct to_python_int
17+
{
18+
bool convertible() const { return true; }
19+
PyObject* operator()(T x) const { return PyInt_FromLong(long(x)); }
20+
};
21+
22+
# define BOOST_PYTHON_TO_INT(T) \
23+
template <> struct to_python_lookup<signed T const&> : to_python_int<signed T const&> {}; \
24+
template <> struct to_python_lookup<unsigned T const&> : to_python_int<unsigned T const&> {};
25+
26+
BOOST_PYTHON_TO_INT(char)
27+
BOOST_PYTHON_TO_INT(short)
28+
BOOST_PYTHON_TO_INT(int)
29+
BOOST_PYTHON_TO_INT(long)
30+
# undef BOOST_TO_PYTHON_INT
31+
32+
template <>
33+
struct to_python_lookup<char const*const&>
34+
{
35+
bool convertible() const { return true; }
36+
PyObject* operator()(char const* x) const { return PyString_FromString(x); }
37+
};
38+
39+
template <>
40+
struct to_python_lookup<std::string const&>
41+
{
42+
bool convertible() const { return true; }
43+
PyObject* operator()(std::string const& x) const
44+
{
45+
return PyString_FromString(x.c_str());
46+
}
47+
};
48+
49+
template <>
50+
struct to_python_lookup<float const&>
51+
{
52+
bool convertible() const { return true; }
53+
PyObject* operator()(float x) const { return PyFloat_FromDouble(x); }
54+
};
55+
56+
template <>
57+
struct to_python_lookup<double const&>
58+
{
59+
bool convertible() const { return true; }
60+
PyObject* operator()(double x) const { return PyFloat_FromDouble(x); }
61+
};
62+
63+
template <>
64+
struct to_python_lookup<long double const&>
65+
{
66+
bool convertible() const { return true; }
67+
PyObject* operator()(long double x) const
68+
{
69+
return PyFloat_FromDouble(x);
70+
}
71+
};
72+
73+
}}} // namespace boost::python::converter
74+
75+
#endif // BUILTIN_TO_PYTHON_CONVERTERS_DWA2002129_HPP
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 FROM_PYTHON_DWA2002127_HPP
7+
# define FROM_PYTHON_DWA2002127_HPP
8+
9+
# include <boost/python/detail/config.hpp>
10+
# include <boost/python/converter/body.hpp>
11+
# include <boost/python/converter/from_python_function.hpp>
12+
# include <boost/python/converter/from_python_data.hpp>
13+
# include <boost/python/converter/type_id.hpp>
14+
# include <boost/python/converter/registration.hpp>
15+
# include <boost/python/detail/wrap_python.hpp>
16+
17+
namespace boost { namespace python { namespace converter {
18+
19+
// The type of convertibility checking functions
20+
typedef void* (*from_python_check)(PyObject*);
21+
typedef void (*from_python_destructor)(from_python_data&);
22+
23+
// forward declaration
24+
template <class T> struct from_python_lookup;
25+
26+
// from_python --
27+
// A body class representing a conversion from python to C++.
28+
29+
struct BOOST_PYTHON_DECL from_python_converter_base : body
30+
{
31+
from_python_converter_base(type_id_t, from_python_check); // registers
32+
~from_python_converter_base(); // unregisters
33+
34+
// Must return non-null iff the conversion will be successful. Any
35+
// non-null pointer is acceptable, and will be passed on to the
36+
// convert() function, so useful data can be stored there.
37+
inline void* convertible(PyObject*) const;
38+
// inline type_id_t key() const;
39+
private:
40+
// type_id_t m_key;
41+
from_python_check m_convertible;
42+
};
43+
44+
45+
template <class T>
46+
struct from_python_converter : from_python_converter_base
47+
{
48+
public: // types
49+
typedef typename from_python_function<T>::type conversion_function;
50+
51+
public: // member functions
52+
from_python_converter(from_python_check, conversion_function, from_python_destructor = 0);
53+
T convert(PyObject*, from_python_data&) const;
54+
void destroy(from_python_data&) const;
55+
56+
private: // data members
57+
conversion_function m_convert;
58+
from_python_destructor m_destroy;
59+
};
60+
61+
// -------------------------------------------------------------------------
62+
63+
//struct from_python_base
64+
//{
65+
//};
66+
67+
// A class which implements from_python with a registry lookup.
68+
template <class T>
69+
struct from_python_lookup // : from_python_base
70+
{
71+
public: // types
72+
73+
public: // member functions
74+
from_python_lookup(PyObject* source);
75+
~from_python_lookup();
76+
77+
bool convertible() const;
78+
T operator()(PyObject*);
79+
80+
public: // functions for use by conversion implementations
81+
// Get the converter object
82+
from_python_converter<T> const* converter() const;
83+
84+
private: // data members
85+
typedef typename from_python_intermediate_data<T>::type intermediate_t;
86+
mutable intermediate_t m_intermediate_data;
87+
from_python_converter<T> const* m_converter;
88+
};
89+
90+
//
91+
// implementations
92+
//
93+
inline void* from_python_converter_base::convertible(PyObject* o) const
94+
{
95+
return m_convertible(o);
96+
}
97+
98+
# if 0
99+
inline type_id_t from_python_converter_base::key() const
100+
{
101+
return m_key;
102+
}
103+
# endif
104+
105+
template <class T>
106+
inline from_python_converter<T>::from_python_converter(
107+
from_python_check checker
108+
, conversion_function converter
109+
, from_python_destructor destructor // = 0
110+
)
111+
: from_python_converter_base(type_id<T>(), checker)
112+
, m_convert(converter)
113+
, m_destroy(destructor)
114+
{
115+
116+
}
117+
118+
template <class T>
119+
inline T from_python_converter<T>::convert(PyObject* src, from_python_data& data) const
120+
{
121+
return this->m_convert(src, data);
122+
}
123+
124+
template <class T>
125+
inline void from_python_converter<T>::destroy(from_python_data& data) const
126+
{
127+
if (this->m_destroy)
128+
{
129+
this->m_destroy(data);
130+
}
131+
}
132+
133+
template <class T>
134+
inline from_python_lookup<T>::from_python_lookup(PyObject* src)
135+
: m_converter(
136+
registration<T>::get_from_python(
137+
src, m_intermediate_data.stage1))
138+
{
139+
}
140+
141+
template <class T>
142+
inline from_python_lookup<T>::~from_python_lookup()
143+
{
144+
if (m_converter != 0)
145+
m_converter->destroy(m_intermediate_data);
146+
}
147+
148+
template <class T>
149+
inline bool from_python_lookup<T>::convertible() const
150+
{
151+
return this->m_converter != 0;
152+
}
153+
154+
template <class T>
155+
inline T from_python_lookup<T>::operator()(PyObject* obj)
156+
{
157+
return this->m_converter->convert(obj, m_intermediate_data);
158+
}
159+
160+
template <class T>
161+
inline from_python_converter<T> const*
162+
from_python_lookup<T>::converter() const
163+
{
164+
return this->m_converter;
165+
}
166+
167+
}}} // namespace boost::python::converter
168+
169+
#endif // FROM_PYTHON_DWA2002127_HPP

0 commit comments

Comments
 (0)