My Project  v0.0.16
converters_exceptions.hpp
Go to the documentation of this file.
1 /*
2  * File: converters.h
3  * Author: ale
4  *
5  * Created on August 20, 2013, 4:28 AM
6  */
7 
8 #ifndef _mp7_python_converters_h_
9 #define _mp7_python_converters_h_
10 
11 // C++ Headers
12 #include <vector>
13 #include <map>
14 
15 // Boost Headers
16 #include "boost/unordered_map.hpp"
17 #include "boost/python.hpp"
18 #include "boost/python/converter/rvalue_from_python_data.hpp"
19 
20 namespace pycomp7
21 {
22 
23  //----------------------------------------------//
24  // --- Converter_pair_uint32_bool_from_tuple ---//
25  //----------------------------------------------//
26 
27  struct Converter_pair_uint32_bool_from_tuple
28  {
29  // Default CTOR. Registers this converter to boost::python
30 
32  {
33  boost::python::converter::registry::push_back ( &convertible, &construct, boost::python::type_id < std::pair<uint32_t, bool > >() );
34  }
35 
36  // Determine if obj_ptr can be converted to vector<uint32_t>
37  static void* convertible ( PyObject* obj_ptr );
38 
39  // Convert obj_ptr to a C++ vector<uint32_t>
40  static void construct ( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data );
41  };
42 
43  //------------------//
44  // --- Templates ---//
45  //------------------//
46 
47  template<class T>
48  struct Converter_std_vector_from_list
49  {
50  // Default CTOR. Registers this converter to boost::python
51 
53  {
54  boost::python::converter::registry::push_back ( &convertible, &construct, boost::python::type_id< std::vector<T> >() );
55  }
56 
57  // Determine if obj_ptr can be converted to vector<T>
58  static void* convertible ( PyObject* obj_ptr );
59 
60  // Convert obj_ptr to a C++ vector<T>
61  static void construct ( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data );
62  };
63 
64  template <class T>
65  struct Converter_std_vector_to_list
66  {
67  static PyObject* convert ( const std::vector<T>& v );
68  };
69 
70  template <class U, class T>
71  struct Converter_std_map_to_dict
72  {
73  static PyObject* convert ( const std::map<U, T>& m );
74  };
75 
76  template <class U, class T>
77  struct Converter_boost_unorderedmap_to_dict
78  {
79  static PyObject* convert ( const boost::unordered_map<U, T>& m );
80  };
81 
82  template<class T1, class T2>
83  struct PairToTupleConverter
84  {
85  static PyObject* convert ( const std::pair<T1, T2>& pair );
86  };
87 
88  void register_converters();
89 
90  PyObject* create_exception_class ( const std::string& excName, PyObject* baseTypeObj = PyExc_Exception );
91 
94  template<class ExceptionType>
95  class ExceptionTranslator
96  {
97  public:
98  ExceptionTranslator ( PyObject* exception_pyType );
99 
101  void operator() ( const ExceptionType& e ) const;
102 
103  private:
104  PyObject* exception_pyType_;
105  };
106 
107 
108  template<class ExceptionType>
109  void wrap_derived_exception ( const std::string& exceptionName, PyObject* base_exception_pyType )
110  {
111  PyObject* derived_exception_pyType = pycomp7::create_exception_class ( exceptionName, base_exception_pyType );
112  boost::python::register_exception_translator<ExceptionType> ( pycomp7::ExceptionTranslator<ExceptionType> ( derived_exception_pyType ) );
113  }
114 
115  void wrap_exceptions();
116 
117 }
118 //----------------------------------------//
119 // --- Converter_std_map_to_dict --- //
120 //----------------------------------------//
121 
122 template <class U, class T>
123 PyObject* pycomp7::Converter_std_map_to_dict<U,T>::convert ( const std::map<U, T>& m )
124 {
125  namespace bpy = boost::python;
126  bpy::dict theDict;
127 
128  for ( typename std::map<U, T>::const_iterator it = m.begin(); it != m.end(); it++ )
129  {
130  theDict[it->first] = bpy::object ( it->second );
131  }
132 
133  return bpy::incref ( theDict.ptr() );
134 }
135 
136 //----------------------------------------//
137 // --- Converter_boost_unorderedmap_to_dict --- //
138 //----------------------------------------//
139 template <class U, class T>
140 PyObject* pycomp7::Converter_boost_unorderedmap_to_dict<U,T>::convert ( const boost::unordered_map<U, T>& m )
141 {
142  namespace bpy = boost::python;
143  bpy::dict theDict;
144 
145  for ( typename boost::unordered_map<U, T>::const_iterator it = m.begin(); it != m.end(); it++ )
146  {
147  theDict[it->first] = bpy::object ( it->second );
148  }
149 
150  return bpy::incref ( theDict.ptr() );
151 }
152 
153 
154 //------------------------------------------//
155 // --- Converter_std_vector_from_list --- //
156 //------------------------------------------//
157 
158 template <class T>
160 {
161  if ( !PyList_Check ( obj_ptr ) )
162  {
163  return 0;
164  }
165  else
166  {
167  return obj_ptr;
168  }
169 }
170 
171 template <class T>
172 void pycomp7::Converter_std_vector_from_list<T>::construct ( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data )
173 {
174  namespace bpy = boost::python;
175  // Grab pointer to memory in which to construct the new vector
176  void* storage = ( ( bpy::converter::rvalue_from_python_storage< std::vector<T> >* ) data )->storage.bytes;
177  // Grab list object from obj_ptr
178  bpy::list py_list ( bpy::handle<> ( bpy::borrowed ( obj_ptr ) ) );
179  // Construct vector in requested location, and set element values.
180  // boost::python::extract will throw appropriate exception if can't convert to type T ; boost::python will then call delete itself as well.
181  size_t nItems = bpy::len ( py_list );
182  std::vector<T>* vec_ptr = new ( storage ) std::vector<T> ( nItems );
183 
184  for ( size_t i = 0; i < nItems; i++ )
185  {
186  vec_ptr->at ( i ) = bpy::extract<T> ( py_list[i] );
187  }
188 
189  // If successful, then register memory chunk pointer for later use by boost.python
190  data->convertible = storage;
191 }
192 
193 //----------------------------------------//
194 // --- Converter_std_vector_to_list --- //
195 //----------------------------------------//
196 
197 template <class T>
198 PyObject* pycomp7::Converter_std_vector_to_list<T>::convert ( const std::vector<T>& vec )
199 {
200  namespace bpy = boost::python;
201  bpy::list theList;
202 
203  for ( typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); it++ )
204  {
205  theList.append ( bpy::object ( *it ) );
206  }
207 
208  return bpy::incref ( theList.ptr() );
209 }
210 
211 //----------------------------------------//
212 // --- Converter_std_pair_to_tuple --- //
213 //----------------------------------------//
214 
215 template<class T1, class T2>
216 PyObject* pycomp7::PairToTupleConverter<T1, T2>::convert ( const std::pair<T1, T2>& pair )
217 {
218  namespace bpy = boost::python;
219  return bpy::incref ( bpy::make_tuple ( pair.first, pair.second ).ptr() );
220 }
221 
222 
223 //-------------------------------//
224 // --- ExceptionTranslator --- //
225 //-------------------------------//
226 // TODO: Copied from pycohal converters_exceptions.cpp. Should use that instead
227 template <class ExceptionType>
229  exception_pyType_ ( exception_pyType )
230 { }
231 
232 
233 template <class ExceptionType>
235 {
236  namespace bpy = boost::python;
237  bpy::object pyException ( bpy::handle<> ( bpy::borrowed ( exception_pyType_ ) ) );
238  pyException.attr ( "what" ) = e.what();
239  PyErr_SetObject ( exception_pyType_, pyException.ptr() );
240 }
241 
242 #endif /* _mp7_python_converters_h_ */
243 
244 
void wrap_exceptions()
Definition: converters_exceptions.cpp:116
PyObject* create_exception_class( const std::string& excName, PyObject* baseTypeObj = PyExc_Exception )
Definition: converters_exceptions.cpp:100
static PyObject* convert( const boost::unordered_map<U, T>& m )
Definition: converters_exceptions.hpp:140
v
Definition: mp7-test-ipbusaccess.py:58
static void construct( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data )
Definition: converters_exceptions.hpp:172
static PyObject* convert( const std::vector<T>& v )
Definition: converters_exceptions.hpp:198
void operator()( const ExceptionType& e ) const
Translation function called at the C-python boundary.
Definition: converters_exceptions.hpp:234
static PyObject* convert( const std::map<U, T>& m )
Definition: converters_exceptions.hpp:123
ExceptionTranslator( PyObject* exception_pyType )
Definition: converters_exceptions.hpp:228
void wrap_derived_exception( const std::string& exceptionName, PyObject* base_exception_pyType )
Definition: converters_exceptions.hpp:109
PyObject* exception_pyType_
Pointer to PyObject corresponding to C++ exception class ExceptionType.
Definition: converters_exceptions.hpp:104
void register_converters()
Definition: converters_exceptions.cpp:44
static PyObject* convert( const std::pair<T1, T2>& pair )
Definition: converters_exceptions.hpp:216
static void* convertible( PyObject* obj_ptr )
Definition: converters_exceptions.cpp:16
Definition: converters_exceptions.hpp:20
Converter_std_vector_from_list()
Definition: converters_exceptions.hpp:52
Definition: converters_exceptions.hpp:95
def convert( string)
Definition: cli_utils.py:4
static void construct( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data )
Definition: converters_exceptions.cpp:28
dictionary data
Definition: test-datavalid.py:31
Converter_pair_uint32_bool_from_tuple()
Definition: converters_exceptions.hpp:31
static void* convertible( PyObject* obj_ptr )
Definition: converters_exceptions.hpp:159