1
// This file may be redistributed and modified only under the terms of
2
// the GNU Lesser General Public License (See COPYING for details).
3
// Copyright (C) 2000 Aloril
4
// Copyright (C) 2000-2005 Al Riddoch
6
#ifndef ATLAS_OBJECTS_SMARTPTR_H
7
#define ATLAS_OBJECTS_SMARTPTR_H
9
#include <Atlas/Exception.h>
11
namespace Atlas { namespace Objects {
13
class NullSmartPtrDereference : public Atlas::Exception
16
NullSmartPtrDereference() : Atlas::Exception("Null SmartPtr dereferenced") {}
17
virtual ~NullSmartPtrDereference() throw ();
26
typedef typename T::iterator iterator;
27
typedef typename T::const_iterator const_iterator;
29
SmartPtr() : ptr(T::alloc()) {
31
SmartPtr(const SmartPtr<T>& a) : ptr(a.get()) {
34
SmartPtr(T *a_ptr) : ptr(a_ptr)
38
template<class oldType>
39
explicit SmartPtr(const SmartPtr<oldType>& a) : ptr(a.get()) {
44
SmartPtr& operator=(const SmartPtr<T>& a) {
45
if (a.get() != this->get()) {
52
template<class newType>
53
operator SmartPtr<newType>() const {
54
return SmartPtr<newType>(ptr);
56
template<class newType>
57
operator SmartPtr<const newType>() const {
58
return SmartPtr<const newType>(ptr);
60
bool isValid() const {
63
T& operator*() const {
65
throw NullSmartPtrDereference();
69
T* operator->() const {
71
throw NullSmartPtrDereference();
78
SmartPtr<T> copy() const
80
SmartPtr<T> ret = SmartPtr(ptr->copy());
84
SmartPtr<T> getDefaultObject() const
86
return SmartPtr(ptr->getDefaultObject());
88
// If you want to make these protected, please ensure that the
89
// detructor is made virtual to ensure your new class bahaves
105
template<typename returnPtrType, class fromType>
106
returnPtrType smart_dynamic_cast(const SmartPtr<fromType> & o)
108
return returnPtrType(dynamic_cast<typename returnPtrType::DataT*>(o.get()));
111
template<typename returnPtrType, class fromType>
112
returnPtrType smart_static_cast(const SmartPtr<fromType> & o)
114
return returnPtrType((typename returnPtrType::DataT *)o.get());
117
} } // namespace Atlas::Objects
119
#endif // ATLAS_OBJECTS_SMARTPTR_H