All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tmpNrc.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) 2016-2023 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::tmpNrc
26 
27 Description
28  A class for managing temporary objects without reference counting.
29 
30 SourceFiles
31  tmpNrcI.H
32 
33 See also
34  Foam::autoPtr
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef tmpNrc_H
39 #define tmpNrc_H
40 
41 #include "word.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class tmpNrc Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class T>
53 class tmpNrc
54 {
55  // Private Data
56 
57  //- Object types
58  enum type
59  {
60  TMP,
61  CONST_REF
62  };
63 
64  //- Type of object
65  mutable type type_;
66 
67  //- Pointer to object
68  mutable T* ptr_;
69 
70 
71 public:
72 
73  // Null reference counter class
74  class refCount
75  {
76  public:
77 
78  refCount()
79  {}
80  };
81 
82 
83  // Constructors
84 
85  //- Store object pointer
86  inline explicit tmpNrc(T* = 0);
87 
88  //- Store object const reference
89  inline tmpNrc(const T&);
90 
91  //- Construct copy
92  inline tmpNrc(const tmpNrc<T>&);
93 
94  //- Construct copy moving content
95  inline tmpNrc(const tmpNrc<T>&&);
96 
97  //- Construct copy transferring content of temporary if required
98  inline tmpNrc(const tmpNrc<T>&, bool allowTransfer);
99 
100 
101  //- Destructor: deletes temporary object when the reference count is 0
102  inline ~tmpNrc();
103 
104 
105  // Member Functions
106 
107  // Access
108 
109  //- Return true if this is really a temporary object
110  inline bool isTmp() const;
111 
112  //- Return true if this temporary object empty,
113  // ie, a temporary without allocation
114  inline bool empty() const;
115 
116  //- Is this temporary object valid,
117  // ie, it is a reference or a temporary that has been allocated
118  inline bool valid() const;
119 
120  //- Return the type name of the tmpNrc
121  // constructed from the type name of T
122  inline word typeName() const;
123 
124 
125  // Edit
126 
127  //- Return non-const reference or generate a fatal error
128  // if the object is const.
129  inline T& ref();
130 
131  //- Return tmpNrc pointer for reuse.
132  // Returns a clone if the object is not a temporary
133  inline T* ptr() const;
134 
135  //- If object pointer points to valid object:
136  // delete object and set pointer to nullptr
137  inline void clear() const;
138 
139 
140  // Member Operators
141 
142  //- Const dereference operator
143  inline const T& operator()() const;
144 
145  //- Const cast to the underlying type reference
146  inline operator const T&() const;
147 
148  //- Return object pointer
149  inline T* operator->();
150 
151  //- Return const object pointer
152  inline const T* operator->() const;
153 
154  //- Assignment to pointer changing this tmpNrc to a temporary T
155  inline void operator=(T*);
156 
157  //- Assignment transferring the temporary T to this tmpNrc
158  inline void operator=(const tmpNrc<T>&);
159 
160  //- Move assignment transferring the temporary T to this tmpNrc
161  inline void operator=(const tmpNrc<T>&&);
162 };
163 
164 
165 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
166 
167 } // End namespace Foam
168 
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 
171 #include "tmpNrcI.H"
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 #endif
176 
177 // ************************************************************************* //
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:53
void operator=(T *)
Assignment to pointer changing this tmpNrc to a temporary T.
Definition: tmpNrcI.H:272
bool valid() const
Is this temporary object valid,.
Definition: tmpNrcI.H:135
T & ref()
Return non-const reference or generate a fatal error.
Definition: tmpNrcI.H:149
T * operator->()
Return object pointer.
Definition: tmpNrcI.H:235
word typeName() const
Return the type name of the tmpNrc.
Definition: tmpNrcI.H:142
~tmpNrc()
Destructor: deletes temporary object when the reference count is 0.
Definition: tmpNrcI.H:112
bool empty() const
Return true if this temporary object empty,.
Definition: tmpNrcI.H:128
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpNrcI.H:121
tmpNrc(T *=0)
Store object pointer.
Definition: tmpNrcI.H:32
T * ptr() const
Return tmpNrc pointer for reuse.
Definition: tmpNrcI.H:173
void clear() const
If object pointer points to valid object:
Definition: tmpNrcI.H:197
const T & operator()() const
Const dereference operator.
Definition: tmpNrcI.H:210
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)