MIRA
BoostOptionalWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
60 #ifndef _MIRA_TOOLBOXES_PYTHON_BOOSTOPTIONALWRAPPER_H_
61 #define _MIRA_TOOLBOXES_PYTHON_BOOSTOPTIONALWRAPPER_H_
62 
64 
65 namespace mira { namespace python {
66 
68 
69 template <typename T, typename TfromPy>
71 {
73  {
74  boost::python::converter::registry::push_back(&TfromPy::convertible, &TfromPy::construct, boost::python::type_id<T>());
75  }
76 };
77 
78 template <typename T, typename TtoPy, typename TfromPy>
80 {
82  {
83  boost::python::to_python_converter<T, TtoPy>();
85  }
86 };
87 
88 template <typename T>
89 struct python_optional : public boost::noncopyable
90 {
92  {
93  static PyObject * convert(const boost::optional<T>& value)
94  {
95  return (value ? boost::python::incref(boost::python::object(value.get()).ptr()) : boost::python::detail::none());
96  }
97  };
98 
100  {
101  static void * convertible(PyObject * source)
102  {
103  using namespace boost::python::converter;
104 
105  if (source == Py_None) return source;
106 
107  const registration& converters(registered<T>::converters);
108 
109  if (implicit_rvalue_convertible_from_python(source, converters))
110  {
111  rvalue_from_python_stage1_data data = rvalue_from_python_stage1(source, converters);
112  return rvalue_from_python_stage2(source, data, converters);
113  }
114  return nullptr;
115  }
116 
117  static void construct(PyObject * source, boost::python::converter::rvalue_from_python_stage1_data * data)
118  {
119  using namespace boost::python::converter;
120 
121  void * const storage = ((rvalue_from_python_storage<T> *)data)->storage.bytes;
122 
123  if (data->convertible == source) // == None
124  {
125  new (storage) boost::optional<T>(); // A Boost uninitialized value
126  }
127  else
128  {
129  new (storage) boost::optional<T>(*static_cast<T *>(data->convertible));
130  }
131 
132  data->convertible = storage;
133  }
134  };
135 
136  explicit python_optional()
137  {
139  }
140 };
141 
143 
144 }} // namespace
145 
146 #endif
static void * convertible(PyObject *source)
Definition: BoostOptionalWrapper.h:101
Definition: BoostOptionalWrapper.h:70
static PyObject * convert(const boost::optional< T > &value)
Definition: BoostOptionalWrapper.h:93
Definition: BoostOptionalWrapper.h:89
python_optional()
Definition: BoostOptionalWrapper.h:136
Definition: BoostOptionalWrapper.h:91
Definition: BoostOptionalWrapper.h:79
Definition: BoostOptionalWrapper.h:99
object_from_python()
Definition: BoostOptionalWrapper.h:72
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
static void construct(PyObject *source, boost::python::converter::rvalue_from_python_stage1_data *data)
Definition: BoostOptionalWrapper.h:117
register_python_conversion()
Definition: BoostOptionalWrapper.h:81