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-2019 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward Declarations
51 class Istream;
52 class Ostream;
53 class NullObject;
54 
55 /*---------------------------------------------------------------------------*\
56  Class NullObject Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 class NullObject
60 {
61  //- A %union of zero data types
62  union zeros
63  {
64  void* ptr;
65  unsigned long val;
66  };
67 
68 
69  // Private Data
70 
71  //- The zero data content
72  zeros data_[4];
73 
74 
75  // Constructors
76 
77  //- Private constructor for singleton only
78  // Could also rely on bit-wise zero initialization for union content
79  NullObject()
80  :
81  data_{{nullptr}, {nullptr}, {nullptr}, {nullptr}}
82  {}
83 
84  //- No copy construct
85  NullObject(const NullObject&) = delete;
86 
87  //- No copy assignment
88  void operator=(const NullObject&) = delete;
89 
90 
91 public:
92 
93  // Static Data
94 
95  //- A unique null object
96  static const NullObject nullObject;
97 
98 
99  // Member Functions
100 
101  //- A nullptr pointer content
102  inline const void* pointer() const
103  {
104  return data_[0].ptr;
105  }
106 
107  //- Zero valued integer content
108  inline unsigned long value() const
109  {
110  return data_[0].val;
111  }
112 
113  //- No elements
114  inline bool empty() const
115  {
116  return true;
117  }
118 
119  //- Zero elements
120  inline label size() const
121  {
122  return 0;
123  }
124 
125  //- No-op method (for HashTable replacement)
126  inline const NullObject& toc() const
127  {
128  return *this;
129  }
130 
131  //- No-op method (for HashTable replacement)
132  inline const NullObject& sortedToc() const
133  {
134  return *this;
135  }
136 };
137 
138 
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 
141 // Globals
142 
143 //- Pointer to the unique nullObject
144 extern const NullObject* nullObjectPtr;
145 
146 
147 // IOstream Operators
148 
149 //- Read from Istream consumes no content.
150 inline Istream& operator>>(Istream& is, NullObject&) noexcept
151 {
152  return is;
153 }
154 
155 //- Write to Ostream emits no content.
156 inline Ostream& operator<<(Ostream& os, const NullObject&) noexcept
157 {
158  return os;
159 }
160 
161 
162 // Global Functions
163 
164 //- Pointer (of type T) to the nullObject
165 template<class T>
166 inline const T* NullObjectPtr()
167 {
168  return reinterpret_cast<const T*>(nullObjectPtr);
169 }
170 
171 //- Reference (of type T) to the nullObject
172 template<class T>
173 inline const T& NullObjectRef()
174 {
175  return *reinterpret_cast<const T*>(nullObjectPtr);
176 }
177 
178 
179 //- True if ptr is a pointer (of type T) to the nullObject
180 template<class T>
181 inline bool isNull(const T* ptr)
182 {
183  return ptr == NullObjectPtr<T>();
184 }
185 
186 //- True if obj is a reference (of type T) to the nullObject
187 template<class T>
188 inline bool isNull(const T& obj)
189 {
190  return &obj == NullObjectPtr<T>();
191 }
192 
193 
194 //- True if ptr is not a pointer (of type T) to the nullObject
195 template<class T>
196 inline bool notNull(const T* ptr)
197 {
198  return ptr != NullObjectPtr<T>();
199 }
200 
201 //- True if obj is not a reference (of type T) to the nullObject
202 template<class T>
203 inline bool notNull(const T& obj)
204 {
205  return &obj != NullObjectPtr<T>();
206 }
207 
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 } // End namespace Foam
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 #endif
216 
217 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::nullObjectPtr
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::NullObject::value
unsigned long value() const
Zero valued integer content.
Definition: nullObject.H:107
Foam::NullObjectRef
const T & NullObjectRef()
Reference (of type T) to the nullObject.
Definition: nullObject.H:172
Foam::NullObject::pointer
const void * pointer() const
A nullptr pointer content.
Definition: nullObject.H:101
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:165
Foam::notNull
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:195
Foam::NullObject::nullObject
static const NullObject nullObject
A unique null object.
Definition: nullObject.H:95
Foam::NullObject::toc
const NullObject & toc() const
No-op method (for HashTable replacement)
Definition: nullObject.H:125
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::NullObject::empty
bool empty() const
No elements.
Definition: nullObject.H:113
Foam::NullObject::size
label size() const
Zero elements.
Definition: nullObject.H:119
Foam::NullObject
Singleton null-object class and instance.
Definition: nullObject.H:58
Foam::isNull
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:180
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:131
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102