Commit 8548009e authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: generalize instant to be templated, movable, etc

- allows reuse for other purposes
parent 6c91048e
Test-instant.C
EXE = $(FOAM_USER_APPBIN)/Test-instant
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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<instant> 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<fileNameInstant> 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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "Instant.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
Foam::Instant<T>::Instant()
:
val_(0),
key_()
{}
template<class T>
Foam::Instant<T>::Instant::Instant(scalar val, const T& key)
:
val_(val),
key_(key)
{}
template<class T>
Foam::Instant<T>::Instant::Instant(scalar val, T&& key)
:
val_(val),
key_(std::move(key))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
bool Foam::Instant<T>::equal(scalar val) const
{
return ((val_ > val - SMALL) && (val_ < val + SMALL));
}
template<class T>
template<class T2>
bool Foam::Instant<T>::equal(const Instant<T2>& other) const
{
return this->equal(other.value());
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
template<class T1, class T2>
bool Foam::operator==(const Instant<T1>& a, const Instant<T2>& b)
{
return a.equal(b.value());
}
template<class T1, class T2>
bool Foam::operator!=(const Instant<T1>& a, const Instant<T2>& b)
{
return !a.equal(b.value());
}
template<class T1, class T2>
bool Foam::operator<(const Instant<T1>& a, const Instant<T2>& b)
{
return a.value() < b.value();
}
template<class T1, class T2>
bool Foam::operator>(const Instant<T1>& a, const Instant<T2>& b)
{
return a.value() > b.value();
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T>
Foam::Istream& Foam::operator>>(Istream& is, Instant<T>& inst)
{
is >> inst.value() >> inst.name();
return is;
}
template<class T>
Foam::Ostream& Foam::operator<<(Ostream& os, const Instant<T>& inst)
{
os << inst.value() << '\t' << inst.name();
return os;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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 T>
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<class T2>
bool equal(const Instant<T2>& other) const;
// Operators
//- Copy assignment
Instant& operator=(const Instant&) = default;
//- Move assignment
Instant& operator=(Instant&&) = default;
};
// IOstream Operators
template<class T> Istream& operator>>(Istream& is, Instant<T>& inst);
template<class T> Ostream& operator<<(Ostream& os, const Instant<T>& inst);
// Global Operators
//- Compare instant values for equality
template<class T1, class T2>
bool operator==(const Instant<T1>& a, const Instant<T2>& b);
//- Compare instant values for inequality
template<class T1, class T2>
bool operator!=(const Instant<T1>& a, const Instant<T2>& b);
//- Compare instant values for less-than
template<class T1, class T2>
bool operator<(const Instant<T1>& a, const Instant<T2>& b);
//- Compare instant values for greater-than
template<class T1, class T2>
bool operator>(const Instant<T1>& a, const Instant<T2>& b);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "Instant.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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<fileName> fileNameInstant;
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
......@@ -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 <cstdlib>
// * * * * * * * * * * * * * * 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<word>(timeValue, Time::timeName(timeValue))
{}
Foam::instant::instant(const scalar val)
Foam::instant::instant(const word& timeName)
:
value_(val),
name_(Time::timeName(val))
Instant<word>(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<word>(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;
}
// ************************************************************************* //
......@@ -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<word>
{
// 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();
//- Construct from components
instant(const scalar val, const word& tname);
//- Construct from time value
explicit instant(const scalar val);
//- Copy and move construct from components
using Instant<word>::Instant;