diff --git a/applications/test/instant/Make/files b/applications/test/instant/Make/files new file mode 100644 index 0000000000..c7dd9f8146 --- /dev/null +++ b/applications/test/instant/Make/files @@ -0,0 +1,3 @@ +Test-instant.C + +EXE = $(FOAM_USER_APPBIN)/Test-instant diff --git a/applications/test/instant/Make/options b/applications/test/instant/Make/options new file mode 100644 index 0000000000..6a9e9810b3 --- /dev/null +++ b/applications/test/instant/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/instant/Test-instant.C b/applications/test/instant/Test-instant.C new file mode 100644 index 0000000000..f0487c9857 --- /dev/null +++ b/applications/test/instant/Test-instant.C @@ -0,0 +1,82 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Test instant, fileNameInstant + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "instant.H" +#include "fileNameInstant.H" +#include "DynamicList.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + DynamicList times; + + times.append(instant{}); + times.append({12, "abc"}); + times.append(instant{3.14159}); + times.append({300.456, "def"}); + times.append({454.456, "xyz"}); + times.append({10, "ten"}); + + { + word timeName("twenty"); + + Info<<"move append: " << timeName << nl; + times.append({20, std::move(timeName)}); + Info<<"after append: " << timeName << nl; + } + + Info<< nl << "times:" << times << nl; + sort(times); + Info<< "Sorted:" << times << nl; + + + DynamicList files; + files.append(fileNameInstant{}); + files.append({12, "twelve"}); + files.append({3.14, "/path/almost-pi"}); + files.append({300, "/dev/value"}); + files.append({454, "/tmp/xyz"}); + files.append({10, "ten"}); + + Info<< nl << "files:" << files << nl; + sort(files); + Info<< "Sorted:" << files << nl; + + + Info<< "\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/Instant.C b/src/OpenFOAM/db/Time/instant/Instant.C new file mode 100644 index 0000000000..7963c97e63 --- /dev/null +++ b/src/OpenFOAM/db/Time/instant/Instant.C @@ -0,0 +1,119 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "Instant.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::Instant::Instant() +: + val_(0), + key_() +{} + + +template +Foam::Instant::Instant::Instant(scalar val, const T& key) +: + val_(val), + key_(key) +{} + + +template +Foam::Instant::Instant::Instant(scalar val, T&& key) +: + val_(val), + key_(std::move(key)) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +bool Foam::Instant::equal(scalar val) const +{ + return ((val_ > val - SMALL) && (val_ < val + SMALL)); +} + + +template +template +bool Foam::Instant::equal(const Instant& other) const +{ + return this->equal(other.value()); +} + + +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // + +template +bool Foam::operator==(const Instant& a, const Instant& b) +{ + return a.equal(b.value()); +} + + +template +bool Foam::operator!=(const Instant& a, const Instant& b) +{ + return !a.equal(b.value()); +} + + +template +bool Foam::operator<(const Instant& a, const Instant& b) +{ + return a.value() < b.value(); +} + + +template +bool Foam::operator>(const Instant& a, const Instant& b) +{ + return a.value() > b.value(); +} + + +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +template +Foam::Istream& Foam::operator>>(Istream& is, Instant& inst) +{ + is >> inst.value() >> inst.name(); + return is; +} + + +template +Foam::Ostream& Foam::operator<<(Ostream& os, const Instant& inst) +{ + os << inst.value() << '\t' << inst.name(); + return os; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/Instant.H b/src/OpenFOAM/db/Time/instant/Instant.H new file mode 100644 index 0000000000..8291714a5d --- /dev/null +++ b/src/OpenFOAM/db/Time/instant/Instant.H @@ -0,0 +1,179 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::Instant + +Description + A tuple of value and key. + The value often corresponds to a time value, thus the naming of the class. + The key will usually be a time name or a file name etc. + +SourceFiles + Instant.C + +\*---------------------------------------------------------------------------*/ + +#ifndef Instant_H +#define Instant_H + +#include "scalar.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class Instant Declaration +\*---------------------------------------------------------------------------*/ + +template +class Instant +{ + // Private Data + + //- The value (eg, time) + scalar val_; + + //- The name/key + T key_; + + +public: + + // Public classes + + //- Less function for sorting + struct less + { + bool operator()(const Instant& a, const Instant& b) const + { + return a.value() < b.value(); + } + }; + + + // Constructors + + //- Construct null, with value = 0. + Instant(); + + //- Copy construct from components + Instant(scalar val, const T& key); + + //- Move construct from components + Instant(scalar val, T&& key); + + //- Copy construct + Instant(const Instant&) = default; + + //- Move construct + Instant(Instant&&) = default; + + + // Member Functions + + //- The value (const access) + scalar value() const + { + return val_; + } + + //- The value (non-const access) + scalar& value() + { + return val_; + } + + //- The name/key (const access) + const T& name() const + { + return key_; + } + + //- The name/key (non-const access) + T& name() + { + return key_; + } + + //- True if values are equal (includes SMALL for rounding) + bool equal(scalar val) const; + + //- True if values are equal (includes SMALL for rounding) + template + bool equal(const Instant& other) const; + + + // Operators + + //- Copy assignment + Instant& operator=(const Instant&) = default; + + //- Move assignment + Instant& operator=(Instant&&) = default; +}; + + +// IOstream Operators + +template Istream& operator>>(Istream& is, Instant& inst); +template Ostream& operator<<(Ostream& os, const Instant& inst); + + +// Global Operators + +//- Compare instant values for equality +template +bool operator==(const Instant& a, const Instant& b); + +//- Compare instant values for inequality +template +bool operator!=(const Instant& a, const Instant& b); + +//- Compare instant values for less-than +template +bool operator<(const Instant& a, const Instant& b); + +//- Compare instant values for greater-than +template +bool operator>(const Instant& a, const Instant& b); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "Instant.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/fileNameInstant.H b/src/OpenFOAM/db/Time/instant/fileNameInstant.H new file mode 100644 index 0000000000..8391d9591b --- /dev/null +++ b/src/OpenFOAM/db/Time/instant/fileNameInstant.H @@ -0,0 +1,50 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Typedef + Foam::fileNameInstant + +Description + A tuple of value and fileName. + +\*---------------------------------------------------------------------------*/ + +#ifndef fileNameInstant_H +#define fileNameInstant_H + +#include "fileName.H" +#include "Instant.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + typedef Instant fileNameInstant; + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/instant.C b/src/OpenFOAM/db/Time/instant/instant.C index 0e3232efb6..e59d65d125 100644 --- a/src/OpenFOAM/db/Time/instant/instant.C +++ b/src/OpenFOAM/db/Time/instant/instant.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,8 +23,9 @@ License \*---------------------------------------------------------------------------*/ -#include "instantList.H" +#include "instant.H" #include "Time.H" +#include // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -33,81 +34,22 @@ const char* const Foam::instant::typeName = "instant"; // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::instant::instant() -{} - - -Foam::instant::instant(const scalar val, const word& tname) +Foam::instant::instant(scalar timeValue) : - value_(val), - name_(tname) + Instant(timeValue, Time::timeName(timeValue)) {} -Foam::instant::instant(const scalar val) +Foam::instant::instant(const word& timeName) : - value_(val), - name_(Time::timeName(val)) + Instant(std::atof(timeName.c_str()), timeName) {} -Foam::instant::instant(const word& tname) +Foam::instant::instant(word&& timeName) : - value_(atof(tname.c_str())), - name_(tname) + Instant(std::atof(timeName.c_str()), std::move(timeName)) {} -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -bool Foam::instant::equal(const scalar val) const -{ - return ((value_ > val - SMALL) && (value_ < val + SMALL)); -} - - -// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // - -bool Foam::operator==(const instant& a, const instant& b) -{ - return a.equal(b.value()); -} - - -bool Foam::operator!=(const instant& a, const instant& b) -{ - return !a.equal(b.value()); -} - - -bool Foam::operator<(const instant& a, const instant& b) -{ - return a.value() < b.value(); -} - - -bool Foam::operator>(const instant& a, const instant& b) -{ - return a.value() > b.value(); -} - - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -Foam::Istream& Foam::operator>>(Istream& is, instant& inst) -{ - is >> inst.value_ >> inst.name_; - - return is; -} - - -Foam::Ostream& Foam::operator<<(Ostream& os, const instant& inst) -{ - os << inst.value() << tab << inst.name(); - - return os; -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/instant.H b/src/OpenFOAM/db/Time/instant/instant.H index 72ceea2497..23453f0588 100644 --- a/src/OpenFOAM/db/Time/instant/instant.H +++ b/src/OpenFOAM/db/Time/instant/instant.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,113 +36,62 @@ SourceFiles #define instant_H #include "word.H" -#include "scalar.H" +#include "Instant.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -// Forward declarations -class instant; - -Istream& operator>>(Istream& is, instant& inst); -Ostream& operator<<(Ostream& os, const instant& inst); - - /*---------------------------------------------------------------------------*\ Class instant Declaration \*---------------------------------------------------------------------------*/ class instant +: + public Instant { - // Private data - - scalar value_; - - word name_; - public: - // Public classes - - //- Less function class used in sorting instants - class less - { - public: - - bool operator()(const instant& a, const instant& b) const - { - return a.value() < b.value(); - } - }; - - - // Static data members + // Static Data Members static const char* const typeName; // Constructors - //- Construct null - instant(); + //- Copy and move construct from components + using Instant::Instant; - //- Construct from components - instant(const scalar val, const word& tname); + //- Construct null, with time-value = 0. + instant() = default; - //- Construct from time value - explicit instant(const scalar val); + //- Copy construct + instant(const instant&) = default; - //- Construct from time name - explicit instant(const word& tname); + //- Move construct + instant(instant&&) = default; + + //- Construct from timeValue, auto generating the name + explicit instant(scalar timeValue); + + //- Construct from timeName, parsing timeName for a value + explicit instant(const word& timeName); + + //- Construct from timeName, parsing timeName for a value + explicit instant(word&& timeName); - // Member Functions + // Operators - //- Value (const access) - scalar value() const - { - return value_; - } + //- Copy assignment + instant& operator=(const instant&) = default; - //- Value (non-const access) - scalar& value() - { - return value_; - } + //- Move assignment + instant& operator=(instant&&) = default; - //- Name (const access) - const word& name() const - { - return name_; - } - - //- Name (non-const access) - word& name() - { - return name_; - } - - //- Compare instant values to be equal (includes SMALL for rounding) - bool equal(const scalar val) const; - - - // IOstream Operators - - friend Istream& operator>>(Istream& is, instant& inst); - friend Ostream& operator<<(Ostream& os, const instant& inst); }; - -// Global Operators - -bool operator==(const instant& a, const instant& b); -bool operator!=(const instant& a, const instant& b); -bool operator<(const instant& a, const instant& b); -bool operator>(const instant& a, const instant& b); - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam