Advertisement
dan-masek

Pimpl example

Apr 5th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. === header core.h ===
  2.  
  3. class CoreImpl
  4.  
  5. class Core
  6. {
  7. private:
  8.     CoreImpl* impl; // Maybe even smart pointer
  9.  
  10. public:
  11.     Core();
  12.    
  13.     ~Core();
  14.     // Note: Needs to handle copying correctly as well, or forbid it
  15.  
  16.     void foo();
  17.     // more functions
  18.    
  19. };
  20.  
  21. ===== source core.cpp ======
  22.  
  23. #include "core.h"
  24.  
  25. #include <functional>
  26. #include <iostream>
  27. #include <string>
  28. #include <vector>
  29.  
  30. #include <pybind11/pybind11.h>
  31. #include <pybind11/embed.h>
  32. #include <pybind11/stl.h>
  33. #include <pybind11/functional.h>
  34. #include <pybind11/numpy.h>
  35.  
  36. namespace py = pybind11;
  37. using namespace py::literals;
  38.  
  39. typedef void(*CallbackFn)(bool, std::string, py::array_t<uint8_t>&);
  40. typedef std::function<void(std::string)> LogFunction;
  41.  
  42. class CoreImpl
  43. {
  44. private:
  45.     py::scoped_interpreter guard{};
  46.     py::object cls;
  47.     py::object obj;
  48.     py::object startFunc;
  49.     py::object stopFunc;
  50.     py::object setCpuAffinityFunc;
  51.     py::object addCallbackFunc;
  52.     py::object removeCallbackFunc;
  53.     py::object getCallbacksFunc;
  54.  
  55.     py::dict kwargs;
  56.     py::list callbackList;
  57.  
  58. public:
  59.     CoreImpl();
  60.    
  61.     void foo();
  62.     // more functions
  63.    
  64. };
  65.  
  66. Core::Core()
  67.     : impl(new CoreImpl())
  68. {
  69. }
  70.  
  71. Core::~Core()
  72. {
  73.     delete impl;
  74. }
  75.  
  76. Core::foo()
  77. {
  78.     impl->foo();
  79. }
  80.  
  81. // And so on, forward all the interface calls to matching implementations
  82.  
  83.  
  84. CoreImpl::CoreImpl()
  85. {
  86.     // all the code that was originally in Core
  87. }
  88.  
  89. // And the rest of the member implementation -- none of this is visible to client.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement