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.

492 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/smartptr/shared_ptr.hpp (originally MLShrPtr.h, ::SynesisStd)
  3. *
  4. * Purpose: Contains the shared_ptr template class.
  5. *
  6. * Created: 17th June 2002
  7. * Updated: 3rd March 2011
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2011, 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/shared_ptr.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::shared_ptr smart
  42. * pointer class template
  43. * (\ref group__library__smart_pointers "Smart Pointers" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SHARED_PTR
  46. #define STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SHARED_PTR
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_SHARED_PTR_MAJOR 3
  49. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_SHARED_PTR_MINOR 3
  50. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_SHARED_PTR_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_SMARTPTR_HPP_SHARED_PTR_EDIT 38
  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. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
  60. # include <stlsoft/util/std_swap.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifndef _STLSOFT_NO_NAMESPACE
  66. namespace stlsoft
  67. {
  68. #endif /* _STLSOFT_NO_NAMESPACE */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Classes
  71. */
  72. /** \brief This class enables sharing of arbitrary types.
  73. *
  74. * \ingroup group__library__smart_pointers
  75. *
  76. * \remarks The functionality is based in concept on the Boost shared_ptr,
  77. * which is set for inclusion in the next instalment of the C++ standard.
  78. * The implementation is entirely original.
  79. *
  80. * \param T The value type
  81. */
  82. template <ss_typename_param_k T>
  83. class shared_ptr
  84. {
  85. /// \name Types
  86. /// @{
  87. public:
  88. typedef T value_type;
  89. typedef value_type* pointer;
  90. typedef value_type const* const_pointer;
  91. typedef value_type& reference;
  92. typedef value_type const& const_reference;
  93. typedef shared_ptr<T> class_type;
  94. typedef pointer resource_type;
  95. typedef const_pointer const_resource_type;
  96. /// @}
  97. /// \name Construction
  98. /// @{
  99. public:
  100. shared_ptr()
  101. : m_p(NULL)
  102. , m_pc(NULL)
  103. {
  104. STLSOFT_ASSERT(is_valid());
  105. }
  106. ///
  107. ///
  108. /// \note If exception handling is not enabled and memory cannot be
  109. /// acquired to hold the sharing resource the object represented
  110. /// by \c p will be deleted, and get() will return \c NULL
  111. ///
  112. /// \exception std::bad_alloc If exception support is enabled,
  113. /// an instance of <code>std::bad_alloc</code> will be thrown if
  114. /// memory cannot be acquired to hold the sharing resource. In this
  115. /// case, the object represented by \c p will be deleted
  116. explicit shared_ptr(T* p)
  117. : m_p(p)
  118. , m_pc(NULL)
  119. {
  120. // This code prevents leaks in the following case:
  121. //
  122. // shared_ptr<Class> p(new Class());
  123. //
  124. // if the count cannot be allocated
  125. if(NULL != p)
  126. {
  127. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  128. try
  129. {
  130. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  131. m_pc = new long(1);
  132. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  133. }
  134. catch(std::bad_alloc&)
  135. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  136. if(NULL == m_pc)
  137. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  138. {
  139. delete m_p;
  140. m_p = NULL; // benign for X; necessary for NoX
  141. throw;
  142. }
  143. }
  144. STLSOFT_ASSERT(is_valid());
  145. }
  146. shared_ptr(class_type const& rhs)
  147. : m_p(rhs.m_p)
  148. , m_pc(rhs.m_pc)
  149. {
  150. STLSOFT_ASSERT(rhs.is_valid());
  151. if(NULL != m_pc)
  152. {
  153. ++*m_pc;
  154. }
  155. STLSOFT_ASSERT(is_valid());
  156. }
  157. #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
  158. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  159. _MSC_VER > 1200)
  160. template <ss_typename_param_k T2>
  161. shared_ptr(shared_ptr<T2> const& rhs)
  162. : m_p(rhs.m_p)
  163. , m_pc(rhs.m_pc)
  164. {
  165. STLSOFT_ASSERT(rhs.is_valid());
  166. STLSOFT_ASSERT((NULL == m_p) == (NULL == m_pc));
  167. if(NULL != m_pc)
  168. {
  169. ++*m_pc;
  170. }
  171. STLSOFT_ASSERT(is_valid());
  172. }
  173. #endif /* member template support? */
  174. ~shared_ptr() stlsoft_throw_0()
  175. {
  176. STLSOFT_ASSERT(is_valid());
  177. STLSOFT_ASSERT((NULL == m_p) == (NULL == m_pc));
  178. STLSOFT_ASSERT((NULL == m_pc) || (0 < *m_pc));
  179. if( NULL != m_pc &&
  180. 0 == --*m_pc)
  181. {
  182. delete m_p;
  183. delete m_pc;
  184. }
  185. }
  186. class_type& operator =(class_type const& rhs)
  187. {
  188. STLSOFT_ASSERT(is_valid());
  189. class_type this_(rhs);
  190. this_.swap(*this);
  191. STLSOFT_ASSERT(is_valid());
  192. return *this;
  193. }
  194. #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
  195. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  196. _MSC_VER > 1200)
  197. template <ss_typename_param_k T2>
  198. class_type& operator =(shared_ptr<T2> const& rhs)
  199. {
  200. STLSOFT_ASSERT(rhs.is_valid());
  201. STLSOFT_ASSERT(is_valid());
  202. class_type this_(rhs);
  203. this_.swap(*this);
  204. STLSOFT_ASSERT(is_valid());
  205. return *this;
  206. }
  207. #endif /* member template support? */
  208. /// @}
  209. /// \name Operations
  210. /// @{
  211. public:
  212. void close()
  213. {
  214. STLSOFT_ASSERT((NULL == m_p) == (NULL == m_pc));
  215. STLSOFT_ASSERT((NULL == m_pc) || (0 < *m_pc));
  216. STLSOFT_ASSERT(is_valid());
  217. if(NULL != m_pc)
  218. {
  219. pointer p = m_p;
  220. long *pc = m_pc;
  221. // Set the members to NULL prior to possibly
  222. // deleting, in case close() is called on a
  223. // shared_ptr member which is holding a
  224. // reference to the enclosing instance.
  225. m_p = NULL;
  226. m_pc = NULL;
  227. if(0 == --*pc)
  228. {
  229. delete p;
  230. delete pc;
  231. }
  232. }
  233. STLSOFT_ASSERT(is_valid());
  234. }
  235. #if 1 && !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  236. void reset()
  237. {
  238. close();
  239. }
  240. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  241. pointer detach()
  242. {
  243. STLSOFT_ASSERT((NULL == m_p) == (NULL == m_pc));
  244. STLSOFT_ASSERT((NULL == m_pc) || (0 < *m_pc));
  245. STLSOFT_ASSERT(is_valid());
  246. pointer p = NULL;
  247. if(NULL != m_pc)
  248. {
  249. if(0 == --*m_pc)
  250. {
  251. delete m_pc;
  252. m_pc = NULL;
  253. }
  254. std_swap(p, m_p);
  255. }
  256. STLSOFT_ASSERT(is_valid());
  257. return p;
  258. }
  259. void swap(class_type& rhs)
  260. {
  261. STLSOFT_ASSERT(rhs.is_valid());
  262. STLSOFT_ASSERT(is_valid());
  263. std_swap(m_p, rhs.m_p);
  264. std_swap(m_pc, rhs.m_pc);
  265. STLSOFT_ASSERT(is_valid());
  266. }
  267. /// @}
  268. /// \name Accessors
  269. /// @{
  270. public:
  271. const_pointer operator ->() const
  272. {
  273. STLSOFT_ASSERT(NULL != m_p);
  274. STLSOFT_ASSERT(is_valid());
  275. return m_p;
  276. }
  277. pointer operator ->()
  278. {
  279. STLSOFT_ASSERT(NULL != m_p);
  280. STLSOFT_ASSERT(is_valid());
  281. return m_p;
  282. }
  283. pointer get() const
  284. {
  285. STLSOFT_ASSERT(is_valid());
  286. return m_p;
  287. }
  288. const_reference operator *() const
  289. {
  290. STLSOFT_ASSERT(NULL != m_p);
  291. STLSOFT_ASSERT(is_valid());
  292. return *m_p;
  293. }
  294. reference operator *()
  295. {
  296. STLSOFT_ASSERT(NULL != m_p);
  297. STLSOFT_ASSERT(is_valid());
  298. return *m_p;
  299. }
  300. /// @}
  301. /// \name Attributes
  302. /// @{
  303. public:
  304. long count() const
  305. {
  306. STLSOFT_ASSERT(is_valid());
  307. return (NULL == m_pc) ? 0 : *m_pc;
  308. }
  309. /// \brief Returns count()
  310. long use_count() const
  311. {
  312. STLSOFT_ASSERT(is_valid());
  313. return this->count();
  314. }
  315. /// @}
  316. /// \name Implementation
  317. /// @{
  318. private:
  319. ss_bool_t is_valid() const
  320. {
  321. if((NULL == m_p) != (NULL == m_pc))
  322. {
  323. #ifdef STLSOFT_UNITTEST
  324. fprintf(err, "Managed object's pointer and shared count pointer must both be NULL, or both non-NULL!\n");
  325. #endif /* STLSOFT_UNITTEST */
  326. return false;
  327. }
  328. if( NULL != m_pc &&
  329. *m_pc < 1)
  330. {
  331. #ifdef STLSOFT_UNITTEST
  332. fprintf(err, "Shared count cannot be less than 1!\n");
  333. #endif /* STLSOFT_UNITTEST */
  334. return false;
  335. }
  336. return true;
  337. }
  338. /// @}
  339. /// \name Members
  340. /// @{
  341. private:
  342. pointer m_p;
  343. long *m_pc;
  344. /// @}
  345. };
  346. /* /////////////////////////////////////////////////////////////////////////
  347. * swapping
  348. */
  349. template <ss_typename_param_k T>
  350. void swap(shared_ptr<T>& lhs, shared_ptr<T>& rhs)
  351. {
  352. lhs.swap(rhs);
  353. }
  354. /* /////////////////////////////////////////////////////////////////////////
  355. * Shims
  356. */
  357. #if !defined(STLSOFT_COMPILER_IS_WATCOM)
  358. /** \brief get_ptr shim
  359. *
  360. * \ingroup group__library__smart_pointers
  361. */
  362. template<ss_typename_param_k T>
  363. inline T* get_ptr(shared_ptr<T> const& p)
  364. {
  365. return p.get();
  366. }
  367. /** \brief Insertion operator shim
  368. *
  369. * \ingroup group__library__smart_pointers
  370. */
  371. template< ss_typename_param_k S
  372. , ss_typename_param_k T
  373. >
  374. inline S& operator <<(S& s, shared_ptr<T> const& p)
  375. {
  376. return s << *p;
  377. }
  378. #endif /* compiler */
  379. /* ////////////////////////////////////////////////////////////////////// */
  380. #ifndef _STLSOFT_NO_NAMESPACE
  381. } // namespace stlsoft
  382. #endif /* _STLSOFT_NO_NAMESPACE */
  383. /* In the special case of Intel behaving as VC++ 7.0 or earlier on Win32, we
  384. * illegally insert into the std namespace.
  385. */
  386. #if defined(STLSOFT_CF_std_NAMESPACE)
  387. # if ( ( defined(STLSOFT_COMPILER_IS_INTEL) && \
  388. defined(_MSC_VER))) && \
  389. _MSC_VER < 1310
  390. namespace std
  391. {
  392. template< ss_typename_param_k T
  393. >
  394. inline void swap(stlsoft_ns_qual(shared_ptr)<T>& lhs, stlsoft_ns_qual(shared_ptr)<T>& rhs)
  395. {
  396. lhs.swap(rhs);
  397. }
  398. } // namespace std
  399. # endif /* INTEL && _MSC_VER < 1310 */
  400. #endif /* STLSOFT_CF_std_NAMESPACE */
  401. /* ////////////////////////////////////////////////////////////////////// */
  402. #endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SHARED_PTR */
  403. /* ///////////////////////////// end of file //////////////////////////// */