ENH: add low-level polymorphic/boxed exprValue

- primarily for handling expression results,
  but can also be used as a universal value holder.

  Has some characteristics suitable for type-less IO:
  eg, is_integral(), nComponents()

ENH: add is_pointer() check for expression scanToken
This commit is contained in:
Mark Olesen
2023-06-30 18:43:41 +02:00
parent fc2760ab9c
commit 5635e14f81
13 changed files with 1243 additions and 90 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -17,8 +17,9 @@ Description
#include "IOstreams.H"
#include "ITstream.H"
#include "exprTraits.H"
#include "uLabel.H"
#include "exprTraits.H"
#include "error.H"
#include "stringList.H"
#include "exprScanToken.H"
@ -27,16 +28,18 @@ using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class T>
template<class Type>
void printTraits()
{
const auto typeCode = exprTypeTraits<T>::value;
const auto typeCode = exprTypeTraits<Type>::value;
Info<< "type " << pTraits<T>::typeName
<< " code:" << int(typeCode)
<< " name:" << exprTypeTraits<T>::name;
Info<< "Type '" << pTraits<Type>::typeName
<< "' = code:" << int(typeCode)
<< " rank:" << exprTypeTraits<Type>::rank
<< " cmpt:" << exprTypeTraits<Type>::nComponents
<< " name:" << exprTypeTraits<Type>::name;
if (pTraits<T>::typeName != word(exprTypeTraits<T>::name))
if (pTraits<Type>::typeName != word(exprTypeTraits<Type>::name))
{
Info<< " (UNSUPPORTED)";
}
@ -45,6 +48,17 @@ void printTraits()
}
void print(const expressions::scanToken& tok)
{
Info<< " type:" << int(tok.type_);
if (tok.is_pointer())
{
Info<< " ptr:" << Foam::name(tok.name_);
}
Info<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main()
@ -56,6 +70,7 @@ int main()
printTraits<bool>();
printTraits<label>();
printTraits<scalar>();
printTraits<complex>();
printTraits<vector>();
printTraits<tensor>();
printTraits<symmTensor>();
@ -71,33 +86,27 @@ int main()
Info<< "Name of typeCode: "
<< getName(expressions::valueTypeCode::type_bool) << nl;
{
expressions::scanToken tok;
expressions::scanToken tok2;
expressions::scanToken tok(expressions::scanToken::null());
expressions::scanToken tok2(expressions::scanToken::null());
Info<< nl << "sizeof(scanToken): "
<< sizeof(tok) << nl;
Info<< " type:" << int(tok.type_) << nl;
Info<< " ptr:" << Foam::name(tok.name_) << nl;
Info<< " type:" << int(tok2.type_) << nl;
Info<< " ptr:" << Foam::name(tok2.name_) << nl;
print(tok);
print(tok2);
tok.setWord("hello");
Info<< " type:" << int(tok.type_) << nl;
Info<< " ptr:" << Foam::name(tok.name_) << nl;
print(tok);
tok2 = tok;
Info<< " type:" << int(tok2.type_) << nl;
Info<< " ptr:" << Foam::name(tok2.name_) << nl;
print(tok2);
tok2.destroy();
Info<< " type:" << int(tok2.type_) << nl;
Info<< " ptr:" << Foam::name(tok2.name_) << nl;
print(tok); // Not a leak, but old rubbish
print(tok2);
}
Info<< nl << "Done" << nl;

View File

@ -0,0 +1,3 @@
Test-exprValue.C
EXE = $(FOAM_USER_APPBIN)/Test-exprValue

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,141 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-exprValue
Description
Test low-level polymorphic value container (exprValue)
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "ITstream.H"
#include "exprValue.H"
using namespace Foam;
void printInfo(const expressions::exprValue& val)
{
Info<< "Boxed type:" << int(val.typeCode())
<< " (" << val.valueTypeName() << ") good:"
<< val.good() << " => " << val << nl;
}
expressions::exprValue tryParse(const std::string& str)
{
expressions::exprValue val, val2;
ITstream is(str);
const bool ok = val.read(is);
Info<< "read " << Foam::name(val.typeCode()) << " from " << str;
if (ok)
{
Info<< " trailing tokens:" << is.nRemainingTokens() << nl
<< "value: " << val << nl;
}
else
{
Info<< " FAILED" << nl;
}
if (ok)
{
Info<< "Direct from string: ";
if (expressions::exprValue::read(str, val2))
{
Info<< "good" << nl;
}
else
{
Info<< "bad" << nl;
}
}
return val;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
#include "setRootCase.H"
// Aborts
// expressions::exprValue value(std::string(""));
{
expressions::exprValue value;
// Nothing
printInfo(value);
value.set(scalar(100));
printInfo(value);
value.set(vector(1,2,3));
printInfo(value);
value = vector(4,5,6);
printInfo(value);
value = Zero;
printInfo(value);
value.clear();
printInfo(value);
value = 100 * vector(1,0,0);
printInfo(value);
}
{
Info<< nl << "Test parsing" << nl << nl;
for
(
const auto& input :
stringList
({
"()", // bad
"( 1 2 ", // also bad
"( ", // really bad
"(1 16 12)",
"(1 bad)",
"(5)",
"1.2345",
"5.678 trailing",
"true",
"false",
" 1 ",
" yes no "
})
)
{
(void) tryParse(input);
}
}
return 0;
}
// ************************************************************************* //