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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::Instant
28
29Description
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
44namespace Foam
45{
46
47/*---------------------------------------------------------------------------*\
48 Class Instant Declaration
49\*---------------------------------------------------------------------------*/
50
51template<class T>
52class Instant
53{
54 // Private Data
55
56 //- The value (eg, time)
57 scalar val_;
58
59 //- The name/key
60 T key_;
61
62
63public:
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
120 {
121 return val_;
122 }
123
124 //- The value (non-const access)
125 scalar& value() noexcept
126 {
127 return val_;
128 }
129
130 //- The name/key (const access)
131 const T& name() const noexcept
132 {
133 return key_;
134 }
135
136 //- The name/key (non-const access)
137 T& name() noexcept
138 {
139 return key_;
140 }
141
142 //- True if values are equal (includes SMALL for rounding)
143 bool equal(scalar val) const noexcept
144 {
145 return ((val_ > val - SMALL) && (val_ < val + SMALL));
146 }
147
148 //- True if values are equal (includes SMALL for rounding)
149 template<class T2>
150 bool equal(const Instant<T2>& other) const noexcept
151 {
152 return (*this).equal(other.value());
153 }
154};
155
156
157// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
158
159template<class T1, class T2>
160inline bool operator==(const Instant<T1>& a, const Instant<T2>& b) noexcept
161{
162 return a.equal(b.value());
163}
164
165
166template<class T1, class T2>
167inline bool operator!=(const Instant<T1>& a, const Instant<T2>& b) noexcept
168{
169 return !a.equal(b.value());
170}
171
172
173template<class T1, class T2>
174inline bool operator<(const Instant<T1>& a, const Instant<T2>& b) noexcept
175{
176 return (a.value() < b.value());
177}
178
179
180template<class T1, class T2>
181inline bool operator>(const Instant<T1>& a, const Instant<T2>& b) noexcept
182{
183 return (b.value() < a.value());
184}
185
186
187// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
188
189//- Read instant tuple from Istream
190template<class T>
191inline Istream& operator>>(Istream& is, Instant<T>& inst)
192{
193 is >> inst.value() >> inst.name();
194 return is;
195}
196
197
198//- Write instant tuple to Ostream
199template<class T>
200inline Ostream& operator<<(Ostream& os, const Instant<T>& inst)
201{
202 os << inst.value() << '\t' << inst.name();
203 return os;
204}
205
206
207// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208
209} // End namespace Foam
210
211// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212
213#endif
214
215// ************************************************************************* //
A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the ...
Definition: Instant.H:52
Instant(scalar val, T &&key)
Move construct from components.
Definition: Instant.H:108
Instant & operator=(Instant &&)=default
Move assignment.
Instant(const Instant &)=default
Copy construct.
T & name() noexcept
The name/key (non-const access)
Definition: Instant.H:136
scalar value() const noexcept
The value (const access)
Definition: Instant.H:118
Instant()
Default construct, with value = 0 and empty name.
Definition: Instant.H:94
const T & name() const noexcept
The name/key (const access)
Definition: Instant.H:130
Instant(scalar val, const T &key)
Copy construct from components.
Definition: Instant.H:101
scalar & value() noexcept
The value (non-const access)
Definition: Instant.H:124
Instant(Instant &&)=default
Move construct.
bool equal(const Instant< T2 > &other) const noexcept
True if values are equal (includes SMALL for rounding)
Definition: Instant.H:149
Instant & operator=(const Instant &)=default
Copy assignment.
bool equal(scalar val) const noexcept
True if values are equal (includes SMALL for rounding)
Definition: Instant.H:142
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:239
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
volScalarField & b
Definition: createFields.H:27
Less function for sorting.
Definition: Instant.H:68
bool operator()(const Instant &a, const Instant &b) const noexcept
Definition: Instant.H:69