From 411ac5fcfa7ae0763034e31e0b2dc4befb0e11dd Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 23 Apr 2024 12:11:24 +0200 Subject: [PATCH] ENH: adjust return type for token compound factory method - return autoPtr instead of the derived type, otherwise cannot easily construct a token from it ENH: additional typed version of refCompoundToken() - symmetric with typed version of transferCompoundToken() and isCompound() - add ITstream::findCompound() method. Useful for searching within token streams --- applications/test/token/Test-token.cxx | 194 +++++++++++------- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H | 13 +- .../db/IOstreams/Tstreams/ITstreamI.H | 52 +++++ src/OpenFOAM/db/IOstreams/token/token.H | 26 ++- src/OpenFOAM/db/IOstreams/token/tokenI.H | 19 +- .../mappedPatchBaseTemplates.C | 4 +- 6 files changed, 222 insertions(+), 86 deletions(-) create mode 100644 src/OpenFOAM/db/IOstreams/Tstreams/ITstreamI.H diff --git a/applications/test/token/Test-token.cxx b/applications/test/token/Test-token.cxx index 055833fbbb..52b2eb7571 100644 --- a/applications/test/token/Test-token.cxx +++ b/applications/test/token/Test-token.cxx @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2023 OpenCFD Ltd. + Copyright (C) 2017-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,6 +35,9 @@ Description #include "labelList.H" #include "scalarList.H" #include "DynamicList.H" +#include "labelField.H" +#include "scalarField.H" +#include "SubField.H" #include "SpanStream.H" #include "formattingEntry.H" @@ -90,7 +93,7 @@ int main(int argc, char *argv[]) { // This also works, but not actually using the autoPtr directly - autoPtr> ptr + autoPtr ptr ( new token::Compound(identity(10, -9)) ); @@ -131,44 +134,50 @@ int main(int argc, char *argv[]) Info<< "resized: " << ctok1.info() << nl << ctok1 << endl; - const scalarList* listptr = ctok1.compoundToken().isA(); - if (listptr) { - for (scalar& val : const_cast(*listptr)) + // Using isA<> on compoundToken() + const auto* listptr = ctok1.compoundToken().isA(); + if (listptr) { - val *= 5; - } + // sneaky, SubField bypasses const! + scalarField::subField fld(*listptr); + fld *= 5; - Info<< "multiplied List: " - << ctok1.info() << nl << ctok1 << endl; + Info<< "multiplied List: " + << ctok1.info() << nl << ctok1 << endl; + } } - listptr = ctok1.isCompound(); - if (listptr) { - for (scalar& val : const_cast(*listptr)) - { - val /= 2; - } + // Using isCompound<...> - combined check - Info<< "divided List: " - << ctok1.info() << nl << ctok1 << endl; + const auto* listptr = ctok1.isCompound(); + if (listptr) + { + scalarField::subField fld(*listptr); + fld /= 2; + + Info<< "divided List: " + << ctok1.info() << nl << ctok1 << endl; + } } - const labelList* listptr2 = ctok1.isCompound(); - if (listptr2) { - for (label& val : const_cast(*listptr2)) - { - val /= 2; - } + // Using isCompound<...> - combined check - Info<< "divided List