blob: 4197cf799f8f26e09993bda86c3bf5241faaa67d [file] [log] [blame]
Phil Nashb2132022012-05-04 07:55:11 +01001/*
Phil Nashb2132022012-05-04 07:55:11 +01002 * 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 Nashb2132022012-05-04 07:55:11 +01007 */
8#ifndef TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
9#define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
10
11#include "catch_common.h"
12
Phil Nashdc2735c2013-03-13 08:04:50 +000013#ifdef __clang__
14#pragma clang diagnostic push
15#pragma clang diagnostic ignored "-Wpadded"
16#endif
17
Phil Nashd0be9ed2012-05-15 08:02:36 +010018namespace Catch {
19
Phil Nashb2132022012-05-04 07:55:11 +010020 // An intrusive reference counting smart pointer.
21 // T must implement addRef() and release() methods
22 // typically implementing the IShared interface
23 template<typename T>
Phil Nashd0be9ed2012-05-15 08:02:36 +010024 class Ptr {
Phil Nashb2132022012-05-04 07:55:11 +010025 public:
Martin Hořeňovský71df6632017-04-25 12:41:30 +020026 Ptr() : m_p( nullptr ){}
Phil Nashb2132022012-05-04 07:55:11 +010027 Ptr( T* p ) : m_p( p ){
Phil Nash61756972012-07-28 20:37:07 +010028 if( m_p )
29 m_p->addRef();
Phil Nashb2132022012-05-04 07:55:11 +010030 }
Phil Nash7f04b562012-11-30 18:54:06 +000031 Ptr( Ptr const& other ) : m_p( other.m_p ){
Phil Nash61756972012-07-28 20:37:07 +010032 if( m_p )
33 m_p->addRef();
Phil Nashb2132022012-05-04 07:55:11 +010034 }
35 ~Ptr(){
36 if( m_p )
37 m_p->release();
38 }
Phil Nashfe981232012-12-05 08:40:53 +000039 void reset() {
40 if( m_p )
41 m_p->release();
Martin Hořeňovský71df6632017-04-25 12:41:30 +020042 m_p = nullptr;
Phil Nashfe981232012-12-05 08:40:53 +000043 }
Phil Nashb2132022012-05-04 07:55:11 +010044 Ptr& operator = ( T* p ){
45 Ptr temp( p );
46 swap( temp );
47 return *this;
48 }
Phil Nash7f04b562012-11-30 18:54:06 +000049 Ptr& operator = ( Ptr const& other ){
Phil Nashb2132022012-05-04 07:55:11 +010050 Ptr temp( other );
51 swap( temp );
52 return *this;
53 }
Phil Nash7f04b562012-11-30 18:54:06 +000054 void swap( Ptr& other ) { std::swap( m_p, other.m_p ); }
Phil Nash34fa25e2015-07-28 18:55:11 +010055 T* get() const{ return m_p; }
Phil Nash7f04b562012-11-30 18:54:06 +000056 T& operator*() const { return *m_p; }
57 T* operator->() const { return m_p; }
Martin Hořeňovský71df6632017-04-25 12:41:30 +020058 bool operator !() const { return m_p == nullptr; }
Phil Nashcc8206f2017-04-25 14:46:48 +000059 explicit operator bool() const { return m_p != nullptr; }
Phil Nashf3d1f082013-07-03 19:14:59 +010060
Phil Nashb2132022012-05-04 07:55:11 +010061 private:
62 T* m_p;
63 };
Phil Nashf3d1f082013-07-03 19:14:59 +010064
Phil Nashb2132022012-05-04 07:55:11 +010065 struct IShared : NonCopyable {
Phil Nasha695eb92012-08-13 07:46:10 +010066 virtual ~IShared();
Phil Nash7f04b562012-11-30 18:54:06 +000067 virtual void addRef() const = 0;
68 virtual void release() const = 0;
Phil Nashb2132022012-05-04 07:55:11 +010069 };
Phil Nashf3d1f082013-07-03 19:14:59 +010070
Phil Nash7f04b562012-11-30 18:54:06 +000071 template<typename T = IShared>
Phil Nashb2132022012-05-04 07:55:11 +010072 struct SharedImpl : T {
Phil Nashf3d1f082013-07-03 19:14:59 +010073
Phil Nashb2132022012-05-04 07:55:11 +010074 SharedImpl() : m_rc( 0 ){}
Phil Nash9444bbc2012-10-12 07:58:17 +010075
Phil Nash7f04b562012-11-30 18:54:06 +000076 virtual void addRef() const {
Phil Nashb2132022012-05-04 07:55:11 +010077 ++m_rc;
78 }
Phil Nash7f04b562012-11-30 18:54:06 +000079 virtual void release() const {
Phil Nashb2132022012-05-04 07:55:11 +010080 if( --m_rc == 0 )
81 delete this;
82 }
Phil Nashf3d1f082013-07-03 19:14:59 +010083
Phil Nash7f04b562012-11-30 18:54:06 +000084 mutable unsigned int m_rc;
Phil Nashb2132022012-05-04 07:55:11 +010085 };
Phil Nashf3d1f082013-07-03 19:14:59 +010086
Phil Nashb2132022012-05-04 07:55:11 +010087} // end namespace Catch
88
Phil Nashdc2735c2013-03-13 08:04:50 +000089#ifdef __clang__
90#pragma clang diagnostic pop
91#endif
92
Phil Nashb2132022012-05-04 07:55:11 +010093#endif // TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED