SmartPtr.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef UTILSPP_SMARTPTR_HPP
  24. #define UTILSPP_SMARTPTR_HPP
  25. #include <stdexcept>
  26. #include "NonCopyable.hpp"
  27. #define NULL_BODY_ERROR "the smart pointer contain a NULL pointer"
  28. namespace utilspp
  29. {
  30. template <typename Type = unsigned int>
  31. class FastCount
  32. {
  33. public:
  34. FastCount(Type count = 1) : mCount(count)
  35. {}
  36. FastCount & operator++()
  37. {
  38. mCount++;
  39. return *this;
  40. }
  41. FastCount & operator--()
  42. {
  43. mCount--;
  44. return *this;
  45. }
  46. operator Type()
  47. {
  48. return mCount;
  49. }
  50. Type useCount()
  51. {
  52. return mCount;
  53. }
  54. private:
  55. Type mCount;
  56. };
  57. template <typename ContentType, typename CountPolicy = FastCount>
  58. class CountingBody : public utilspp::NonCopyable
  59. {
  60. public:
  61. CountingBody(ContentType * body) : mBody(body)
  62. {}
  63. void inc()
  64. {
  65. ++mCount;
  66. }
  67. void dec()
  68. {
  69. --mCount;
  70. if (mCount <= 0) {
  71. delete this;
  72. }
  73. }
  74. ContentType * get()
  75. {
  76. return mBody;
  77. }
  78. protected:
  79. ~CountingBody()
  80. {
  81. if (mBody != NULL) {
  82. delete mBody;
  83. mBody = NULL;
  84. }
  85. }
  86. private:
  87. CountPolicy mCount;
  88. ContentType * mBody;
  89. };
  90. template <typename ContentType, typename CountingBodyPolicy = CountingBody>
  91. class SharedPtr
  92. {
  93. public:
  94. SharedPtr() : mContent(new CountingPolicy<ContentType>(NULL))
  95. {}
  96. explicit SharedPtr(ContentType * content) : mContent(new CountingBodyPolicy<ContentType>(content))
  97. {}
  98. ~SharedPtr()
  99. {
  100. mContent->dec();
  101. }
  102. SharedPtr(const SharedPtr & other) : mContent(other.mContent)
  103. {
  104. mContent->inc();
  105. }
  106. SharedPtr & operator=(const SharedPtr & other)
  107. {
  108. if(mContent->get() != other.mContent->get()) {
  109. mContent->dec();
  110. mContent = other.mContent;
  111. mContent->inc();
  112. }
  113. return (*this);
  114. }
  115. SharedPtr & operator=(ContentType * content)
  116. {
  117. mContent--;
  118. mContent = new CountingBodyPolicy<ContentType>(content);
  119. }
  120. bool operator==(const SharedPtr & other) const
  121. {
  122. return (mContent->get() == other.mContent->get());
  123. }
  124. bool operator!=(const SharedPtr & other) const
  125. {
  126. return (mContent->get() != other.mContent->get());
  127. }
  128. bool operator<(const SharedPtr & other) const
  129. {
  130. return (mContent->get() < other.mContent->get());
  131. }
  132. operator ContentType*()
  133. {
  134. return mContent->get();
  135. }
  136. ContentType & operator*()
  137. {
  138. if(mContent->get() == NULL) {
  139. throw std::runtime_error(NULL_BODY_ERROR);
  140. }
  141. return *mContent->get();
  142. }
  143. ContentType* operator->()
  144. {
  145. if(mContent->get() == NULL) {
  146. throw std::runtime_error(NULL_BODY_ERROR);
  147. }
  148. return mContent->get();
  149. }
  150. private:
  151. CountingBodyPolicy * mContent;
  152. };
  153. }
  154. #endif