FieldReuseFunctions.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #ifndef Foam_FieldReuseFunctions_H
30 #define Foam_FieldReuseFunctions_H
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // One-parameter versions
39 
40 template<class TypeR, class Type1>
41 struct reuseTmp
42 {
43  //- Pass-through to tmp New
44  static tmp<Field<TypeR>> New(const Field<Type1>& f1)
45  {
46  return tmp<Field<TypeR>>::New(f1.size());
47  }
48 
49  //- Dissimilar types: just use size
50  static tmp<Field<TypeR>> New(const tmp<Field<Type1>>& tf1)
51  {
52  return tmp<Field<TypeR>>::New(tf1().size());
53  }
54 };
55 
56 
57 template<class TypeR>
58 struct reuseTmp<TypeR, TypeR>
59 {
60  //- Identical input and return types:
61  //- allow optional copy assignment of the initial content
63  (
64  const tmp<Field<TypeR>>& tf1,
65  const bool initCopy = false
66  )
67  {
68  if (tf1.movable())
69  {
70  return tf1;
71  }
72 
73  auto rtf = tmp<Field<TypeR>>::New(tf1().size());
74 
75  if (initCopy)
76  {
77  rtf.ref() = tf1();
78  }
79 
80  return rtf;
81  }
82 };
83 
84 
85 //- This global function forwards to reuseTmp::New
86 template<class TypeR> tmp<Field<TypeR>> New
87 (
88  const tmp<Field<TypeR>>& tf1,
89  const bool initCopy = false
90 )
91 {
92  return reuseTmp<TypeR, TypeR>::New(tf1, initCopy);
93 }
94 
95 
96 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 // Two-parameter versions
98 
99 template<class TypeR, class Type1, class Type12, class Type2>
100 struct reuseTmpTmp
101 {
102  //- Dissimilar types: just use size
103  static tmp<Field<TypeR>> New
104  (
105  const tmp<Field<Type1>>& tf1,
106  const tmp<Field<Type2>>& tf2
107  )
108  {
109  return tmp<Field<TypeR>>::New(tf1().size());
110  }
111 };
112 
113 
114 template<class TypeR, class Type1, class Type12>
115 struct reuseTmpTmp<TypeR, Type1, Type12, TypeR>
116 {
117  //- Second input has return type
119  (
120  const tmp<Field<Type1>>& tf1,
121  const tmp<Field<TypeR>>& tf2
122  )
123  {
124  if (tf2.movable())
125  {
126  return tf2;
127  }
128 
129  return tmp<Field<TypeR>>::New(tf1().size());
130  }
131 };
132 
133 
134 template<class TypeR, class Type2>
135 struct reuseTmpTmp<TypeR, TypeR, TypeR, Type2>
136 {
137  //- First input has return type
138  static tmp<Field<TypeR>> New
139  (
140  const tmp<Field<TypeR>>& tf1,
141  const tmp<Field<Type2>>& tf2
142  )
143  {
144  if (tf1.movable())
145  {
146  return tf1;
147  }
148 
149  return tmp<Field<TypeR>>::New(tf1().size());
150  }
151 };
152 
153 
154 template<class TypeR>
155 struct reuseTmpTmp<TypeR, TypeR, TypeR, TypeR>
156 {
157  //- Both inputs have return type
158  static tmp<Field<TypeR>> New
159  (
160  const tmp<Field<TypeR>>& tf1,
161  const tmp<Field<TypeR>>& tf2
162  )
163  {
164  if (tf1.movable())
165  {
166  return tf1;
167  }
168  if (tf2.movable())
169  {
170  return tf2;
171  }
172 
173  return tmp<Field<TypeR>>::New(tf1().size());
174  }
175 };
176 
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 } // End namespace Foam
181 
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
183 
184 #endif
185 
186 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
static tmp< Field< TypeR > > New(const Field< Type1 > &f1)
Pass-through to tmp New.
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
Generic templated field type.
Definition: Field.H:62
static tmp< Field< TypeR > > New(const tmp< Field< Type1 >> &tf1, const tmp< Field< Type2 >> &tf2)
Dissimilar types: just use size.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Namespace for OpenFOAM.