mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: basics for expression string handling
This commit is contained in:
committed by
Andrew Heather
parent
42a9a6ae5a
commit
fae91edd85
62
src/OpenFOAM/expressions/exprString/exprString.C
Normal file
62
src/OpenFOAM/expressions/exprString/exprString.C
Normal file
@ -0,0 +1,62 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
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/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "exprString.H"
|
||||
#include "stringOps.H"
|
||||
#include "expressionEntry.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
Foam::expressions::exprString&
|
||||
Foam::expressions::exprString::expand
|
||||
(
|
||||
const dictionary& dict,
|
||||
const bool removeComments
|
||||
)
|
||||
{
|
||||
if (removeComments)
|
||||
{
|
||||
stringOps::inplaceRemoveComments(*this);
|
||||
}
|
||||
|
||||
// Not quite as efficient as it could be, but wish to have a copy
|
||||
// of the original input for the sake of reporting errors
|
||||
|
||||
if (std::string::npos != find('$'))
|
||||
{
|
||||
(*this) = exprTools::expressionEntry::expand(*this, dict);
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
(void)valid();
|
||||
#endif
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
172
src/OpenFOAM/expressions/exprString/exprString.H
Normal file
172
src/OpenFOAM/expressions/exprString/exprString.H
Normal file
@ -0,0 +1,172 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Original code Copyright (C) 2012-2018 Bernhard Gschaider
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
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/>.
|
||||
|
||||
Namespace
|
||||
Foam::expressions::exprString
|
||||
|
||||
Description
|
||||
A variant of Foam::string with expansion of dictionary variables
|
||||
into a comma-separated form.
|
||||
|
||||
SourceFiles
|
||||
exprString.C
|
||||
exprStringI.H
|
||||
|
||||
SeeAlso
|
||||
Foam::exprTools::expressionEntry
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef expressions_exprString_H
|
||||
#define expressions_exprString_H
|
||||
|
||||
#include "string.H"
|
||||
#include "dictionary.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
namespace expressions
|
||||
{
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class exprString Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
class exprString
|
||||
:
|
||||
public string
|
||||
{
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct null
|
||||
exprString() = default;
|
||||
|
||||
//- Copy construct
|
||||
exprString(const exprString& rhs) = default;
|
||||
|
||||
//- Move construct
|
||||
exprString(exprString&& rhs) = default;
|
||||
|
||||
//- Copy construct from std::string
|
||||
inline explicit exprString(const std::string& s, bool doValidate=true);
|
||||
|
||||
//- Move construct from std::string
|
||||
inline explicit exprString(std::string&& s, bool doValidate=true);
|
||||
|
||||
//- Construct as copy of character array
|
||||
inline explicit exprString(const char* s, bool doValidate=true);
|
||||
|
||||
//- Copy construct and expand with dictionary variables
|
||||
// Optionally strip C/C++ comments from the input
|
||||
inline exprString
|
||||
(
|
||||
const std::string& str,
|
||||
const dictionary& dict,
|
||||
const bool removeComments = false
|
||||
);
|
||||
|
||||
//- Move construct and expand with dictionary variables
|
||||
// Optionally strip C/C++ comments from the input
|
||||
inline exprString
|
||||
(
|
||||
std::string&& str,
|
||||
const dictionary& dict,
|
||||
const bool removeComments = false
|
||||
);
|
||||
|
||||
//- Construct from Istream and expand with dictionary variables
|
||||
// Optionally strip C/C++ comments from the input
|
||||
inline exprString
|
||||
(
|
||||
Istream& is,
|
||||
const dictionary& dict,
|
||||
const bool removeComments = false
|
||||
);
|
||||
|
||||
|
||||
//- Destructor
|
||||
~exprString() = default;
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
//- Inplace expansion with dictionary variables
|
||||
// Optionally strip C/C++ comments from the input
|
||||
exprString& expand
|
||||
(
|
||||
const dictionary& dict,
|
||||
const bool removeComments = false
|
||||
);
|
||||
|
||||
//- Check for unexpanded '$' entries. Fatal if any exist.
|
||||
inline bool valid() const;
|
||||
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Copy assign
|
||||
exprString& operator=(const exprString& str) = default;
|
||||
|
||||
//- Move assign
|
||||
exprString& operator=(exprString&& str) = default;
|
||||
|
||||
//- Copy assign from c-string (no expansions)
|
||||
inline exprString& operator=(const char* str);
|
||||
|
||||
//- Copy assign from string (no expansions)
|
||||
inline exprString& operator=(const std::string& str);
|
||||
|
||||
//- Move assign from string (no expansions)
|
||||
inline exprString& operator=(std::string&& str);
|
||||
};
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
} // End namespace expressions
|
||||
} // End namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#include "exprStringI.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
// ************************************************************************* //
|
||||
205
src/OpenFOAM/expressions/exprString/exprStringI.H
Normal file
205
src/OpenFOAM/expressions/exprString/exprStringI.H
Normal file
@ -0,0 +1,205 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Original code Copyright (C) 2012-2018 Bernhard Gschaider
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
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/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "error.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::expressions::exprString::exprString
|
||||
(
|
||||
const std::string& s,
|
||||
bool doValidate
|
||||
)
|
||||
:
|
||||
string(s)
|
||||
{
|
||||
#ifdef FULLDEBUG
|
||||
if (doValidate)
|
||||
{
|
||||
(void)valid();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString::exprString
|
||||
(
|
||||
std::string&& s,
|
||||
bool doValidate
|
||||
)
|
||||
:
|
||||
string(std::move(s))
|
||||
{
|
||||
#ifdef FULLDEBUG
|
||||
if (doValidate)
|
||||
{
|
||||
(void)valid();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString::exprString
|
||||
(
|
||||
const char* s,
|
||||
bool doValidate
|
||||
)
|
||||
:
|
||||
string(s)
|
||||
{
|
||||
#ifdef FULLDEBUG
|
||||
if (doValidate)
|
||||
{
|
||||
(void)valid();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString::exprString
|
||||
(
|
||||
const std::string& str,
|
||||
const dictionary& dict,
|
||||
const bool removeComments
|
||||
)
|
||||
:
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString::exprString
|
||||
(
|
||||
Istream& is,
|
||||
const dictionary& dict,
|
||||
const bool removeComments
|
||||
)
|
||||
:
|
||||
string(is)
|
||||
{
|
||||
expand(dict, removeComments);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::expressions::exprString::valid() const
|
||||
{
|
||||
const bool ok = (std::string::npos == find('$'));
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
if (!ok)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Unexpanded '$' in " << *this << nl
|
||||
<< exit(FatalError);
|
||||
}
|
||||
#endif
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::expressions::exprString&
|
||||
Foam::expressions::exprString::operator=(const char* str)
|
||||
{
|
||||
string::operator=(str);
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
(void)valid();
|
||||
#endif
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString&
|
||||
Foam::expressions::exprString::operator=(const std::string& str)
|
||||
{
|
||||
string::operator=(str);
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
(void)valid();
|
||||
#endif
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::expressions::exprString&
|
||||
Foam::expressions::exprString::operator=(std::string&& str)
|
||||
{
|
||||
string::operator=(std::move(str));
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
(void)valid();
|
||||
#endif
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user