libcommonc++  0.7
ScopedPtr.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_ScopedPtr_hxx
24 #define __ccxx_ScopedPtr_hxx
25 
26 #include <commonc++/Common.h++>
28 
29 namespace ccxx {
30 
38 template<class T> class ScopedPtr
39 {
40  public:
41 
44  : _object(NULL)
45  { }
46 
48  explicit ScopedPtr(T* object)
49  : _object(object)
50  { }
51 
54  { delete _object; }
55 
61  T& operator*() const
62  {
63  if(! _object)
64  throw NullPointerException();
65 
66  return(*_object);
67  }
68 
74  T* operator->() const
75  {
76  if(! _object)
77  throw NullPointerException();
78 
79  return(_object);
80  }
81 
83  void reset(T* object = NULL)
84  {
85  if(_object != object)
86  {
87  delete _object;
88  _object = object;
89  }
90  }
91 
93  T* get() const
94  { return(_object); }
95 
97  T* release()
98  {
99  T* tmp = _object;
100  _object = NULL;
101  return(tmp);
102  }
103 
105  bool operator!() const
106  { return(isNull()); }
107 
109  operator const void*() const
110  { return(_object ? this : NULL); }
111 
113  bool isNull() const
114  { return(_object == NULL); }
115 
117  void swap(ScopedPtr& other)
118  {
119  T* tmp = other._object;
120  other._object = _object;
121  _object = tmp;
122  }
123 
124  private:
125 
126  T* _object;
127 
128  CCXX_COPY_DECLS(ScopedPtr);
129 };
130 
133 template<class T> inline void swap(ScopedPtr<T>& a, ScopedPtr<T>& b)
134 { a.swap(b); }
135 
136 } // namespace ccxx
137 
138 #endif // __ccxx_ScopedPtr_hxx
ScopedPtr()
Construct a new, NULL ScopedPtr.
Definition: ScopedPtr.h++:43
bool operator!() const
Test if the pointer is NULL.
Definition: ScopedPtr.h++:105
T * release()
Release ownership of the object.
Definition: ScopedPtr.h++:97
T & operator*() const
Dereference operator.
Definition: ScopedPtr.h++:61
An exception indicating an attempt to dereference a null pointer.
Definition: NullPointerException.h++:37
void reset(T *object=NULL)
Reset the pointer.
Definition: ScopedPtr.h++:83
bool isNull() const
Test if the pointer is NULL.
Definition: ScopedPtr.h++:113
ScopedPtr(T *object)
Construct a new ScopedPtr for the given object pointer.
Definition: ScopedPtr.h++:48
void swap(ScopedPtr &other)
Swap this pointer with another.
Definition: ScopedPtr.h++:117
A non-copyable scoped pointer.
Definition: ScopedPtr.h++:38
~ScopedPtr()
Destructor.
Definition: ScopedPtr.h++:53
T * operator->() const
Pointer-to-member operator.
Definition: ScopedPtr.h++:74
Definition: AllocationMap.c++:25