From c378893bcb64c803be5d5b5859cf910ecb5f92ac Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Sep 2025 14:54:06 +0200 Subject: [PATCH] ENH: simplify direct creation of dictionary primitive entries - can now create an empty entry and add/overwrite with tokens afterwards, or copy/move construct from a list of tokens. ENH: provided named token setter methods (disambiguates bool/char/label) COMP: use 'if constexpr' for FlatOutput DOC: more description for Field --- .../test/HashPtrTable/Test-HashPtrTable.cxx | 54 +++++--- applications/test/ISLList/Make/files | 2 +- .../{Test-ISLList.C => Test-ISLList.cxx} | 0 applications/test/PtrDictionary1/Make/files | 2 +- ...rDictionary1.C => Test-PtrDictionary1.cxx} | 8 +- applications/test/PtrList/Test-PtrList.cxx | 126 +++++++++++++++--- .../test/PtrListDictionary/Make/files | 2 +- ...ictionary.C => Test-PtrListDictionary.cxx} | 12 +- applications/test/dictionary/Make/files | 2 +- ...{Test-dictionary.C => Test-dictionary.cxx} | 0 applications/test/dictionary2/Make/files | 2 +- ...est-dictionary2.C => Test-dictionary2.cxx} | 45 +++++-- applications/test/dictionaryCopy/Make/files | 2 +- ...ctionaryCopy.C => Test-dictionaryCopy.cxx} | 0 src/OpenFOAM/db/IOstreams/output/FlatOutput.H | 32 ++--- src/OpenFOAM/db/IOstreams/token/token.H | 9 ++ src/OpenFOAM/db/IOstreams/token/tokenI.H | 32 ++++- src/OpenFOAM/db/dictionary/dictionary.C | 20 ++- src/OpenFOAM/db/dictionary/dictionary.H | 15 +++ src/OpenFOAM/fields/Fields/Field/Field.H | 8 +- 20 files changed, 284 insertions(+), 89 deletions(-) rename applications/test/ISLList/{Test-ISLList.C => Test-ISLList.cxx} (100%) rename applications/test/PtrDictionary1/{Test-PtrDictionary1.C => Test-PtrDictionary1.cxx} (97%) rename applications/test/PtrListDictionary/{Test-PtrListDictionary.C => Test-PtrListDictionary.cxx} (92%) rename applications/test/dictionary/{Test-dictionary.C => Test-dictionary.cxx} (100%) rename applications/test/dictionary2/{Test-dictionary2.C => Test-dictionary2.cxx} (90%) rename applications/test/dictionaryCopy/{Test-dictionaryCopy.C => Test-dictionaryCopy.cxx} (100%) diff --git a/applications/test/HashPtrTable/Test-HashPtrTable.cxx b/applications/test/HashPtrTable/Test-HashPtrTable.cxx index cd4b81184f..c2bb47012e 100644 --- a/applications/test/HashPtrTable/Test-HashPtrTable.cxx +++ b/applications/test/HashPtrTable/Test-HashPtrTable.cxx @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,33 +42,55 @@ Description using namespace Foam; -class Scalar +bool verbosity = true; + +// Gratuitous class inheritance +template +class BoxedType { - scalar data_; + T data_; public: static bool verbose; - constexpr Scalar() noexcept : data_(0) {} - Scalar(scalar val) noexcept : data_(val) {} + constexpr BoxedType() noexcept : data_(0) {} + BoxedType(T val) noexcept : data_(val) {} + + ~BoxedType() + { + if (verbosity) Info<< " [delete BoxedType: " << value() << ']' << nl; + } + + T value() const noexcept { return data_; } + T& value() noexcept { return data_; } + + auto clone() const { return autoPtr>::New(*this); } +}; + +template Ostream& operator<<(Ostream& os, const BoxedType& item) +{ + return (os << " -> " << item.value()); +} + + +class Scalar : public BoxedType +{ +public: + + using BoxedType::BoxedType; ~Scalar() { - if (verbose) Info<< "delete Scalar: " << data_ << endl; - } - - const scalar& value() const noexcept { return data_; } - scalar& value() noexcept { return data_; } - - friend Ostream& operator<<(Ostream& os, const Scalar& item) - { - os << item.value(); - return os; + if (verbosity) Info<< "delete Scalar: " << value() << nl; } + auto clone() const { return autoPtr::New(*this); } }; -bool Scalar::verbose = true; +Ostream& operator<<(Ostream& os, const Scalar& item) +{ + return (os << item.value()); +} template diff --git a/applications/test/ISLList/Make/files b/applications/test/ISLList/Make/files index ceb5fc148f..26f3e12d5f 100644 --- a/applications/test/ISLList/Make/files +++ b/applications/test/ISLList/Make/files @@ -1,3 +1,3 @@ -Test-ISLList.C +Test-ISLList.cxx EXE = $(FOAM_USER_APPBIN)/Test-ISLList diff --git a/applications/test/ISLList/Test-ISLList.C b/applications/test/ISLList/Test-ISLList.cxx similarity index 100% rename from applications/test/ISLList/Test-ISLList.C rename to applications/test/ISLList/Test-ISLList.cxx diff --git a/applications/test/PtrDictionary1/Make/files b/applications/test/PtrDictionary1/Make/files index 9ced445619..c21cc1053a 100644 --- a/applications/test/PtrDictionary1/Make/files +++ b/applications/test/PtrDictionary1/Make/files @@ -1,3 +1,3 @@ -Test-PtrDictionary1.C +Test-PtrDictionary1.cxx EXE = $(FOAM_USER_APPBIN)/Test-PtrDictionary1 diff --git a/applications/test/PtrDictionary1/Test-PtrDictionary1.C b/applications/test/PtrDictionary1/Test-PtrDictionary1.cxx similarity index 97% rename from applications/test/PtrDictionary1/Test-PtrDictionary1.C rename to applications/test/PtrDictionary1/Test-PtrDictionary1.cxx index 3f407568bc..c9bedb58e9 100644 --- a/applications/test/PtrDictionary1/Test-PtrDictionary1.C +++ b/applications/test/PtrDictionary1/Test-PtrDictionary1.cxx @@ -42,6 +42,8 @@ Description using namespace Foam; +bool verbosity = true; + class ent : public Dictionary::link @@ -73,14 +75,12 @@ class Scalar public: - static bool verbose; - constexpr Scalar() noexcept : data_(0) {} Scalar(scalar val) noexcept : data_(val) {} ~Scalar() { - if (verbose) Info<< "delete Scalar: " << data_ << endl; + if (verbosity) Info<< "delete Scalar: " << data_ << endl; } scalar value() const noexcept { return data_; } @@ -93,8 +93,6 @@ public: } }; -bool Scalar::verbose = true; - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: diff --git a/applications/test/PtrList/Test-PtrList.cxx b/applications/test/PtrList/Test-PtrList.cxx index 88c8cfbffe..17f72460c7 100644 --- a/applications/test/PtrList/Test-PtrList.cxx +++ b/applications/test/PtrList/Test-PtrList.cxx @@ -41,35 +41,85 @@ Description using namespace Foam; -class Scalar +bool verbosity = true; + +// Gratuitous class inheritance +template +class BoxedType { - scalar data_; + T data_; public: static bool verbose; - constexpr Scalar() noexcept : data_(0) {} - Scalar(scalar val) noexcept : data_(val) {} + constexpr BoxedType() noexcept : data_(0) {} + BoxedType(T val) noexcept : data_(val) {} + + ~BoxedType() + { + if (verbosity) Info<< " [delete BoxedType: " << value() << ']' << nl; + } + + T value() const noexcept { return data_; } + T& value() noexcept { return data_; } + + auto clone() const { return autoPtr>::New(*this); } +}; + +template Ostream& operator<<(Ostream& os, const BoxedType& item) +{ + return (os << " -> " << item.value()); +} + + +class Scalar : public BoxedType +{ +public: + + using BoxedType::BoxedType; ~Scalar() { - if (verbose) Info<< "delete Scalar: " << data_ << endl; - } - - scalar value() const noexcept { return data_; } - scalar& value() noexcept { return data_; } - - autoPtr clone() const { return autoPtr::New(data_); } - - friend Ostream& operator<<(Ostream& os, const Scalar& item) - { - os << item.value(); - return os; + if (verbosity) Info<< "delete Scalar: " << value() << nl; } + auto clone() const { return autoPtr::New(*this); } }; -bool Scalar::verbose = true; +Ostream& operator<<(Ostream& os, const Scalar& item) +{ + return (os << item.value()); +} + + +class Integer : public BoxedType