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.
This commit is contained in:
Mark Olesen
2017-05-01 22:39:36 +02:00
parent 7cceda620d
commit 0298c02b2d
3 changed files with 40 additions and 6 deletions

View File

@ -1,3 +1,3 @@
Test-nullObject.C Test-nullObject.C
EXE = $(FOAM_USER_APPBIN)/nullObject EXE = $(FOAM_USER_APPBIN)/Test-nullObject

View File

@ -21,6 +21,17 @@ int main()
SimpleClass* ptrToClass = new SimpleClass; SimpleClass* ptrToClass = new SimpleClass;
SimpleClass& refToClass(*ptrToClass); 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)) if (notNull(ptrToClass))
{ {
Info<< "Pass: ptrToClass is not null" << endl; Info<< "Pass: ptrToClass is not null" << endl;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,7 +25,9 @@ Class
Foam::nullObject Foam::nullObject
Description 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 SourceFiles
nullObjectI.H nullObjectI.H
@ -47,20 +49,41 @@ namespace Foam
class NullObject 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 //- Private constructor
NullObject() NullObject()
:
null{nullptr}
{} {}
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
NullObject(const NullObject&); NullObject(const NullObject&) = delete;
//- Disallow default bitwise assignment //- Disallow default bitwise assignment
void operator=(const NullObject&); void operator=(const NullObject&) = delete;
public: public:
//- The unique null object //- The unique null object
static const NullObject nullObject; 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;
}
}; };