1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
//
// Copyright 1997-2009 Torsten Rohlfing
//
// Copyright 2004-2011 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 5436 $
//
// $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#ifndef __cmtkSmartPtr_h_included_
#define __cmtkSmartPtr_h_included_
#include <cmtkconfig.h>
#ifndef NULL
# define NULL 0
#endif
#include <algorithm>
#include <cassert>
#include <System/cmtkSmartConstPtr.h>
namespace
cmtk
{
/** \addtogroup System */
//@{
/** Smart pointer with reference counting.
*/
template<class T>
class SmartPointer :
/// Inherit from smart pointer-to-const.
public SmartConstPointer<T>
{
public:
/// This class instance.
typedef SmartPointer<T> Self;
/// This class instance.
typedef SmartConstPointer<T> Superclass;
/// Use "const" inherited member functions.
using Superclass::operator*;
using Superclass::operator->;
using Superclass::ReleasePtr;
/// The underlying raw pointer type.
typedef T* PointerType;
/// Reference to static null object.
static Self& Null()
{
static Self null;
return null;
}
/** Default constructor.
*/
SmartPointer() {}
/** Construct from dumb pointer.
* Note that you MUST NEVER use this constructor more than once for each
* dumb pointer, other than NULL!
*/
explicit SmartPointer( T *const object ) : SmartConstPointer<T>( object ) {}
/** Copy constructor template.
*/
template<class T2>
SmartPointer( const SmartPointer<T2>& ptr ) : SmartConstPointer<T>( ptr ) {}
/** Copy constructor to prevent compiler-generated copy constructor.
*/
SmartPointer( const Self& ptr ) : SmartConstPointer<T>( ptr ) {}
/// De-referencing operator (returns non-constant object).
T& operator*() { return *this->m_Object.ptr; }
/// De-referencing operator (returns non-constant object pointer).
T* operator->() { return this->m_Object.ptr; }
/// Explicit conversion to non-constant pointer.
T* GetPtr() const { return this->m_Object.ptr; }
/** Release control of this pointer.
*\note This is a dangerous function. Be sure you know what you are doing!
*/
T* ReleasePtr()
{
T* object = this->m_Object.ptr;
this->m_Object.ptr = NULL;
return object;
}
///Dynamic cast between smart pointer types.
template<class T2>
static Self DynamicCastFrom( const T2& from_P )
{
return Self( dynamic_cast<typename Self::PointerType>( from_P.GetPtr() ), from_P.m_ReferenceCount );
}
private:
/** Construct from dumb pointer and existing reference counter.
* The reference counter is increased in the process.
*/
SmartPointer( T *const object, SafeCounter *const counter ) : Superclass( object, counter ) {}
/// Make all template instances friends for easy type casting.
template<class T2> friend class SmartPointer;
};
//@}
} // namespace cmtk
#endif // define __cmtkSmartPtr_h_included_
|