부스트 사용해서 래퍼 작성

c++에서 struct의 vector인 애를 받아서 python 의 list로 변환해주기



1.C++벡터를 파이썬 리스트로 변환

http://stackoverflow.com/questions/6157409/stdvector-to-boostpythonlist





2. 원하는 스트럭트의 wrapper를 작성


C++ struct to Python object

(원문 http://boost.2283326.n4.nabble.com/Boost-Python-to-python-converter-td4529040.html )



#include <boost/python.hpp> 
#include <string> 
struct Unit 

   int health; 
   std::string name; 
   std::string type; 
   std::pair<int,int> coord; 
}; 

struct Unit_to_python 

   static PyObject* convert(Unit const& unit) 
   { 
      return boost::python::incref(boost::python::object(unit).ptr()); 
   } 
}; 

BOOST_PYTHON_MODULE(game) 

   boost::python::class_<Unit>("Unit") 
      .def_readwrite("health", &Unit::health) 
      .def_readwrite("name", &Unit::name) 
      .def_readwrite("type", &Unit::type) 
      .def_readwrite("coord", &Unit::coord) 
   ; 


int main(int argc, char** argv) 

   Py_Initialize(); 
   boost::python::to_python_converter<Unit, Unit_to_python>(); 
   Unit unit1; 
   unit1.health = 100; 
   unit1.name = "Tank"; 
   unit1.type = "Armor"; 
   boost::python::object foo(unit1); // crash: stack overflow 
   return 0; 

___

'COMPUTER' 카테고리의 다른 글

정규 표현식-메타문자사용하기  (0) 2014.02.18
pip, easy_install 설치  (0) 2014.02.07
spectrogram, cepstrum, mfcc 설명 잘 되어있는 슬라이드  (0) 2013.08.19
CUDA 지원 Nvidia GPU list  (0) 2013.05.21
Eclipse Python Setting  (0) 2013.04.29

+ Recent posts