2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
|
// This code accompanies the book:
|
|
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
|
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The author or Addison-Wesley Longman make no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
|
#ifndef LOKI_SMARTPTR_INC_
|
|
|
|
|
#define LOKI_SMARTPTR_INC_
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-01-16 19:05:09 +00:00
|
|
|
|
// $Header$
|
|
|
|
|
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \defgroup SmartPointerGroup Smart pointers
|
|
|
|
|
/// Policy based implementation of a smart pointer
|
|
|
|
|
/// \defgroup SmartPointerOwnershipGroup Ownership policies
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
|
|
|
|
/// \defgroup SmartPointerStorageGroup Storage policies
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
|
|
|
|
/// \defgroup SmartPointerConversionGroup Conversion policies
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
|
|
|
|
/// \defgroup SmartPointerCheckingGroup Checking policies
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-02-27 19:59:20 +00:00
|
|
|
|
#include "LokiExport.h"
|
2002-08-11 05:49:45 +00:00
|
|
|
|
#include "SmallObj.h"
|
|
|
|
|
#include "TypeManip.h"
|
|
|
|
|
#include "static_check.h"
|
2006-02-14 11:54:46 +00:00
|
|
|
|
#include "RefToValue.h"
|
2006-02-19 22:04:28 +00:00
|
|
|
|
#include "ConstPolicy.h"
|
2006-02-14 11:54:46 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <cassert>
|
2006-03-17 22:52:56 +00:00
|
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#endif
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
namespace Loki
|
|
|
|
|
{
|
|
|
|
|
|
2006-03-27 18:34:36 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
/// \class HeapStorage
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerStorageGroup
|
|
|
|
|
/// Implementation of the StoragePolicy used by SmartPtr. Uses explicit call
|
|
|
|
|
/// to T's destructor followed by call to free.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
class HeapStorage
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef T* StoredType; // the type of the pointee_ object
|
|
|
|
|
typedef T* PointerType; // type returned by operator->
|
|
|
|
|
typedef T& ReferenceType; // type returned by operator*
|
|
|
|
|
|
|
|
|
|
HeapStorage() : pointee_(Default())
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// The storage policy doesn't initialize the stored pointer
|
|
|
|
|
// which will be initialized by the OwnershipPolicy's Clone fn
|
|
|
|
|
HeapStorage(const HeapStorage&) : pointee_(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
HeapStorage(const HeapStorage<U>&) : pointee_(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
HeapStorage(const StoredType& p) : pointee_(p) {}
|
|
|
|
|
|
|
|
|
|
PointerType operator->() const { return pointee_; }
|
|
|
|
|
|
|
|
|
|
ReferenceType operator*() const { return *pointee_; }
|
|
|
|
|
|
|
|
|
|
void Swap(HeapStorage& rhs)
|
|
|
|
|
{ std::swap(pointee_, rhs.pointee_); }
|
|
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
friend inline PointerType GetImpl(const HeapStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
friend inline const StoredType& GetImplRef(const HeapStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
friend inline StoredType& GetImplRef(HeapStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Destroys the data stored
|
|
|
|
|
// (Destruction might be taken over by the OwnershipPolicy)
|
|
|
|
|
void Destroy()
|
|
|
|
|
{
|
2006-03-27 18:38:30 +00:00
|
|
|
|
if ( 0 != pointee_ )
|
|
|
|
|
{
|
|
|
|
|
pointee_->~T();
|
|
|
|
|
::free( pointee_ );
|
|
|
|
|
}
|
2006-03-27 18:34:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default value to initialize the pointer
|
|
|
|
|
static StoredType Default()
|
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Data
|
|
|
|
|
StoredType pointee_;
|
|
|
|
|
};
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class DefaultSPStorage
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerStorageGroup
|
|
|
|
|
/// Implementation of the StoragePolicy used by SmartPtr
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
class DefaultSPStorage
|
|
|
|
|
{
|
2003-02-02 16:35:14 +00:00
|
|
|
|
public:
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typedef T* StoredType; // the type of the pointee_ object
|
|
|
|
|
typedef T* PointerType; // type returned by operator->
|
|
|
|
|
typedef T& ReferenceType; // type returned by operator*
|
2003-02-02 16:35:14 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
DefaultSPStorage() : pointee_(Default())
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// The storage policy doesn't initialize the stored pointer
|
|
|
|
|
// which will be initialized by the OwnershipPolicy's Clone fn
|
2006-01-18 17:21:31 +00:00
|
|
|
|
DefaultSPStorage(const DefaultSPStorage&) : pointee_(0)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class U>
|
2006-01-18 17:21:31 +00:00
|
|
|
|
DefaultSPStorage(const DefaultSPStorage<U>&) : pointee_(0)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
DefaultSPStorage(const StoredType& p) : pointee_(p) {}
|
|
|
|
|
|
|
|
|
|
PointerType operator->() const { return pointee_; }
|
|
|
|
|
|
|
|
|
|
ReferenceType operator*() const { return *pointee_; }
|
|
|
|
|
|
|
|
|
|
void Swap(DefaultSPStorage& rhs)
|
|
|
|
|
{ std::swap(pointee_, rhs.pointee_); }
|
|
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
friend inline PointerType GetImpl(const DefaultSPStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
2003-08-21 12:55:14 +00:00
|
|
|
|
friend inline const StoredType& GetImplRef(const DefaultSPStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2003-08-21 12:55:14 +00:00
|
|
|
|
friend inline StoredType& GetImplRef(DefaultSPStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Destroys the data stored
|
|
|
|
|
// (Destruction might be taken over by the OwnershipPolicy)
|
|
|
|
|
void Destroy()
|
2006-03-17 22:52:56 +00:00
|
|
|
|
{
|
|
|
|
|
delete pointee_;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
// Default value to initialize the pointer
|
|
|
|
|
static StoredType Default()
|
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Data
|
|
|
|
|
StoredType pointee_;
|
|
|
|
|
};
|
|
|
|
|
|
2006-01-30 20:07:38 +00:00
|
|
|
|
|
2006-01-30 20:01:37 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
/// \class ArrayStorage
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerStorageGroup
|
|
|
|
|
/// Implementation of the ArrayStorage used by SmartPtr
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-01-30 20:07:38 +00:00
|
|
|
|
template <class T>
|
2006-01-30 20:01:37 +00:00
|
|
|
|
class ArrayStorage
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef T* StoredType; // the type of the pointee_ object
|
|
|
|
|
typedef T* PointerType; // type returned by operator->
|
|
|
|
|
typedef T& ReferenceType; // type returned by operator*
|
|
|
|
|
|
|
|
|
|
ArrayStorage() : pointee_(Default())
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// The storage policy doesn't initialize the stored pointer
|
|
|
|
|
// which will be initialized by the OwnershipPolicy's Clone fn
|
|
|
|
|
ArrayStorage(const ArrayStorage&) : pointee_(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
ArrayStorage(const ArrayStorage<U>&) : pointee_(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
ArrayStorage(const StoredType& p) : pointee_(p) {}
|
|
|
|
|
|
|
|
|
|
PointerType operator->() const { return pointee_; }
|
|
|
|
|
|
|
|
|
|
ReferenceType operator*() const { return *pointee_; }
|
|
|
|
|
|
|
|
|
|
void Swap(ArrayStorage& rhs)
|
|
|
|
|
{ std::swap(pointee_, rhs.pointee_); }
|
|
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
friend inline PointerType GetImpl(const ArrayStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
friend inline const StoredType& GetImplRef(const ArrayStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
friend inline StoredType& GetImplRef(ArrayStorage& sp)
|
|
|
|
|
{ return sp.pointee_; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Destroys the data stored
|
|
|
|
|
// (Destruction might be taken over by the OwnershipPolicy)
|
|
|
|
|
void Destroy()
|
|
|
|
|
{ delete [] pointee_; }
|
|
|
|
|
|
|
|
|
|
// Default value to initialize the pointer
|
|
|
|
|
static StoredType Default()
|
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Data
|
|
|
|
|
StoredType pointee_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class RefCounted
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Provides a classic external reference counting implementation
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
class RefCounted
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
RefCounted()
|
2006-01-27 08:58:17 +00:00
|
|
|
|
: pCount_(static_cast<uintptr_t*>(
|
|
|
|
|
SmallObject<>::operator new(sizeof(uintptr_t))))
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2006-01-27 08:58:17 +00:00
|
|
|
|
assert(pCount_!=0);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
*pCount_ = 1;
|
|
|
|
|
}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
RefCounted(const RefCounted& rhs)
|
|
|
|
|
: pCount_(rhs.pCount_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// MWCW lacks template friends, hence the following kludge
|
|
|
|
|
template <typename P1>
|
|
|
|
|
RefCounted(const RefCounted<P1>& rhs)
|
|
|
|
|
: pCount_(reinterpret_cast<const RefCounted&>(rhs).pCount_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
P Clone(const P& val)
|
|
|
|
|
{
|
|
|
|
|
++*pCount_;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Release(const P&)
|
|
|
|
|
{
|
|
|
|
|
if (!--*pCount_)
|
|
|
|
|
{
|
2006-01-27 08:58:17 +00:00
|
|
|
|
SmallObject<>::operator delete(pCount_, sizeof(uintptr_t));
|
2006-02-25 01:52:17 +00:00
|
|
|
|
pCount_ = NULL;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Swap(RefCounted& rhs)
|
|
|
|
|
{ std::swap(pCount_, rhs.pCount_); }
|
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Data
|
2006-01-27 08:58:17 +00:00
|
|
|
|
uintptr_t* pCount_;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct RefCountedMT
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Implements external reference counting for multithreaded programs
|
|
|
|
|
/// Policy Usage: RefCountedMTAdj<ThreadingModel>::RefCountedMT
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2003-02-27 20:09:08 +00:00
|
|
|
|
|
2006-01-22 13:31:45 +00:00
|
|
|
|
template <template <class, class> class ThreadingModel,
|
2006-01-30 20:07:38 +00:00
|
|
|
|
class MX = LOKI_DEFAULT_MUTEX >
|
2003-02-27 20:09:08 +00:00
|
|
|
|
struct RefCountedMTAdj
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2003-02-27 20:09:08 +00:00
|
|
|
|
template <class P>
|
2006-01-22 13:31:45 +00:00
|
|
|
|
class RefCountedMT : public ThreadingModel< RefCountedMT<P>, MX >
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2006-01-22 13:31:45 +00:00
|
|
|
|
typedef ThreadingModel< RefCountedMT<P>, MX > base_type;
|
2003-02-27 20:09:08 +00:00
|
|
|
|
typedef typename base_type::IntType CountType;
|
|
|
|
|
typedef volatile CountType *CountPtrType;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
RefCountedMT()
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2003-02-27 20:09:08 +00:00
|
|
|
|
pCount_ = static_cast<CountPtrType>(
|
2005-12-17 13:15:30 +00:00
|
|
|
|
SmallObject<LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL>::operator new(
|
2003-02-27 20:09:08 +00:00
|
|
|
|
sizeof(*pCount_)));
|
|
|
|
|
assert(pCount_);
|
2005-08-27 09:21:02 +00:00
|
|
|
|
//*pCount_ = 1;
|
2006-01-22 13:31:45 +00:00
|
|
|
|
ThreadingModel<RefCountedMT, MX>::AtomicAssign(*pCount_, 1);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-02-27 20:09:08 +00:00
|
|
|
|
RefCountedMT(const RefCountedMT& rhs)
|
|
|
|
|
: pCount_(rhs.pCount_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
//MWCW lacks template friends, hence the following kludge
|
|
|
|
|
template <typename P1>
|
|
|
|
|
RefCountedMT(const RefCountedMT<P1>& rhs)
|
|
|
|
|
: pCount_(reinterpret_cast<const RefCountedMT<P>&>(rhs).pCount_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
P Clone(const P& val)
|
|
|
|
|
{
|
2006-01-22 13:31:45 +00:00
|
|
|
|
ThreadingModel<RefCountedMT, MX>::AtomicIncrement(*pCount_);
|
2003-02-27 20:09:08 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Release(const P&)
|
|
|
|
|
{
|
2006-01-22 13:31:45 +00:00
|
|
|
|
if (!ThreadingModel<RefCountedMT, MX>::AtomicDecrement(*pCount_))
|
2003-02-27 20:09:08 +00:00
|
|
|
|
{
|
2005-12-17 13:15:30 +00:00
|
|
|
|
SmallObject<LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL>::operator delete(
|
2003-02-27 20:09:08 +00:00
|
|
|
|
const_cast<CountType *>(pCount_),
|
|
|
|
|
sizeof(*pCount_));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Swap(RefCountedMT& rhs)
|
|
|
|
|
{ std::swap(pCount_, rhs.pCount_); }
|
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Data
|
|
|
|
|
CountPtrType pCount_;
|
|
|
|
|
};
|
2002-08-11 05:49:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class COMRefCounted
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Adapts COM intrusive reference counting to OwnershipPolicy-specific syntax
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
class COMRefCounted
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
COMRefCounted()
|
|
|
|
|
{}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template <class U>
|
|
|
|
|
COMRefCounted(const COMRefCounted<U>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static P Clone(const P& val)
|
|
|
|
|
{
|
2005-10-21 14:17:46 +00:00
|
|
|
|
if(val!=0)
|
|
|
|
|
val->AddRef();
|
2002-08-11 05:49:45 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Release(const P& val)
|
2005-10-21 14:17:46 +00:00
|
|
|
|
{
|
|
|
|
|
if(val!=0)
|
|
|
|
|
val->Release();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
|
|
|
|
|
static void Swap(COMRefCounted&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct DeepCopy
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Implements deep copy semantics, assumes existence of a Clone() member
|
|
|
|
|
/// function of the pointee type
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct DeepCopy
|
|
|
|
|
{
|
|
|
|
|
DeepCopy()
|
|
|
|
|
{}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template <class P1>
|
|
|
|
|
DeepCopy(const DeepCopy<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static P Clone(const P& val)
|
|
|
|
|
{ return val->Clone(); }
|
|
|
|
|
|
2006-02-25 01:52:17 +00:00
|
|
|
|
static bool Release(const P&)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
|
|
static void Swap(DeepCopy&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class RefLinked
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Implements reference linking
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
namespace Private
|
|
|
|
|
{
|
2006-02-27 19:59:20 +00:00
|
|
|
|
class LOKI_EXPORT RefLinkedBase
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
RefLinkedBase()
|
|
|
|
|
{ prev_ = next_ = this; }
|
2006-02-08 18:12:29 +00:00
|
|
|
|
|
2006-02-25 01:52:17 +00:00
|
|
|
|
RefLinkedBase(const RefLinkedBase& rhs);
|
|
|
|
|
|
|
|
|
|
bool Release();
|
|
|
|
|
|
|
|
|
|
void Swap(RefLinkedBase& rhs);
|
|
|
|
|
|
2006-03-17 22:52:56 +00:00
|
|
|
|
bool Merge( RefLinkedBase & rhs );
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
|
|
|
|
|
private:
|
2006-03-17 22:52:56 +00:00
|
|
|
|
static unsigned int CountPrevCycle( const RefLinkedBase * pThis );
|
|
|
|
|
static unsigned int CountNextCycle( const RefLinkedBase * pThis );
|
|
|
|
|
bool HasPrevNode( const RefLinkedBase * p ) const;
|
|
|
|
|
bool HasNextNode( const RefLinkedBase * p ) const;
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
mutable const RefLinkedBase* prev_;
|
|
|
|
|
mutable const RefLinkedBase* next_;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
class RefLinked : public Private::RefLinkedBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
RefLinked()
|
|
|
|
|
{}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template <class P1>
|
|
|
|
|
RefLinked(const RefLinked<P1>& rhs)
|
|
|
|
|
: Private::RefLinkedBase(rhs)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static P Clone(const P& val)
|
|
|
|
|
{ return val; }
|
|
|
|
|
|
|
|
|
|
bool Release(const P&)
|
|
|
|
|
{ return Private::RefLinkedBase::Release(); }
|
2006-03-17 22:52:56 +00:00
|
|
|
|
|
|
|
|
|
template < class P1 >
|
|
|
|
|
bool Merge( RefLinked< P1 > & rhs )
|
|
|
|
|
{
|
|
|
|
|
return Private::RefLinkedBase::Merge( rhs );
|
|
|
|
|
}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class DestructiveCopy
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Implements destructive copy semantics (a la std::auto_ptr)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
class DestructiveCopy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DestructiveCopy()
|
|
|
|
|
{}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template <class P1>
|
|
|
|
|
DestructiveCopy(const DestructiveCopy<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
static P Clone(P1& val)
|
|
|
|
|
{
|
|
|
|
|
P result(val);
|
|
|
|
|
val = P1();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Release(const P&)
|
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
|
|
static void Swap(DestructiveCopy&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = true };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class NoCopy
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerOwnershipGroup
|
|
|
|
|
/// Implementation of the OwnershipPolicy used by SmartPtr
|
|
|
|
|
/// Implements a policy that doesn't allow copying objects
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
class NoCopy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
NoCopy()
|
|
|
|
|
{}
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template <class P1>
|
|
|
|
|
NoCopy(const NoCopy<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static P Clone(const P&)
|
|
|
|
|
{
|
2003-02-02 16:35:14 +00:00
|
|
|
|
// Make it depended on template parameter
|
|
|
|
|
static const bool DependedFalse = sizeof(P*) == 0;
|
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
|
LOKI_STATIC_CHECK(DependedFalse, This_Policy_Disallows_Value_Copying);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Release(const P&)
|
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
|
|
static void Swap(NoCopy&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
enum { destructiveCopy = false };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct AllowConversion
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerConversionGroup
|
|
|
|
|
/// Implementation of the ConversionPolicy used by SmartPtr
|
|
|
|
|
/// Allows implicit conversion from SmartPtr to the pointee type
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
struct AllowConversion
|
|
|
|
|
{
|
|
|
|
|
enum { allow = true };
|
|
|
|
|
|
|
|
|
|
void Swap(AllowConversion&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct DisallowConversion
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerConversionGroup
|
|
|
|
|
/// Implementation of the ConversionPolicy used by SmartPtr
|
|
|
|
|
/// Does not allow implicit conversion from SmartPtr to the pointee type
|
|
|
|
|
/// You can initialize a DisallowConversion with an AllowConversion
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
struct DisallowConversion
|
|
|
|
|
{
|
|
|
|
|
DisallowConversion()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
DisallowConversion(const AllowConversion&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
enum { allow = false };
|
|
|
|
|
|
|
|
|
|
void Swap(DisallowConversion&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct NoCheck
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Well, it's clear what it does :o)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct NoCheck
|
|
|
|
|
{
|
|
|
|
|
NoCheck()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
NoCheck(const NoCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDefault(const P&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnInit(const P&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDereference(const P&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void Swap(NoCheck&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct AssertCheck
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Checks the pointer before dereference
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct AssertCheck
|
|
|
|
|
{
|
|
|
|
|
AssertCheck()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
AssertCheck(const AssertCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
AssertCheck(const NoCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDefault(const P&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnInit(const P&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDereference(P val)
|
2003-02-27 20:09:08 +00:00
|
|
|
|
{ assert(val); (void)val; }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
static void Swap(AssertCheck&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct AssertCheckStrict
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Checks the pointer against zero upon initialization and before dereference
|
|
|
|
|
/// You can initialize an AssertCheckStrict with an AssertCheck
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct AssertCheckStrict
|
|
|
|
|
{
|
|
|
|
|
AssertCheckStrict()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
AssertCheckStrict(const AssertCheckStrict<U>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
|
AssertCheckStrict(const AssertCheck<U>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
AssertCheckStrict(const NoCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDefault(P val)
|
|
|
|
|
{ assert(val); }
|
|
|
|
|
|
|
|
|
|
static void OnInit(P val)
|
|
|
|
|
{ assert(val); }
|
|
|
|
|
|
|
|
|
|
static void OnDereference(P val)
|
|
|
|
|
{ assert(val); }
|
|
|
|
|
|
|
|
|
|
static void Swap(AssertCheckStrict&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct NullPointerException
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
|
|
|
|
/// Used by some implementations of the CheckingPolicy used by SmartPtr
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
struct NullPointerException : public std::runtime_error
|
|
|
|
|
{
|
|
|
|
|
NullPointerException() : std::runtime_error("")
|
|
|
|
|
{ }
|
2002-08-15 02:43:10 +00:00
|
|
|
|
const char* what() const throw()
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return "Null Pointer Exception"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct RejectNullStatic
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Checks the pointer upon initialization and before dereference
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct RejectNullStatic
|
|
|
|
|
{
|
|
|
|
|
RejectNullStatic()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStatic(const RejectNullStatic<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStatic(const NoCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStatic(const AssertCheck<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStatic(const AssertCheckStrict<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnDefault(const P&)
|
|
|
|
|
{
|
2003-02-02 16:35:14 +00:00
|
|
|
|
// Make it depended on template parameter
|
|
|
|
|
static const bool DependedFalse = sizeof(P*) == 0;
|
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
|
LOKI_STATIC_CHECK(DependedFalse, ERROR_This_Policy_Does_Not_Allow_Default_Initialization);
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void OnInit(const P& val)
|
|
|
|
|
{ if (!val) throw NullPointerException(); }
|
|
|
|
|
|
|
|
|
|
static void OnDereference(const P& val)
|
|
|
|
|
{ if (!val) throw NullPointerException(); }
|
|
|
|
|
|
|
|
|
|
static void Swap(RejectNullStatic&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct RejectNull
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Checks the pointer before dereference
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct RejectNull
|
|
|
|
|
{
|
|
|
|
|
RejectNull()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNull(const RejectNull<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
2006-02-28 16:55:56 +00:00
|
|
|
|
static void OnInit(P)
|
2006-02-28 12:59:59 +00:00
|
|
|
|
{}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-02-28 16:55:56 +00:00
|
|
|
|
static void OnDefault(P)
|
2006-02-28 12:59:59 +00:00
|
|
|
|
{}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
void OnDereference(P val)
|
2006-02-28 16:55:56 +00:00
|
|
|
|
{ if (!val) throw NullPointerException(); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-02-25 13:01:40 +00:00
|
|
|
|
void OnDereference(P val) const
|
2006-02-28 16:55:56 +00:00
|
|
|
|
{ if (!val) throw NullPointerException(); }
|
2006-02-25 13:01:40 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
void Swap(RejectNull&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \struct RejectNullStrict
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerCheckingGroup
|
|
|
|
|
/// Implementation of the CheckingPolicy used by SmartPtr
|
|
|
|
|
/// Checks the pointer upon initialization and before dereference
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
|
struct RejectNullStrict
|
|
|
|
|
{
|
|
|
|
|
RejectNullStrict()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStrict(const RejectNullStrict<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <class P1>
|
|
|
|
|
RejectNullStrict(const RejectNull<P1>&)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
static void OnInit(P val)
|
|
|
|
|
{ if (!val) throw NullPointerException(); }
|
|
|
|
|
|
|
|
|
|
void OnDereference(P val)
|
|
|
|
|
{ OnInit(val); }
|
2006-02-25 13:01:40 +00:00
|
|
|
|
|
|
|
|
|
void OnDereference(P val) const
|
|
|
|
|
{ OnInit(val); }
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
void Swap(RejectNullStrict&)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// class template SmartPtr (declaration)
|
|
|
|
|
// The reason for all the fuss above
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-01-30 20:07:38 +00:00
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OwnershipPolicy = RefCounted,
|
|
|
|
|
class ConversionPolicy = DisallowConversion,
|
|
|
|
|
template <class> class CheckingPolicy = AssertCheck,
|
|
|
|
|
template <class> class StoragePolicy = DefaultSPStorage,
|
|
|
|
|
template<class> class ConstnessPolicy = LOKI_DEFAULT_CONSTNESS
|
|
|
|
|
>
|
|
|
|
|
class SmartPtr;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2003-02-27 20:09:08 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// class template SmartPtrDef (definition)
|
|
|
|
|
// this class added to unify the usage of SmartPtr
|
|
|
|
|
// instead of writing SmartPtr<T,OP,CP,KP,SP> write SmartPtrDef<T,OP,CP,KP,SP>::type
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OwnershipPolicy = RefCounted,
|
|
|
|
|
class ConversionPolicy = DisallowConversion,
|
|
|
|
|
template <class> class CheckingPolicy = AssertCheck,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class StoragePolicy = DefaultSPStorage,
|
2006-03-08 17:07:20 +00:00
|
|
|
|
template<class> class ConstnessPolicy = LOKI_DEFAULT_CONSTNESS
|
2003-02-27 20:09:08 +00:00
|
|
|
|
>
|
|
|
|
|
struct SmartPtrDef
|
|
|
|
|
{
|
|
|
|
|
typedef SmartPtr
|
|
|
|
|
<
|
|
|
|
|
T,
|
|
|
|
|
OwnershipPolicy,
|
|
|
|
|
ConversionPolicy,
|
|
|
|
|
CheckingPolicy,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
StoragePolicy,
|
2006-03-08 17:07:20 +00:00
|
|
|
|
ConstnessPolicy
|
2003-02-27 20:09:08 +00:00
|
|
|
|
>
|
|
|
|
|
type;
|
|
|
|
|
};
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \class SmartPtr
|
|
|
|
|
///
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
|
|
|
|
///
|
|
|
|
|
/// \param OwnershipPolicy default = RefCounted,
|
|
|
|
|
/// \param ConversionPolicy default = DisallowConversion,
|
|
|
|
|
/// \param CheckingPolicy default = AssertCheck,
|
|
|
|
|
/// \param StoragePolicy default = DefaultSPStorage
|
2006-02-25 13:48:54 +00:00
|
|
|
|
/// \param ConstnessPolicy default = LOKI_DEFAULT_CONSTNESS
|
2005-11-03 12:43:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// \par IMPORTANT NOTE
|
|
|
|
|
/// Due to threading issues, the OwnershipPolicy has been changed as follows:
|
|
|
|
|
///
|
|
|
|
|
/// - Release() returns a boolean saying if that was the last release
|
|
|
|
|
/// so the pointer can be deleted by the StoragePolicy
|
|
|
|
|
/// - IsUnique() was removed
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OwnershipPolicy,
|
|
|
|
|
class ConversionPolicy,
|
|
|
|
|
template <class> class CheckingPolicy,
|
2006-01-30 20:01:37 +00:00
|
|
|
|
template <class> class StoragePolicy,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class ConstnessPolicy
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
|
|
|
|
class SmartPtr
|
|
|
|
|
: public StoragePolicy<T>
|
|
|
|
|
, public OwnershipPolicy<typename StoragePolicy<T>::PointerType>
|
|
|
|
|
, public CheckingPolicy<typename StoragePolicy<T>::StoredType>
|
|
|
|
|
, public ConversionPolicy
|
|
|
|
|
{
|
|
|
|
|
typedef StoragePolicy<T> SP;
|
|
|
|
|
typedef OwnershipPolicy<typename StoragePolicy<T>::PointerType> OP;
|
|
|
|
|
typedef CheckingPolicy<typename StoragePolicy<T>::StoredType> KP;
|
|
|
|
|
typedef ConversionPolicy CP;
|
|
|
|
|
|
|
|
|
|
public:
|
2006-01-30 20:07:38 +00:00
|
|
|
|
typedef typename ConstnessPolicy<T>::Type* ConstPointerType;
|
|
|
|
|
typedef typename ConstnessPolicy<T>::Type& ConstReferenceType;
|
2006-01-30 20:01:37 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typedef typename SP::PointerType PointerType;
|
|
|
|
|
typedef typename SP::StoredType StoredType;
|
|
|
|
|
typedef typename SP::ReferenceType ReferenceType;
|
|
|
|
|
|
2006-02-25 13:48:54 +00:00
|
|
|
|
typedef typename Select<OP::destructiveCopy,SmartPtr, const SmartPtr>::Result
|
|
|
|
|
CopyArg;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2003-08-21 12:55:14 +00:00
|
|
|
|
private:
|
|
|
|
|
struct NeverMatched;
|
|
|
|
|
|
|
|
|
|
#ifdef LOKI_SMARTPTR_CONVERSION_CONSTRUCTOR_POLICY
|
|
|
|
|
typedef typename Select< CP::allow, const StoredType&, NeverMatched>::Result ImplicitArg;
|
|
|
|
|
typedef typename Select<!CP::allow, const StoredType&, NeverMatched>::Result ExplicitArg;
|
|
|
|
|
#else
|
|
|
|
|
typedef const StoredType& ImplicitArg;
|
|
|
|
|
typedef typename Select<false, const StoredType&, NeverMatched>::Result ExplicitArg;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
SmartPtr()
|
|
|
|
|
{ KP::OnDefault(GetImpl(*this)); }
|
2003-08-21 12:55:14 +00:00
|
|
|
|
|
|
|
|
|
explicit
|
2006-05-17 16:04:32 +00:00
|
|
|
|
SmartPtr(ExplicitArg p) : SP(p)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ KP::OnInit(GetImpl(*this)); }
|
2003-08-21 12:55:14 +00:00
|
|
|
|
|
2006-05-17 16:04:32 +00:00
|
|
|
|
SmartPtr(ImplicitArg p) : SP(p)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
{ KP::OnInit(GetImpl(*this)); }
|
|
|
|
|
|
|
|
|
|
SmartPtr(CopyArg& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
: SP(rhs), OP(rhs), KP(rhs), CP(rhs)
|
|
|
|
|
{ GetImplRef(*this) = OP::Clone(GetImplRef(rhs)); }
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
SmartPtr(const SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
: SP(rhs), OP(rhs), KP(rhs), CP(rhs)
|
|
|
|
|
{ GetImplRef(*this) = OP::Clone(GetImplRef(rhs)); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
SmartPtr(SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
: SP(rhs), OP(rhs), KP(rhs), CP(rhs)
|
|
|
|
|
{ GetImplRef(*this) = OP::Clone(GetImplRef(rhs)); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-02-14 11:54:46 +00:00
|
|
|
|
SmartPtr(RefToValue<SmartPtr> rhs)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
: SP(rhs), OP(rhs), KP(rhs), CP(rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{}
|
|
|
|
|
|
2006-02-14 11:54:46 +00:00
|
|
|
|
operator RefToValue<SmartPtr>()
|
|
|
|
|
{ return RefToValue<SmartPtr>(*this); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2003-08-21 12:55:14 +00:00
|
|
|
|
SmartPtr& operator=(CopyArg& rhs)
|
|
|
|
|
{
|
|
|
|
|
SmartPtr temp(rhs);
|
|
|
|
|
temp.Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
SmartPtr& operator=(const SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
{
|
|
|
|
|
SmartPtr temp(rhs);
|
|
|
|
|
temp.Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
SmartPtr& operator=(SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs)
|
2003-08-21 12:55:14 +00:00
|
|
|
|
{
|
|
|
|
|
SmartPtr temp(rhs);
|
|
|
|
|
temp.Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Swap(SmartPtr& rhs)
|
|
|
|
|
{
|
|
|
|
|
OP::Swap(rhs);
|
|
|
|
|
CP::Swap(rhs);
|
|
|
|
|
KP::Swap(rhs);
|
|
|
|
|
SP::Swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~SmartPtr()
|
|
|
|
|
{
|
|
|
|
|
if (OP::Release(GetImpl(*static_cast<SP*>(this))))
|
|
|
|
|
{
|
|
|
|
|
SP::Destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend inline void Release(SmartPtr& sp, typename SP::StoredType& p)
|
|
|
|
|
{
|
|
|
|
|
p = GetImplRef(sp);
|
|
|
|
|
GetImplRef(sp) = SP::Default();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend inline void Reset(SmartPtr& sp, typename SP::StoredType p)
|
|
|
|
|
{ SmartPtr(p).Swap(sp); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-03-17 22:52:56 +00:00
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
|
|
|
|
>
|
|
|
|
|
bool Merge( SmartPtr< T1, OP1, CP1, KP1, SP1, CNP1 > & rhs )
|
|
|
|
|
{
|
|
|
|
|
if ( GetImpl( *this ) != GetImpl( rhs ) )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return OP::Merge( rhs );
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
PointerType operator->()
|
|
|
|
|
{
|
|
|
|
|
KP::OnDereference(GetImplRef(*this));
|
|
|
|
|
return SP::operator->();
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-30 20:01:37 +00:00
|
|
|
|
ConstPointerType operator->() const
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
|
|
|
|
KP::OnDereference(GetImplRef(*this));
|
|
|
|
|
return SP::operator->();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReferenceType operator*()
|
|
|
|
|
{
|
|
|
|
|
KP::OnDereference(GetImplRef(*this));
|
|
|
|
|
return SP::operator*();
|
|
|
|
|
}
|
2003-08-21 12:55:14 +00:00
|
|
|
|
|
2006-01-30 20:01:37 +00:00
|
|
|
|
ConstReferenceType operator*() const
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
|
|
|
|
KP::OnDereference(GetImplRef(*this));
|
|
|
|
|
return SP::operator*();
|
|
|
|
|
}
|
2003-08-21 12:55:14 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
bool operator!() const // Enables "if (!sp) ..."
|
|
|
|
|
{ return GetImpl(*this) == 0; }
|
2006-03-17 22:52:56 +00:00
|
|
|
|
|
|
|
|
|
static inline T * GetPointer( const SmartPtr & sp )
|
|
|
|
|
{ return GetImpl( sp ); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
bool operator==(const SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs) const
|
2004-03-14 16:41:35 +00:00
|
|
|
|
{ return GetImpl(*this) == GetImpl(rhs); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
bool operator!=(const SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs) const
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(*this == rhs); }
|
|
|
|
|
|
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
bool operator<(const SmartPtr<T1, OP1, CP1, KP1, SP1, CNP1 >& rhs) const
|
2004-03-14 16:41:35 +00:00
|
|
|
|
{ return GetImpl(*this) < GetImpl(rhs); }
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
2006-03-17 22:52:56 +00:00
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
|
|
|
|
>
|
|
|
|
|
inline bool operator > ( const SmartPtr< T1, OP1, CP1, KP1, SP1, CNP1 > & rhs )
|
|
|
|
|
{
|
|
|
|
|
return ( GetImpl( rhs ) < GetImpl( *this ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
|
|
|
|
>
|
|
|
|
|
inline bool operator <= ( const SmartPtr< T1, OP1, CP1, KP1, SP1, CNP1 > & rhs )
|
|
|
|
|
{
|
|
|
|
|
return !( GetImpl( rhs ) < GetImpl( *this ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ambiguity buster
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T1,
|
|
|
|
|
template <class> class OP1,
|
|
|
|
|
class CP1,
|
|
|
|
|
template <class> class KP1,
|
|
|
|
|
template <class> class SP1,
|
|
|
|
|
template <class> class CNP1
|
|
|
|
|
>
|
|
|
|
|
inline bool operator >= ( const SmartPtr< T1, OP1, CP1, KP1, SP1, CNP1 > & rhs )
|
|
|
|
|
{
|
|
|
|
|
return !( GetImpl( *this ) < GetImpl( rhs ) );
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
private:
|
|
|
|
|
// Helper for enabling 'if (sp)'
|
|
|
|
|
struct Tester
|
|
|
|
|
{
|
2004-03-14 16:41:35 +00:00
|
|
|
|
Tester(int) {}
|
|
|
|
|
void dummy() {}
|
2002-08-11 05:49:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2004-03-14 16:41:35 +00:00
|
|
|
|
typedef void (Tester::*unspecified_boolean_type_)();
|
|
|
|
|
|
|
|
|
|
typedef typename Select<CP::allow, Tester, unspecified_boolean_type_>::Result
|
|
|
|
|
unspecified_boolean_type;
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
public:
|
|
|
|
|
// enable 'if (sp)'
|
2004-03-14 16:41:35 +00:00
|
|
|
|
operator unspecified_boolean_type() const
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2004-03-14 16:41:35 +00:00
|
|
|
|
return !*this ? 0 : &Tester::dummy;
|
2002-08-11 05:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Helper for disallowing automatic conversion
|
|
|
|
|
struct Insipid
|
|
|
|
|
{
|
|
|
|
|
Insipid(PointerType) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef typename Select<CP::allow, PointerType, Insipid>::Result
|
|
|
|
|
AutomaticConversionResult;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
operator AutomaticConversionResult() const
|
|
|
|
|
{ return GetImpl(*this); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// free comparison operators for class template SmartPtr
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator== for lhs = SmartPtr, rhs = raw pointer
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class CNP1,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 01:52:17 +00:00
|
|
|
|
inline bool operator==(const SmartPtr<T, OP, CP, KP, SP, CNP1 >& lhs,
|
2004-03-14 16:41:35 +00:00
|
|
|
|
U* rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return GetImpl(lhs) == rhs; }
|
2006-03-17 22:52:56 +00:00
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator== for lhs = raw pointer, rhs = SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class CNP1,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator==(U* lhs,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP1 >& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return rhs == lhs; }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator!= for lhs = SmartPtr, rhs = raw pointer
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
inline bool operator!=(const SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
2004-03-14 16:41:35 +00:00
|
|
|
|
U* rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(lhs == rhs); }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator!= for lhs = raw pointer, rhs = SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator!=(U* lhs,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP >& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return rhs != lhs; }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-03-17 22:52:56 +00:00
|
|
|
|
/// operator< for lhs = SmartPtr, rhs = raw pointer
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
inline bool operator<(const SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
2006-03-17 22:52:56 +00:00
|
|
|
|
U* rhs)
|
|
|
|
|
{
|
|
|
|
|
return ( GetImpl( lhs ) < rhs );
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-03-17 22:52:56 +00:00
|
|
|
|
/// operator< for lhs = raw pointer, rhs = SmartPtr
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator<(U* lhs,
|
2006-03-17 22:52:56 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP >& rhs)
|
|
|
|
|
{
|
|
|
|
|
return ( GetImpl( rhs ) < lhs );
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-03-17 22:52:56 +00:00
|
|
|
|
// operator> for lhs = SmartPtr, rhs = raw pointer
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
inline bool operator>(const SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
2004-03-14 16:41:35 +00:00
|
|
|
|
U* rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator> for lhs = raw pointer, rhs = SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator>(U* lhs,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP >& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator<= for lhs = SmartPtr, rhs = raw pointer
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
inline bool operator<=(const SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
2004-03-14 16:41:35 +00:00
|
|
|
|
U* rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(rhs < lhs); }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator<= for lhs = raw pointer, rhs = SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator<=(U* lhs,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP >& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(rhs < lhs); }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator>= for lhs = SmartPtr, rhs = raw pointer
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
inline bool operator>=(const SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
2004-03-14 16:41:35 +00:00
|
|
|
|
U* rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(lhs < rhs); }
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// operator>= for lhs = raw pointer, rhs = SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP,
|
2002-08-11 05:49:45 +00:00
|
|
|
|
typename U
|
|
|
|
|
>
|
2004-03-14 16:41:35 +00:00
|
|
|
|
inline bool operator>=(U* lhs,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
const SmartPtr<T, OP, CP, KP, SP, CNP >& rhs)
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return !(lhs < rhs); }
|
|
|
|
|
|
|
|
|
|
} // namespace Loki
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2005-11-03 12:43:55 +00:00
|
|
|
|
/// specialization of std::less for SmartPtr
|
|
|
|
|
/// \ingroup SmartPointerGroup
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
|
{
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename T,
|
|
|
|
|
template <class> class OP,
|
|
|
|
|
class CP,
|
|
|
|
|
template <class> class KP,
|
2006-02-25 01:52:17 +00:00
|
|
|
|
template <class> class SP,
|
2006-02-25 13:48:54 +00:00
|
|
|
|
template <class> class CNP
|
2002-08-11 05:49:45 +00:00
|
|
|
|
>
|
2006-02-25 13:48:54 +00:00
|
|
|
|
struct less< Loki::SmartPtr<T, OP, CP, KP, SP, CNP > >
|
|
|
|
|
: public binary_function<Loki::SmartPtr<T, OP, CP, KP, SP, CNP >,
|
|
|
|
|
Loki::SmartPtr<T, OP, CP, KP, SP, CNP >, bool>
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{
|
2006-02-25 13:48:54 +00:00
|
|
|
|
bool operator()(const Loki::SmartPtr<T, OP, CP, KP, SP, CNP >& lhs,
|
|
|
|
|
const Loki::SmartPtr<T, OP, CP, KP, SP, CNP >& rhs) const
|
2002-08-11 05:49:45 +00:00
|
|
|
|
{ return less<T*>()(GetImpl(lhs), GetImpl(rhs)); }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Change log:
|
2003-02-02 16:35:14 +00:00
|
|
|
|
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
|
2002-08-11 05:49:45 +00:00
|
|
|
|
// December 09, 2001: Included <cassert>
|
2003-02-02 16:35:14 +00:00
|
|
|
|
// February 2, 2003: fixed dependent names - credit due to Rani Sharoni
|
2003-08-21 12:55:14 +00:00
|
|
|
|
// August 21, 2003: Added support for policy based explicit/implicit constructor.
|
|
|
|
|
// The new code will be effective if a macro named
|
|
|
|
|
// *** LOKI_SMARTPTR_CONVERSION_CONSTRUCTOR_POLICY ***
|
|
|
|
|
// is defined (due to backward computability concerns).
|
|
|
|
|
// In case that the macro is defined and the conversion policy allow flag is off
|
|
|
|
|
// (e.g. DisallowConversion) then the conversion from the "pointer" to the
|
|
|
|
|
// SmartPtr must be explicit.
|
2005-11-03 12:43:55 +00:00
|
|
|
|
// October 21, 2005: fix void Swap(RefLinkedBase& rhs). Peter K<>mmel
|
|
|
|
|
// November 3, 2005: doxygen like documentation. Peter K<>mmel
|
2002-08-11 05:49:45 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#endif // SMARTPTR_INC_
|
2006-01-16 19:05:09 +00:00
|
|
|
|
|
2006-05-17 16:04:32 +00:00
|
|
|
|
|
2006-01-16 19:05:09 +00:00
|
|
|
|
// $Log$
|
2006-05-17 16:04:32 +00:00
|
|
|
|
// Revision 1.30 2006/05/17 16:04:32 syntheticpp
|
|
|
|
|
// undo commit 1.29, reject bug: [ 1459838 ] Loki::COMRefCounted doesn't call AddRef() on assignment
|
|
|
|
|
//
|
2006-04-05 22:41:43 +00:00
|
|
|
|
// Revision 1.29 2006/04/05 22:41:43 rich_sposato
|
|
|
|
|
// Fixed bug 1459838 using fix made by Thomas Albrecht. (Thanks!)
|
|
|
|
|
// Also added a constructor for NoCheck policy.
|
|
|
|
|
//
|
2006-03-27 18:38:30 +00:00
|
|
|
|
// Revision 1.28 2006/03/27 18:38:30 rich_sposato
|
|
|
|
|
// Added check for NULL pointer in HeapStorage policy.
|
|
|
|
|
//
|
2006-03-27 18:34:36 +00:00
|
|
|
|
// Revision 1.27 2006/03/27 18:34:36 rich_sposato
|
|
|
|
|
// Added HeapStorage policy as mentioned in Feature Request 1441024.
|
|
|
|
|
//
|
2006-03-17 22:52:56 +00:00
|
|
|
|
// Revision 1.26 2006/03/17 22:52:55 rich_sposato
|
|
|
|
|
// Fixed bugs 1452805 and 1451835. Added Merge ability for RefLink policy.
|
|
|
|
|
// Added more tests for SmartPtr.
|
|
|
|
|
//
|
2006-03-17 20:22:14 +00:00
|
|
|
|
// Revision 1.25 2006/03/17 20:22:14 syntheticpp
|
|
|
|
|
// patch undefined uintptr_t, thx to Regis Desgroppes
|
|
|
|
|
//
|
2006-03-08 17:07:20 +00:00
|
|
|
|
// Revision 1.24 2006/03/08 17:07:11 syntheticpp
|
|
|
|
|
// replace tabs with 4 spaces in all files
|
|
|
|
|
//
|
2006-02-28 16:55:56 +00:00
|
|
|
|
// Revision 1.23 2006/02/28 16:55:56 syntheticpp
|
|
|
|
|
// undo disabling checking, remove warnings, many thanks to Sam Miller
|
|
|
|
|
//
|
2006-02-28 12:59:59 +00:00
|
|
|
|
// Revision 1.22 2006/02/28 12:59:59 syntheticpp
|
|
|
|
|
// fix wrong RejectNull implementation, thanks to Sam Miller
|
|
|
|
|
//
|
2006-02-27 19:59:20 +00:00
|
|
|
|
// Revision 1.21 2006/02/27 19:59:20 syntheticpp
|
|
|
|
|
// add support of loki.dll
|
|
|
|
|
//
|
2006-02-25 13:48:54 +00:00
|
|
|
|
// Revision 1.20 2006/02/25 13:48:54 syntheticpp
|
|
|
|
|
// add constness policy to doc
|
|
|
|
|
//
|
2006-02-25 13:01:40 +00:00
|
|
|
|
// Revision 1.19 2006/02/25 13:01:40 syntheticpp
|
|
|
|
|
// add const member function OnDereference to non static RejectNull policies
|
|
|
|
|
//
|
2006-02-25 01:52:17 +00:00
|
|
|
|
// Revision 1.18 2006/02/25 01:52:17 rich_sposato
|
|
|
|
|
// Moved a monolithic base class from header file to new source file.
|
|
|
|
|
//
|
2006-02-19 22:04:28 +00:00
|
|
|
|
// Revision 1.17 2006/02/19 22:04:28 rich_sposato
|
|
|
|
|
// Moved Const-policy structs from SmartPtr.h to ConstPolicy.h.
|
|
|
|
|
//
|
2006-02-14 11:54:46 +00:00
|
|
|
|
// Revision 1.16 2006/02/14 11:54:46 syntheticpp
|
|
|
|
|
// rename SmartPtr-ByRef and ScopeGuard-ByRefHolder into RefToValue and move it to loki/RefToValue.h
|
|
|
|
|
//
|
2006-02-08 18:12:29 +00:00
|
|
|
|
// Revision 1.15 2006/02/08 18:12:29 rich_sposato
|
|
|
|
|
// Fixed bug 1425890. Last SmartPtr in linked chain NULLs its prev & next
|
|
|
|
|
// pointers to prevent infinite recursion. Added asserts.
|
|
|
|
|
//
|
2006-01-30 20:07:38 +00:00
|
|
|
|
// Revision 1.14 2006/01/30 20:07:38 syntheticpp
|
|
|
|
|
// replace tabss
|
|
|
|
|
//
|
2006-01-30 20:01:37 +00:00
|
|
|
|
// Revision 1.13 2006/01/30 20:01:37 syntheticpp
|
|
|
|
|
// add ArrayStorage and propagating constness policies
|
|
|
|
|
//
|
2006-01-27 08:58:17 +00:00
|
|
|
|
// Revision 1.12 2006/01/27 08:58:17 syntheticpp
|
|
|
|
|
// replace unsigned int with the platform independent uintptr_t to make it more 64bit portable, and work around for mac gcc 4.0.0 bug in assert
|
|
|
|
|
//
|
2006-01-22 13:31:45 +00:00
|
|
|
|
// Revision 1.11 2006/01/22 13:31:12 syntheticpp
|
|
|
|
|
// add additional template parameter for the changed threading classes
|
|
|
|
|
//
|
2006-01-18 17:21:31 +00:00
|
|
|
|
// Revision 1.10 2006/01/18 17:21:31 lfittl
|
|
|
|
|
// - Compile library with -Weffc++ and -pedantic (gcc)
|
|
|
|
|
// - Fix most issues raised by using -Weffc++ (initialization lists)
|
|
|
|
|
//
|
2006-01-16 19:05:09 +00:00
|
|
|
|
// Revision 1.9 2006/01/16 19:05:09 rich_sposato
|
|
|
|
|
// Added cvs keywords.
|
|
|
|
|
//
|