oneOrTmpI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "oneOrTmp.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 :
33  isOne_(true),
34  tt_(nullptr)
35 {}
36 
37 
38 template<class Type>
39 inline Foam::oneOrTmp<Type>::oneOrTmp(const Type& t)
40 :
41  oneOrTmp(tmp<Type>(t))
42 {}
43 
44 
45 template<class Type>
47 :
48  isOne_(false),
49  tt_(tt)
50 {
51  tt.clear();
52 }
53 
54 
55 template<class Type>
57 :
58  isOne_(oot.isOne_),
59  tt_(oot.tt_)
60 {}
61 
62 
63 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
64 
65 template<class Type>
67 {}
68 
69 
70 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
71 
72 template<class Type>
73 template<class Other>
74 void Foam::oneOrTmp<Type>::scale(Other& o) const
75 {
76  if (!isOne_)
77  {
78  o *= tt_;
79  }
80 }
81 
82 
83 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
84 
85 template<class Type>
87 {
88  isOne_ = true;
89  tt_.clear();
90 }
91 
92 
93 template<class Type>
95 {
96  isOne_ = false;
97  tt_ = tt;
98 }
99 
100 
101 template<class Type>
103 {
104  if (!isOne_)
105  {
106  return oneOrTmp<Type>(tt_());
107  }
108  else
109  {
110  return oneOrTmp();
111  }
112 }
113 
114 
115 template<class Type>
116 template<class Other>
118 {
119  if (!isOne_)
120  {
121  return tt_*o;
122  }
123  else
124  {
125  return tmp<Other>(o);
126  }
127 }
128 
129 
130 template<class Type>
131 template<class Other>
133 {
134  if (!isOne_)
135  {
136  return tt_*to;
137  }
138  else
139  {
140  tmp<Other> tresult(to);
141 
142  to.clear();
143 
144  return tresult;
145  }
146 }
147 
148 
149 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
150 
151 template<class Type, class Other>
152 Foam::tmp<Other> Foam::operator*(const Other& a, const oneOrTmp<Type>& b)
153 {
154  // Multiplication is commutative
155  return b*a;
156 }
157 
158 
159 template<class Type, class Other>
160 Foam::tmp<Other> Foam::operator*(const tmp<Other>& ta, const oneOrTmp<Type>& b)
161 {
162  // Multiplication is commutative
163  return b*ta;
164 }
165 
166 
167 template<class Type, class Other>
168 void Foam::operator*=(Other& a, const oneOrTmp<Type>& b)
169 {
170  b.scale(a);
171 }
172 
173 
174 // ************************************************************************* //
Variant type that either contains and behaves as tmp object or represents a value of one....
Definition: oneOrTmp.H:53
oneOrTmp()
Construct as one.
Definition: oneOrTmpI.H:31
void scale(Other &o) const
Scale an object by this value.
Definition: oneOrTmpI.H:74
~oneOrTmp()
Destructor.
Definition: oneOrTmpI.H:66
tmp< Other > operator*(const Other &)
Multiply with an object.
oneOrTmp< Type > operator()() const
Return an instance of the class which refers to this one. Used in.
Definition: oneOrTmpI.H:102
void operator=(const one &)
Assign to one.
Definition: oneOrTmpI.H:86
A class representing the concept of 1 (scalar(1)) used to avoid unnecessary manipulations for objects...
Definition: one.H:51
A class for managing temporary objects.
Definition: tmp.H:55
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:253
volScalarField & b
Definition: createFields.H:27
tmp< DimensionedField< Type, GeoMesh, Field > > operator*(const DimensionedField< Type, GeoMesh, PrimitiveField1 > &df1, const DimensionedField< scalar, GeoMesh, PrimitiveField2 > &df2)
void operator*=(Other &, const oneOrTmp< Type > &)
Multiply-assign with an object.
Definition: oneOrTmpI.H:168