mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-11-04 00:51:52 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 *  Created by Phil on 02/05/2012.
 | 
						|
 *  Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
 | 
						|
 *
 | 
						|
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 | 
						|
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
						|
 */
 | 
						|
#ifndef TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
 | 
						|
#define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
 | 
						|
 | 
						|
#include "catch_common.h"
 | 
						|
 | 
						|
#ifdef __clang__
 | 
						|
#pragma clang diagnostic push
 | 
						|
#pragma clang diagnostic ignored "-Wpadded"
 | 
						|
#endif
 | 
						|
 | 
						|
namespace Catch {
 | 
						|
 | 
						|
    // An intrusive reference counting smart pointer.
 | 
						|
    // T must implement addRef() and release() methods
 | 
						|
    // typically implementing the IShared interface
 | 
						|
    template<typename T>
 | 
						|
    class Ptr {
 | 
						|
    public:
 | 
						|
        Ptr() : m_p( CATCH_NULL ){}
 | 
						|
        Ptr( T* p ) : m_p( p ){
 | 
						|
            if( m_p )
 | 
						|
                m_p->addRef();
 | 
						|
        }
 | 
						|
        Ptr( Ptr const& other ) : m_p( other.m_p ){
 | 
						|
            if( m_p )
 | 
						|
                m_p->addRef();
 | 
						|
        }
 | 
						|
        ~Ptr(){
 | 
						|
            if( m_p )
 | 
						|
                m_p->release();
 | 
						|
        }
 | 
						|
        void reset() {
 | 
						|
            if( m_p )
 | 
						|
                m_p->release();
 | 
						|
            m_p = CATCH_NULL;
 | 
						|
        }
 | 
						|
        Ptr& operator = ( T* p ){
 | 
						|
            Ptr temp( p );
 | 
						|
            swap( temp );
 | 
						|
            return *this;
 | 
						|
        }
 | 
						|
        Ptr& operator = ( Ptr const& other ){
 | 
						|
            Ptr temp( other );
 | 
						|
            swap( temp );
 | 
						|
            return *this;
 | 
						|
        }
 | 
						|
        void swap( Ptr& other ) { std::swap( m_p, other.m_p ); }
 | 
						|
        T* get() const{ return m_p; }
 | 
						|
        T& operator*() const { return *m_p; }
 | 
						|
        T* operator->() const { return m_p; }
 | 
						|
        bool operator !() const { return m_p == CATCH_NULL; }
 | 
						|
        operator SafeBool::type() const { return SafeBool::makeSafe( m_p != CATCH_NULL ); }
 | 
						|
 | 
						|
    private:
 | 
						|
        T* m_p;
 | 
						|
    };
 | 
						|
 | 
						|
    struct IShared : NonCopyable {
 | 
						|
        virtual ~IShared();
 | 
						|
        virtual void addRef() const = 0;
 | 
						|
        virtual void release() const = 0;
 | 
						|
    };
 | 
						|
 | 
						|
    template<typename T = IShared>
 | 
						|
    struct SharedImpl : T {
 | 
						|
 | 
						|
        SharedImpl() : m_rc( 0 ){}
 | 
						|
 | 
						|
        virtual void addRef() const {
 | 
						|
            ++m_rc;
 | 
						|
        }
 | 
						|
        virtual void release() const {
 | 
						|
            if( --m_rc == 0 )
 | 
						|
                delete this;
 | 
						|
        }
 | 
						|
 | 
						|
        mutable unsigned int m_rc;
 | 
						|
    };
 | 
						|
 | 
						|
} // end namespace Catch
 | 
						|
 | 
						|
#ifdef __clang__
 | 
						|
#pragma clang diagnostic pop
 | 
						|
#endif
 | 
						|
 | 
						|
#endif // TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
 |