Atlas-C++
SmartPtr.h
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 // $Id$
7 
8 #ifndef ATLAS_OBJECTS_SMARTPTR_H
9 #define ATLAS_OBJECTS_SMARTPTR_H
10 
11 #include <Atlas/Exception.h>
12 
13 namespace Atlas { namespace Objects {
14 
16 {
17  public:
18  NullSmartPtrDereference() noexcept : Atlas::Exception("Null SmartPtr dereferenced") {}
19 
20  ~NullSmartPtrDereference() noexcept override = default;
21 };
22 
23 template <class T>
24 class SmartPtr
25 {
26  public:
27  template<class U> friend
28  class SmartPtr;
29 
30  typedef T DataT;
31 
32  typedef typename T::iterator iterator;
33  typedef typename T::const_iterator const_iterator;
34 
35  SmartPtr() : ptr(T::allocator.alloc()) {
36  }
37  SmartPtr(const SmartPtr<T>& a) : ptr(a.get()) {
38  incRef();
39  }
40  SmartPtr(SmartPtr<T>&& a) : ptr(a.get()) {
41  a.ptr = nullptr;
42  }
43  SmartPtr(T *a_ptr) : ptr(a_ptr)
44  {
45  incRef();
46  }
47  template<class oldType>
48  explicit SmartPtr(const SmartPtr<oldType>& a) : ptr(a.get()) {
49  incRef();
50  }
51  template<class oldType>
52  explicit SmartPtr(SmartPtr<oldType>&& a) : ptr(a.get()) {
53  a.ptr = nullptr;
54  }
55  ~SmartPtr() {
56  decRef();
57  }
58  SmartPtr& operator=(const SmartPtr<T>& a) {
59  if (a.get() != this->get()) {
60  decRef();
61  ptr = a.get();
62  incRef();
63  }
64  return *this;
65  }
66  template<class newType>
67  operator SmartPtr<newType>() const {
68  return SmartPtr<newType>(ptr);
69  }
70  template<class newType>
71  operator SmartPtr<const newType>() const {
72  return SmartPtr<const newType>(ptr);
73  }
74  bool isValid() const {
75  return ptr != nullptr;
76  }
77  bool operator!() const noexcept {
78  return this->ptr == nullptr;
79  }
80 
81  explicit operator bool () const noexcept
82  {
83  return !this->operator!();
84  }
85 
86  T& operator*() const {
87  if (ptr == nullptr) {
89  }
90  return *ptr;
91  }
92  T* operator->() const {
93  if (ptr == nullptr) {
95  }
96  return ptr;
97  }
98  T* get() const {
99  return ptr;
100  }
101  SmartPtr<T> copy() const
102  {
103  SmartPtr<T> ret = SmartPtr(ptr->copy());
104  ret.decRef();
105  return ret;
106  }
107  // If you want to make these protected, please ensure that the
108  // destructor is made virtual to ensure your new class behaves
109  // correctly.
110  private:
111  void decRef() const {
112  if (ptr != nullptr) {
113  ptr->decRef();
114  }
115  }
116  void incRef() const {
117  if (ptr != nullptr) {
118  ptr->incRef();
119  }
120  }
121  T * ptr;
122 };
123 
124 template<typename returnPtrType, class fromType>
125 returnPtrType smart_dynamic_cast(const SmartPtr<fromType> & o)
126 {
127  return returnPtrType(dynamic_cast<typename returnPtrType::DataT*>(o.get()));
128 }
129 
130 template<typename returnPtrType, class fromType>
131 returnPtrType smart_static_cast(const SmartPtr<fromType> & o)
132 {
133  return returnPtrType((typename returnPtrType::DataT *)o.get());
134 }
135 
136 } } // namespace Atlas::Objects
137 
138 #endif // ATLAS_OBJECTS_SMARTPTR_H
The Atlas namespace.
Definition: Bridge.h:20
Definition: Decoder.h:16
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:17

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.