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 nullObject_H
43 #define 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  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
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 }
166 
167 //- Write to Ostream emits no content
168 inline 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
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 // ************************************************************************* //
Foam::nullObjectPtr
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::NullObject::value
unsigned long value() const
Zero valued integer content.
Definition: nullObject.H:109
Foam::NullObjectRef
const T & NullObjectRef()
Reference (of type T) to the nullObject.
Definition: nullObject.H:184
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::NullObject::pointer
const void * pointer() const
A nullptr pointer content.
Definition: nullObject.H:103
Foam::NullObject::operator=
const NullObject & operator=(const T &) const
Swallow assignment (cf, std::ignore)
Definition: nullObject.H:143
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::NullObjectPtr
const T * NullObjectPtr()
Pointer (of type T) to the nullObject.
Definition: nullObject.H:177
Foam::notNull
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:207
labelFwd.H
Typedefs for label/uLabel without requiring label.H.
Foam::NullObject::nullObject
static const NullObject nullObject
A unique null object.
Definition: nullObject.H:97
os
OBJstream os(runTime.globalPath()/outputName)
Foam::NullObject::toc
const NullObject & toc() const
No-op method (for HashTable replacement)
Definition: nullObject.H:127
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::NullObject::empty
bool empty() const
No elements.
Definition: nullObject.H:115
Foam::NullObject::size
label size() const
Zero elements.
Definition: nullObject.H:121
Foam::NullObject
Singleton null-object class and instance.
Definition: nullObject.H:60
Foam::isNull
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:192
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::NullObject::sortedToc
const NullObject & sortedToc() const
No-op method (for HashTable replacement)
Definition: nullObject.H:133