From 91a1eaa01b8fd2890dbb53dc7cb6cd87f7e7e7b9 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Tue, 6 Feb 2024 15:43:41 +0100 Subject: [PATCH] ENH: support invisible formattingEntry --- .../test/dictionary3/Test-dictionary3.cxx | 14 ++++++-- src/OpenFOAM/db/IOstreams/token/token.H | 2 +- .../formattingEntry/formattingEntry.C | 25 ++++++--------- .../formattingEntry/formattingEntry.H | 32 +++++++++++++++++-- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/applications/test/dictionary3/Test-dictionary3.cxx b/applications/test/dictionary3/Test-dictionary3.cxx index 425fbd16382..8dae613c923 100644 --- a/applications/test/dictionary3/Test-dictionary3.cxx +++ b/applications/test/dictionary3/Test-dictionary3.cxx @@ -64,6 +64,7 @@ int main(int argc, char *argv[]) // Add some more entries { + label idx = 0; dictionary subdict; subdict.add("key", 100); @@ -72,23 +73,30 @@ int main(int argc, char *argv[]) subdict.add ( - new formattingEntry(10, "// comment - without newline.") + new formattingEntry(++idx, "// comment - without newline.") ); subdict.add ( // NB newline must be part of the content! - new formattingEntry(11, "// some comment - with newline?\n") + new formattingEntry(++idx, "// some comment - with newline?\n") ); subdict.add ( // NB newline must be part of the content! - new formattingEntry(12, "/* other comment */\n") + new formattingEntry(++idx, "/* other comment */\n") ); + // Other - invisible + subdict.add(new formattingEntry(++idx, token(123), false)); + + // Other - visible (probably not what anyone wants!) + subdict.add(new formattingEntry(++idx, token(456))); + subdict.add("val", 42); + Info<< "subdict keys:" << flatOutput(subdict.toc()) << nl; dict.add("subdict", std::move(subdict)); } diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index a9b2d9cd15b..07180d10504 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -541,7 +541,7 @@ public: // No character stripping inline explicit token(tokenType typ, const std::string&, label line=0); - //- Copy construct word/string token with the specified variant. + //- Move construct word/string token with the specified variant. // A invalid word/string variant type is silently treated as STRING. // No character stripping inline explicit token(tokenType typ, std::string&&, label line=0); diff --git a/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.C b/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.C index ba45223d962..a3f80dd28f1 100644 --- a/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.C +++ b/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2023 Sergey Lesnik - Copyright (C) 2023 OpenCFD Ltd. + Copyright (C) 2023-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,7 +35,7 @@ namespace Foam { // Write tokens without keyword, suppress/ignore bad tokens. -// Mostly like primitiveEntry::write(os, false); +// Mostly like primitiveEntry::write(os, true); static void writeTokens(Ostream& os, const tokenList& toks) { @@ -56,11 +56,11 @@ static void writeTokens(Ostream& os, const tokenList& toks) started = true; } - // Output token with direct handling in Ostream(s), - // or use normal '<<' output operator + // Token output via direct handling in Ostream(s), + // or normal '<<' output operator if (!os.write(tok)) { - os << tok; + os << tok; } if (tok.isCharData()) @@ -73,18 +73,10 @@ static void writeTokens(Ostream& os, const tokenList& toks) if (s.starts_with("//") && !s.ends_with('\n')) { os << '\n'; - started = false; // already have newline as separator + started = false; // Does not need further space separator } } } - - // Always finish up with a newline? - // eg, - // - // if (started) - // { - // os << nl; - // } } } // End namespace Foam @@ -141,7 +133,10 @@ Foam::formattingEntry::formattingEntry void Foam::formattingEntry::write(Ostream& os) const { - writeTokens(os, *this); + if (active_) + { + writeTokens(os, *this); + } } diff --git a/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.H b/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.H index c672ac08ffc..fb517c258e4 100644 --- a/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.H +++ b/src/OpenFOAM/db/dictionary/formattingEntry/formattingEntry.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2023 Sergey Lesnik - Copyright (C) 2023 OpenCFD Ltd. + Copyright (C) 2023-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,12 +51,17 @@ class formattingEntry : public primitiveEntry { + // Private Data + + //- The output visibility + bool active_ = true; + public: // Static Member Functions - //- Generate a default entry keyword: "__format-entry__NNN" - // The generated names are unlikely to collide with user dictionaries + //- Generate an entry keyword: "__format-entry__NNN". + //- The generated names are unlikely to collide with user dictionaries static keyType defaultName(label n) { return keyType @@ -99,6 +104,12 @@ public: formattingEntry(defaultName(n), std::move(content)) {} + //- Construct with token data, using a generated keyword + formattingEntry(const label n, token&& tok, bool visible=true) + : + primitiveEntry(defaultName(n), std::move(tok)), + active_(visible) + {} //- Clone the entry virtual autoPtr<entry> clone(const dictionary&) const @@ -112,6 +123,21 @@ public: // Member Functions + //- Set output visibility on/off. + // \return the previous value + bool active(bool on) noexcept + { + bool old(active_); + active_ = on; + return old; + } + + //- Get the output visibility + bool active() const noexcept + { + return active_; + } + //- Write content without the keyword. // Special properties: // - ignores any bad tokens on output. -- GitLab