Loading...
Searching...
No Matches
nullObject.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) 2014 OpenFOAM Foundation
9 Copyright (C) 2017-2024 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::NullObject
29
30Description
31 Singleton null-object class and instance.
32
33 Its contents occupy enough space to also be reinterpreted
34 as another class with a null pointer or zero long for its first
35 member, with additional zero parameters for safe casting to List etc.
36
37SourceFiles
38 nullObject.C
39
40\*---------------------------------------------------------------------------*/
41
42#ifndef Foam_nullObject_H
43#define Foam_nullObject_H
44
45#include "labelFwd.H"
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53class Istream;
54class Ostream;
55class NullObject;
57/*---------------------------------------------------------------------------*\
58 Class NullObject Declaration
59\*---------------------------------------------------------------------------*/
60
61class NullObject
62{
63 //- A %union of zero data types
64 union zeros
65 {
66 void* ptr;
67 unsigned long val;
68 };
69
70
71 // Private Data
72
73 //- The zero data content
74 zeros data_[4];
75
76
77 // Constructors
78
79 //- Private constructor for singleton only
80 // Could also rely on bit-wise zero initialization for union content
81 NullObject()
82 :
83 data_{{nullptr}, {nullptr}, {nullptr}, {nullptr}}
84 {}
85
86 //- No copy construct
87 NullObject(const NullObject&) = delete;
88
89 //- No copy assignment
90 void operator=(const NullObject&) = delete;
91
92
93public:
94
95 // Static Data
96
97 //- A unique null object
98 static const NullObject nullObject;
99
100
101 // Member Functions
102
103 //- A nullptr pointer content
104 const void* pointer() const noexcept
105 {
106 return data_[0].ptr;
107 }
108
109 //- Zero valued integer content
110 unsigned long value() const noexcept
111 {
112 return 0;
113 }
115 //- No elements
116 bool empty() const noexcept
117 {
118 return true;
119 }
120
121 //- Zero elements
122 label size() const noexcept
123 {
124 return 0;
125 }
126
127 //- No-op method (for HashTable replacement)
128 const NullObject& toc() const noexcept
129 {
130 return *this;
131 }
132
133 //- No-op method (for HashTable replacement)
134 const NullObject& sortedToc() const noexcept
135 {
136 return *this;
137 }
139
140 // Member Operators
141
142 //- Swallow assignment (cf, std::ignore)
143 template<class T>
144 const NullObject& operator=(const T&) const noexcept
145 {
146 return *this;
147 }
148};
149
150
151// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152
153// Globals
155//- Pointer to the unique nullObject
156extern const NullObject* nullObjectPtr;
157
158
159// IOstream Operators
160
161//- Read from Istream consumes no content, does not change NullObject
162inline Istream& operator>>(Istream& is, const NullObject&) noexcept
163{
164 return is;
165}
167//- Write to Ostream emits no content
168inline Ostream& operator<<(Ostream& os, const NullObject&) noexcept
169{
170 return os;
171}
172
173
174// Global Functions
175
176//- Const pointer (of type T) to the nullObject
177template<class T>
178inline const T* NullObjectPtr() noexcept
179{
180 return reinterpret_cast<const T*>(nullObjectPtr);
181}
182
183//- Non-const pointer (of type T) to the nullObject.
184//- Only use when nothing will be written into it!
185template<class T>
187{
188 return reinterpret_cast<T*>(const_cast<NullObject*>(nullObjectPtr));
189}
190
191
192//- Const reference (of type T) to the nullObject
193template<class T>
194inline const T& NullObjectRef() noexcept
195{
196 return *reinterpret_cast<const T*>(nullObjectPtr);
197}
198
199//- Non-const reference (of type T) to the nullObject
200//- Only use when nothing will be written into it!
201template<class T>
203{
204 return *reinterpret_cast<T*>(const_cast<NullObject*>(nullObjectPtr));
205}
206
207
208//- True if ptr is a pointer (of type T) to the nullObject
209template<class T>
210inline bool isNull(const T* ptr) noexcept
211{
212 return ptr == NullObjectPtr<T>();
213}
214
215//- True if obj is a reference (of type T) to the nullObject
216template<class T>
217inline bool isNull(const T& obj) noexcept
219 return &obj == NullObjectPtr<T>();
220}
221
222
223//- True if ptr is not a pointer (of type T) to the nullObject
224template<class T>
225inline bool notNull(const T* ptr) noexcept
226{
227 return ptr != NullObjectPtr<T>();
229
230//- True if obj is not a reference (of type T) to the nullObject
231template<class T>
232inline bool notNull(const T& obj) noexcept
233{
234 return &obj != NullObjectPtr<T>();
235}
236
237
238// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239
240} // End namespace Foam
241
242// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243
244#endif
245
246// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
Singleton null-object class and instance.
Definition nullObject.H:57
const NullObject & sortedToc() const noexcept
No-op method (for HashTable replacement).
Definition nullObject.H:154
bool empty() const noexcept
No elements.
Definition nullObject.H:130
const void * pointer() const noexcept
A nullptr pointer content.
Definition nullObject.H:114
const NullObject & operator=(const T &) const noexcept
Swallow assignment (cf, std::ignore).
Definition nullObject.H:166
unsigned long value() const noexcept
Zero valued integer content.
Definition nullObject.H:122
label size() const noexcept
Zero elements.
Definition nullObject.H:138
static const NullObject nullObject
A unique null object.
Definition nullObject.H:106
const NullObject & toc() const noexcept
No-op method (for HashTable replacement).
Definition nullObject.H:146
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Typedefs for label/uLabel without requiring label.H.
Namespace for OpenFOAM.
const T & NullObjectRef() noexcept
Const reference (of type T) to the nullObject.
Definition nullObject.H:228
T * NullObjectPtr_constCast() noexcept
Non-const pointer (of type T) to the nullObject. Only use when nothing will be written into it!
Definition nullObject.H:218
T & NullObjectRef_constCast() noexcept
Non-const reference (of type T) to the nullObject Only use when nothing will be written into it!
Definition nullObject.H:238
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition nullObject.C:29
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const T * NullObjectPtr() noexcept
Const pointer (of type T) to the nullObject.
Definition nullObject.H:208
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
bool notNull(const T *ptr) noexcept
True if ptr is not a pointer (of type T) to the nullObject.
Definition nullObject.H:267
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool isNull(const T *ptr) noexcept
True if ptr is a pointer (of type T) to the nullObject.
Definition nullObject.H:248