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-2021 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 operators
75 
76  //- Return true if the object is a reusable or non-reusable temporary
77  inline bool isAnyTmp() const;
78 
79  inline void operator++();
80 
81 
82 public:
83 
84  typedef T Type;
85  typedef Foam::refCount refCount;
86 
87 
88  // Constructors
89 
90  //- Store object pointer of a non-reusable or reusable temporary object
91  inline explicit tmp(T* = 0, bool nonReusable = false);
92 
93  //- Store object const reference
94  inline tmp(const T&);
95 
96  //- Construct copy and increment reference count
97  inline tmp(const tmp<T>&);
98 
99  //- Construct copy moving content, does not increment reference count
100  inline tmp(const tmp<T>&&);
101 
102  //- Construct copy transferring content of temporary if required
103  inline tmp(const tmp<T>&, bool allowTransfer);
104 
105 
106  //- Destructor: deletes temporary object when the reference count is 0
107  inline ~tmp();
108 
109 
110  // Member Functions
111 
112  // Access
113 
114  //- Return true if this is really a temporary object
115  inline bool isTmp() const;
116 
117  //- Return true if this temporary object empty,
118  // ie, a temporary without allocation
119  inline bool empty() const;
120 
121  //- Is this temporary object valid,
122  // ie, it is a reference or a temporary that has been allocated
123  inline bool valid() const;
124 
125  //- Return the type name of the tmp
126  // constructed from the type name of T
127  inline word typeName() const;
128 
129 
130  // Edit
131 
132  //- Return non-const reference or generate a fatal error
133  // if the object is const.
134  inline T& ref() const;
135 
136  //- Return tmp pointer for reuse.
137  // Returns a clone if the object is not a temporary
138  inline T* ptr() const;
139 
140  //- If object pointer points to valid object:
141  // delete object and set pointer to nullptr
142  inline void clear() const;
143 
144 
145  // Member Operators
146 
147  #ifdef NON_CONST_TMP
148  //- Deprecated non-const dereference operator.
149  // Use ref() where non-const access is required
150  inline T& operator()();
151  #endif
152 
153  //- Const dereference operator
154  inline const T& operator()() const;
155 
156  //- Const cast to the underlying type reference
157  inline operator const T&() const;
158 
159  //- Return object pointer
160  inline T* operator->();
161 
162  //- Return const object pointer
163  inline const T* operator->() const;
164 
165  //- Assignment to pointer changing this tmp to a temporary T
166  inline void operator=(T*);
167 
168  //- Assignment transferring the temporary T to this tmp
169  inline void operator=(const tmp<T>&);
170 
171  //- Move assignment transferring the temporary T to this tmp
172  inline void operator=(const tmp<T>&&);
173 };
174 
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 } // End namespace Foam
179 
180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
181 
182 #include "tmpI.H"
183 
184 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
185 
186 #endif
187 
188 // ************************************************************************* //
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:341
bool valid() const
Is this temporary object valid,.
Definition: tmpI.H:167
Foam::refCount refCount
Definition: tmp.H:84
T * operator->()
Return object pointer.
Definition: tmpI.H:304
word typeName() const
Return the type name of the tmp.
Definition: tmpI.H:174
bool empty() const
Return true if this temporary object empty,.
Definition: tmpI.H:160
T Type
Definition: tmp.H:83
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpI.H:153
tmp(T *=0, bool nonReusable=false)
Store object pointer of a non-reusable or reusable temporary object.
Definition: tmpI.H:49
~tmp()
Destructor: deletes temporary object when the reference count is 0.
Definition: tmpI.H:137
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:205
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:237
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
const T & operator()() const
Const dereference operator.
Definition: tmpI.H:279
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)