mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: improve/simplify streaming of exprValue
- ensure that operator<< and operator>> behave symmetrically
This commit is contained in:
3
applications/test/exprValue1/Make/files
Normal file
3
applications/test/exprValue1/Make/files
Normal file
@ -0,0 +1,3 @@
|
||||
Test-exprValue1.cxx
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/Test-exprValue1
|
||||
2
applications/test/exprValue1/Make/options
Normal file
2
applications/test/exprValue1/Make/options
Normal file
@ -0,0 +1,2 @@
|
||||
/* EXE_INC = */
|
||||
/* EXE_LIBS = */
|
||||
194
applications/test/exprValue1/Test-exprValue1.cxx
Normal file
194
applications/test/exprValue1/Test-exprValue1.cxx
Normal file
@ -0,0 +1,194 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / 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-exprValue1
|
||||
|
||||
Description
|
||||
Test low-level polymorphic value container (exprValue)
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "argList.H"
|
||||
#include "IOstreams.H"
|
||||
#include "ITstream.H"
|
||||
#include "exprValue.H"
|
||||
#include "Pstream.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
void printInfo(const expressions::exprValue& val)
|
||||
{
|
||||
Pout<< "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<< "OK" << nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "NOK" << nl;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
argList::noBanner();
|
||||
argList::noCheckProcessorDirectories();
|
||||
|
||||
#include "setRootCase.H"
|
||||
|
||||
// Aborts
|
||||
// expressions::exprValue value(std::string(""));
|
||||
|
||||
// Regular broadcast doesn't work
|
||||
Info<< "exprValue"
|
||||
<< " sizeof:" << sizeof(expressions::exprValue)
|
||||
<< " contiguous:" << is_contiguous<expressions::exprValue>::value
|
||||
<< nl << nl;
|
||||
|
||||
{
|
||||
expressions::exprValue value;
|
||||
|
||||
Info<< "exprValue"
|
||||
<< " sizeof:" << value.size_bytes()
|
||||
<< nl << nl;
|
||||
|
||||
// Info<< "value: " << value << nl;
|
||||
|
||||
// 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);
|
||||
|
||||
if (UPstream::parRun())
|
||||
{
|
||||
Info<< "Before broadcast" << nl;
|
||||
}
|
||||
|
||||
if (UPstream::master())
|
||||
{
|
||||
value = 100 * vector(1,0,0);
|
||||
}
|
||||
printInfo(value);
|
||||
|
||||
expressions::exprValue oldValue(value);
|
||||
|
||||
|
||||
if (true)
|
||||
{
|
||||
// Broadcast with serialization
|
||||
Pstream::broadcast(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Broadcast manually
|
||||
UPstream::broadcast
|
||||
(
|
||||
value.data_bytes(),
|
||||
value.size_bytes(),
|
||||
UPstream::worldComm
|
||||
);
|
||||
}
|
||||
|
||||
Pout<< "same values: " << (oldValue == value) << nl;
|
||||
|
||||
// Can put them in a Map and write, but reading will not work...
|
||||
Map<expressions::exprValue> map;
|
||||
map(1) = value;
|
||||
Info<< "map: " << map << nl;
|
||||
|
||||
if (UPstream::parRun())
|
||||
{
|
||||
Info<< "After broadcast" << nl;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user