From 1c354ce2991a14d0e2b96c1c5dcdcf30b42ed0e4 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 1 Nov 2021 14:04:27 +0100 Subject: [PATCH] ENH: expose dictionary relativeName() method STYLE: more consistent noexcept for dictionary and entry --- src/OpenFOAM/db/dictionary/dictionary.C | 25 +++------- src/OpenFOAM/db/dictionary/dictionary.H | 48 +++++++++++-------- .../dictionaryEntry/dictionaryEntry.C | 10 ++-- .../dictionaryEntry/dictionaryEntry.H | 14 +++--- .../db/dictionary/dictionaryTemplates.C | 4 +- src/OpenFOAM/db/dictionary/entry/entry.H | 8 ++-- .../primitiveEntry/primitiveEntry.H | 2 +- 7 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 75c74cc909..9ef4d995d6 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2015-2019 OpenCFD Ltd. + Copyright (C) 2015-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -59,23 +59,6 @@ registerInfoSwitch ); -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - -Foam::fileName Foam::dictionary::relativeName(const bool caseTag) const -{ - const fileName caseDir(argList::envGlobalPath()); - - if (!caseDir.empty() && name().isAbsolute()) - { - return name().relative(caseDir, caseTag); - } - else - { - return name(); - } -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::dictionary::dictionary() @@ -189,6 +172,12 @@ Foam::dictionary::~dictionary() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::fileName Foam::dictionary::relativeName(const bool caseTag) const +{ + return argList::envRelativePath(name(), caseTag); +} + + const Foam::dictionary& Foam::dictionary::topDict() const { const dictionary& p = parent(); diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 724a9b8efb..bb4d914dc2 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -171,7 +171,8 @@ public: pointer eptr_; - //- Construct for the given dictionary context + //- Construct for the given dictionary context. + // Allow implicit conversion Searcher(dict_pointer dict) : dict_(dict), @@ -196,62 +197,62 @@ public: //- True if entry was found - inline bool good() const + bool good() const noexcept { return eptr_; } //- True if entry was found - inline bool found() const + bool found() const noexcept { return eptr_; } //- The containing dictionary context - inline dict_reference context() const + dict_reference context() const { return *dict_; } //- A pointer to the entry (nullptr if not found) - inline pointer ptr() const + pointer ptr() const noexcept { return eptr_; } //- A reference to the entry (Error if not found) - inline reference ref() const + reference ref() const { return *eptr_; } //- True if found entry is a dictionary. - inline bool isDict() const + bool isDict() const noexcept { - return eptr_ && eptr_->isDict(); + return (eptr_ && eptr_->dictPtr()); } - //- Pointer to the found entry as a dictionary or nullptr otherwise. - inline dict_pointer dictPtr() const + //- Pointer to the found entry as a dictionary, nullptr otherwise + dict_pointer dictPtr() const noexcept { - return eptr_ && eptr_->isDict() ? eptr_->dictPtr() : nullptr; + return eptr_ ? eptr_->dictPtr() : nullptr; } //- Reference the found entry as a dictionary. // (Error if not found, or not a dictionary). - inline dict_reference dict() const + dict_reference dict() const { return eptr_->dict(); } //- Permit an explicit cast to the other (const/non-const) searcher - inline explicit operator const Searcher&() const + explicit operator const Searcher&() const { return *reinterpret_cast*>(this); } //- A pointer to the entry (nullptr if not found) - pointer operator->() const + pointer operator->() const noexcept { return eptr_; } @@ -359,9 +360,6 @@ private: //- Emit IOError about bad input for the entry void raiseBadInput(const ITstream& is, const word& keyword) const; - //- The case-relative dictionary name. Uses FOAM_CASE - fileName relativeName(const bool caseTag=false) const; - //- Report (on stderr) that the keyword default value was used template void reportDefault @@ -379,7 +377,7 @@ public: //- Report optional keywords and values if not present in dictionary // For value greater than 1: fatal. - // Set/unset via an InfoSwitch + // Set/unset via an InfoSwitch or -info-switch at the command-line static int writeOptionalEntries; //- An empty dictionary, which is also the parent for all dictionaries @@ -445,13 +443,13 @@ public: // Access //- The dictionary name - const fileName& name() const + const fileName& name() const noexcept { return name_; } //- The dictionary name for modification (use with caution). - fileName& name() + fileName& name() noexcept { return name_; } @@ -470,8 +468,16 @@ public: return scopedName.substr(i+1); } + //- The dictionary name relative to the case. + // Uses argList::envRelativePath to obtain FOAM_CASE + // + // \param caseTag replace globalPath with \ for later + // use with expand(), or prefix \ if the file name was + // not an absolute location + fileName relativeName(const bool caseTag = false) const; + //- Return the parent dictionary - const dictionary& parent() const + const dictionary& parent() const noexcept { return parent_; } diff --git a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C index 797d735698..f8ca742897 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C +++ b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -87,25 +87,25 @@ Foam::ITstream& Foam::dictionaryEntry::stream() const } -const Foam::dictionary* Foam::dictionaryEntry::dictPtr() const +const Foam::dictionary* Foam::dictionaryEntry::dictPtr() const noexcept { return this; } -Foam::dictionary* Foam::dictionaryEntry::dictPtr() +Foam::dictionary* Foam::dictionaryEntry::dictPtr() noexcept { return this; } -const Foam::dictionary& Foam::dictionaryEntry::dict() const +const Foam::dictionary& Foam::dictionaryEntry::dict() const noexcept { return *this; } -Foam::dictionary& Foam::dictionaryEntry::dict() +Foam::dictionary& Foam::dictionaryEntry::dict() noexcept { return *this; } diff --git a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.H b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.H index 080a45915c..21ed4be264 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.H +++ b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntry.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -137,16 +137,16 @@ public: //- Return pointer to this dictionary - virtual const dictionary* dictPtr() const; + virtual const dictionary* dictPtr() const noexcept; //- Return non-const pointer to this dictionary - virtual dictionary* dictPtr(); + virtual dictionary* dictPtr() noexcept; - //- Return dictionary - virtual const dictionary& dict() const; + //- Return dictionary (ie, this) + virtual const dictionary& dict() const noexcept; //- Return non-const access to dictionary - virtual dictionary& dict(); + virtual dictionary& dict() noexcept; //- Write @@ -160,7 +160,7 @@ public: } - // Ostream operator + // Ostream Operator friend Ostream& operator<<(Ostream& os, const dictionaryEntry& e); }; diff --git a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C index 4d5f38bc17..042c79cd10 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C +++ b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,7 +40,7 @@ void Foam::dictionary::reportDefault ) const { InfoErr - << "Dictionary: " << relativeName(true).c_str() + << "Dictionary: " << this->relativeName().c_str() << " Entry: " << keyword; if (added) diff --git a/src/OpenFOAM/db/dictionary/entry/entry.H b/src/OpenFOAM/db/dictionary/entry/entry.H index b9176518d5..a180e93c23 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.H +++ b/src/OpenFOAM/db/dictionary/entry/entry.H @@ -210,7 +210,7 @@ public: //- Return true if this entry is a stream - virtual bool isStream() const + virtual bool isStream() const noexcept { return false; } @@ -220,21 +220,21 @@ public: //- Return true if this entry is a dictionary - virtual bool isDict() const + virtual bool isDict() const noexcept { return this->dictPtr(); } //- Return pointer to dictionary, if entry is a dictionary. // Return nullptr if the entry is not a dictionary. - virtual const dictionary* dictPtr() const + virtual const dictionary* dictPtr() const noexcept { return nullptr; } //- Return non-const pointer to dictionary, if entry is a dictionary // Return nullptr if the entry is not a dictionary. - virtual dictionary* dictPtr() + virtual dictionary* dictPtr() noexcept { return nullptr; } diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H index 1cc1428578..f05a9b19c6 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H @@ -162,7 +162,7 @@ public: virtual label endLineNumber() const; //- Return true - this entry is a stream - virtual bool isStream() const + virtual bool isStream() const noexcept { return true; }