Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <memory>
  2.  
  3. namespace std
  4. {
  5.  
  6. namespace sr1
  7. {
  8.  
  9. template <typename T>
  10. class shared_ptr_lock
  11. {
  12. protected:
  13. std::shared_ptr<T> data;
  14.  
  15. public:
  16. shared_ptr_lock(std::shared_ptr<T> data)
  17. {
  18. this->data = data;
  19. }
  20. };
  21.  
  22. template <typename T>
  23. class shared_ptr_lock_ptr : public shared_ptr_lock<T>
  24. {
  25. public:
  26. shared_ptr_lock_ptr(std::shared_ptr<T> data) : shared_ptr_lock(data) { }
  27.  
  28. operator T*()
  29. {
  30. return data.get();
  31. }
  32.  
  33. T& operator*()
  34. {
  35. return *data;
  36. }
  37.  
  38. T* operator->()
  39. {
  40. return data.get();
  41. }
  42. };
  43.  
  44. template <typename T>
  45. class shared_ptr_lock_ref : public shared_ptr_lock<T>
  46. {
  47. public:
  48. shared_ptr_lock_ref(std::shared_ptr<T> data) : shared_ptr_lock(data) { }
  49.  
  50. operator T&()
  51. {
  52. return *data;
  53. }
  54. };
  55.  
  56. template <typename T>
  57. class shared_ptr
  58. {
  59. public: // TODO: make_shared friend class?
  60. std::shared_ptr<T> data;
  61.  
  62. public:
  63. void reset()
  64. {
  65. data.reset();
  66. }
  67.  
  68. shared_ptr_lock_ptr<T> operator->()
  69. {
  70. return shared_ptr_lock_ptr<T>(data);
  71. }
  72.  
  73. shared_ptr_lock_ptr<T> get()
  74. {
  75. return shared_ptr_lock_ptr<T>(data);
  76. }
  77.  
  78. shared_ptr_lock_ref<T> operator*()
  79. {
  80. return shared_ptr_lock_ref<T>(data);
  81. }
  82. };
  83.  
  84. template <typename T, typename A>
  85. shared_ptr<T> make_shared(A& a)
  86. {
  87. shared_ptr<T> rtn;
  88.  
  89. rtn.data = std::make_shared<T>(a);
  90.  
  91. return rtn;
  92. }
  93.  
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement