Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 1 | /* |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 2 | * Created by Phil on 02/05/2012. |
| 3 | * Copyright 2012 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 7 | */ |
| 8 | #ifndef TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED |
| 10 | |
| 11 | #include "catch_common.h" |
| 12 | |
Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 13 | namespace Catch { |
| 14 | |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 15 | // An intrusive reference counting smart pointer. |
| 16 | // T must implement addRef() and release() methods |
| 17 | // typically implementing the IShared interface |
| 18 | template<typename T> |
Phil Nash | d0be9ed | 2012-05-15 08:02:36 +0100 | [diff] [blame] | 19 | class Ptr { |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 20 | public: |
| 21 | Ptr() : m_p( NULL ){} |
| 22 | Ptr( T* p ) : m_p( p ){ |
Phil Nash | 6175697 | 2012-07-28 20:37:07 +0100 | [diff] [blame] | 23 | if( m_p ) |
| 24 | m_p->addRef(); |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 25 | } |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 26 | Ptr( Ptr const& other ) : m_p( other.m_p ){ |
Phil Nash | 6175697 | 2012-07-28 20:37:07 +0100 | [diff] [blame] | 27 | if( m_p ) |
| 28 | m_p->addRef(); |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 29 | } |
| 30 | ~Ptr(){ |
| 31 | if( m_p ) |
| 32 | m_p->release(); |
| 33 | } |
Phil Nash | fe98123 | 2012-12-05 08:40:53 +0000 | [diff] [blame^] | 34 | void reset() { |
| 35 | if( m_p ) |
| 36 | m_p->release(); |
| 37 | m_p = NULL; |
| 38 | } |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 39 | Ptr& operator = ( T* p ){ |
| 40 | Ptr temp( p ); |
| 41 | swap( temp ); |
| 42 | return *this; |
| 43 | } |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 44 | Ptr& operator = ( Ptr const& other ){ |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 45 | Ptr temp( other ); |
| 46 | swap( temp ); |
| 47 | return *this; |
| 48 | } |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 49 | void swap( Ptr& other ) { std::swap( m_p, other.m_p ); } |
| 50 | T* get() { return m_p; } |
| 51 | const T* get() const{ return m_p; } |
| 52 | T& operator*() const { return *m_p; } |
| 53 | T* operator->() const { return m_p; } |
| 54 | bool operator !() const { return m_p == NULL; } |
Phil Nash | fe98123 | 2012-12-05 08:40:53 +0000 | [diff] [blame^] | 55 | operator SafeBool::type() const { return SafeBool::makeSafe( m_p != NULL ); } |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | T* m_p; |
| 59 | }; |
| 60 | |
| 61 | struct IShared : NonCopyable { |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 62 | virtual ~IShared(); |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 63 | virtual void addRef() const = 0; |
| 64 | virtual void release() const = 0; |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 67 | template<typename T = IShared> |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 68 | struct SharedImpl : T { |
| 69 | |
| 70 | SharedImpl() : m_rc( 0 ){} |
Phil Nash | 9444bbc | 2012-10-12 07:58:17 +0100 | [diff] [blame] | 71 | |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 72 | virtual void addRef() const { |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 73 | ++m_rc; |
| 74 | } |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 75 | virtual void release() const { |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 76 | if( --m_rc == 0 ) |
| 77 | delete this; |
| 78 | } |
| 79 | |
Phil Nash | 7f04b56 | 2012-11-30 18:54:06 +0000 | [diff] [blame] | 80 | mutable unsigned int m_rc; |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // end namespace Catch |
| 84 | |
| 85 | #endif // TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED |