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 -------------------------------------------------------------------------------
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::NullObject
29 
30 Description
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 
37 SourceFiles
38  nullObject.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_nullObject_H
43 #define Foam_nullObject_H
44 
45 #include "labelFwd.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class Istream;
54 class Ostream;
55 class NullObject;
56 
57 /*---------------------------------------------------------------------------*\
58  Class NullObject Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class 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 
93 public:
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
156 extern const NullObject* nullObjectPtr;
157 
158 
159 // IOstream Operators
160 
161 //- Read from Istream consumes no content, does not change NullObject
162 inline Istream& operator>>(Istream& is, const NullObject&) noexcept
163 {
164  return is;
165 }
167 //- Write to Ostream emits no content
169 {
170  return os;
171 }
172 
173 
174 // Global Functions
175 
176 //- Const pointer (of type T) to the nullObject
177 template<class T>
178 inline 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!
185 template<class T>
187 {
188  return reinterpret_cast<T*>(const_cast<NullObject*>(nullObjectPtr));
189 }
190 
191 
192 //- Const reference (of type T) to the nullObject
193 template<class T>
194 inline 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!
201 template<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
209 template<class T>
210 inline 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
216 template<class T>
217 inline bool isNull(const T& obj) noexcept
218 {
219  return &obj == NullObjectPtr<T>();
220 }
221 
222 
223 //- True if ptr is not a pointer (of type T) to the nullObject
224 template<class T>
225 inline bool notNull(const T* ptr) noexcept
226 {
227  return ptr != NullObjectPtr<T>();
228 }
229 
230 //- True if obj is not a reference (of type T) to the nullObject
231 template<class T>
232 inline 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 // ************************************************************************* //
const NullObject & toc() const noexcept
No-op method (for HashTable replacement)
Definition: nullObject.H:146
const T & NullObjectRef() noexcept
Const reference (of type T) to the nullObject.
Definition: nullObject.H:228
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
unsigned long value() const noexcept
Zero valued integer content.
Definition: nullObject.H:122
const NullObject & sortedToc() const noexcept
No-op method (for HashTable replacement)
Definition: nullObject.H:154
const void * pointer() const noexcept
A nullptr pointer content.
Definition: nullObject.H:114
label size() const noexcept
Zero elements.
Definition: nullObject.H:138
const T * NullObjectPtr() noexcept
Const pointer (of type T) to the nullObject.
Definition: nullObject.H:208
Typedefs for label/uLabel without requiring label.H.
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
Istream & operator>>(Istream &, directionInfo &)
static const NullObject nullObject
A unique null object.
Definition: nullObject.H:106
bool isNull(const T *ptr) noexcept
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:248
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:29
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool notNull(const T *ptr) noexcept
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:267
Singleton null-object class and instance.
Definition: nullObject.H:56
Namespace for OpenFOAM.
bool empty() const noexcept
No elements.
Definition: nullObject.H:130