Free Electron
Convertor.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __pyfe_Convertor_h__
8 #define __pyfe_Convertor_h__
9 /* This is separated from BaseConvertor and the rest of pyfe since it is
10  dependent on boost.python and is just for convienience.
11  */
12 #include <boost/python.hpp>
13 using namespace boost::python;
14 
15 namespace pyfe
16 {
17 template<class rawtype, class bindtype>
18 class Convertor : public BaseConvertor
19 {
20  public:
21  Convertor(object boostclass)
22  {
23  m_boostclass = boostclass;
24  }
25 virtual ~Convertor(void) { }
26 virtual PyObject *instanceToPyObject(fe::sp<fe::BaseType> spBT, fe::Instance &instance)
27  {
28  if(instance.type()->typeinfo() == spBT->typeinfo())
29  {
30  object o = m_boostclass();
31  extract<bindtype&> ex(o);
32  bindtype &bt = ex();
33 
34  bt.assign( *(reinterpret_cast<rawtype *>(instance.data())));
35  Py_INCREF(o.ptr());
36  return o.ptr();
37  }
38  return Py_BuildValue("");
39  }
40 virtual bool PyObjectToInstance(fe::sp<fe::BaseType> spBT, PyObject *pObject, fe::Instance &instance)
41  {
42  handle<> h(borrowed(pObject));
43  object o(h);
44  extract<bindtype&> x(o);
45  if(!x.check())
46  {
47  return false;
48  }
49  bindtype &bt = x();
50  rawtype *pR = bt.rawptr();
51  fe::Instance i(reinterpret_cast<void *>(pR),spBT,NULL);
52  instance = i;
53  return true;
54  }
55  private:
56  object m_boostclass;
57 };
58 
59 } /* namepsace */
60 
61 #endif /* __pyfe_Convertor_h__ */
Smart pointer used with types represented by BaseType.
Definition: Instance.h:28
python binding
Definition: namespace.dox:7