tmpNrc.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::tmpNrc
29 
30 Description
31  A class for managing temporary objects without reference counting.
32 
33 SourceFiles
34  tmpNrcI.H
35 
36 See also
37  Foam::autoPtr
38  Foam::tmp
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef tmpNrc_H
43 #define tmpNrc_H
44 
45 #include "tmp.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class tmpNrc Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 template<class T>
57 class tmpNrc
58 {
59  // Private Data
60 
61  //- Object types
62  enum refType
63  {
64  PTR,
65  CREF
66  };
67 
68  //- The managed pointer or the address of const-reference object
69  mutable T* ptr_;
70 
71  //- The type (managed pointer | const-reference object)
72  mutable refType type_;
73 
74 
75 public:
76 
77  // STL type definitions
78 
79  //- Type of object being managed
80  typedef T element_type;
81 
82  //- Pointer to type of object being managed
83  typedef T* pointer;
84 
85 
86  //- Null reference counter class
88 
89 
90  // Factory Methods
91 
92  //- Construct tmpNrc of T with forwarding arguments
93  // \param args list of arguments with which an instance of T
94  // will be constructed.
95  //
96  // \note Similar to std::make_shared, but the overload for
97  // array types is not disabled.
98  template<class... Args>
99  inline static tmpNrc<T> New(Args&&... args);
100 
101  //- Construct tmpNrc from derived type with forwarding arguments
102  // \param args list of arguments with which an instance of U
103  // will be constructed.
104  //
105  // \note Similar to New but for derived types
106  template<class U, class... Args>
107  inline static tmpNrc<T> NewFrom(Args&&... args);
108 
109 
110  // Constructors
111 
112  //- Construct with no managed pointer.
113  inline constexpr tmpNrc() noexcept;
114 
115  //- Construct with no managed pointer.
116  inline constexpr tmpNrc(std::nullptr_t) noexcept;
117 
118  //- Construct, taking ownership of the pointer.
119  inline explicit tmpNrc(T* p) noexcept;
120 
121  //- Construct for a const reference to an object.
122  inline tmpNrc(const T& obj) noexcept;
123 
124  //- Move construct, transferring ownership.
125  inline tmpNrc(tmpNrc<T>&& t) noexcept;
126 
127  //- Copy construct
128  inline tmpNrc(const tmpNrc<T>& t);
129 
130  //- Copy construct. Optionally reusing pointer.
131  inline tmpNrc(const tmpNrc<T>& t, bool reuse);
132 
133 
134  //- Destructor: deletes managed pointer
135  inline ~tmpNrc();
136 
137 
138  // Member Functions
139 
140  // Query
141 
142  //- True if this is a managed pointer (not a const reference)
143  inline bool isTmp() const noexcept;
144 
145  //- True if this is a non-null managed pointer
146  inline bool empty() const noexcept;
147 
148  //- True if this is a non-null managed pointer,
149  //- or is a const object reference
150  inline bool valid() const noexcept;
151 
152  //- True if this is a non-null managed pointer with a unique ref-count
153  inline bool movable() const noexcept;
154 
155  //- Return type-name of the tmp, constructed from type-name of T
156  inline word typeName() const;
157 
158 
159  // Access
160 
161  //- Return pointer without nullptr checking.
162  inline T* get() noexcept;
163 
164  //- Return const pointer without nullptr checking.
165  inline const T* get() const noexcept;
166 
167  //- Return the const object reference or a const reference to the
168  //- contents of a non-null managed pointer.
169  // Fatal for a null managed pointer
170  inline const T& cref() const;
171 
172  //- Return non-const reference to the contents of a non-null
173  //- managed pointer.
174  // Fatal for a null managed pointer or if the object is const.
175  inline T& ref() const;
176 
177  //- Non-const dereference, even if the object is const.
178  // This is similar to ref(), but applies a const_cast to access
179  // const objects.
180  // Fatal for a null managed pointer.
181  inline T& constCast() const;
182 
183 
184  // Edit
185 
186  //- Return managed pointer for reuse, or clone() the const object
187  //- reference.
188  inline T* ptr() const;
189 
190  //- If object pointer points to valid object:
191  //- delete object and set pointer to nullptr
192  inline void clear() const noexcept;
193 
194  //- Release ownership of managed temporary object.
195  // After this call no object is managed.
196  inline void reset() noexcept;
197 
198  //- Delete managed temporary object and set to new given pointer
199  inline void reset(T* p) noexcept;
200 
201  //- Clear existing and transfer ownership.
202  inline void reset(tmpNrc<T>&& other) noexcept;
203 
204  //- Delete managed temporary object and set to const reference
205  inline void cref(const T& obj) noexcept;
206 
207  //- Swaps the managed object with other.
208  inline void swap(tmpNrc<T>& other) noexcept;
209 
210 
211  // Member Operators
212 
213  //- Return const reference to the object.
214  // Identical to cref() method.
215  inline const T& operator()() const;
216 
217  //- Cast to underlying data type, using the cref() method.
218  inline operator const T&() const;
219 
220  //- Dereferences (const) pointer to the managed object.
221  // Fatal for a null managed pointer.
222  inline const T* operator->() const;
223 
224  //- Dereferences (non-const) pointer to the managed object.
225  // Fatal for a null managed pointer or if the object is const.
226  inline T* operator->();
227 
228  //- Is non-null managed pointer or const object reference : valid()
229  explicit inline operator bool() const noexcept;
230 
231  //- Take ownership of the pointer.
232  // Fatal for a null pointer, or when the pointer is non-unique.
233  inline void operator=(T* p);
234 
235  //- Transfer ownership of the managed pointer.
236  // Fatal for a null managed pointer or if the object is const.
237  inline void operator=(const tmpNrc<T>& t);
238 
239  //- Clear existing and transfer ownership.
240  inline void operator=(tmpNrc<T>&& other) noexcept;
241 
242  //- Conversion to tmp
243  inline operator tmp<T>();
244 
245 
246  // Housekeeping
247 
248  //- No assignment from literal nullptr.
249  // Consistent with run-time check for nullptr on assignment.
250  void operator=(std::nullptr_t) = delete;
251 };
252 
253 
254 // Global Functions
255 
256 //- Specializes the Swap algorithm for tmpNrc.
257 // Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
258 template<class T>
259 void Swap(tmpNrc<T>& lhs, tmpNrc<T>& rhs)
260 {
261  lhs.swap(rhs);
262 }
263 
264 
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 
267 } // End namespace Foam
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 #include "tmpNrcI.H"
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #endif
276 
277 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::tmpNrc::refCount
Foam::refCount::zero refCount
Null reference counter class.
Definition: tmpNrc.H:86
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmpNrc::typeName
word typeName() const
Return type-name of the tmp, constructed from type-name of T.
Definition: tmpNrcI.H:184
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::tmpNrc::cref
const T & cref() const
Definition: tmpNrcI.H:205
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::tmpNrc::swap
void swap(tmpNrc< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpNrcI.H:337
tmpNrcI.H
Foam::tmpNrc::element_type
T element_type
Type of object being managed.
Definition: tmpNrc.H:79
Foam::tmpNrc
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:56
Foam::tmpNrc::ptr
T * ptr() const
Definition: tmpNrcI.H:260
Foam::tmpNrc::constCast
T & constCast() const
Non-const dereference, even if the object is const.
Definition: tmpNrcI.H:246
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::tmpNrc::clear
void clear() const noexcept
Definition: tmpNrcI.H:282
Foam::tmpNrc::reset
void reset() noexcept
Release ownership of managed temporary object.
Definition: tmpNrcI.H:293
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::tmpNrc::New
static tmpNrc< T > New(Args &&... args)
Construct tmpNrc of T with forwarding arguments.
U
U
Definition: pEqn.H:72
Foam::refCount::zero
A non-counting (dummy) refCount.
Definition: refCount.H:59
Foam::tmpNrc::NewFrom
static tmpNrc< T > NewFrom(Args &&... args)
Construct tmpNrc from derived type with forwarding arguments.
tmp.H
Foam::tmpNrc::valid
bool valid() const noexcept
Definition: tmpNrcI.H:170
Foam::tmpNrc::movable
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition: tmpNrcI.H:177
Foam::tmpNrc::pointer
T * pointer
Pointer to type of object being managed.
Definition: tmpNrc.H:82
Foam::tmpNrc::empty
bool empty() const noexcept
True if this is a non-null managed pointer.
Definition: tmpNrcI.H:163
Foam::tmpNrc::tmpNrc
constexpr tmpNrc() noexcept
Construct with no managed pointer.
Definition: tmpNrcI.H:53
args
Foam::argList args(argc, argv)
Foam::tmpNrc::isTmp
bool isTmp() const noexcept
True if this is a managed pointer (not a const reference)
Definition: tmpNrcI.H:156
Foam::tmpNrc::ref
T & ref() const
Definition: tmpNrcI.H:222
Foam::tmpNrc::get
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmpNrcI.H:191