FreeFOAM The Cross-Platform CFD Toolkit
autoPtrI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/error.H>
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class T>
32 :
33  ptr_(p)
34 {}
35 
36 
37 template<class T>
39 :
40  ptr_(ap.ptr_)
41 {
42  ap.ptr_ = 0;
43 }
44 
45 
46 template<class T>
48 {
49  clear();
50 }
51 
52 
53 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
54 
55 template<class T>
56 inline bool Foam::autoPtr<T>::empty() const
57 {
58  return !ptr_;
59 }
60 
61 
62 template<class T>
63 inline bool Foam::autoPtr<T>::valid() const
64 {
65  return ptr_;
66 }
67 
68 
69 template<class T>
71 {
72  T* ptr = ptr_;
73  ptr_ = 0;
74  return ptr;
75 }
76 
77 
78 template<class T>
79 inline void Foam::autoPtr<T>::set(T* p)
80 {
81  if (ptr_)
82  {
83  FatalErrorIn("void autoPtr<T>::set(T*)")
84  << "object already allocated"
85  << abort(FatalError);
86  }
87 
88  ptr_ = p;
89 }
90 
91 
92 template<class T>
94 {
95  if (ptr_)
96  {
97  delete ptr_;
98  }
99 
100  ptr_ = p;
101 }
102 
103 
104 template<class T>
106 {
107  reset(0);
108 }
109 
110 
111 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
112 
113 template<class T>
115 {
116  if (!ptr_)
117  {
118  FatalErrorIn("T& autoPtr<T>::operator()()")
119  << "object is not allocated"
120  << abort(FatalError);
121  }
122 
123  return *ptr_;
124 }
125 
126 
127 template<class T>
128 inline const T& Foam::autoPtr<T>::operator()() const
129 {
130  if (!ptr_)
131  {
132  FatalErrorIn("const T& autoPtr<T>::operator()() const")
133  << "object is not allocated"
134  << abort(FatalError);
135  }
136 
137  return *ptr_;
138 }
139 
140 
141 /*
142 template<class T>
143 inline T& Foam::autoPtr<T>::operator*()
144 {
145  return operator()();
146 }
147 
148 
149 template<class T>
150 inline const T& Foam::autoPtr<T>::operator*() const
151 {
152  return operator()();
153 }
154 */
155 
156 
157 template<class T>
158 inline Foam::autoPtr<T>::operator const T&() const
159 {
160  return operator()();
161 }
162 
163 
164 template<class T>
166 {
167  if (!ptr_)
168  {
169  FatalErrorIn("autoPtr<T>::operator->()")
170  << "object is not allocated"
171  << abort(FatalError);
172  }
173 
174  return ptr_;
175 }
176 
177 
178 template<class T>
179 inline const T* Foam::autoPtr<T>::operator->() const
180 {
181  return const_cast<autoPtr<T>&>(*this).operator->();
182 }
183 
184 
185 template<class T>
187 {
188  if (this != &ap)
189  {
190  reset(const_cast<autoPtr<T>&>(ap).ptr());
191  }
192 }
193 
194 
195 // ************************ vim: set sw=4 sts=4 et: ************************ //