diff --git a/applications/test/dictionary3/Test-dictionary3.cxx b/applications/test/dictionary3/Test-dictionary3.cxx index 425fbd1638282190642e1af756a4414be216b383..8dae613c9238c99937d71b767dace672b1145924 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 a9b2d9cd15ba4af4f34952b3ee9c48320d880776..07180d105042dd46a9e138bdf4e564595d13fcc2 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 ba45223d962ce18ec6a31b507a5675d20c1b7c04..a3f80dd28f1df126a897cdfb0b126da47465018b 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 c672ac08ffc17463139c1dacc163d78439673cdf..fb517c258e419f87655b828a8aef59c62b7fdc2a 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.