fork(2) download
  1. #include <iostream>
  2.  
  3. template <class Handle, class Impl>
  4. class Pimpl
  5. {
  6. private:
  7. Impl* _make() const
  8. { return ((Handle const*)this)->make(); }
  9.  
  10. void _unmake(Impl *p) const
  11. { ((Handle const*)this)->unmake(p); }
  12.  
  13. Impl* _clone(Impl *p) const
  14. { return ((Handle const*)this)->clone(p); }
  15.  
  16. void swap(Pimpl &other) {
  17. Impl *temp = d_ptr;
  18. d_ptr = other.d_ptr;
  19. other.d_ptr = temp;
  20. }
  21.  
  22. public:
  23. explicit Pimpl();
  24. ~Pimpl();
  25.  
  26. Pimpl(const Pimpl &other);
  27. Pimpl &operator=(const Pimpl &other);
  28.  
  29. // fall-backs
  30. static Impl* make() { return new Impl; }
  31. static void unmake(Impl* p) { delete p; }
  32. static Impl* clone(Impl* p) { return new Impl(*p); }
  33.  
  34. protected:
  35.  
  36. Impl *d_ptr;
  37. };
  38.  
  39. template<class Handle, class Impl>
  40. Pimpl<Handle, Impl>::Pimpl() :
  41. d_ptr(_make())
  42. {
  43.  
  44. }
  45.  
  46. template<class Handle, class Impl>
  47. Pimpl<Handle, Impl>::~Pimpl()
  48. {
  49. _unmake(d_ptr);
  50. d_ptr = 0;
  51. }
  52.  
  53. template<class Handle, class Impl>
  54. Pimpl<Handle, Impl>::Pimpl(const Pimpl &other) :
  55. d_ptr(_clone(other.d_ptr))
  56. {
  57.  
  58. }
  59.  
  60. template<class Handle, class Impl>
  61. Pimpl<Handle, Impl> &Pimpl<Handle, Impl>::operator=(const Pimpl &other)
  62. {
  63. Pimpl copy(other);
  64. swap(copy);
  65.  
  66. return *this;
  67. }
  68.  
  69. class ObjectPrivate;
  70.  
  71. class Object : private Pimpl<Object, ObjectPrivate>
  72. {
  73. public:
  74. Object();
  75. ~Object();
  76. };
  77.  
  78. int main() {
  79. // your code goes here
  80. return 0;
  81. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty