PSCF v1.1
FlexPtr.h
1#ifndef UTIL_FLEX_PTR_H
2#define UTIL_FLEX_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 "isNull.h"
12#include <util/global.h>
13
14namespace Util
15{
16
34 template <typename T>
35 class FlexPtr
36 {
37
38 public:
39
41 typedef T element_type;
42
47 : ptr_(0),
48 isOwner_(0)
49 {}
50
57 {
58 if (ptr_ != 0 && isOwner_) {
59 delete ptr_;
60 }
61 }
62
73 void acquire(T* p)
74 {
75 if (p == 0) {
76 UTIL_THROW("Cannot acquire a null pointer");
77 }
78 if (ptr_ != 0 && isOwner_) {
79 delete ptr_;
80 }
81 ptr_ = p;
82 isOwner_ = 1;
83 }
84
93 void copy(T* p)
94 {
95 if (p == 0) {
96 UTIL_THROW("Cannot copy a null pointer");
97 }
98 if (ptr_ != 0 && isOwner_) {
99 delete ptr_;
100 }
101 ptr_ = p;
102 isOwner_ = 0;
103 }
104
108 T& operator*() const
109 { return *ptr_; }
110
114 T* operator->() const
115 { return ptr_; }
116
120 T* get() const
121 { return ptr_; }
122
123 private:
124
126 T* ptr_;
127
129 int isOwner_;
130
132 FlexPtr(const FlexPtr&);
133
135 FlexPtr& operator = (const FlexPtr& );
136
137 };
138
142 template <typename T>
143 inline bool isNull(FlexPtr<T> p)
144 { return (p.get() == 0); }
145
146}
147
148#endif
A pointer that may or may not own the object to which it points.
Definition: FlexPtr.h:36
T element_type
Type of object pointed to.
Definition: FlexPtr.h:41
void copy(T *p)
Copy a built-in pointer, without accepting ownership.
Definition: FlexPtr.h:93
void acquire(T *p)
Copy a built-in pointer, and accept ownership.
Definition: FlexPtr.h:73
T & operator*() const
Dereference.
Definition: FlexPtr.h:108
FlexPtr()
Constructor.
Definition: FlexPtr.h:46
T * get() const
Return the built-in pointer.
Definition: FlexPtr.h:120
T * operator->() const
Member access.
Definition: FlexPtr.h:114
~FlexPtr()
Destructor.
Definition: FlexPtr.h:56
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
bool isNull(FlexPtr< T > p)
Return true iff the enclosed built-in pointer is null.
Definition: FlexPtr.h:143