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 -------------------------------------------------------------------------------
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
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 //- Pointer (of type T) to the nullObject
177 template<class T>
178 inline const T* NullObjectPtr()
179 {
180  return reinterpret_cast<const T*>(nullObjectPtr);
181 }
182 
183 //- Reference (of type T) to the nullObject
184 template<class T>
185 inline 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
192 template<class T>
193 inline 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
199 template<class T>
200 inline 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
207 template<class T>
208 inline 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
214 template<class T>
215 inline 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 // ************************************************************************* //
const NullObject & toc() const noexcept
No-op method (for HashTable replacement)
Definition: nullObject.H:146
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
Typedefs for label/uLabel without requiring label.H.
Istream & operator>>(Istream &, directionInfo &)
static const NullObject nullObject
A unique null object.
Definition: nullObject.H:106
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
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:227
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
const T * NullObjectPtr()
Pointer (of type T) to the nullObject.
Definition: nullObject.H:208
Singleton null-object class and instance.
Definition: nullObject.H:56
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:246
const T & NullObjectRef()
Reference (of type T) to the nullObject.
Definition: nullObject.H:217
Namespace for OpenFOAM.
bool empty() const noexcept
No elements.
Definition: nullObject.H:130