ENH: dummy I/O for nullObject, additional null subclass for zero/one

- the zero::null and one::null sub-classes add an additional null
  output adapter.

  The function of the nil class (special-purpose class only used for
  HashSet) is now taken by zero::null.
This commit is contained in:
Mark Olesen 2017-10-31 10:54:17 +01:00
parent c5344c3161
commit 930d6dcaf2
8 changed files with 192 additions and 129 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,6 +26,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "hashedWordList.H"
#include "nil.H"
#include "HashSet.H"
#include "Map.H"
#include "labelPairHashes.H"
@ -69,6 +70,8 @@ int main(int argc, char *argv[])
Info<< "tableA keys: "; tableA.writeKeys(Info) << endl;
Info<< "tableB content: " << tableB << endl;
auto keyIterPair = tableA.keys();
for (const auto& i : keyIterPair)
{

View File

@ -61,6 +61,10 @@ int main(int argc, char *argv[])
nil x;
cout<<"nil:" << sizeof(x) << nl;
}
{
zero x;
cout<<"zero:" << sizeof(x) << nl;
}
{
bool x(0);
cout<<"bool:" << sizeof(x) << nl;
@ -93,6 +97,10 @@ int main(int argc, char *argv[])
cout<<"double:" << sizeof(double) << nl;
}
{
cout<<"string:" << sizeof(Foam::string) << nl;
}
Info << "---\nEnd\n" << endl;

View File

@ -1,97 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::nil
Description
A zero-sized class without any storage. Used, for example, in HashSet.
Note
A zero-sized class actually does still require at least 1 byte storage.
\*---------------------------------------------------------------------------*/
#ifndef nil_H
#define nil_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class Istream;
class Ostream;
// Forward declaration of friend functions and operators
class nil;
Istream& operator>>(Istream&, nil&);
Ostream& operator<<(Ostream&, const nil&);
/*---------------------------------------------------------------------------*\
Class nil Declaration
\*---------------------------------------------------------------------------*/
class nil
{
public:
// Constructors
//- Construct null
nil()
{}
//- Construct from Istream
nil(Istream&)
{}
// IOstream Operators
friend Istream& operator>>(Istream& is, nil&)
{
return is;
}
friend Ostream& operator<<(Ostream& os, const nil&)
{
return os;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -22,12 +22,13 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::nullObject
Foam::NullObject
Description
Singleton null-object class and instance.
It occupies enough space to reinterpret its content as a class with
a null pointer for its content.
Its contents occupy just enough space to also be reinterpreted
as another class with a null pointer or zero long for its first
member.
SourceFiles
nullObjectI.H
@ -43,8 +44,14 @@ SourceFiles
namespace Foam
{
// Forward declarations
class Istream;
class Ostream;
class NullObject;
/*---------------------------------------------------------------------------*\
Class nullObject Declaration
Class NullObject Declaration
\*---------------------------------------------------------------------------*/
class NullObject
@ -55,12 +62,12 @@ class NullObject
{
void* ptr;
unsigned long val;
} null;
} content;
//- Private constructor
//- Private constructor for singleton only
NullObject()
:
null{nullptr}
content{nullptr}
{}
//- Disallow default bitwise copy construct
@ -70,23 +77,44 @@ class 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;
}
// Static Data
//- A zero value content
inline unsigned long value() const
{
return null.val;
}
//- A unique null object
static const NullObject nullObject;
// Member Functions
//- A nullptr pointer content
inline const void* pointer() const
{
return content.ptr;
}
//- Zero valued integer content
inline unsigned long value() const
{
return content.val;
}
};
// IOstream Operators
//- Read from Istream consumes no content.
inline Istream& operator>>(Istream& is, NullObject&)
{
return is;
}
//- Write to Ostream emits no content.
inline Ostream& operator<<(Ostream& os, const NullObject&)
{
return os;
}
//- Pointer to the unique nullObject
extern const NullObject* nullObjectPtr;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -25,9 +25,8 @@ Class
Foam::one
Description
A class representing the concept of 1 (scalar(1.0)) used to avoid
unnecessary manipulations for objects which are known to be one at
compile-time.
A class representing the concept of 1 (or 1.0) used to avoid unnecessary
manipulations for objects that are known to be one at compile-time.
SourceFiles
oneI.H
@ -44,6 +43,11 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes, friend functions and operators
class Istream;
class Ostream;
class one;
/*---------------------------------------------------------------------------*\
Class one Declaration
\*---------------------------------------------------------------------------*/
@ -51,15 +55,22 @@ namespace Foam
class one
{
public:
typedef one value_type;
// Forward declaration
class null;
// Constructors
//- Construct null
//- Null constructible
one()
{}
//- Construct null from Istream. Consumes no content.
explicit one(Istream&)
{}
// Member operators
@ -83,6 +94,43 @@ public:
};
/*---------------------------------------------------------------------------*\
Class one::null Declaration
\*---------------------------------------------------------------------------*/
//- A one class with a null output adapter.
class one::null
:
public one
{
public:
typedef null value_type;
//- Null constructible
null()
{}
//- Construct null from Istream without consuming any content.
explicit null(Istream&)
{}
};
// IOstream Operators
//- Read from Istream consumes no content.
inline Istream& operator>>(Istream& is, one&)
{
return is;
}
//- Write to Ostream emits no content.
inline Ostream& operator<<(Ostream& os, const one::null&)
{
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -39,13 +39,11 @@ Description
namespace Foam
{
typedef wordRes wordReListMatcher;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
#endif
// ************************************************************************* //

View File

@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------*\
Compatibility include
Typedef
Foam::nil
Description
The older name for Foam::zero::null.
\*---------------------------------------------------------------------------*/
#ifndef nil_H
#define nil_H
#include "zero.H"
namespace Foam
{
typedef zero::null nil;
}
#endif
// ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -43,6 +43,12 @@ SourceFiles
namespace Foam
{
// Forward declarations
class Istream;
class Ostream;
class zero;
/*---------------------------------------------------------------------------*\
Class zero Declaration
\*---------------------------------------------------------------------------*/
@ -50,15 +56,22 @@ namespace Foam
class zero
{
public:
typedef zero value_type;
// Forward declaration
class null;
// Constructors
//- Construct null
//- Null constructible
zero()
{}
//- Construct null from Istream. Consumes no content.
explicit zero(Istream&)
{}
// Member operators
@ -85,9 +98,47 @@ public:
{
return 0;
}
};
/*---------------------------------------------------------------------------*\
Class zero::null Declaration
\*---------------------------------------------------------------------------*/
//- A zero class with a null output adapter.
class zero::null
:
public zero
{
public:
typedef null value_type;
//- Null constructible
null()
{}
//- Construct null from Istream without consuming any content.
explicit null(Istream&)
{}
};
// IOstream Operators
//- Read from Istream consumes no content.
inline Istream& operator>>(Istream& is, zero&)
{
return is;
}
//- Write to Ostream emits no content.
inline Ostream& operator<<(Ostream& os, const zero::null&)
{
return os;
}
// Global zero
static const zero Zero;