ENH: Field assign and construct from dictionary with readOption

- This simplifies definition of 'lazier' (READ_IF_PRESENT)
  construction or assignment.

  For construction:
    - For MUST_READ and key not found: FatalIOError.
    - For LAZY_READ and key not found: initialise field with Zero.
    - For NO_READ and key not found: simply size the field.

  For assignment:
    - If len == 0 : a no-op and return True.
    - For NO_READ : a no-op and return False.
    - For MUST_READ and key not found : FatalIOError
This commit is contained in:
Mark Olesen
2023-01-30 10:30:41 +01:00
parent bebc6195a8
commit 72bfaa2be9
3 changed files with 79 additions and 28 deletions

View File

@ -179,20 +179,32 @@ Foam::Field<Type>::Field
template<class Type>
Foam::Field<Type>::Field(const entry& e, const label len)
{
assign(e, len);
Field<Type>::assign(e, len);
}
template<class Type>
Foam::Field<Type>::Field
(
const word& keyword,
const word& key,
const dictionary& dict,
const label len,
enum keyType::option matchOpt
IOobjectOption::readOption readOpt
)
{
assign(keyword, dict, len, matchOpt);
if (!Field<Type>::assign(key, dict, len, readOpt))
{
if (IOobjectOption::isReadOptional(readOpt))
{
// Lazy read: init with zero value
if (len > 0) this->resize(len, Zero);
}
else
{
// No read: set length only
if (len > 0) this->resize(len);
}
}
}
@ -213,7 +225,7 @@ void Foam::Field<Type>::assign(const entry& e, const label len)
// Resize to expected length (or -1 : retain current length)
if (len >= 0)
{
this->resize(len);
this->resize_nocopy(len);
}
operator=(pTraits<Type>(is));
}
@ -239,7 +251,7 @@ void Foam::Field<Type>::assign(const entry& e, const label len)
else
{
FatalIOErrorInFunction(is)
<< "size " << lenRead
<< "Size " << lenRead
<< " is not equal to the expected length " << len
<< exit(FatalIOError);
}
@ -257,18 +269,39 @@ void Foam::Field<Type>::assign(const entry& e, const label len)
template<class Type>
void Foam::Field<Type>::assign
bool Foam::Field<Type>::assign
(
const word& keyword,
const word& key,
const dictionary& dict,
const label len,
enum keyType::option matchOpt
IOobjectOption::readOption readOpt
)
{
if (len)
if (!len)
{
assign(dict.lookupEntry(keyword, matchOpt), len);
return true;
}
else if (readOpt != IOobjectOption::NO_READ)
{
const entry* eptr = dict.findEntry(key, keyType::LITERAL);
if (eptr)
{
Field<Type>::assign(*eptr, len);
return true;
}
// Missing (mandatory or optional)
if (IOobjectOption::isReadRequired(readOpt))
{
FatalIOErrorInFunction(dict)
<< "Required entry '" << key << "' missing in dictionary "
<< dict.relativeName() << nl
<< exit(FatalIOError);
}
}
return false;
}

View File

@ -52,6 +52,7 @@ SourceFiles
#include "keyType.H"
#include "scalarList.H"
#include "VectorSpace.H"
#include "IOobjectOption.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -69,7 +70,6 @@ template<class Type> class SubField;
template<class Type> Ostream& operator<<(Ostream&, const Field<Type>&);
template<class Type> Ostream& operator<<(Ostream&, const tmp<Field<Type>>&);
/*---------------------------------------------------------------------------*\
Class FieldBase Declaration
\*---------------------------------------------------------------------------*/
@ -264,17 +264,22 @@ public:
//- Construct from Istream
inline Field(Istream& is);
//- Construct from a dictionary (primitive) entry
//- Construct from a dictionary (primitive) entry.
Field(const entry& e, const label len);
//- Construct from lookup of a dictionary (primitive) entry.
// Uses REGEX lookup for legacy compatibility.
//- Lookup of a primitive dictionary entry by (literal) name
//- and assign its contents to this. The behaviour largely as
//- described in assign():
// - For MUST_READ and key not found: FatalIOError.
// - For LAZY_READ and key not found: initialise field with Zero.
// - For NO_READ and key not found: simply size the field.
// .
Field
(
const word& keyword,
const dictionary& dict,
const label len,
enum keyType::option matchOpt = keyType::REGEX
const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary
const label len, //!< length
IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
);
//- Clone
@ -297,17 +302,31 @@ public:
// Member Functions
//- Assign from a dictionary (primitive) entry
//- Assign from a primitive dictionary entry with the following
//- behaviour:
// - If len == 0 : a no-op
// - If len < 0 : no size checking
// - If len > 0 : resize \em uniform entries to this size
// before assigning. Use as length check for \em nonuniform
// entries. A mismatch is possibly Fatal, except when
// allowConstructFromLargerSize is true (eg, for column mesh).
// .
void assign(const entry& e, const label len);
//- Assign from lookup of a dictionary (primitive) entry
// Uses REGEX lookup for legacy compatibility.
void assign
//- Lookup a primitive dictionary entry by (literal) name
//- and assign its contents to this (behaviour as described above).
// - If len == 0 : a no-op and return True.
// - For NO_READ : a no-op and return False.
// - For LAZY_READ and key not found : a no-op and return False.
// - For MUST_READ and key not found : FatalIOError
// .
// \returns True if an assignment occurred or for zero-length.
bool assign
(
const word& keyword,
const dictionary& dict,
const label len,
enum keyType::option matchOpt = keyType::REGEX
const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary
const label len, //!< expected length
IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
);
//- 1 to 1 map from the given field

View File

@ -1 +0,0 @@
#warning File removed - left for old dependency check only