2012-05-04 08:55:11 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2012-05-04 08:55:11 +02:00
|
|
|
// An intrusive reference counting smart pointer.
|
|
|
|
// T must implement addRef() and release() methods
|
|
|
|
// typically implementing the IShared interface
|
|
|
|
template<typename T>
|
2012-05-15 09:02:36 +02:00
|
|
|
class Ptr {
|
2012-05-04 08:55:11 +02:00
|
|
|
public:
|
|
|
|
Ptr() : m_p( NULL ){}
|
|
|
|
Ptr( T* p ) : m_p( p ){
|
2012-07-28 21:37:07 +02:00
|
|
|
if( m_p )
|
|
|
|
m_p->addRef();
|
2012-05-04 08:55:11 +02:00
|
|
|
}
|
|
|
|
Ptr( const Ptr& other ) : m_p( other.m_p ){
|
2012-07-28 21:37:07 +02:00
|
|
|
if( m_p )
|
|
|
|
m_p->addRef();
|
2012-05-04 08:55:11 +02:00
|
|
|
}
|
|
|
|
~Ptr(){
|
|
|
|
if( m_p )
|
|
|
|
m_p->release();
|
|
|
|
}
|
|
|
|
Ptr& operator = ( T* p ){
|
|
|
|
Ptr temp( p );
|
|
|
|
swap( temp );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Ptr& operator = ( Ptr& other ){
|
|
|
|
Ptr temp( other );
|
|
|
|
swap( temp );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void swap( Ptr& other ){
|
|
|
|
std::swap( m_p, other.m_p );
|
|
|
|
}
|
|
|
|
|
|
|
|
T* get(){
|
|
|
|
return m_p;
|
|
|
|
}
|
|
|
|
const T* get() const{
|
|
|
|
return m_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*(){
|
|
|
|
return *m_p;
|
|
|
|
}
|
|
|
|
const T& operator*() const{
|
|
|
|
return *m_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->(){
|
|
|
|
return m_p;
|
|
|
|
}
|
|
|
|
const T* operator->() const{
|
|
|
|
return m_p;
|
|
|
|
}
|
2012-07-28 21:37:07 +02:00
|
|
|
bool operator !() const {
|
|
|
|
return m_p == NULL;
|
|
|
|
}
|
2012-05-04 08:55:11 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_p;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IShared : NonCopyable {
|
|
|
|
virtual ~IShared(){}
|
|
|
|
virtual void addRef() = 0;
|
|
|
|
virtual void release() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct SharedImpl : T {
|
|
|
|
|
|
|
|
SharedImpl() : m_rc( 0 ){}
|
|
|
|
|
|
|
|
virtual void addRef(){
|
|
|
|
++m_rc;
|
|
|
|
}
|
|
|
|
virtual void release(){
|
|
|
|
if( --m_rc == 0 )
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_rc;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
|