IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
PythonStdPairUtils.h
Go to the documentation of this file.
1 // PythonStdPairUtils.h:
3 // ---------------------
4 //
14 
15 #ifndef __PYIPSDKBASE_PYTHONSTDPAIRUTILS_H__
16 #define __PYIPSDKBASE_PYTHONSTDPAIRUTILS_H__
17 
18 #include <vector>
19 #include <boost/python/iterator.hpp>
20 #include <boost/python/self.hpp>
21 #include <boost/python/operators.hpp>
22 #include <boost/python/tuple.hpp>
23 #include <iostream>
24 
27 
28 namespace std {
29 
30  // default ostream operator for std::pair
31  template<typename T1, typename T2>
32  static std::ostream&
33  operator<< (std::ostream& os, const std::pair<T1, T2>& pairData)
34  {
35  os << "{" << pairData.first << ", " << pairData.second << "}";
36 
37  return os;
38  }
39 }
40 
43 
44 namespace ipsdk {
45 namespace python {
46 
49 
50 // from https://stackoverflow.com/questions/16497889/how-to-expose-stdpair-to-python-using-boostpython/41593748
51 template<typename T1, typename T2>
53 {
54  static PyObject* convert(const std::pair<T1, T2>& pair)
55  {
56  return boost::python::incref(boost::python::make_tuple(pair.first, pair.second).ptr());
57  }
58 };
59 
60 template<typename T1, typename T2>
62 {
63  static void* convertible(PyObject* obj)
64  {
65  if (!PyTuple_CheckExact(obj)) return 0;
66  if (PyTuple_Size(obj) != 2) return 0;
67  return obj;
68  }
69 
70  static void construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data)
71  {
72  boost::python::tuple tuple(boost::python::borrowed(obj));
73  void* storage = ((boost::python::converter::rvalue_from_python_storage<std::pair<T1, T2> >*) data)->storage.bytes;
74  new (storage) std::pair<T1, T2>(boost::python::extract<T1>(tuple[0]), boost::python::extract<T2>(tuple[1]));
75  data->convertible = storage;
76  }
77 };
78 
79 // export pair
80 template<typename PairType>
81 void
82 export_pair(const char* py_name)
83 {
84  typedef typename PairType::first_type T1;
85  typedef typename PairType::second_type T2;
86  boost::python::type_info info = boost::python::type_id<PairType>();
87  const boost::python::converter::registration* reg = boost::python::converter::registry::query(info);
88  if (reg == 0 || reg->m_to_python == 0) {
89 
90  boost::python::to_python_converter<PairType, PairToPythonConverter<T1, T2> > toPython;
91  boost::python::converter::registry::push_back(
94  boost::python::type_id<PairType>());
95  }
96 }
97 
100 
101 } // end of namespace python
102 } // end of namespace ipsdk
103 
104 #endif // __PYIPSDKBASE_PYTHONSTDPAIRUTILS_H__
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: PythonStdPairUtils.h:61
Definition: PythonStdPairUtils.h:52
STL namespace.