diff --git a/applications/test/nullObject/Make/files b/applications/test/nullObject/Make/files index 596014a446..dffa06d747 100644 --- a/applications/test/nullObject/Make/files +++ b/applications/test/nullObject/Make/files @@ -1,3 +1,3 @@ Test-nullObject.C -EXE = $(FOAM_USER_APPBIN)/nullObject +EXE = $(FOAM_USER_APPBIN)/Test-nullObject diff --git a/applications/test/nullObject/Test-nullObject.C b/applications/test/nullObject/Test-nullObject.C index 9b43405f45..18524006dc 100644 --- a/applications/test/nullObject/Test-nullObject.C +++ b/applications/test/nullObject/Test-nullObject.C @@ -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; diff --git a/src/OpenFOAM/primitives/nullObject/nullObject.H b/src/OpenFOAM/primitives/nullObject/nullObject.H index 71217836c1..e0ab12029e 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObject.H +++ b/src/OpenFOAM/primitives/nullObject/nullObject.H @@ -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; + } };