autoPtr.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) 2011-2016 OpenFOAM Foundation
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 Class
25  Foam::autoPtr
26 
27 Description
28  An auto-pointer similar to the STL auto_ptr but with automatic casting
29  to a reference to the type and with pointer allocation checking on access.
30 
31 SourceFiles
32  autoPtrI.H
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef autoPtr_H
37 #define autoPtr_H
38 
39 #include <cstddef>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class autoPtr Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 template<class T>
51 class autoPtr
52 {
53  // Public data
54 
55  //- Pointer to object
56  mutable T* ptr_;
57 
58 
59 public:
60 
61  typedef T Type;
62 
63 
64  // Constructors
65 
66  //- Store object pointer
67  inline explicit autoPtr(T* = 0);
68 
69  //- Construct as copy by transferring pointer to this autoPtr and
70  // setting the arguments pointer to NULL
71  inline autoPtr(const autoPtr<T>&);
72 
73  //- Construct either by transferring pointer or cloning. Should
74  // only be called with type that supports cloning.
75  inline autoPtr(const autoPtr<T>&, const bool reuse);
76 
77 
78  //- Destructor, delete object if pointer is not NULL
79  inline ~autoPtr();
80 
81 
82  // Member Functions
83 
84  // Check
85 
86  //- Return true if the autoPtr is empty (ie, no pointer set).
87  inline bool empty() const;
88 
89  //- Return true if the autoPtr valid (ie, the pointer is set).
90  inline bool valid() const;
91 
92 
93  // Edit
94 
95  //- Return object pointer for reuse
96  inline T* ptr();
97 
98  //- Set pointer to that given.
99  // If object pointer already set issue a FatalError.
100  inline void set(T*);
101 
102  //- If object pointer already set, delete object and set to given
103  // pointer
104  inline void reset(T* = 0);
105 
106  //- Delete object (if the pointer is valid) and set pointer to NULL.
107  inline void clear();
108 
109 
110  // Member operators
111 
112  //- Return reference to the object data
113  inline T& operator()();
114 
115  //- Return const reference to the object data
116  inline const T& operator()() const;
117 
118  //- Const cast to the underlying type reference
119  inline operator const T&() const;
120 
121  //- Return object pointer
122  inline T* operator->();
123 
124  //- Return const object pointer
125  inline const T* operator->() const;
126 
127  //- Take over the object pointer from parameter
128  inline void operator=(const autoPtr<T>&);
129 };
130 
131 
132 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
133 
134 } // End namespace Foam
135 
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
137 
138 #include "autoPtrI.H"
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 #endif
143 
144 // ************************************************************************* //
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set).
Definition: autoPtrI.H:76
void clear()
Delete object (if the pointer is valid) and set pointer to NULL.
Definition: autoPtrI.H:126
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
void operator=(const autoPtr< T > &)
Take over the object pointer from parameter.
Definition: autoPtrI.H:194
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
~autoPtr()
Destructor, delete object if pointer is not NULL.
Definition: autoPtrI.H:67
T * operator->()
Return object pointer.
Definition: autoPtrI.H:172
autoPtr(T *=0)
Store object pointer.
Definition: autoPtrI.H:32
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:135
Namespace for OpenFOAM.