~ubuntu-branches/ubuntu/breezy/atlas-cpp/breezy

« back to all changes in this revision

Viewing changes to Atlas/Objects/SmartPtr.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2005-10-02 11:41:44 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051002114144-8qmn4d1cdn9g27ta
Tags: 0.5.98-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 
 
6
#ifndef ATLAS_OBJECTS_SMARTPTR_H
 
7
#define ATLAS_OBJECTS_SMARTPTR_H
 
8
 
 
9
#include <Atlas/Exception.h>
 
10
 
 
11
namespace Atlas { namespace Objects {
 
12
 
 
13
class NullSmartPtrDereference : public Atlas::Exception
 
14
{
 
15
  public:
 
16
    NullSmartPtrDereference() : Atlas::Exception("Null SmartPtr dereferenced") {}
 
17
    virtual ~NullSmartPtrDereference() throw ();
 
18
};
 
19
 
 
20
template <class T> 
 
21
class SmartPtr
 
22
{
 
23
  public:
 
24
    typedef T DataT;
 
25
 
 
26
    typedef typename T::iterator iterator;
 
27
    typedef typename T::const_iterator const_iterator;
 
28
 
 
29
    SmartPtr() : ptr(T::alloc()) { 
 
30
    }
 
31
    SmartPtr(const SmartPtr<T>& a) : ptr(a.get()) {
 
32
        incRef();
 
33
    }
 
34
    SmartPtr(T *a_ptr) : ptr(a_ptr)
 
35
    {
 
36
        incRef();
 
37
    }
 
38
    template<class oldType>
 
39
    explicit SmartPtr(const SmartPtr<oldType>& a) : ptr(a.get()) {
 
40
    }
 
41
    ~SmartPtr() { 
 
42
        decRef();
 
43
    }
 
44
    SmartPtr& operator=(const SmartPtr<T>& a) {
 
45
        if (a.get() != this->get()) {
 
46
            decRef();
 
47
            ptr = a.get();
 
48
            incRef();
 
49
        }
 
50
        return *this;
 
51
    }
 
52
    template<class newType>
 
53
    operator SmartPtr<newType>() const {
 
54
        return SmartPtr<newType>(ptr);
 
55
    }
 
56
    template<class newType>
 
57
    operator SmartPtr<const newType>() const {
 
58
        return SmartPtr<const newType>(ptr);
 
59
    }
 
60
    bool isValid() const {
 
61
        return ptr != 0;
 
62
    }
 
63
    T& operator*() const { 
 
64
        if (ptr == 0) {
 
65
            throw NullSmartPtrDereference();
 
66
        }
 
67
        return *ptr;
 
68
    }
 
69
    T* operator->() const {
 
70
        if (ptr == 0) {
 
71
            throw NullSmartPtrDereference();
 
72
        }
 
73
        return ptr;
 
74
    }
 
75
    T* get() const {
 
76
        return ptr;
 
77
    }
 
78
    SmartPtr<T> copy() const
 
79
    {
 
80
        SmartPtr<T> ret = SmartPtr(ptr->copy());
 
81
        ret.decRef();
 
82
        return ret;
 
83
    }
 
84
    SmartPtr<T> getDefaultObject() const
 
85
    {
 
86
        return SmartPtr(ptr->getDefaultObject());
 
87
    }
 
88
    // If you want to make these protected, please ensure that the
 
89
    // detructor is made virtual to ensure your new class bahaves
 
90
    // correctly.
 
91
  private:
 
92
    void decRef() const {
 
93
        if (ptr != 0) {
 
94
            ptr->decRef();
 
95
        }
 
96
    }
 
97
    void incRef() const {
 
98
        if (ptr != 0) {
 
99
            ptr->incRef();
 
100
        }
 
101
    }
 
102
    T * ptr;
 
103
};
 
104
 
 
105
template<typename returnPtrType, class fromType>
 
106
returnPtrType smart_dynamic_cast(const SmartPtr<fromType> & o)
 
107
{
 
108
    return returnPtrType(dynamic_cast<typename returnPtrType::DataT*>(o.get()));
 
109
}
 
110
 
 
111
template<typename returnPtrType, class fromType>
 
112
returnPtrType smart_static_cast(const SmartPtr<fromType> & o)
 
113
{
 
114
    return returnPtrType((typename returnPtrType::DataT *)o.get());
 
115
}
 
116
 
 
117
} } // namespace Atlas::Objects
 
118
 
 
119
#endif // ATLAS_OBJECTS_SMARTPTR_H