mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -1,3 +1,3 @@
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user