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-2020 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 nullObject_H
43#define nullObject_H
44
45#include "labelFwd.H"
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53class Istream;
54class Ostream;
55class NullObject;
56
57/*---------------------------------------------------------------------------*\
58 Class NullObject Declaration
59\*---------------------------------------------------------------------------*/
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
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 inline const void* pointer() const
105 {
106 return data_[0].ptr;
107 }
108
109 //- Zero valued integer content
110 inline unsigned long value() const
111 {
112 return 0;
113 }
114
115 //- No elements
116 inline bool empty() const
117 {
118 return true;
119 }
120
121 //- Zero elements
122 inline label size() const
123 {
124 return 0;
125 }
126
127 //- No-op method (for HashTable replacement)
128 inline const NullObject& toc() const
129 {
130 return *this;
131 }
132
133 //- No-op method (for HashTable replacement)
134 inline const NullObject& sortedToc() const
135 {
136 return *this;
137 }
138
139
140 // Member Operators
141
142 //- Swallow assignment (cf, std::ignore)
143 template<class T>
144 inline const NullObject& operator=(const T&) const
145 {
146 return *this;
147 }
148};
149
150
151// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152
153// Globals
154
155//- Pointer to the unique nullObject
156extern const NullObject* nullObjectPtr;
157
158
159// IOstream Operators
160
161//- Read from Istream consumes no content
162inline Istream& operator>>(Istream& is, const NullObject&) noexcept
163{
164 return is;
165}
166
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//- Pointer (of type T) to the nullObject
177template<class T>
178inline const T* NullObjectPtr()
179{
180 return reinterpret_cast<const T*>(nullObjectPtr);
181}
182
183//- Reference (of type T) to the nullObject
184template<class T>
185inline const T& NullObjectRef()
186{
187 return *reinterpret_cast<const T*>(nullObjectPtr);
188}
189
190
191//- True if ptr is a pointer (of type T) to the nullObject
192template<class T>
193inline bool isNull(const T* ptr)
194{
195 return ptr == NullObjectPtr<T>();
196}
197
198//- True if obj is a reference (of type T) to the nullObject
199template<class T>
200inline bool isNull(const T& obj)
201{
202 return &obj == NullObjectPtr<T>();
203}
204
205
206//- True if ptr is not a pointer (of type T) to the nullObject
207template<class T>
208inline bool notNull(const T* ptr)
209{
210 return ptr != NullObjectPtr<T>();
211}
212
213//- True if obj is not a reference (of type T) to the nullObject
214template<class T>
215inline bool notNull(const T& obj)
216{
217 return &obj != NullObjectPtr<T>();
218}
219
220
221// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222
223} // End namespace Foam
224
225// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226
227#endif
228
229// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Singleton null-object class and instance.
Definition: nullObject.H:61
const NullObject & operator=(const T &) const
Swallow assignment (cf, std::ignore)
Definition: nullObject.H:143
bool empty() const
No elements.
Definition: nullObject.H:115
const NullObject & toc() const
No-op method (for HashTable replacement)
Definition: nullObject.H:127
unsigned long value() const
Zero valued integer content.
Definition: nullObject.H:109
const NullObject & sortedToc() const
No-op method (for HashTable replacement)
Definition: nullObject.H:133
label size() const
Zero elements.
Definition: nullObject.H:121
static const NullObject nullObject
A unique null object.
Definition: nullObject.H:97
const void * pointer() const
A nullptr pointer content.
Definition: nullObject.H:103
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Typedefs for label/uLabel without requiring label.H.
Namespace for OpenFOAM.
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:192
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const T * NullObjectPtr()
Pointer (of type T) to the nullObject.
Definition: nullObject.H:177
const T & NullObjectRef()
Reference (of type T) to the nullObject.
Definition: nullObject.H:184
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:207