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.

230 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/smartptr/proxy_ptr.hpp
  3. *
  4. * Purpose: Contains the proxy_ptr template class.
  5. *
  6. * Created: 17th January 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/smartptr/proxy_ptr.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::proxy_ptr smart
  42. * pointer class template
  43. * (\ref group__library__smart_pointers "Smart Pointers" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_PROXY_PTR
  46. #define STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_PROXY_PTR
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_PROXY_PTR_MAJOR 5
  49. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_PROXY_PTR_MINOR 1
  50. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_PROXY_PTR_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_PROXY_PTR_EDIT 72
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _STLSOFT_NO_NAMESPACE
  63. namespace stlsoft
  64. {
  65. #endif /* _STLSOFT_NO_NAMESPACE */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Classes
  68. */
  69. // class proxy_ptr
  70. /** \brief This class emulates a pointer in all respects, and is useful as
  71. * a generic code testing tool, and is simply an aid to
  72. * self-documentation.
  73. *
  74. * \ingroup group__library__smart_pointers
  75. *
  76. * \param T The value type
  77. */
  78. template <ss_typename_param_k T>
  79. class proxy_ptr
  80. {
  81. public:
  82. /// The value type
  83. typedef T value_type;
  84. /// The current parameterisation of the type
  85. typedef proxy_ptr<T> class_type;
  86. typedef value_type* resource_type;
  87. typedef value_type const* const_resource_type;
  88. // Construction
  89. public:
  90. /// Construct from a pointer to "borrow"
  91. ss_explicit_k proxy_ptr(value_type *t)
  92. : m_value(t)
  93. {}
  94. /// Assignment from a new pointer
  95. proxy_ptr& operator =(value_type *t)
  96. {
  97. m_value = t;
  98. return *this;
  99. }
  100. // Conversion
  101. public:
  102. /// Implicit conversion to pointer to the underlying pointer
  103. operator value_type *()
  104. {
  105. return m_value;
  106. }
  107. /// Implicit conversion to pointer-to-const to the underlying pointer
  108. operator value_type const* () const
  109. {
  110. return m_value;
  111. }
  112. /// Indirection operator
  113. ///
  114. /// \note This method does a debug-time assertion that the underlying pointer is non-null
  115. value_type& operator *() // indirection
  116. {
  117. STLSOFT_MESSAGE_ASSERT("Dereferencing a null pointer!", NULL != m_value);
  118. return *m_value;
  119. }
  120. /// Indirection operator
  121. ///
  122. /// \note This method does a debug-time assertion that the underlying pointer is non-null
  123. value_type const& operator *() const // indirection
  124. {
  125. STLSOFT_MESSAGE_ASSERT("Dereferencing a null pointer!", NULL != m_value);
  126. return *m_value;
  127. }
  128. /// Member-selection operator
  129. ///
  130. /// \note This method does a debug-time assertion that the underlying pointer is non-null
  131. value_type* operator ->() // member-selection
  132. {
  133. STLSOFT_MESSAGE_ASSERT("Dereferencing a null pointer!", NULL != m_value);
  134. return m_value;
  135. }
  136. /// Member-selection operator
  137. ///
  138. /// \note This method does a debug-time assertion that the underlying pointer is non-null
  139. value_type const* operator ->() const // member-selection
  140. {
  141. STLSOFT_MESSAGE_ASSERT("Dereferencing a null pointer!", NULL != m_value);
  142. return m_value;
  143. }
  144. /// Returns the underlying pointer value
  145. ///
  146. /// \deprecated This function will be removed in a future release. Users
  147. /// should instead invoke get()
  148. value_type* get_ptr() const
  149. {
  150. return get();
  151. }
  152. /// Returns the underlying pointer value
  153. value_type* get() const
  154. {
  155. return m_value;
  156. }
  157. /// Returns the underlying pointer value
  158. ///
  159. /// \deprecated This method will be removed in a future version
  160. value_type* GetPointer() const
  161. {
  162. return m_value;
  163. }
  164. /// Sets the underlying pointer value to null
  165. void clear()
  166. {
  167. m_value = NULL;
  168. }
  169. // Members
  170. private:
  171. value_type *m_value;
  172. };
  173. /* /////////////////////////////////////////////////////////////////////////
  174. * Shims
  175. */
  176. /** \brief get_ptr shim
  177. *
  178. * \ingroup group__library__smart_pointers
  179. */
  180. template <ss_typename_param_k T>
  181. inline T* get_ptr(proxy_ptr<T> const& p)
  182. {
  183. return p.get();
  184. }
  185. ////////////////////////////////////////////////////////////////////////////
  186. // Unit-testing
  187. #ifdef STLSOFT_UNITTEST
  188. # include "./unittest/proxy_ptr_unittest_.h"
  189. #endif /* STLSOFT_UNITTEST */
  190. /* ////////////////////////////////////////////////////////////////////// */
  191. #ifndef _STLSOFT_NO_NAMESPACE
  192. } // namespace stlsoft
  193. #endif /* _STLSOFT_NO_NAMESPACE */
  194. /* ////////////////////////////////////////////////////////////////////// */
  195. #endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_PROXY_PTR */
  196. /* ///////////////////////////// end of file //////////////////////////// */