You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
5.3 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: pointer.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2010 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. && defined (LOG4CPLUS_HAVE_CXX11_ATOMICS)
  37. #include <atomic>
  38. #endif
  39. namespace log4cplus {
  40. namespace helpers {
  41. /******************************************************************************
  42. * Class SharedObject (from pp. 204-205) *
  43. ******************************************************************************/
  44. class LOG4CPLUS_EXPORT SharedObject
  45. {
  46. public:
  47. void addReference() const;
  48. void removeReference() const;
  49. protected:
  50. // Ctor
  51. SharedObject()
  52. : access_mutex()
  53. , count(0)
  54. { }
  55. SharedObject(const SharedObject&)
  56. : access_mutex()
  57. , count(0)
  58. { }
  59. // Dtor
  60. virtual ~SharedObject();
  61. // Operators
  62. SharedObject& operator=(const SharedObject&) { return *this; }
  63. public:
  64. thread::Mutex access_mutex;
  65. private:
  66. #if defined (LOG4CPLUS_SINGLE_THREADED)
  67. typedef unsigned count_type;
  68. #elif defined (LOG4CPLUS_HAVE_CXX11_ATOMICS)
  69. typedef std::atomic<unsigned> count_type;
  70. #elif defined (_WIN32) || defined (__CYGWIN__)
  71. typedef long count_type;
  72. #else
  73. typedef 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)
  87. : pointee(realPtr)
  88. {
  89. addref ();
  90. }
  91. SharedObjectPtr(const SharedObjectPtr& rhs)
  92. : pointee(rhs.pointee)
  93. {
  94. addref ();
  95. }
  96. #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
  97. SharedObjectPtr(SharedObjectPtr && rhs)
  98. : pointee (std::move (rhs.pointee))
  99. {
  100. rhs.pointee = 0;
  101. }
  102. SharedObjectPtr & operator = (SharedObjectPtr && rhs)
  103. {
  104. rhs.swap (*this);
  105. return *this;
  106. }
  107. #endif
  108. // Dtor
  109. ~SharedObjectPtr()
  110. {
  111. if (pointee)
  112. pointee->removeReference();
  113. }
  114. // Operators
  115. bool operator==(const SharedObjectPtr& rhs) const { return (pointee == rhs.pointee); }
  116. bool operator!=(const SharedObjectPtr& rhs) const { 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) throw ()
  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
  148. {
  149. if (pointee)
  150. pointee->addReference();
  151. }
  152. // Data
  153. T* pointee;
  154. };
  155. } // end namespace helpers
  156. } // end namespace log4cplus
  157. #endif // LOG4CPLUS_HELPERS_POINTERS_HEADER_