casacore
Loading...
Searching...
No Matches
WrapperData.h
Go to the documentation of this file.
1// # WrapperData.h: Aid in constructing function objects from C++ functions
2// # Copyright (C) 2001,2002
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_WRAPPERDATA_H
27#define SCIMATH_WRAPPERDATA_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Functionals/WrapperBase.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// # Forward declarations
36
37// <summary> Aid in constructing function objects from C++ functions
38// </summary>
39//
40// <use visibility=local>
41//
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
43// </reviewed>
44//
45// <prerequisite>
46// <li> <linkto class="Function">Function</linkto> class
47// <li> <linkto class="FunctionParam">FunctionParam</linkto>
48// </prerequisite>
49//
50// <synopsis>
51// This class is provided to enable compile time selection of the
52// appropriate function call. Each template incarnation represent a
53// function call interface definition.
54// </synopsis>
55//
56// <example>
57// <srcblock>
58// Float func(const Vector<Float>& x) {return x(0)*x(1);} // x*y
59// // Convert C++ functions to Functionals
60// FunctionWrapper<Float> Func(func, 2);
61// </srcblock>
62//
63
64template <class T, class U, class V, Bool hasX, Bool hasParam>
65class WrapperData : public WrapperBase<T> {
66 public:
67 // # Constructors
68 // Default constructor: to allow arrays of functions
70
71 // Destructor
72 virtual ~WrapperData() {}
73
74 // # Operators
75 // Evaluate the function at <src>x</src>.
76 // <group>
77 virtual T eval(typename Function<T>::FunctionArg, const V &) const {}
78 // </group>
79
80 // # Member functions
81
82 protected:
83 // # Make members of parent classes known.
84 using WrapperBase<T>::ndim_p;
85 using WrapperBase<T>::arg_p;
86};
87
88#define WrapperData_TT WrapperData
89
90// <summary> Specialization for calls with argument and parameter
91// </summary>
92// <synopsis> Note that the actual name of the class is
93// <src>WrapperData</src>. The special name is only for the use of
94// cxx2html.
95// </synopsis>
96
97template <class T>
98class WrapperData_TT<T, T, T, True, True> : public WrapperBase<T> {
100
101 public:
102 // # Constructors
103 // Standard constructor
104 explicit WrapperData_TT(T (*f)(const T &, const T &), uInt dim = 1)
105 : WrapperBase<T>(dim), pf_p(f) {}
106
107 // Destructor
108 virtual ~WrapperData_TT() {}
109
110 // # Operators
111 // Evaluate the function at <src>x</src>.
112 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &par) const {
113 if (pf_p) {
114 return pf_p((*static_cast<const typename Function<T>::FunctionArg>(x)), par[0]);
115 }
116 return T(0);
117 }
118
119 // # Member functions
120
121 protected:
122 // # Data
123 // Function to call
124 T (*pf_p)(const T &, const T &);
125
126 private:
127 // Copy constructor and assignment (not implemented)
128 // <group>
129 WrapperData_TT(const myData &other);
130 myData &operator=(const myData &other);
131 // </group>
132
133 protected:
134 // # Make members of parent classes known.
135 using WrapperBase<T>::ndim_p;
136 using WrapperBase<T>::arg_p;
137};
138
139#undef WrapperData_TT
140
141#define WrapperData_VT WrapperData
142
143// <summary> Specialization for calls with argument and parameter
144// </summary>
145// <synopsis> Note that the actual name of the class is
146// <src>WrapperData</src>. The special name is only for the use of
147// cxx2html.
148// </synopsis>
149
150template <class T>
151class WrapperData_VT<T, Vector<T>, T, True, True> : public WrapperBase<T> {
153
154 public:
155 explicit WrapperData_VT(T (*f)(const Vector<T> &, const T &), uInt dim = 1)
156 : WrapperBase<T>(dim), pf_p(f) {}
157 virtual ~WrapperData_VT() {}
158 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &par) const {
159 if (pf_p) {
160 for (uInt i = 0; i < ndim_p; ++i) arg_p[i] = x[i];
161 return pf_p(arg_p, par[0]);
162 }
163 return T(0);
164 }
165
166 protected:
167 T (*pf_p)(const Vector<T> &, const T &);
168
169 private:
170 WrapperData_VT(const myData &other);
171 myData &operator=(const myData &other);
172
173 protected:
174 // # Make members of parent classes known.
175 using WrapperBase<T>::ndim_p;
176 using WrapperBase<T>::arg_p;
177};
178
179#undef WrapperData_VT
180
181#define WrapperData_TV WrapperData
182
183// <summary> Specialization for calls with argument and parameters
184// </summary>
185// <synopsis> Note that the actual name of the class is
186// <src>WrapperData</src>. The special name is only for the use of
187// cxx2html.
188// </synopsis>
189
190template <class T>
191class WrapperData_TV<T, T, Vector<T>, True, True> : public WrapperBase<T> {
193
194 public:
195 explicit WrapperData_TV(T (*f)(const T &, const Vector<T> &), uInt dim = 1)
196 : WrapperBase<T>(dim), pf_p(f) {}
197 virtual ~WrapperData_TV() {}
198 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &par) const {
199 if (pf_p) {
200 return pf_p((*static_cast<const typename Function<T>::FunctionArg>(x)), par);
201 }
202 return T(0);
203 }
204
205 protected:
206 T (*pf_p)(const T &, const Vector<T> &);
207
208 private:
209 WrapperData_TV(const myData &other);
210 myData &operator=(const myData &other);
211
212 protected:
213 // # Make members of parent classes known.
214 using WrapperBase<T>::ndim_p;
215 using WrapperBase<T>::arg_p;
216};
217
218#undef WrapperData_TV
219
220#define WrapperData_VV WrapperData
221
222// <summary> Specialization for calls with argument and parameters
223// </summary>
224// <synopsis> Note that the actual name of the class is
225// <src>WrapperData</src>. The special name is only for the use of
226// cxx2html.
227// </synopsis>
228
229template <class T>
230class WrapperData_VV<T, Vector<T>, Vector<T>, True, True> : public WrapperBase<T> {
232
233 public:
234 explicit WrapperData_VV(T (*f)(const Vector<T> &, const Vector<T> &), uInt dim = 1)
235 : WrapperBase<T>(dim), pf_p(f) {}
236 virtual ~WrapperData_VV() {}
237 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &par) const {
238 if (pf_p) {
239 for (uInt i = 0; i < ndim_p; ++i) arg_p[i] = x[i];
240 return pf_p(arg_p, par);
241 }
242 return T(0);
243 }
244
245 protected:
246 T (*pf_p)(const Vector<T> &, const Vector<T> &);
247
248 private:
249 WrapperData_VV(const myData &other);
250 myData &operator=(const myData &other);
251
252 protected:
253 // # Make members of parent classes known.
254 using WrapperBase<T>::ndim_p;
255 using WrapperBase<T>::arg_p;
256};
257
258#undef WrapperData_VV
259
260#define WrapperData_FT WrapperData
261
262// <summary> Specialization for calls with no arguments and parameter
263// </summary>
264// <synopsis> Note that the actual name of the class is
265// <src>WrapperData</src>. The special name is only for the use of
266// cxx2html.
267// </synopsis>
268
269template <class T>
270class WrapperData_FT<T, T, T, False, True> : public WrapperBase<T> {
272
273 public:
274 explicit WrapperData_FT(T (*f)(const T &)) : WrapperBase<T>(0), pf_p(f) {}
275 virtual ~WrapperData_FT() {}
276 virtual T eval(typename Function<T>::FunctionArg, const Vector<T> &par) const {
277 if (pf_p) return pf_p(par[0]);
278 return T(0);
279 }
280
281 protected:
282 T (*pf_p)(const T &);
283
284 private:
285 WrapperData_FT(const myData &other);
286 myData &operator=(const myData &other);
287
288 protected:
289 // # Make members of parent classes known.
290 using WrapperBase<T>::ndim_p;
291 using WrapperBase<T>::arg_p;
292};
293
294#undef WrapperData_FT
295
296#define WrapperData_FV WrapperData
297
298// <summary> Specialization for calls with no arguments and parameters
299// </summary>
300// <synopsis> Note that the actual name of the class is
301// <src>WrapperData</src>. The special name is only for the use of
302// cxx2html.
303// </synopsis>
304
305template <class T>
306class WrapperData_FV<T, T, Vector<T>, False, True> : public WrapperBase<T> {
308
309 public:
310 explicit WrapperData_FV(T (*f)(const Vector<T> &)) : WrapperBase<T>(0), pf_p(f) {}
311 virtual ~WrapperData_FV() {}
312 virtual T eval(typename Function<T>::FunctionArg, const Vector<T> &par) const {
313 if (pf_p) return pf_p(par);
314 return T(0);
315 }
316
317 protected:
318 T (*pf_p)(const Vector<T> &);
319
320 private:
321 WrapperData_FV(const myData &other);
322 myData &operator=(const myData &other);
323
324 protected:
325 // # Make members of parent classes known.
326 using WrapperBase<T>::ndim_p;
327 using WrapperBase<T>::arg_p;
328};
329
330#undef WrapperData_FV
331
332#define WrapperData_TF WrapperData
333
334// <summary> Specialization for calls with argument and no parameters
335// </summary>
336// <synopsis> Note that the actual name of the class is
337// <src>WrapperData</src>. The special name is only for the use of
338// cxx2html.
339// </synopsis>
340
341template <class T>
342class WrapperData_TF<T, T, T, True, False> : public WrapperBase<T> {
344
345 public:
346 explicit WrapperData_TF(T (*f)(const T &), uInt dim = 1) : WrapperBase<T>(dim), pf_p(f) {}
347 virtual ~WrapperData_TF() {}
348 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &) const {
349 if (pf_p) {
350 return pf_p((*static_cast<const typename Function<T>::FunctionArg>(x)));
351 }
352 return T(0);
353 }
354
355 protected:
356 T (*pf_p)(const T &);
357
358 private:
359 WrapperData_TF(const myData &other);
360 myData &operator=(const myData &other);
361
362 protected:
363 // # Make members of parent classes known.
364 using WrapperBase<T>::ndim_p;
365 using WrapperBase<T>::arg_p;
366};
367
368#undef WrapperData_TF
369
370#define WrapperData_VF WrapperData
371
372// <summary> Specialization for calls with argument and no parameters
373// </summary>
374// <synopsis> Note that the actual name of the class is
375// <src>WrapperData</src>. The special name is only for the use of
376// cxx2html.
377// </synopsis>
378
379template <class T>
380class WrapperData_VF<T, Vector<T>, T, True, False> : public WrapperBase<T> {
382
383 public:
384 explicit WrapperData_VF(T (*f)(const Vector<T> &), uInt dim = 1) : WrapperBase<T>(dim), pf_p(f) {}
385 virtual ~WrapperData_VF() {}
386 virtual T eval(typename Function<T>::FunctionArg x, const Vector<T> &) const {
387 if (pf_p) {
388 for (uInt i = 0; i < ndim_p; ++i) arg_p[i] = x[i];
389 return pf_p(arg_p);
390 }
391 return T(0);
392 }
393
394 protected:
395 T (*pf_p)(const Vector<T> &);
396
397 private:
398 WrapperData_VF(const myData &other);
399 myData &operator=(const myData &other);
400
401 protected:
402 // # Make members of parent classes known.
403 using WrapperBase<T>::ndim_p;
404 using WrapperBase<T>::arg_p;
405};
406
407#undef WrapperData_VF
408
409#define WrapperData_FF WrapperData
410
411// <summary> Specialization for calls with no arguments and no parameters
412// </summary>
413// <synopsis> Note that the actual name of the class is
414// <src>WrapperData</src>. The special name is only for the use of
415// cxx2html.
416// </synopsis>
417
418template <class T>
419class WrapperData_FF<T, T, T, False, False> : public WrapperBase<T> {
421
422 public:
423 explicit WrapperData_FF(T (*f)()) : WrapperBase<T>(0), pf_p(f) {}
424 virtual ~WrapperData_FF() {}
425 virtual T eval(typename Function<T>::FunctionArg, const Vector<T> &) const {
426 if (pf_p) return pf_p();
427 return T(0);
428 }
429
430 protected:
431 T (*pf_p)();
432
433 private:
434 WrapperData_FF(const myData &other);
435 myData &operator=(const myData &other);
436
437 protected:
438 // # Make members of parent classes known.
439 using WrapperBase<T>::ndim_p;
440 using WrapperBase<T>::arg_p;
441};
442
443#undef WrapperData_FF
444
445} // namespace casacore
446
447#endif
const ArgType * FunctionArg
Definition Function.h:204
WrapperBase()
Default constructor: zero dimension.
Definition WrapperBase.h:69
Vector< T > arg_p
Vector argument interface.
Definition WrapperBase.h:91
uInt ndim_p
Dimensionality.
Definition WrapperBase.h:89
virtual T eval(typename Function< T >::FunctionArg, const Vector< T > &) const
Evaluate the function at x.
WrapperData_FF< T, T, T, True, False > myData
virtual T eval(typename Function< T >::FunctionArg, const Vector< T > &par) const
Evaluate the function at x.
WrapperData_FT< T, T, T, False, True > myData
WrapperData_FV< T, T, Vector< T >, False, True > myData
virtual T eval(typename Function< T >::FunctionArg, const Vector< T > &par) const
Evaluate the function at x.
WrapperData_TF(T(*f)(const T &), uInt dim=1)
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &) const
Evaluate the function at x.
WrapperData_TF< T, T, T, True, False > myData
myData & operator=(const myData &other)
WrapperData_TT< T, T, T, True, True > myData
Definition WrapperData.h:99
WrapperData_TT(T(*f)(const T &, const T &), uInt dim=1)
Standard constructor.
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &par) const
Evaluate the function at x.
T(* pf_p)(const T &, const T &)
Function to call.
WrapperData_TT(const myData &other)
Copy constructor and assignment (not implemented).
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &par) const
Evaluate the function at x.
WrapperData_TV(T(*f)(const T &, const Vector< T > &), uInt dim=1)
WrapperData_TV< T, T, Vector< T >, True, True > myData
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &) const
Evaluate the function at x.
WrapperData_VF< T, Vector< T >, T, True, False > myData
WrapperData_VF(T(*f)(const Vector< T > &), uInt dim=1)
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &par) const
Evaluate the function at x.
WrapperData_VT(T(*f)(const Vector< T > &, const T &), uInt dim=1)
WrapperData_VT< T, Vector< T >, T, True, True > myData
virtual T eval(typename Function< T >::FunctionArg x, const Vector< T > &par) const
Evaluate the function at x.
WrapperData_VV< T, Vector< T >, Vector< T >, True, True > myData
T(* pf_p)(const Vector< T > &, const Vector< T > &)
WrapperData_VV(T(*f)(const Vector< T > &, const Vector< T > &), uInt dim=1)
virtual T eval(typename Function< T >::FunctionArg, const V &) const
Evaluate the function at x.
Definition WrapperData.h:77
WrapperData()
Default constructor: to allow arrays of functions.
virtual ~WrapperData()
Destructor.
Definition WrapperData.h:72
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
const Bool True
Definition aipstype.h:41