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