From c90a167e5aa9f486e7d14d5603141ddb9c7572ae Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 12 Dec 2008 19:25:48 +0100 Subject: [PATCH] bugfixing, documentation in global/debug - catch missing "controlDict" file immediately via findEtcFile() instead of checking IFstream::good(). This avoids segfaulting, since the error handling in IFstream uses classes that are not yet initialized. - use new dictionary features (eg, lookupOrAddDefault) to reduce code - fixed debug::switchSet to use ref-to-pointer so pointer actually gets set! --- src/OpenFOAM/global/debug/debug.C | 158 ++++++++++++------------------ src/OpenFOAM/global/debug/debug.H | 44 ++++----- 2 files changed, 85 insertions(+), 117 deletions(-) diff --git a/src/OpenFOAM/global/debug/debug.C b/src/OpenFOAM/global/debug/debug.C index 82ec7d7eaf..3a3274a1e2 100644 --- a/src/OpenFOAM/global/debug/debug.C +++ b/src/OpenFOAM/global/debug/debug.C @@ -36,19 +36,16 @@ Description namespace Foam { - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - namespace debug { +//! @cond ignoreDocumentation - local scope dictionary* controlDictPtr_(NULL); dictionary* debugSwitchesPtr_(NULL); dictionary* infoSwitchesPtr_(NULL); dictionary* optimisationSwitchesPtr_(NULL); -//- Class to ensure controlDictPtr_ is deleted at the end of the run -// @cond ignore documentation for this class +// to ensure controlDictPtr_ is deleted at the end of the run class deleteControlDictPtr { public: @@ -61,135 +58,110 @@ public: if (controlDictPtr_) { delete controlDictPtr_; + controlDictPtr_ = 0; } } }; -//! @endcond deleteControlDictPtr deleteControlDictPtr_; +//! @endcond ignoreDocumentation -dictionary& switchSet(const char* switchSetName, dictionary* switchSetPtr) -{ - if (!switchSetPtr) - { - if (!controlDict().found(switchSetName)) - { - cerr<< "debug::switchSet(const char*, dictionary*): " << std::endl - << " Cannot find " << switchSetName - << " in dictionary " << controlDictPtr_->name().c_str() - << std::endl << std::endl; - - ::exit(1); - } - - switchSetPtr = - const_cast(&(controlDict().subDict(switchSetName))); - } - - return *switchSetPtr; -} - - -int debugSwitch -( - dictionary& switchSet, - const char* switchName, - const int defaultValue -) -{ - if (switchSet.found(switchName)) - { - return readInt(switchSet.lookup(switchName)); - } - else - { - switchSet.add(switchName, defaultValue); - return defaultValue; - } -} - -} - +} // End namespace debug +} // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -dictionary& debug::controlDict() +Foam::dictionary& Foam::debug::controlDict() { if (!controlDictPtr_) { - fileName controlDictFileName(dotFoam("controlDict")); - - IFstream dictFile(controlDictFileName); - - if (!dictFile.good()) - { - cerr<< "debug::controlDict(): " - << "Cannot open essential file " << controlDictFileName.c_str() - << std::endl << std::endl; - ::exit(1); - } - - controlDictPtr_ = new dictionary(dictFile); + controlDictPtr_ = new dictionary + ( + IFstream(findEtcFile("controlDict", true))() + ); } return *controlDictPtr_; } -dictionary& debug::debugSwitches() +Foam::dictionary& Foam::debug::switchSet +( + const char* subDictName, + dictionary*& subDictPtr +) +{ + if (!subDictPtr) + { + entry* ePtr = controlDict().lookupEntryPtr + ( + subDictName, false, false + ); + + if (!ePtr || !ePtr->isDict()) + { + cerr<< "debug::switchSet(const char*, dictionary*&):\n" + << " Cannot find " << subDictName << " in dictionary " + << controlDict().name().c_str() + << std::endl << std::endl; + + ::exit(1); + } + + subDictPtr = &ePtr->dict(); + } + + return *subDictPtr; +} + + +Foam::dictionary& Foam::debug::debugSwitches() { return switchSet("DebugSwitches", debugSwitchesPtr_); } -int debug::debugSwitch(const char* switchName, const int defaultValue) -{ - return debugSwitch - ( - debugSwitches(), - switchName, - defaultValue - ); -} - - -dictionary& debug::infoSwitches() +Foam::dictionary& Foam::debug::infoSwitches() { return switchSet("InfoSwitches", infoSwitchesPtr_); } -int debug::infoSwitch(const char* switchName, const int defaultValue) -{ - return debugSwitch - ( - infoSwitches(), - switchName, - defaultValue - ); -} - - -dictionary& debug::optimisationSwitches() +Foam::dictionary& Foam::debug::optimisationSwitches() { return switchSet("OptimisationSwitches", optimisationSwitchesPtr_); } -int debug::optimisationSwitch(const char* switchName, const int defaultValue) +int Foam::debug::debugSwitch(const char* name, const int defaultValue) { - return debugSwitch + return debugSwitches().lookupOrAddDefault ( - optimisationSwitches(), - switchName, - defaultValue + name, defaultValue, false, false + ); +} + + +int Foam::debug::infoSwitch(const char* name, const int defaultValue) +{ + return infoSwitches().lookupOrAddDefault + ( + name, defaultValue, false, false + ); +} + + +int Foam::debug::optimisationSwitch(const char* name, const int defaultValue) +{ + return optimisationSwitches().lookupOrAddDefault + ( + name, defaultValue, false, false ); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam // ************************************************************************* // diff --git a/src/OpenFOAM/global/debug/debug.H b/src/OpenFOAM/global/debug/debug.H index 8787f3397d..9431a4f78b 100644 --- a/src/OpenFOAM/global/debug/debug.H +++ b/src/OpenFOAM/global/debug/debug.H @@ -41,44 +41,40 @@ SourceFiles namespace Foam { +// Forward declaration of classes class dictionary; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace debug { + //- The central control dictionary. + // Located in ~/.OpenFOAM/VERSION or $WM_PROJECT_DIR/etc + // @sa Foam::findEtcFile() dictionary& controlDict(); - - dictionary& switchSet(const char* switchSetName, dictionary* switchSetPtr); - - + //- The DebugSwitches sub-dictionary in the central controlDict. dictionary& debugSwitches(); - int debugSwitch - ( - const char* switchName, - const int defaultValue = 0 - ); - - + //- The InfoSwitches sub-dictionary in the central controlDict. dictionary& infoSwitches(); - int infoSwitch - ( - const char* switchName, - const int defaultValue = 0 - ); - - + //- The OptimisationSwitches sub-dictionary in the central controlDict. dictionary& optimisationSwitches(); - int optimisationSwitch - ( - const char* switchName, - const int defaultValue = 0 - ); -} + //- Lookup debug switch or add default value. + int debugSwitch(const char* name, const int defaultValue=0); + + //- Lookup info switch or add default value. + int infoSwitch(const char* name, const int defaultValue=0); + + //- Lookup optimisation switch or add default value. + int optimisationSwitch(const char* name, const int defaultValue=0); + + //- Internal function to lookup a sub-dictionary from controlDict. + dictionary& switchSet(const char* subDictName, dictionary*& subDictPtr); + +} // End namespace debug // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //