Instant.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::Instant
28 
29 Description
30  A tuple of scalar value and key.
31  The value often corresponds to a time value, thus the naming of the class.
32  The key will usually be a time name or a file name etc.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef Foam_Instant_H
37 #define Foam_Instant_H
38 
39 #include "scalar.H"
40 #include <utility> // std::move
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class Instant Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 template<class T>
52 class Instant
53 {
54  // Private Data
55 
56  //- The value (eg, time)
57  scalar val_;
58 
59  //- The name/key
60  T key_;
61 
62 
63 public:
64 
65  // Public Classes
66 
67  //- Less function for sorting
68  struct less
69  {
70  bool operator()(const Instant& a, const Instant& b) const noexcept
71  {
72  return a.value() < b.value();
73  }
74  };
75 
76 
77  // Generated Methods
78 
79  //- Copy construct
80  Instant(const Instant&) = default;
81 
82  //- Move construct
83  Instant(Instant&&) = default;
84 
85  //- Copy assignment
86  Instant& operator=(const Instant&) = default;
87 
88  //- Move assignment
89  Instant& operator=(Instant&&) = default;
90 
91 
92  // Constructors
93 
94  //- Default construct, with value = 0 and empty name
95  Instant()
96  :
97  val_(0),
98  key_()
99  {}
100 
101  //- Copy construct from components
102  Instant(scalar val, const T& key)
103  :
104  val_(val),
105  key_(key)
106  {}
107 
108  //- Move construct from components
109  Instant(scalar val, T&& key)
110  :
111  val_(val),
112  key_(std::move(key))
113  {}
114 
115 
116  // Member Functions
117 
118  //- The value (const access)
119  scalar value() const noexcept { return val_; }
120 
121  //- The value (non-const access)
122  scalar& value() noexcept { return val_; }
123 
124  //- The name/key (const access)
125  const T& name() const noexcept { return key_; }
126 
127  //- The name/key (non-const access)
128  T& name() noexcept { return key_; }
129 
130 
131  //- True if values are equal (includes SMALL for rounding)
132  bool equal(scalar val) const noexcept
133  {
134  return ((val_ > val - SMALL) && (val_ < val + SMALL));
135  }
136 
137  //- True if values are equal (includes SMALL for rounding)
138  template<class T2>
139  bool equal(const Instant<T2>& other) const noexcept
140  {
141  return (*this).equal(other.value());
142  }
143 };
145 
146 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
147 
148 template<class T1, class T2>
149 inline bool operator==(const Instant<T1>& a, const Instant<T2>& b) noexcept
150 {
151  return a.equal(b.value());
152 }
153 
154 
155 template<class T1, class T2>
156 inline bool operator!=(const Instant<T1>& a, const Instant<T2>& b) noexcept
157 {
158  return !a.equal(b.value());
159 }
160 
161 
162 template<class T1, class T2>
163 inline bool operator<(const Instant<T1>& a, const Instant<T2>& b) noexcept
164 {
165  return (a.value() < b.value());
166 }
167 
168 
169 template<class T1, class T2>
170 inline bool operator>(const Instant<T1>& a, const Instant<T2>& b) noexcept
171 {
172  return (b.value() < a.value());
173 }
175 
176 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
177 
178 //- Read instant tuple from Istream
179 template<class T>
180 inline Istream& operator>>(Istream& is, Instant<T>& inst)
181 {
182  is >> inst.value() >> inst.name();
183  return is;
184 }
185 
186 
187 //- Write instant tuple to Ostream
188 template<class T>
189 inline Ostream& operator<<(Ostream& os, const Instant<T>& inst)
190 {
191  os << inst.value() << '\t' << inst.name();
192  return os;
193 }
194 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 } // End namespace Foam
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #endif
203 
204 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
Instant & operator=(const Instant &)=default
Copy assignment.
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition: OSstream.H:128
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Instant()
Default construct, with value = 0 and empty name.
Definition: Instant.H:104
scalar value() const noexcept
The value (const access)
Definition: Instant.H:134
const T & name() const noexcept
The name/key (const access)
Definition: Instant.H:144
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the ...
Definition: Instant.H:45
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
bool equal(scalar val) const noexcept
True if values are equal (includes SMALL for rounding)
Definition: Instant.H:155
Less function for sorting.
Definition: Instant.H:67
Namespace for OpenFOAM.
bool operator()(const Instant &a, const Instant &b) const noexcept
Definition: Instant.H:69