Commit 0298c02b authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: ensure nullObject is large enough for reinterpret

- previously occupied 1 byte. Now occupies enough to hold a pointer,
  which helps if using reinterpret_cast to something that has some
  member content.
parent 7cceda62
Test-nullObject.C
EXE = $(FOAM_USER_APPBIN)/nullObject
EXE = $(FOAM_USER_APPBIN)/Test-nullObject
......@@ -21,6 +21,17 @@ int main()
SimpleClass* ptrToClass = new SimpleClass;
SimpleClass& refToClass(*ptrToClass);
typedef unsigned long ptrval;
Info<<"nullObject address=" << ptrval(&(nullObjectPtr)) << endl;
Info<<"sizeof(nullObject)" << " == "
<< sizeof(NullObject::nullObject)
<< " vs. sizeof(void*)" << " == " << sizeof(void*)
<< endl;
Info<<"nullObject pointer:" << ptrval(nullObjectPtr->pointer()) << endl;
Info<<"nullObject value:" << nullObjectPtr->value() << endl;
if (notNull(ptrToClass))
{
Info<< "Pass: ptrToClass is not null" << endl;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,7 +25,9 @@ Class
Foam::nullObject
Description
Singleton null-object class and instance
Singleton null-object class and instance.
It occupies enough space to reinterpret its content as a class with
a null pointer for its content.
SourceFiles
nullObjectI.H
......@@ -47,20 +49,41 @@ namespace Foam
class NullObject
{
//- Ensure it occupies enough space to reinterpret_cast to a class
// having some member data
const union
{
void* ptr;
unsigned long val;
} null;
//- Private constructor
NullObject()
:
null{nullptr}
{}
//- Disallow default bitwise copy construct
NullObject(const NullObject&);
NullObject(const NullObject&) = delete;
//- Disallow default bitwise assignment
void operator=(const NullObject&);
void operator=(const NullObject&) = delete;
public:
//- The unique null object
static const NullObject nullObject;
//- A nullptr pointer content
inline const void* pointer() const
{
return null.ptr;
}
//- A zero value content
inline unsigned long value() const
{
return null.val;
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment