autoPtr.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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* = nullptr);
68 
69  //- Construct as copy by transferring pointer to this autoPtr and
70  // setting the arguments pointer to nullptr
71  inline autoPtr(const autoPtr<T>&);
72 
73  //- Construct either by transferring pointer or cloning.
74  // Should 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 nullptr
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* = nullptr);
105 
106  //- Delete object (if the pointer is valid) and set pointer to
107  // nullptr
108  inline void clear();
109 
110 
111  // Member Operators
112 
113  //- Return reference to the object data
114  inline T& operator()();
115 
116  //- Return const reference to the object data
117  inline const T& operator()() const;
118 
119  //- Return reference to the object data
120  inline T& operator*();
121 
122  //- Return const reference to the object data
123  inline const T& operator*() const;
124 
125  //- Const cast to the underlying type reference
126  inline operator const T&() const;
127 
128  //- Return object pointer
129  inline T* operator->();
130 
131  //- Return const object pointer
132  inline const T* operator->() const;
133 
134  //- Take over the object pointer from parameter
135  inline void operator=(T*);
136 
137  //- Take over the object pointer from parameter
138  inline void operator=(const autoPtr<T>&);
139 };
140 
141 
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 
144 } // End namespace Foam
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 #include "autoPtrI.H"
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 #endif
153 
154 // ************************************************************************* //
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
autoPtr(T *=nullptr)
Store object pointer.
Definition: autoPtrI.H:32
~autoPtr()
Destructor, delete object if pointer is not nullptr.
Definition: autoPtrI.H:67
void operator=(T *)
Take over the object pointer from parameter.
Definition: autoPtrI.H:224
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
T & operator*()
Return reference to the object data.
Definition: autoPtrI.H:165
T * operator->()
Return object pointer.
Definition: autoPtrI.H:202
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:135
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set)
Definition: autoPtrI.H:76
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
Namespace for OpenFOAM.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)