Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #pragma once
  2. #include<iostream>
  3. #include"pybind11pybind11.h"
  4.  
  5. namespace py = pybind11;
  6.  
  7. class Test
  8. {
  9. public:
  10. Test(const std::string &s);
  11. ~Test();
  12.  
  13. void printStr();
  14.  
  15. private:
  16. std::string _s;
  17. };
  18.  
  19. #include "Test.h"
  20.  
  21. PYBIND11_MODULE(TestModule, m)
  22. {
  23. py::class_<Test>(m, "Test")
  24. .def(py::init<const std::string &>())
  25. .def("printStr", &Test::printStr);
  26. }
  27.  
  28.  
  29. Test::Test(const std::string &s) : _s(s)
  30. {
  31.  
  32. }
  33. Test::~Test()
  34. {
  35.  
  36. }
  37.  
  38. void Test::printStr()
  39. {
  40. std::cout << "---> " << _s << std::endl;
  41. }
  42.  
  43. #include"Test.h"
  44.  
  45. int main(int argc, char **argv)
  46. {
  47.  
  48. PyImport_AppendInittab("TestModule", PyInit_TestModule);
  49. Py_Initialize();
  50.  
  51. PyRun_SimpleString("import TestModule");
  52. PyRun_SimpleString("t = TestModule.Test("str")");
  53. PyRun_SimpleString("t.printStr()");
  54. Py_Finalize();
  55.  
  56. getchar();
  57.  
  58. return 1;
  59. }
Add Comment
Please, Sign In to add comment