From 7ac55ac74ac2433416e00e966d9eb70f5d67d9c4 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 3 Jul 2017 13:24:08 +0200 Subject: [PATCH] ENH: consolidate arch and version strings (issue #513) - make some IOobject writing routines non-templated. - relocate more IOobject methods into the IOobjectI.H (generally cleanup the main header). --- .../graphics/ensightFoamReader/libuserd.C | 1 + src/OpenFOAM/db/IOobject/IOobject.C | 14 +- src/OpenFOAM/db/IOobject/IOobject.H | 286 ++++++++---------- src/OpenFOAM/db/IOobject/IOobjectI.H | 192 +++++++----- src/OpenFOAM/db/IOobject/IOobjectTemplates.C | 5 +- .../db/IOobject/IOobjectWriteHeader.C | 104 +++++-- src/OpenFOAM/global/argList/argList.C | 30 +- src/OpenFOAM/global/foamVersion.H | 14 +- src/OpenFOAM/global/global.Cver | 20 ++ 9 files changed, 369 insertions(+), 297 deletions(-) diff --git a/applications/utilities/postProcessing/graphics/ensightFoamReader/libuserd.C b/applications/utilities/postProcessing/graphics/ensightFoamReader/libuserd.C index 88bf926c42..4df3379e3c 100644 --- a/applications/utilities/postProcessing/graphics/ensightFoamReader/libuserd.C +++ b/applications/utilities/postProcessing/graphics/ensightFoamReader/libuserd.C @@ -45,6 +45,7 @@ Description #include "fvMesh.H" #include "cellModeller.H" #include "globalFoam.H" +#include "foamVersion.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C index 5953185baa..f61465bcb3 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.C +++ b/src/OpenFOAM/db/IOobject/IOobject.C @@ -351,6 +351,12 @@ const Foam::Time& Foam::IOobject::time() const } +const Foam::fileName& Foam::IOobject::rootPath() const +{ + return time().rootPath(); +} + + const Foam::fileName& Foam::IOobject::caseName() const { return time().caseName(); @@ -387,12 +393,6 @@ Foam::word Foam::IOobject::member() const } -const Foam::fileName& Foam::IOobject::rootPath() const -{ - return time().rootPath(); -} - - Foam::fileName Foam::IOobject::path() const { if (instance().isAbsolute()) @@ -617,6 +617,8 @@ void Foam::IOobject::setBad(const string& s) } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + void Foam::IOobject::operator=(const IOobject& io) { name_ = io.name_; diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index b08b3bb661..a0502fb1df 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -191,6 +191,12 @@ public: TypeName("IOobject"); + // Static data members + + //- Type of file modification checking + static fileCheckTypes fileModificationChecking; + + // Static Member Functions //- Split path into instance, local, name components @@ -202,11 +208,9 @@ public: word& name ); - template - static inline word groupName(Name name, const word& group); - - //- Type of file modification checking - static fileCheckTypes fileModificationChecking; + //- Create dot-delimited name.group + template + static inline word groupName(StringType name, const word& group); // Constructors @@ -280,214 +284,162 @@ public: // Member Functions - // General access + // General access - //- Return time - const Time& time() const; + //- Return the local objectRegistry + const objectRegistry& db() const; - //- Return the local objectRegistry - const objectRegistry& db() const; + //- Return time + const Time& time() const; - //- Return name - const word& name() const - { - return name_; - } + //- Return name + inline const word& name() const; - //- Return name of the class name read from header - const word& headerClassName() const - { - return headerClassName_; - } + //- Return name of the class name read from header + inline const word& headerClassName() const; - //- Return non-constant access to the optional note - string& note() - { - return note_; - } + //- Return the optional note + inline const string& note() const; - //- Return the optional note - const string& note() const - { - return note_; - } + //- Return non-constant access to the optional note + inline string& note(); - //- Rename - virtual void rename(const word& newName) - { - name_ = newName; - } + //- Rename + virtual void rename(const word& newName) + { + name_ = newName; + } - //- Register object created from this IOobject with registry if true - bool& registerObject() - { - return registerObject_; - } + //- Register object created from this IOobject with registry if true + inline bool registerObject() const; - //- Register object created from this IOobject with registry if true - bool registerObject() const - { - return registerObject_; - } + //- Register object created from this IOobject with registry if true + inline bool& registerObject(); - //- Is object same for all processors - bool& globalObject() - { - return globalObject_; - } + //- Is object same for all processors + inline bool globalObject() const; - //- Is object same for all processors - bool globalObject() const - { - return globalObject_; - } + //- Is object same for all processors + inline bool& globalObject(); - // Read/write options + // Read/write options - readOption readOpt() const - { - return rOpt_; - } + //- The read option + inline readOption readOpt() const; - readOption& readOpt() - { - return rOpt_; - } + //- Non-constant access to the read option + inline readOption& readOpt(); - writeOption writeOpt() const - { - return wOpt_; - } + //- The write option + inline writeOption writeOpt() const; - writeOption& writeOpt() - { - return wOpt_; - } + //- Non-constant access to the write option + inline writeOption& writeOpt(); - // Path components + // Path components - //- Return group (extension part of name) - word group() const; + //- Return group (extension part of name) + word group() const; - //- Return member (name without the extension) - word member() const; + //- Return member (name without the extension) + word member() const; - const fileName& rootPath() const; + const fileName& rootPath() const; - const fileName& caseName() const; + const fileName& caseName() const; - const fileName& instance() const - { - return instance_; - } + inline const fileName& instance() const; - fileName& instance() - { - return instance_; - } + inline fileName& instance(); - const fileName& local() const - { - return local_; - } + inline const fileName& local() const; - //- Return complete path - fileName path() const; + //- Return complete path + fileName path() const; - //- Return complete path with alternative instance and local - fileName path - ( - const word& instance, - const fileName& local = fileName::null - ) const; + //- Return complete path with alternative instance and local + fileName path + ( + const word& instance, + const fileName& local = fileName::null + ) const; - //- Return complete path + object name - fileName objectPath() const - { - return path()/name(); - } + //- Return complete path + object name + inline fileName objectPath() const; - //- Helper for filePath that searches locally. - // When search is false, simply use the current instance, - // otherwise search previous instances. - fileName localFilePath(const bool search=true) const; + //- Helper for filePath that searches locally. + // When search is false, simply use the current instance, + // otherwise search previous instances. + fileName localFilePath(const bool search=true) const; - //- Helper for filePath that searches up if in parallel - // When search is false, simply use the current instance, - // otherwise search previous instances. - fileName globalFilePath(const bool search=true) const; + //- Helper for filePath that searches up if in parallel + // When search is false, simply use the current instance, + // otherwise search previous instances. + fileName globalFilePath(const bool search=true) const; - // Reading + // Reading - //- Read header - bool readHeader(Istream&); + //- Read header + bool readHeader(Istream& is); - //- Read header (uses typeFilePath to find file) and check its info. - // Optionally checks headerClassName against the type-name. - // When search is false, simply use the current instance, - // otherwise search previous instances. - template - bool typeHeaderOk - ( - const bool checkType = true, - const bool search = true - ); + //- Read header (uses typeFilePath to find file) and check its info. + // Optionally checks headerClassName against the type-name. + // When search is false, simply use the current instance, + // otherwise search previous instances. + template + bool typeHeaderOk + ( + const bool checkType = true, + const bool search = true + ); - //- Helper: warn that type does not support re-reading - template - void warnNoRereading() const; - - // Writing - - //- Write the standard OpenFOAM file/dictionary banner - // Optionally without -*- C++ -*- editor hint (eg, for logs) - template - static inline Stream& writeBanner(Stream& os, bool noHint=false); - - //- Write the standard file section divider - template - static inline Stream& writeDivider(Stream& os); - - //- Write the standard end file divider - template - static inline Stream& writeEndDivider(Stream& os); - - //- Write header - bool writeHeader(Ostream&) const; - - //- Write header. Allow override of type - bool writeHeader(Ostream&, const word& objectType) const; + //- Helper: warn that type does not support re-reading + template + void warnNoRereading() const; - // Error Handling + // Writing - bool good() const - { - return objState_ == GOOD; - } + //- Write the standard OpenFOAM file/dictionary banner + // Optionally without -*- C++ -*- editor hint (eg, for logs) + static Ostream& writeBanner(Ostream& os, bool noHint=false); - bool bad() const - { - return objState_ == BAD; - } + //- Write the standard file section divider + static Ostream& writeDivider(Ostream& os); + + //- Write the standard end file divider + static Ostream& writeEndDivider(Ostream& os); + + //- Write header + bool writeHeader(Ostream& os) const; + + //- Write header. Allow override of type + bool writeHeader(Ostream& os, const word& objectType) const; - // Info + // Error Handling - //- Return info proxy. - // Used to print token information to a stream - InfoProxy info() const - { - return *this; - } + inline bool good() const; + + inline bool bad() const; + + + // Info + + //- Return info proxy. + // Used to print token information to a stream + InfoProxy info() const + { + return *this; + } // Member operators - void operator=(const IOobject&); + void operator=(const IOobject& io); }; diff --git a/src/OpenFOAM/db/IOobject/IOobjectI.H b/src/OpenFOAM/db/IOobject/IOobjectI.H index be0a0760a2..adce4e7fdc 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectI.H +++ b/src/OpenFOAM/db/IOobject/IOobjectI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -23,95 +23,137 @@ License \*---------------------------------------------------------------------------*/ -#include "foamVersion.H" +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template -inline Foam::word Foam::IOobject::groupName(Name name, const word& group) +template +inline Foam::word Foam::IOobject::groupName(StringType name, const word& group) { - if (group != word::null) - { - return name + ('.' + group); - } - else + if (group.empty()) { return name; } -} - - -template -inline Stream& Foam::IOobject::writeBanner(Stream& os, bool noHint) -{ - static bool spacesSet(false); - static char spaces[40]; - - if (!spacesSet) - { - memset(spaces, ' ', 40); - - size_t len = strlen(Foam::FOAMversion); - if (len < 38) - { - spaces[38 - len] = '\0'; - } - else - { - spaces[0] = '\0'; - } - spacesSet = true; - } - - if (noHint) - { - os << - "/*--------------------------------------" - "-------------------------------------*\\\n"; - } else { - os << - "/*--------------------------------*- C++ " - "-*----------------------------------*\\\n"; + return name + ('.' + group); } - - os << - "| ========= |" - " |\n" - "| \\\\ / F ield |" - " OpenFOAM: The Open Source CFD Toolbox |\n" - "| \\\\ / O peration |" - " Version: " << FOAMversion << spaces << "|\n" - "| \\\\ / A nd |" - " Web: www.OpenFOAM.com |\n" - "| \\\\/ M anipulation |" - " |\n" - "\\*-----------------------------------------" - "----------------------------------*/\n"; - - return os; } -template -inline Stream& Foam::IOobject::writeDivider(Stream& os) -{ - os << - "// * * * * * * * * * * * * * * * * * " - "* * * * * * * * * * * * * * * * * * * * //\n"; +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - return os; +// General access + +inline const Foam::word& Foam::IOobject::name() const +{ + return name_; } -template -inline Stream& Foam::IOobject::writeEndDivider(Stream& os) -{ - os << "\n\n" - "// *****************************************" - "******************************** //\n"; - return os; +inline const Foam::word& Foam::IOobject::headerClassName() const +{ + return headerClassName_; +} + + +inline const Foam::string& Foam::IOobject::note() const +{ + return note_; +} + + +inline Foam::string& Foam::IOobject::note() +{ + return note_; +} + + +inline bool Foam::IOobject::registerObject() const +{ + return registerObject_; +} + + +inline bool& Foam::IOobject::registerObject() +{ + return registerObject_; +} + + +inline bool Foam::IOobject::globalObject() const +{ + return globalObject_; +} + + +inline bool& Foam::IOobject::globalObject() +{ + return globalObject_; +} + + +// Read/write options + +inline Foam::IOobject::readOption Foam::IOobject::readOpt() const +{ + return rOpt_; +} + + +inline Foam::IOobject::readOption& Foam::IOobject::readOpt() +{ + return rOpt_; +} + + +inline Foam::IOobject::writeOption Foam::IOobject::writeOpt() const +{ + return wOpt_; +} + + +inline Foam::IOobject::writeOption& Foam::IOobject::writeOpt() +{ + return wOpt_; +} + + +// Path components + +inline const Foam::fileName& Foam::IOobject::instance() const +{ + return instance_; +} + + +inline Foam::fileName& Foam::IOobject::instance() +{ + return instance_; +} + + +inline const Foam::fileName& Foam::IOobject::local() const +{ + return local_; +} + + +inline Foam::fileName Foam::IOobject::objectPath() const +{ + return path()/name(); +} + + +// Error Handling + +inline bool Foam::IOobject::good() const +{ + return objState_ == GOOD; +} + + +inline bool Foam::IOobject::bad() const +{ + return objState_ == BAD; } diff --git a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C index f93384bf8c..58757394db 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C +++ b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C @@ -116,9 +116,8 @@ void Foam::IOobject::warnNoRereading() const { WarningInFunction << Type::typeName << ' ' << name() - << " constructed with IOobject::MUST_READ_IF_MODIFIED" - " but " << Type::typeName - << " does not support automatic rereading." + << " constructed with IOobject::MUST_READ_IF_MODIFIED but " + << Type::typeName << " does not support automatic rereading." << endl; } } diff --git a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C index 51d6f7d853..4e01787843 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C +++ b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,29 +29,91 @@ Description #include "IOobject.H" #include "objectRegistry.H" -#include "endian.H" -#include "label.H" -#include "scalar.H" +#include "foamVersion.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -// file-scope -// Hint about machine endian, OpenFOAM label and scalar sizes -static const std::string archHint = -( -#ifdef WM_LITTLE_ENDIAN - "LSB" -#elif defined (WM_BIG_ENDIAN) - "MSB" -#else - "???" -#endif - ";label=" + std::to_string(8*sizeof(Foam::label)) - + ";scalar=" + std::to_string(8*sizeof(Foam::scalar)) -); +// A banner corresponding to this: +// +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: VERSION | +| \\ / A nd | Web: www.OpenFOAM.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ + +Foam::Ostream& Foam::IOobject::writeBanner(Ostream& os, bool noHint) +{ + // The version padded with spaces to fit after "Version: " + // - initialized with zero-length string to detect if it has been populated + static char paddedVersion[39] = ""; + + if (!*paddedVersion) + { + // Populate: like strncpy but without trailing '\0' + const char *p = Foam::FOAMversion; + + memset(paddedVersion, ' ', 38); + for (int i = 0; *p && i < 38; ++i) + { + paddedVersion[i] = *p++; + } + paddedVersion[38] = '\0'; + } + + os << + "/*--------------------------------"; + + if (noHint) + { + // Without syntax hint + os << "---------"; + } + else + { + // With syntax hint + os << "*- C++ -*"; + } + + os << + "----------------------------------*\\\n" + "| ========= |" + " |\n" + "| \\\\ / F ield |" + " OpenFOAM: The Open Source CFD Toolbox |\n" + "| \\\\ / O peration |" + " Version: " << paddedVersion << "|\n" + "| \\\\ / A nd |" + " Web: www.OpenFOAM.com |\n" + "| \\\\/ M anipulation |" + " |\n" + "\\*-----------------------------------------" + "----------------------------------*/\n"; + + return os; +} -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +Foam::Ostream& Foam::IOobject::writeDivider(Ostream& os) +{ + os << + "// * * * * * * * * * * * * * * * * * " + "* * * * * * * * * * * * * * * * * * * * //\n"; + + return os; +} + + +Foam::Ostream& Foam::IOobject::writeEndDivider(Ostream& os) +{ + os << "\n\n" + "// *****************************************" + "******************************** //\n"; + + return os; +} + bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const { @@ -72,7 +134,7 @@ bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const if (os.format() == IOstream::BINARY) { - os << " arch " << archHint << ";\n"; + os << " arch " << Foam::FOAMbuildArch << ";\n"; } if (!note().empty()) diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index e111b33067..42d497c770 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -37,7 +37,7 @@ License #include "sigInt.H" #include "sigQuit.H" #include "sigSegv.H" -#include "endian.H" +#include "foamVersion.H" #include @@ -54,22 +54,6 @@ Foam::string::size_type Foam::argList::usageMin = 20; Foam::string::size_type Foam::argList::usageMax = 80; Foam::word Foam::argList::postProcessOptionName("postProcess"); -// file-scope -// Hint about machine endian, OpenFOAM label and scalar sizes -static const std::string archHint = -( -#ifdef WM_LITTLE_ENDIAN - "LSB" -#elif defined (WM_BIG_ENDIAN) - "MSB" -#else - "???" -#endif - ";label=" + std::to_string(8*sizeof(Foam::label)) - + ";scalar=" + std::to_string(8*sizeof(Foam::scalar)) -); - - Foam::argList::initValidTables::initValidTables() { argList::addOption @@ -639,7 +623,7 @@ void Foam::argList::parse { IOobject::writeBanner(Info, true) << "Build : " << Foam::FOAMbuild << nl - << "Arch : " << archHint << nl + << "Arch : " << Foam::FOAMbuildArch << nl << "Exec : " << argListStr_.c_str() << nl << "Date : " << dateString.c_str() << nl << "Time : " << timeString.c_str() << nl @@ -650,7 +634,7 @@ void Foam::argList::parse jobInfo.add("startDate", dateString); jobInfo.add("startTime", timeString); jobInfo.add("userName", userName()); - jobInfo.add("foamVersion", word(FOAMversion)); + jobInfo.add("foamVersion", word(Foam::FOAMversion)); jobInfo.add("code", executable_); jobInfo.add("argList", argListStr_); jobInfo.add("currentDir", cwd()); @@ -660,10 +644,10 @@ void Foam::argList::parse // Add build information - only use the first word { std::string build(Foam::FOAMbuild); - std::string::size_type found = build.find(' '); - if (found != std::string::npos) + std::string::size_type space = build.find(' '); + if (space != std::string::npos) { - build.resize(found); + build.resize(space); } jobInfo.add("foamBuild", build); } @@ -1247,7 +1231,7 @@ void Foam::argList::printUsage() const <<"Using: OpenFOAM-" << Foam::FOAMversion << " (see www.OpenFOAM.com)" << nl << "Build: " << Foam::FOAMbuild << nl - << "Arch: " << archHint << nl + << "Arch: " << Foam::FOAMbuildArch << nl << endl; } diff --git a/src/OpenFOAM/global/foamVersion.H b/src/OpenFOAM/global/foamVersion.H index bd3386e786..3b5d9fcb46 100644 --- a/src/OpenFOAM/global/foamVersion.H +++ b/src/OpenFOAM/global/foamVersion.H @@ -32,13 +32,20 @@ Global Foam::FOAMversion Description - OpenFOAM version number static string. + OpenFOAM version number as a static string. Global Foam::FOAMbuild Description - OpenFOAM version number static string with build information + OpenFOAM build information as a static string + +Global + Foam::FOAMbuildArch + +Description + OpenFOAM build architecture information (endian, label/scalar sizes) + as a static string SourceFiles global.Cver @@ -48,6 +55,8 @@ SourceFiles #ifndef foamVersion_H #define foamVersion_H +#include + // The directory name for user-resources (located in the HOME directory) #define WM_USER_RESOURCE_DIRNAME ".OpenFOAM" @@ -57,6 +66,7 @@ namespace Foam { extern const char* const FOAMversion; extern const char* const FOAMbuild; + extern const std::string FOAMbuildArch; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/global/global.Cver b/src/OpenFOAM/global/global.Cver index a868495556..0c9f3fdf31 100644 --- a/src/OpenFOAM/global/global.Cver +++ b/src/OpenFOAM/global/global.Cver @@ -33,10 +33,30 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #include "foamVersion.H" +#include "endian.H" +#include "label.H" +#include "scalar.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // const char* const Foam::FOAMversion = "VERSION_STRING"; const char* const Foam::FOAMbuild = "BUILD_STRING"; +// Information about machine endian, label and scalar sizes +const std::string Foam::FOAMbuildArch = +( +#ifdef WM_LITTLE_ENDIAN + "LSB" +#elif defined (WM_BIG_ENDIAN) + "MSB" +#else + "???" +#endif + ";label=" + std::to_string(8*sizeof(Foam::label)) + + ";scalar=" + std::to_string(8*sizeof(Foam::scalar)) +); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Setup an error handler for the global new operator