pointer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: pointer.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2017 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. //
  22. // Note: Some of this code uses ideas from "More Effective C++" by Scott
  23. // Myers, Addison Wesley Longmain, Inc., (c) 1996, Chapter 29, pp. 183-213
  24. //
  25. /** @file */
  26. #ifndef LOG4CPLUS_HELPERS_POINTERS_HEADER_
  27. #define LOG4CPLUS_HELPERS_POINTERS_HEADER_
  28. #include <log4cplus/config.hxx>
  29. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  30. #pragma once
  31. #endif
  32. #include <log4cplus/thread/syncprims.h>
  33. #include <algorithm>
  34. #include <cassert>
  35. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  36. #include <atomic>
  37. #endif
  38. namespace log4cplus {
  39. namespace helpers {
  40. /******************************************************************************
  41. * Class SharedObject (from pp. 204-205) *
  42. ******************************************************************************/
  43. class LOG4CPLUS_EXPORT SharedObject
  44. {
  45. public:
  46. void addReference() const LOG4CPLUS_NOEXCEPT;
  47. void removeReference() const;
  48. protected:
  49. // Ctor
  50. SharedObject()
  51. : access_mutex()
  52. , count__(0)
  53. { }
  54. SharedObject(const SharedObject&)
  55. : access_mutex()
  56. , count__(0)
  57. { }
  58. SharedObject(SharedObject &&)
  59. : access_mutex()
  60. , count__(0)
  61. { }
  62. // Dtor
  63. virtual ~SharedObject();
  64. // Operators
  65. SharedObject& operator=(const SharedObject&) LOG4CPLUS_NOEXCEPT { return *this; }
  66. SharedObject& operator=(SharedObject &&) LOG4CPLUS_NOEXCEPT { return *this; }
  67. public:
  68. thread::Mutex access_mutex;
  69. private:
  70. #if defined (LOG4CPLUS_SINGLE_THREADED)
  71. typedef unsigned count_type;
  72. #else
  73. typedef std::atomic<unsigned> count_type;
  74. #endif
  75. mutable count_type count__;
  76. };
  77. /******************************************************************************
  78. * Template Class SharedObjectPtr (from pp. 203, 206) *
  79. ******************************************************************************/
  80. template<class T>
  81. class SharedObjectPtr
  82. {
  83. public:
  84. // Ctor
  85. explicit
  86. SharedObjectPtr(T* realPtr = 0) LOG4CPLUS_NOEXCEPT
  87. : pointee(realPtr)
  88. {
  89. addref ();
  90. }
  91. SharedObjectPtr(const SharedObjectPtr& rhs) LOG4CPLUS_NOEXCEPT
  92. : pointee(rhs.pointee)
  93. {
  94. addref ();
  95. }
  96. SharedObjectPtr(SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
  97. : pointee (std::move (rhs.pointee))
  98. {
  99. rhs.pointee = 0;
  100. }
  101. SharedObjectPtr & operator = (SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
  102. {
  103. rhs.swap (*this);
  104. return *this;
  105. }
  106. // Dtor
  107. ~SharedObjectPtr()
  108. {
  109. if (pointee)
  110. pointee->removeReference();
  111. }
  112. // Operators
  113. bool operator==(const SharedObjectPtr& rhs) const
  114. { return (pointee == rhs.pointee); }
  115. bool operator!=(const SharedObjectPtr& rhs) const
  116. { return (pointee != rhs.pointee); }
  117. bool operator==(const T* rhs) const { return (pointee == rhs); }
  118. bool operator!=(const T* rhs) const { return (pointee != rhs); }
  119. T* operator->() const {assert (pointee); return pointee; }
  120. T& operator*() const {assert (pointee); return *pointee; }
  121. SharedObjectPtr& operator=(const SharedObjectPtr& rhs)
  122. {
  123. return this->operator = (rhs.pointee);
  124. }
  125. SharedObjectPtr& operator=(T* rhs)
  126. {
  127. SharedObjectPtr<T> (rhs).swap (*this);
  128. return *this;
  129. }
  130. // Methods
  131. T* get() const { return pointee; }
  132. void swap (SharedObjectPtr & other) LOG4CPLUS_NOEXCEPT
  133. {
  134. std::swap (pointee, other.pointee);
  135. }
  136. typedef T * (SharedObjectPtr:: * unspec_bool_type) () const;
  137. operator unspec_bool_type () const
  138. {
  139. return pointee ? &SharedObjectPtr::get : 0;
  140. }
  141. bool operator ! () const
  142. {
  143. return ! pointee;
  144. }
  145. private:
  146. // Methods
  147. void addref() const LOG4CPLUS_NOEXCEPT
  148. {
  149. if (pointee)
  150. pointee->addReference();
  151. }
  152. // Data
  153. T* pointee;
  154. };
  155. //! Boost `intrusive_ptr` helpers.
  156. //! @{
  157. inline
  158. void
  159. intrusive_ptr_add_ref (SharedObject const * so)
  160. {
  161. so->addReference();
  162. }
  163. inline
  164. void
  165. intrusive_ptr_release (SharedObject const * so)
  166. {
  167. so->removeReference();
  168. }
  169. //! @}
  170. } // end namespace helpers
  171. } // end namespace log4cplus
  172. #endif // LOG4CPLUS_HELPERS_POINTERS_HEADER_