Loading...
Searching...
No Matches
CountedPtr.h
Go to the documentation of this file.
//# CountedPtr.h: Referenced counted pointer classes
//# Copyright (C) 1993,1994,1995,1996,1999,2001
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#ifndef CASA_COUNTEDPTR_H
#define CASA_COUNTEDPTR_H
#include <casacore/casa/aips.h>
#include <memory>
//# Define the old names for backward compatibility.
#define SHARED_PTR std::shared_ptr
#define DYNAMIC_POINTER_CAST std::dynamic_pointer_cast
#define CONST_POINTER_CAST std::const_pointer_cast
#define STATIC_POINTER_CAST std::static_pointer_cast
namespace casacore { //#Begin casa namespace
// <summary> act on dereference error </summary>
// <synopsis>
// Global function that throws an exception. It is called by the
// member functions of the counted pointer classes when an
// un-initialized (null) pointer is followed.
// </synopsis>
// <group name=dereference_error>
void throw_Null_CountedPtr_dereference_error();
// </group>
// <summary>Referenced counted pointer for constant data</summary>
// <use visibility=export>
// <reviewed reviewer="Friso Olnon" date="1995/03/15" tests="tCountedPtr" demos="">
// <etymology>
// This class is <em>Counted</em> because it is reference counted.
// </etymology>
// <synopsis>
// This class implements a reference counting mechanism. It
// allows <src>CountedPtr</src>s to be passed around freely,
// incrementing or decrementing the reference count as needed when one
// <src>CountedPtr</src> is assigned to another. When the
// reference count reaches zero the internal storage is deleted by
// default, but this behavior can be overridden.
//
// Internally the class uses std::shared_ptr to be thread-safe. Note that
// tr1 is used if the compiler does not support C++11 yet.
// </synopsis>
// <motivation>
// Reference counting
// </motivation>
template<class t>
class CountedPtr
{
protected:
// Helper class to make deletion of object optional.
template <typename T>
class Deleter {
public:
Deleter (Bool deleteIt) : reallyDeleteIt_p (deleteIt) {}
void operator() (T * data) const { if (reallyDeleteIt_p) delete data;}
private:
Bool reallyDeleteIt_p;
};
public:
// This constructor allows for the creation of a null
// <src>CountedPtr</src>. The assignment operator can be used
// to assign a null <src>CountedPtr</src> from another
// pointer.
//
CountedPtr() : pointerRep_p () {}
// This constructor sets up a reference count for the <src>val</src>
// pointer. By default, the data pointed to by <src>val</src>
// will be deleted when it is no longer referenced. Passing in
// <src>False</src> for <src>delit</src> will prevent the data
// from being deleted when the reference count reaches zero.
//
// <note role=warning> After the counted pointer is initialized
// the value should no longer be manipulated by the raw pointer of
// type <src>t*</src>.
// </note>
CountedPtr(t *val, Bool delit = True)
: pointerRep_p (val, Deleter<t> (delit))
{}
// This copy constructor allows <src>CountedPtr</src>s to be
// initialized from other <src>CountedPtr</src>s for which the pointer TP*
// is convertible to T*.
template<typename TP>
CountedPtr(const CountedPtr<TP>& that)
: pointerRep_p(that.pointerRep_p)
{}
// Create from a shared_ptr.
CountedPtr (const std::shared_ptr<t>& rep)
: pointerRep_p (rep)
{}
// This destructor only deletes the really stored data when it was
// initialized as deletable and the reference count is zero.
~CountedPtr() {}
// This assignment operator allows <src>CountedPtr</src>s to be
// copied from other <src>CountedPtr</src>s for which the pointer TP*
// is convertible to t*.
template<typename TP>
CountedPtr<t>& operator=(const CountedPtr<TP>& that)
{
pointerRep_p = that.pointerRep_p;
return *this;
}
// Reset the pointer.
// <group>
void reset (t *val, Bool delit=True)
{ pointerRep_p = PointerRep (val, Deleter<t>(delit)); }
void reset()
{ pointerRep_p.reset(); }
// </group>
// The <src>CountedPtr</src> indirection operator simply
// returns a reference to the value being protected. If the pointer
// is un-initialized (null), an exception will be thrown. The member
// function
// <linkto class="CountedPtr:null()const">null</linkto>()
// can be used to catch such a condition in time.
// <note role=tip> The address of the reference returned should
// not be stored for later use.
// </note>
t &operator*() const {
if (null()){
throw_Null_CountedPtr_dereference_error();
}
return pointerRep_p.operator* ();
}
// This dereferencing operator behaves as expected; it returns the
// pointer to the value being protected, and then its dereferencing
// operator will be invoked as appropriate. If the pointer is
// un-initialized (null), an exception will be thrown. The member
// function
// <linkto class="CountedPtr:null()const">null</linkto>()
// can be used to catch such a condition in time.
t *operator->() const {
return get ();
}
// Get the underlying pointer.
t* get () const {
return pointerRep_p.get();
}
// Equality operator which checks to see if two
// <src>CountedPtr</src>s are pointing at the same thing.
Bool operator==(const CountedPtr<t> &other) const {
return (get() == other.get());
}
//# Note: use of const void* gives ambiguius overload error.
Bool operator==(int ptr) const {
return (ptr == 0 && get() == 0);
}
// Non-equality operator which checks to see if two
// <src>CountedPtr</src>s are not pointing at the same thing.
Bool operator!=(const CountedPtr<t> &other) const {
return (get() != other.get() ? True : False);
}
//# Note: use of const void* gives ambiguius overload error.
Bool operator!=(int ptr) const {
return (ptr != 0 || get() != 0);
}
// This assignment operator allows the object to which the current
// <src>CountedPtr</src> points to be changed.
CountedPtr<t> &
operator=(t *v)
{
pointerRep_p = PointerRep (v);
return * this;
}
// Cast functions.
// <group>
template<typename U>
CountedPtr<U> static_ptr_cast() const
{ return CountedPtr<U> (std::static_pointer_cast<U> (pointerRep_p)); }
template<typename U>
CountedPtr<U> const_ptr_cast() const
{ return CountedPtr<U> (std::const_pointer_cast<U> (pointerRep_p)); }
template<typename U>
CountedPtr<U> dynamic_ptr_cast() const
{ return CountedPtr<U> (std::dynamic_pointer_cast<U> (pointerRep_p)); }
// </group>
// Sometimes it is useful to know if there is more than one
// reference made. This is a way of getting that. Of course the point
// of these classes is that this information is normally not required.
uInt nrefs() const
{ return pointerRep_p.use_count(); }
// Check to see if this <src>CountedPtr</src> is
// un-initialized, null.
Bool null() const
{ return get() == 0; }
// Test if it contains a valid pointer.
operator bool() const
{ return get() != 0; }
private:
// Make all types of CountedPtr a friend for the templated operator=.
template<typename TP> friend class CountedPtr;
typedef std::shared_ptr<t> PointerRep;
PointerRep pointerRep_p;
};
// A shared_ptr is used as implementation.
inline Bool countedPtrShared()
{ return True; }
// Cast the CountedPtr from one pointer type to another.
template<typename T, typename U>
CountedPtr<T> static_pointer_cast (const CountedPtr<U>& that)
{ return that.template static_ptr_cast<T>(); }
template<typename T, typename U>
CountedPtr<T> const_pointer_cast (const CountedPtr<U>& that)
{ return that.template const_ptr_cast<T>(); }
template<typename T, typename U>
CountedPtr<T> dynamic_pointer_cast (const CountedPtr<U>& that)
{ return that.template dynamic_ptr_cast<T>(); }
} //#End casa namespace
#ifndef CASACORE_NO_AUTO_TEMPLATES
#include <casacore/casa/Utilities/CountedPtr.tcc>
#endif //# CASACORE_NO_AUTO_TEMPLATES
#endif
casacore::CountedPtr::Deleter
Helper class to make deletion of object optional.
Definition CountedPtr.h:86
casacore::CountedPtr::Deleter::Deleter
Deleter(Bool deleteIt)
Definition CountedPtr.h:88
casacore::CountedPtr::Deleter::reallyDeleteIt_p
Bool reallyDeleteIt_p
Definition CountedPtr.h:91
casacore::CountedPtr::Deleter::operator()
void operator()(T *data) const
Definition CountedPtr.h:89
casacore::CountedPtr
Referenced counted pointer for constant data.
Definition CountedPtr.h:81
casacore::CountedPtr::operator->
t * operator->() const
This dereferencing operator behaves as expected; it returns the pointer to the value being protected,...
Definition CountedPtr.h:178
casacore::CountedPtr::static_ptr_cast
CountedPtr< U > static_ptr_cast() const
Cast functions.
Definition CountedPtr.h:219
casacore::CountedPtr::PointerRep
std::shared_ptr< t > PointerRep
Definition CountedPtr.h:248
casacore::CountedPtr::pointerRep_p
PointerRep pointerRep_p
Definition CountedPtr.h:250
casacore::CountedPtr::dynamic_ptr_cast
CountedPtr< U > dynamic_ptr_cast() const
Definition CountedPtr.h:225
casacore::CountedPtr::operator==
Bool operator==(const CountedPtr< t > &other) const
Equality operator which checks to see if two CountedPtrs are pointing at the same thing.
Definition CountedPtr.h:189
casacore::CountedPtr::~CountedPtr
~CountedPtr()
This destructor only deletes the really stored data when it was initialized as deletable and the refe...
Definition CountedPtr.h:135
casacore::CountedPtr::operator=
CountedPtr< t > & operator=(const CountedPtr< TP > &that)
This assignment operator allows CountedPtrs to be copied from other CountedPtrs for which the pointer...
Definition CountedPtr.h:141
casacore::CountedPtr::CountedPtr
CountedPtr(const std::shared_ptr< t > &rep)
Create from a shared_ptr.
Definition CountedPtr.h:129
casacore::CountedPtr::reset
void reset()
Definition CountedPtr.h:151
casacore::CountedPtr::operator=
CountedPtr< t > & operator=(t *v)
This assignment operator allows the object to which the current CountedPtr points to be changed.
Definition CountedPtr.h:210
casacore::CountedPtr::reset
void reset(t *val, Bool delit=True)
Reset the pointer.
Definition CountedPtr.h:149
casacore::CountedPtr::null
Bool null() const
Check to see if this CountedPtr is un-initialized, null.
Definition CountedPtr.h:237
casacore::CountedPtr::operator!=
Bool operator!=(int ptr) const
Definition CountedPtr.h:203
casacore::CountedPtr::const_ptr_cast
CountedPtr< U > const_ptr_cast() const
Definition CountedPtr.h:222
casacore::CountedPtr::operator==
Bool operator==(int ptr) const
Definition CountedPtr.h:193
casacore::CountedPtr::operator*
t & operator*() const
The CountedPtr indirection operator simply returns a reference to the value being protected.
Definition CountedPtr.h:164
casacore::CountedPtr::operator!=
Bool operator!=(const CountedPtr< t > &other) const
Non-equality operator which checks to see if two CountedPtrs are not pointing at the same thing.
Definition CountedPtr.h:199
casacore::CountedPtr::CountedPtr
CountedPtr(const CountedPtr< TP > &that)
This copy constructor allows CountedPtrs to be initialized from other CountedPtrs for which the point...
Definition CountedPtr.h:124
casacore::CountedPtr::CountedPtr
CountedPtr(t *val, Bool delit=True)
This constructor sets up a reference count for the val pointer.
Definition CountedPtr.h:116
casacore::CountedPtr::CountedPtr
CountedPtr()
This constructor allows for the creation of a null CountedPtr.
Definition CountedPtr.h:104
casacore::CountedPtr::CountedPtr
friend class CountedPtr
Make all types of CountedPtr a friend for the templated operator=.
Definition CountedPtr.h:246
casacore::CountedPtr::nrefs
uInt nrefs() const
Sometimes it is useful to know if there is more than one reference made.
Definition CountedPtr.h:232
casacore::CountedPtr::get
t * get() const
Get the underlying pointer.
Definition CountedPtr.h:183
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::False
const Bool False
Definition aipstype.h:42
casacore::uInt
unsigned int uInt
Definition aipstype.h:49
casacore::const_pointer_cast
CountedPtr< T > const_pointer_cast(const CountedPtr< U > &that)
Definition CountedPtr.h:262
casacore::static_pointer_cast
CountedPtr< T > static_pointer_cast(const CountedPtr< U > &that)
Cast the CountedPtr from one pointer type to another.
Definition CountedPtr.h:259
casacore::countedPtrShared
Bool countedPtrShared()
A shared_ptr is used as implementation.
Definition CountedPtr.h:254
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
casacore::True
const Bool True
Definition aipstype.h:41
casacore::dynamic_pointer_cast
CountedPtr< T > dynamic_pointer_cast(const CountedPtr< U > &that)
Definition CountedPtr.h:265
casacore::CountedPtr_global_functions_dereference_error
Definition CountedPtr.h:51
casacore::CountedPtr_global_functions_dereference_error::throw_Null_CountedPtr_dereference_error
void throw_Null_CountedPtr_dereference_error()