Simpatico  v1.10
ScopedPtr.h
1 #ifndef UTIL_SCOPED_PTR_H
2 #define UTIL_SCOPED_PTR_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/global.h>
12 
13 namespace Util
14 {
15 
28  template <typename T>
29  class ScopedPtr
30  {
31 
32  public:
33 
35  typedef T element_type;
36 
38  explicit ScopedPtr(T* p = 0)
39  { ptr_ = p; }
40 
43  {
44  if (ptr_ != 0) {
45  delete ptr_;
46  }
47  }
48 
54  void reset(T* p = 0)
55  {
56  if (ptr_ != 0) {
57  delete ptr_;
58  }
59  ptr_ = p;
60  }
61 
63  T& operator*() const
64  { return *ptr_; }
65 
67  T* operator->() const
68  { return ptr_; }
69 
71  T* get() const
72  { return ptr_; }
73 
74  private:
75 
76  T* ptr_;
77 
79  ScopedPtr(const ScopedPtr&);
80 
82  ScopedPtr& operator = (const ScopedPtr& );
83 
84  };
85 
89  template <typename T>
90  inline bool isNull(ScopedPtr<T> p)
91  { return (p.get() == 0); }
92 
93 }
94 #endif
~ScopedPtr()
Destructor, destroys object pointed to, if any.
Definition: ScopedPtr.h:42
File containing preprocessor macros for error handling.
Utility classes for scientific computation.
Definition: accumulators.mod:1
T * operator->() const
Member access.
Definition: ScopedPtr.h:67
bool isNull(FlexPtr< T > p)
Return true iff the enclosed built-in pointer is null.
Definition: FlexPtr.h:143
T & operator*() const
Dereference.
Definition: ScopedPtr.h:63
A very simple RAII pointer.
Definition: ScopedPtr.h:29
void reset(T *p=0)
Acquire ownership of a built-in pointer.
Definition: ScopedPtr.h:54
ScopedPtr(T *p=0)
Constructor.
Definition: ScopedPtr.h:38
T * get() const
Return enclosed built-in pointer.
Definition: ScopedPtr.h:71
T element_type
Type of object pointed to.
Definition: ScopedPtr.h:35