Advertisement
sve_vash

Untitled

Dec 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <memory>
  4. #include "Test.h"
  5.  
  6. namespace smart_pointer {
  7. // `exception` class definition
  8. class exception : std::exception {
  9. using base_class = std::exception;
  10. using base_class::base_class;
  11. };
  12.  
  13. // `SmartPointer` class declaration
  14. template<
  15. typename T,
  16. typename Allocator
  17. >
  18. class SmartPointer {
  19. // don't remove this macro
  20. ENABLE_CLASS_TESTS;
  21. public:
  22. using value_type = T;
  23.  
  24. SmartPointer(value_type * p = nullptr) {
  25. core = new Core();
  26. core->pointer = p;
  27. if (p != nullptr) {
  28. core->countpointers = 1;
  29. }
  30. }
  31.  
  32. // copy constructor
  33. SmartPointer(const SmartPointer &smart_pointer) {
  34. core = smart_pointer.core;
  35. core->countpointers++;
  36. }
  37.  
  38. // move constructor
  39. SmartPointer(SmartPointer &&smart_pointer) {
  40. core = smart_pointer.core;
  41. core->countpointers++;
  42. smart_pointer.core = nullptr;
  43. }
  44.  
  45. // copy assigment
  46. SmartPointer &operator=(const SmartPointer &smart_pointer) {
  47. core = smart_pointer.core;
  48. core->countpointers++;
  49. }
  50.  
  51. // move assigment
  52. SmartPointer &operator=(SmartPointer &&smart_pointer) {
  53. core = smart_pointer.core;
  54. core->countpointers++;
  55. smart_pointer.core = nullptr;
  56. }
  57.  
  58. //
  59. SmartPointer &operator=(value_type * p) {
  60. core->pointer = p;
  61. }
  62.  
  63. ~SmartPointer() {
  64. core = nullptr;
  65. }
  66.  
  67. // return reference to the object of class/type T
  68. // if SmartPointer contains nullptr throw `SmartPointer::exception`
  69. value_type &operator*() {
  70. if (core->pointer == nullptr) {
  71. throw exception();
  72. }
  73. return *core->pointer;
  74. }
  75. const value_type &operator*() const {
  76. if (core->pointer == nullptr) {
  77. throw exception();
  78. }
  79. return *core->pointer;
  80. }
  81.  
  82. // return pointer to the object of class/type T
  83. value_type *operator->() const {
  84. return core->pointer;
  85. }
  86.  
  87. value_type *get() const {
  88. if (operator bool()) {
  89. return core->pointer;
  90. }
  91. return nullptr;
  92. }
  93.  
  94. // if pointer == nullptr => return false
  95. operator bool() const {
  96. return core->pointer != nullptr;
  97. }
  98.  
  99. // if pointers points to the same address or both null => true
  100. template<typename U, typename AnotherAllocator>
  101. bool operator==(const SmartPointer<U, AnotherAllocator> &sm) const {
  102. return sm.core->pointer == core->pointer;
  103. }
  104.  
  105. // if pointers points to the same address or both null => false
  106. template<typename U, typename AnotherAllocator>
  107. bool operator!=(const SmartPointer<U, AnotherAllocator> &sm) const {
  108. return sm.core->pointer != core->pointer;
  109. }
  110.  
  111. // if smart pointer contains non-nullptr => return count owners
  112. // if smart pointer contains nullptr => return 0
  113. std::size_t count_owners() const {
  114. if (core->pointer == nullptr) {
  115. return 0;
  116. }
  117. return core->countpointers;
  118. }
  119.  
  120. private:
  121. class Core {
  122. public:
  123. size_t countpointers;
  124. value_type * pointer;
  125. };
  126. Core *core;
  127. };
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement