tmp.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-2025 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::tmp
26 
27 Description
28  A class for managing temporary objects
29 
30 SourceFiles
31  tmpI.H
32 
33 See also
34  Foam::refCount
35  Foam::autoPtr
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef tmp_H
40 #define tmp_H
41 
42 #include "refCount.H"
43 #include "word.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class tmp Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 template<class T>
55 class tmp
56 {
57  // Private Data
58 
59  //- Object types
60  enum type
61  {
62  REUSABLE_TMP,
63  NON_RESUSABLE_TMP,
64  CONST_REF
65  };
66 
67  //- Type of object
68  type type_;
69 
70  //- Pointer to object
71  mutable T* ptr_;
72 
73 
74  // Private Member Functions
75 
76  //- Return true if the object is a reusable or non-reusable temporary
77  inline bool isAnyTmp() const;
78 
79 
80  // Private Member Operators
81 
82  //- Increment the reference count
83  inline void operator++();
84 
85 
86 public:
87 
88  // Public Type Definitions
89 
90  //- The type
91  typedef T Type;
92 
93  //- The reference counting class
94  typedef Foam::refCount refCount;
95 
96 
97  // Constructors
98 
99  //- Store object pointer of a non-reusable or reusable temporary object
100  inline explicit tmp(T* = 0, bool nonReusable = false);
101 
102  //- Store object const reference
103  inline tmp(const T&);
104 
105  //- Construct copy and increment reference count
106  inline tmp(const tmp<T>&);
107 
108  //- Construct copy moving content, does not increment reference count
109  inline tmp(tmp<T>&&);
110 
111  //- Construct copy moving content, does not increment reference count
112  inline tmp(const tmp<T>&&);
113 
114  //- Construct copy transferring content of temporary if required
115  inline tmp(const tmp<T>&, bool allowTransfer);
116 
117 
118  //- Destructor. Deletes temporary object when the reference count is 0.
119  inline ~tmp();
120 
121 
122  // Member Functions
123 
124  // Access
125 
126  //- Return true if this is really a temporary object
127  inline bool isTmp() const;
128 
129  //- Return true if this temporary object empty,
130  // ie, a temporary without allocation
131  inline bool empty() const;
132 
133  //- Is this temporary object valid,
134  // ie, it is a reference or a temporary that has been allocated
135  inline bool valid() const;
136 
137  //- Return the type name of the tmp
138  // constructed from the type name of T
139  inline word typeName() const;
140 
141 
142  // Edit
143 
144  //- Return non-const reference or generate a fatal error
145  // if the object is const.
146  inline T& ref() const;
147 
148  //- Return tmp pointer for reuse.
149  // Returns a clone if the object is not a temporary
150  inline T* ptr() const;
151 
152  //- If object pointer points to valid object:
153  // delete object and set pointer to nullptr
154  inline void clear() const;
155 
156 
157  // Member Operators
158 
159  #ifdef NON_CONST_TMP
160  //- Deprecated non-const dereference operator.
161  // Use ref() where non-const access is required
162  inline T& operator()();
163  #endif
164 
165  //- Const dereference operator
166  inline const T& operator()() const;
167 
168  //- Const cast to the underlying type reference
169  inline operator const T&() const;
170 
171  //- Return object pointer
172  inline T* operator->();
173 
174  //- Return const object pointer
175  inline const T* operator->() const;
176 
177  //- Assignment to pointer changing this tmp to a temporary T
178  inline void operator=(T*);
179 
180  //- Assignment transferring the temporary T to this tmp
181  inline void operator=(const tmp<T>&);
182 
183  //- Move assignment transferring the temporary T to this tmp
184  inline void operator=(tmp<T>&&);
185 
186  //- Move assignment transferring the temporary T to this tmp
187  inline void operator=(const tmp<T>&&);
188 };
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #include "tmpI.H"
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #endif
202 
203 // ************************************************************************* //
Reference counter for various OpenFOAM components.
Definition: refCount.H:50
A class for managing temporary objects.
Definition: tmp.H:55
void operator=(T *)
Assignment to pointer changing this tmp to a temporary T.
Definition: tmpI.H:357
bool valid() const
Is this temporary object valid,.
Definition: tmpI.H:183
Foam::refCount refCount
The reference counting class.
Definition: tmp.H:93
T * operator->()
Return object pointer.
Definition: tmpI.H:320
word typeName() const
Return the type name of the tmp.
Definition: tmpI.H:190
bool empty() const
Return true if this temporary object empty,.
Definition: tmpI.H:176
T Type
The type.
Definition: tmp.H:90
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpI.H:169
tmp(T *=0, bool nonReusable=false)
Store object pointer of a non-reusable or reusable temporary object.
Definition: tmpI.H:59
~tmp()
Destructor. Deletes temporary object when the reference count is 0.
Definition: tmpI.H:160
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:221
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:253
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:197
const T & operator()() const
Const dereference operator.
Definition: tmpI.H:295
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)