Loading...
Searching...
No Matches
pyutils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * @addtogroup python
34 */
35
44#pragma once
45
46namespace ledger { namespace python {
47
48template <typename T, typename TfromPy>
50{
52 boost::python::converter::registry::insert
53 (&TfromPy::convertible, &TfromPy::construct,
54 boost::python::type_id<T>());
55 }
56};
57
58template <typename T, typename TtoPy, typename TfromPy>
60{
62 boost::python::to_python_converter<T, TtoPy>();
64 }
65};
66
67template <typename T>
68struct register_optional_to_python : public boost::noncopyable
69{
71 {
72 static PyObject * convert(const boost::optional<T>& value)
73 {
74 return boost::python::incref
75 (value ? boost::python::to_python_value<T>()(*value) :
76 boost::python::detail::none());
77 }
78 };
79
81 {
82 static void * convertible(PyObject * source)
83 {
84 using namespace boost::python::converter;
85
86 if (source == Py_None)
87 return source;
88
89 const registration& converters(registered<T>::converters);
90
94 return rvalue_from_python_stage2(source, data, converters);
95 }
96 return NULL;
97 }
98
99 static void construct(PyObject * source,
100 boost::python::converter::rvalue_from_python_stage1_data * data)
101 {
102 using namespace boost::python::converter;
103
104 const T value = typename boost::python::extract<T>(source);
105
106 void * storage = ((rvalue_from_python_storage<boost::optional<T>>*) data)->storage.bytes;
107
108 if (source == Py_None) // == None
109 new (storage) boost::optional<T>(); // A Boost uninitialized value
110 else
111 new (storage) boost::optional<T>(value);
112
113 data->convertible = storage;
114 }
115 };
116
121};
122
123template <typename T1, typename T2>
125{
126 static PyObject * convert(const std::pair<T1, T2>& pair) {
127 return boost::python::incref
128 (boost::python::make_tuple(pair.first, pair.second).ptr());
129 }
130};
131
132template <typename MapType>
134{
136 boost::python::to_python_converter
137 <typename MapType::value_type,
138 PairToTupleConverter<const typename MapType::key_type,
139 typename MapType::mapped_type> >();
140 }
141};
142
143template <typename T>
145{
146 using namespace boost::python;
147 PyObject * uni = PyUnicode_FromString(str.c_str());
148 return object(handle<>(borrowed(uni))).ptr();
149}
150
151} } // namespace ledger::python
152
153namespace boost { namespace python {
154
155// Use expr to create the PyObject corresponding to x
156# define BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T, expr, pytype)\
157 template <> struct to_python_value<T&> \
158 : detail::builtin_to_python \
159 { \
160 inline PyObject* operator()(T const& x) const \
161 { \
162 return (expr); \
163 } \
164 inline PyTypeObject const* get_pytype() const \
165 { \
166 return (pytype); \
167 } \
168 }; \
169 template <> struct to_python_value<T const&> \
170 : detail::builtin_to_python \
171 { \
172 inline PyObject* operator()(T const& x) const \
173 { \
174 return (expr); \
175 } \
176 inline PyTypeObject const* get_pytype() const \
177 { \
178 return (pytype); \
179 } \
180 };
181
182# define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr) \
183 namespace converter \
184 { \
185 template <> struct arg_to_python< T > \
186 : handle<> \
187 { \
188 arg_to_python(T const& x) \
189 : boost::python::handle<>(expr) {} \
190 }; \
191 }
192
193// Specialize argument and return value converters for T using expr
194# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr, pytype) \
195 BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr, pytype) \
196 BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr)
197
198} } // namespace boost::python
199
200//boost::python::register_ptr_to_python< boost::shared_ptr<Base> >();
T & downcast(U &object)
Definition utils.h:468
PyObject * str_to_py_unicode(const T &str)
Definition pyutils.h:144
static PyObject * convert(const boost::optional< T > &value)
Definition pyutils.h:72
static void construct(PyObject *source, boost::python::converter::rvalue_from_python_stage1_data *data)
Definition pyutils.h:99
static PyObject * convert(const std::pair< T1, T2 > &pair)
Definition pyutils.h:126