ENH: generalize instant to be templated, movable, etc

- allows reuse for other purposes
This commit is contained in:
Mark Olesen 2018-10-03 14:05:45 +02:00
parent 6c91048e8b
commit 8548009eee
8 changed files with 471 additions and 145 deletions

View File

@ -0,0 +1,3 @@
Test-instant.C
EXE = $(FOAM_USER_APPBIN)/Test-instant

View File

@ -0,0 +1,2 @@
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
/* EXE_LIBS = -lfiniteVolume */

View File

@ -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 <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;
}
// ************************************************************************* //

View File

@ -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 <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;
}
// ************************************************************************* //

View File

@ -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 <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
// ************************************************************************* //

View File

@ -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 <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
// ************************************************************************* //

View File

@ -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;
}
// ************************************************************************* //

View File

@ -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();
//- Copy and move construct from components
using Instant<word>::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