ENH: reduce storage requirement for volumeType

- char instead of integer representation for the enumeration.

- additional constructor as lookupOrDefault from dictionary entry.
This commit is contained in:
Mark Olesen
2018-07-11 08:50:38 +02:00
parent d530bc254a
commit a7e37656e8
6 changed files with 70 additions and 65 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "volumeType.H"
#include "dictionary.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -34,12 +35,33 @@ const Foam::Enum
Foam::volumeType::names
{
{ type::UNKNOWN, "unknown" },
{ type::MIXED, "mixed" },
{ type::INSIDE, "inside" },
{ type::OUTSIDE, "outside" },
{ type::MIXED, "mixed" },
};
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::volumeType::volumeType
(
const word& key,
const dictionary& dict,
const type deflt
)
:
t_(names.lookupOrDefault(key, dict, deflt))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
const Foam::word& Foam::volumeType::str() const
{
return names[t_];
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, volumeType& vt)
@ -47,10 +69,10 @@ Foam::Istream& Foam::operator>>(Istream& is, volumeType& vt)
// Read beginning of volumeType
is.readBegin("volumeType");
int type;
is >> type;
int val;
is >> val;
vt.t_ = static_cast<volumeType::type>(type);
vt.t_ = static_cast<volumeType::type>(val);
// Read end of volumeType
is.readEnd("volumeType");

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -25,6 +25,8 @@ Class
Foam::volumeType
Description
An enumeration wrapper for classification of a location as being
inside/outside of a volume.
SourceFiles
volumeType.C
@ -41,8 +43,8 @@ SourceFiles
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class dictionary;
class volumeType;
Istream& operator>>(Istream& is, volumeType& vt);
Ostream& operator<<(Ostream& os, const volumeType& vt);
@ -56,16 +58,18 @@ class volumeType
{
public:
//- Volume types
enum type
//- Volume classification types
enum type : char
{
UNKNOWN = 0,
MIXED = 1,
INSIDE = 2,
OUTSIDE = 3
UNKNOWN = 0, //!< Unknown state
INSIDE = 0x1, //!< A location inside the volume
OUTSIDE = 0x2, //!< A location outside the volume
MIXED = 0x3 //!< A location that is partly inside and outside
};
// Static data
//- Names for the classification enumeration
static const Enum<volumeType> names;
@ -81,18 +85,21 @@ public:
// Constructors
//- Construct null
//- Construct null as UNKNOWN state
volumeType()
:
t_(UNKNOWN)
{}
//- Construct from components
//- Construct from enumeration
volumeType(type t)
:
t_(t)
{}
//- Construct as lookupOrDefault by name from dictionary
volumeType(const word& key, const dictionary& dict, const type deflt);
//- Construct from integer
explicit volumeType(const int t)
:
@ -102,11 +109,15 @@ public:
// Member Functions
//- Return the enumeration
operator type() const
{
return t_;
}
//- The string representation of the volume type enumeration
const word& str() const;
// IOstream operators