ENH: rationalize expression string reading

- read construct from dictionary.
  Calling syntax similar to dimensionedType, dimensionedSet,...

  Replaces the older getEntry(), getOptional() static methods

- support readIfPresent
This commit is contained in:
Mark Olesen
2022-09-28 09:24:46 +02:00
parent 3a6e427409
commit ee9119f436
25 changed files with 218 additions and 261 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,6 +28,7 @@ License
#include "exprString.H"
#include "stringOps.H"
#include "expressionEntry.H"
#include "error.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
@ -47,36 +48,6 @@ void Foam::expressions::exprString::inplaceExpand
}
Foam::expressions::exprString
Foam::expressions::exprString::getEntry
(
const word& key,
const dictionary& dict,
const bool stripComments
)
{
exprString expr;
expr.readEntry(key, dict, true, stripComments); // mandatory
return expr;
}
Foam::expressions::exprString
Foam::expressions::exprString::getOptional
(
const word& key,
const dictionary& dict,
const bool stripComments
)
{
exprString expr;
expr.readEntry(key, dict, false, stripComments); // optional
return expr;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::expressions::exprString::expand
@ -103,15 +74,14 @@ bool Foam::expressions::exprString::readEntry
(
const word& keyword,
const dictionary& dict,
bool mandatory,
const bool stripComments
bool mandatory
)
{
const bool ok = dict.readEntry(keyword, *this, keyType::LITERAL, mandatory);
if (ok && !empty())
{
this->expand(dict, stripComments); // strip comments
this->expand(dict, true); // strip comments
}
else
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,8 +40,8 @@ SeeAlso
\*---------------------------------------------------------------------------*/
#ifndef expressions_exprString_H
#define expressions_exprString_H
#ifndef Foam_expressions_exprString_H
#define Foam_expressions_exprString_H
#include "string.H"
#include "dictionary.H"
@ -75,34 +75,26 @@ public:
exprString(exprString&& rhs) = default;
//- Copy construct from std::string
inline explicit exprString(const std::string& s, bool doValidate=true);
inline explicit exprString(const std::string& s, bool doCheck=true);
//- Move construct from std::string
inline explicit exprString(std::string&& s, bool doValidate=true);
inline explicit exprString(std::string&& s, bool doCheck=true);
//- Construct as copy of character array
inline explicit exprString(const char* s, bool doValidate=true);
inline explicit exprString(const char* s, bool doCheck=true);
//- Copy construct and expand with dictionary variables,
//- and strip C/C++ comments from the input
//- Construct from dictionary entry lookup.
//- Expands dictionary variables and strips and strips any
//- embedded C/C++ comments
inline exprString
(
const std::string& str,
const word& entryName, //!< Lookup key. LITERAL (not REGEX)
const dictionary& dict,
const bool stripComments = true
const bool mandatory = true
);
//- Move construct and expand with dictionary variables,
//- and strip C/C++ comments from the input
inline exprString
(
std::string&& str,
const dictionary& dict,
const bool stripComments = true
);
//- Construct from Istream and expand with dictionary variables,
//- and strip C/C++ comments from the input
//- Construct from Istream with dictionary context for expanding
//- dictionary variables. Strips any embedded C/C++ comments
inline exprString
(
Istream& is,
@ -117,8 +109,38 @@ public:
// Static Member Functions
//- Copy convert string to exprString.
// No expansions, know what you are doing.
inline static exprString toExpr(const std::string& str);
//- Move convert string to exprString.
// No expansions, know what you are doing.
inline static exprString toExpr(std::string&& str);
//- Copy convert string to exprString with dictionary expansions,
//- stripping any embedded C/C++ comments
//
// \sa inplaceExpand() for expansion details
static exprString toExpr
(
const std::string& str,
const dictionary& dict,
const bool stripComments = true
);
//- Move convert string to exprString with dictionary expansions,
//- stripping any embedded C/C++ comments
//
// \sa inplaceExpand() for expansion details
static exprString toExpr
(
std::string&& str,
const dictionary& dict,
const bool stripComments = true
);
//- Inplace expansion with dictionary variables,
//- and strip C/C++ comments from the input.
//- and strip any embedded C/C++ comments
//
// \par Expansion behaviour
// - alternatives = True
@ -133,34 +155,6 @@ public:
const bool stripComments = true
);
//- Get and expand expression with dictionary entries,
//- optionally strip C/C++ comments from the input.
// Expansion behaviour as per inplaceExpand
static exprString getEntry
(
const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict,
const bool stripComments = true
);
//- Get and expand expression with dictionary entries,
//- optionally strip C/C++ comments from the input.
// Expansion behaviour as per inplaceExpand
static exprString getOptional
(
const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict,
const bool stripComments = true
);
//- Copy convert string to exprString.
// No expansions, know what you are doing.
inline static exprString toExpr(const std::string& str);
//- Move convert string to exprString.
// No expansions, know what you are doing.
inline static exprString toExpr(std::string&& str);
// Member Functions
@ -168,20 +162,28 @@ public:
inline bool valid() const;
//- Inplace expansion with dictionary variables,
//- and strip C/C++ comments from the input
//- and strip any embedded C/C++ comments.
// \sa inplaceExpand() for expansion details
void expand(const dictionary& dict, const bool stripComments = true);
//- Inplace trim leading and trailing whitespace
void trim();
//- Read/expand entry with dictionary variables,
//- and strip C/C++ comments from the input
//- and strip any embedded C/C++ comments from the input
bool readEntry
(
const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict,
bool mandatory = true,
const bool stripComments = true
bool mandatory = true
);
//- Read/expand optional entry with dictionary variables,
//- and strip any embedded C/C++ comments from the input
inline bool readIfPresent
(
const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict
);
@ -206,7 +208,7 @@ public:
// Write
//- Dictionary entry for expression string, normally suppressing
//- empty strings. Generally used verbatim output (readability)
//- empty strings. Generally uses verbatim output (readability)
// \return true if entry was written
bool writeEntry
(

View File

@ -5,8 +5,8 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Original code Copyright (C) 2012-2018 Bernhard Gschaider
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2012-2018 Bernhard Gschaider
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -33,13 +33,13 @@ License
inline Foam::expressions::exprString::exprString
(
const std::string& s,
bool doValidate
bool doCheck
)
:
string(s)
{
#ifdef FULLDEBUG
if (doValidate)
if (doCheck)
{
(void)valid();
}
@ -50,13 +50,13 @@ inline Foam::expressions::exprString::exprString
inline Foam::expressions::exprString::exprString
(
std::string&& s,
bool doValidate
bool doCheck
)
:
string(std::move(s))
{
#ifdef FULLDEBUG
if (doValidate)
if (doCheck)
{
(void)valid();
}
@ -67,13 +67,13 @@ inline Foam::expressions::exprString::exprString
inline Foam::expressions::exprString::exprString
(
const char* s,
bool doValidate
bool doCheck
)
:
string(s)
{
#ifdef FULLDEBUG
if (doValidate)
if (doCheck)
{
(void)valid();
}
@ -83,27 +83,19 @@ inline Foam::expressions::exprString::exprString
inline Foam::expressions::exprString::exprString
(
const std::string& str,
const word& entryName,
const dictionary& dict,
const bool removeComments
const bool mandatory
)
:
string(str)
{
expand(dict, removeComments);
}
inline Foam::expressions::exprString::exprString
(
std::string&& str,
const dictionary& dict,
const bool removeComments
)
:
string(std::move(str))
{
expand(dict, removeComments);
if (mandatory)
{
readEntry(entryName, dict);
}
else
{
readIfPresent(entryName, dict);
}
}
@ -120,6 +112,64 @@ inline Foam::expressions::exprString::exprString
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr(const std::string& str)
{
exprString expr;
expr.string::operator=(str);
return expr;
}
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr(std::string&& str)
{
exprString expr;
expr.string::operator=(std::move(str));
return expr;
}
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr
(
const std::string& str,
const dictionary& dict,
const bool stripComments
)
{
// Copy - no validate
exprString expr(str, false);
expr.expand(dict, stripComments);
return expr;
}
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr
(
std::string&& str,
const dictionary& dict,
const bool stripComments
)
{
// Move - no validate
exprString expr(std::move(str), false);
expr.expand(dict, stripComments);
return expr;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::expressions::exprString::valid() const
@ -139,25 +189,13 @@ inline bool Foam::expressions::exprString::valid() const
}
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr(const std::string& str)
inline bool Foam::expressions::exprString::readIfPresent
(
const word& key,
const dictionary& dict
)
{
exprString expr;
expr.string::operator=(str);
return expr;
}
inline Foam::expressions::exprString
Foam::expressions::exprString::toExpr(std::string&& str)
{
exprString expr;
expr.string::operator=(std::move(str));
return expr;
return readEntry(key, dict, false); // non-mandatory
}