casa
$Rev:20696$
|
00001 //# WrapperData.h: Aid in constructing function objects from C++ functions 00002 //# Copyright (C) 2001,2002 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: WrapperData.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00027 00028 #ifndef SCIMATH_WRAPPERDATA_H 00029 #define SCIMATH_WRAPPERDATA_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <scimath/Functionals/WrapperBase.h> 00034 00035 namespace casa { //# NAMESPACE CASA - BEGIN 00036 00037 //# Forward declarations 00038 00039 // <summary> Aid in constructing function objects from C++ functions 00040 // </summary> 00041 // 00042 // <use visibility=local> 00043 // 00044 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""> 00045 // </reviewed> 00046 // 00047 // <prerequisite> 00048 // <li> <linkto class="Function">Function</linkto> class 00049 // <li> <linkto class="FunctionParam">FunctionParam</linkto> 00050 // </prerequisite> 00051 // 00052 // <synopsis> 00053 // This class is provided to enable compile time selection of the 00054 // appropriate function call. Each template incarnation represent a 00055 // function call interface definition. 00056 // </synopsis> 00057 // 00058 // <example> 00059 // <srcblock> 00060 // Float func(const Vector<Float>& x) {return x(0)*x(1);} // x*y 00061 // // Convert C++ functions to Functionals 00062 // FunctionWrapper<Float> Func(func, 2); 00063 // </srcblock> 00064 // 00065 00066 template <class T, class U, class V, Bool hasX, Bool hasParam> 00067 class WrapperData : public WrapperBase<T> 00068 { 00069 public: 00070 //# Constructors 00071 // Default constructor: to allow arrays of functions 00072 WrapperData(); 00073 00074 // Destructor 00075 virtual ~WrapperData() {} 00076 00077 //# Operators 00078 // Evaluate the function at <src>x</src>. 00079 // <group> 00080 virtual T eval(typename Function<T>::FunctionArg x, const V &par) const {} 00081 // </group> 00082 00083 //# Member functions 00084 00085 protected: 00086 //# Make members of parent classes known. 00087 using WrapperBase<T>::ndim_p; 00088 using WrapperBase<T>::arg_p; 00089 }; 00090 00091 00092 #define WrapperData_TT WrapperData 00093 00094 // <summary> Specialization for calls with argument and parameter 00095 // </summary> 00096 // <synopsis> Note that the actual name of the class is 00097 // <src>WrapperData</src>. The special name is only for the use of 00098 // cxx2html. 00099 // </synopsis> 00100 00101 template <class T> 00102 class WrapperData_TT<T,T,T,True,True> : public WrapperBase<T> 00103 { 00104 typedef WrapperData_TT<T,T,T,True,True> myData; 00105 00106 public: 00107 //# Constructors 00108 // Standard constructor 00109 explicit WrapperData_TT(T(*f)(const T&, const T&), uInt dim=1) : 00110 WrapperBase<T>(dim), pf_p(f) {} 00111 00112 // Destructor 00113 virtual ~WrapperData_TT() {} 00114 00115 //# Operators 00116 // Evaluate the function at <src>x</src>. 00117 virtual T eval(typename Function<T>::FunctionArg x, 00118 const Vector<T> &par) const { 00119 if (pf_p) { 00120 return 00121 pf_p((*static_cast<const typename Function<T>::FunctionArg>(x)), 00122 par[0]); 00123 } 00124 return T(0); } 00125 00126 //# Member functions 00127 00128 protected: 00129 //# Data 00130 // Function to call 00131 T (*pf_p)(const T&, const T&); 00132 00133 private: 00134 // Copy constructor and assignment (not implemented) 00135 // <group> 00136 WrapperData_TT(const myData &other); 00137 myData &operator=(const myData &other); 00138 // </group> 00139 00140 protected: 00141 //# Make members of parent classes known. 00142 using WrapperBase<T>::ndim_p; 00143 using WrapperBase<T>::arg_p; 00144 }; 00145 00146 #undef WrapperData_TT 00147 00148 00149 #define WrapperData_VT WrapperData 00150 00151 // <summary> Specialization for calls with argument and parameter 00152 // </summary> 00153 // <synopsis> Note that the actual name of the class is 00154 // <src>WrapperData</src>. The special name is only for the use of 00155 // cxx2html. 00156 // </synopsis> 00157 00158 template <class T> 00159 class WrapperData_VT<T,Vector<T>,T,True,True> : public WrapperBase<T> 00160 { 00161 typedef WrapperData_VT<T,Vector<T>,T,True,True> myData; 00162 00163 public: 00164 explicit WrapperData_VT(T(*f)(const Vector<T>&, const T&), uInt dim=1) : 00165 WrapperBase<T>(dim), pf_p(f) {} 00166 virtual ~WrapperData_VT() {} 00167 virtual T eval(typename Function<T>::FunctionArg x, 00168 const Vector<T> &par) const { 00169 if (pf_p) { 00170 for (uInt i=0; i<ndim_p; ++i) arg_p[i] = x[i]; 00171 return pf_p(arg_p, par[0]); } 00172 return T(0); } 00173 00174 protected: 00175 T (*pf_p)(const Vector<T>&, const T&); 00176 00177 private: 00178 WrapperData_VT(const myData &other); 00179 myData &operator=(const myData &other); 00180 00181 protected: 00182 //# Make members of parent classes known. 00183 using WrapperBase<T>::ndim_p; 00184 using WrapperBase<T>::arg_p; 00185 }; 00186 00187 #undef WrapperData_VT 00188 00189 00190 #define WrapperData_TV WrapperData 00191 00192 // <summary> Specialization for calls with argument and parameters 00193 // </summary> 00194 // <synopsis> Note that the actual name of the class is 00195 // <src>WrapperData</src>. The special name is only for the use of 00196 // cxx2html. 00197 // </synopsis> 00198 00199 template <class T> 00200 class WrapperData_TV<T,T,Vector<T>,True,True> : public WrapperBase<T> 00201 { 00202 typedef WrapperData_TV<T,T,Vector<T>,True,True> myData; 00203 00204 public: 00205 explicit WrapperData_TV(T(*f)(const T&, const Vector<T>&), uInt dim=1) : 00206 WrapperBase<T>(dim), pf_p(f) {} 00207 virtual ~WrapperData_TV() {} 00208 virtual T eval(typename Function<T>::FunctionArg x, 00209 const Vector<T> &par) const { 00210 if (pf_p) { 00211 return pf_p((*static_cast<const typename Function<T>::FunctionArg>(x)), 00212 par); 00213 } 00214 return T(0); } 00215 00216 protected: 00217 T (*pf_p)(const T&, const Vector<T>&); 00218 00219 private: 00220 WrapperData_TV(const myData &other); 00221 myData &operator=(const myData &other); 00222 00223 protected: 00224 //# Make members of parent classes known. 00225 using WrapperBase<T>::ndim_p; 00226 using WrapperBase<T>::arg_p; 00227 }; 00228 00229 #undef WrapperData_TV 00230 00231 00232 #define WrapperData_VV WrapperData 00233 00234 // <summary> Specialization for calls with argument and parameters 00235 // </summary> 00236 // <synopsis> Note that the actual name of the class is 00237 // <src>WrapperData</src>. The special name is only for the use of 00238 // cxx2html. 00239 // </synopsis> 00240 00241 template <class T> 00242 class WrapperData_VV<T,Vector<T>,Vector<T>,True,True> : 00243 public WrapperBase<T> 00244 { 00245 typedef WrapperData_VV<T,Vector<T>,Vector<T>,True,True> myData; 00246 00247 public: 00248 explicit WrapperData_VV(T(*f)(const Vector<T>&, const Vector<T>&), 00249 uInt dim=1) : 00250 WrapperBase<T>(dim), pf_p(f) {} 00251 virtual ~WrapperData_VV() {} 00252 virtual T eval(typename Function<T>::FunctionArg x, 00253 const Vector<T> &par) const { 00254 if (pf_p) { 00255 for (uInt i=0; i<ndim_p; ++i) arg_p[i] = x[i]; 00256 return pf_p(arg_p, par); } 00257 return T(0); } 00258 00259 protected: 00260 T (*pf_p)(const Vector<T>&, const Vector<T>&); 00261 00262 private: 00263 WrapperData_VV(const myData &other); 00264 myData &operator=(const myData &other); 00265 00266 protected: 00267 //# Make members of parent classes known. 00268 using WrapperBase<T>::ndim_p; 00269 using WrapperBase<T>::arg_p; 00270 }; 00271 00272 #undef WrapperData_VV 00273 00274 00275 #define WrapperData_FT WrapperData 00276 00277 // <summary> Specialization for calls with no arguments and parameter 00278 // </summary> 00279 // <synopsis> Note that the actual name of the class is 00280 // <src>WrapperData</src>. The special name is only for the use of 00281 // cxx2html. 00282 // </synopsis> 00283 00284 template <class T> 00285 class WrapperData_FT<T,T,T,False,True> : public WrapperBase<T> 00286 { 00287 typedef WrapperData_FT<T,T,T,False,True> myData; 00288 00289 public: 00290 explicit WrapperData_FT(T(*f)(const T&)) : 00291 WrapperBase<T>(0), pf_p(f) {} 00292 virtual ~WrapperData_FT() {} 00293 virtual T eval(typename Function<T>::FunctionArg x, 00294 const Vector<T> &par) const { 00295 if (pf_p) return pf_p(par[0]); 00296 return T(0); } 00297 00298 protected: 00299 T (*pf_p)(const T&); 00300 00301 private: 00302 WrapperData_FT(const myData &other); 00303 myData &operator=(const myData &other); 00304 00305 protected: 00306 //# Make members of parent classes known. 00307 using WrapperBase<T>::ndim_p; 00308 using WrapperBase<T>::arg_p; 00309 }; 00310 00311 #undef WrapperData_FT 00312 00313 00314 #define WrapperData_FV WrapperData 00315 00316 // <summary> Specialization for calls with no arguments and parameters 00317 // </summary> 00318 // <synopsis> Note that the actual name of the class is 00319 // <src>WrapperData</src>. The special name is only for the use of 00320 // cxx2html. 00321 // </synopsis> 00322 00323 template <class T> 00324 class WrapperData_FV<T,T,Vector<T>,False,True> : public WrapperBase<T> 00325 { 00326 typedef WrapperData_FV<T,T,Vector<T>,False,True> myData; 00327 00328 public: 00329 explicit WrapperData_FV(T(*f)(const Vector<T>&)) : 00330 WrapperBase<T>(0), pf_p(f) {} 00331 virtual ~WrapperData_FV() {} 00332 virtual T eval(typename Function<T>::FunctionArg x, 00333 const Vector<T> &par) const { 00334 if (pf_p) return pf_p(par); 00335 return T(0); } 00336 00337 protected: 00338 T (*pf_p)(const Vector<T>&); 00339 00340 private: 00341 WrapperData_FV(const myData &other); 00342 myData &operator=(const myData &other); 00343 00344 protected: 00345 //# Make members of parent classes known. 00346 using WrapperBase<T>::ndim_p; 00347 using WrapperBase<T>::arg_p; 00348 }; 00349 00350 #undef WrapperData_FV 00351 00352 00353 #define WrapperData_TF WrapperData 00354 00355 // <summary> Specialization for calls with argument and no parameters 00356 // </summary> 00357 // <synopsis> Note that the actual name of the class is 00358 // <src>WrapperData</src>. The special name is only for the use of 00359 // cxx2html. 00360 // </synopsis> 00361 00362 template <class T> 00363 class WrapperData_TF<T,T,T,True,False> : public WrapperBase<T> 00364 { 00365 typedef WrapperData_TF<T,T,T,True,False> myData; 00366 00367 public: 00368 explicit WrapperData_TF(T(*f)(const T&), uInt dim=1) : 00369 WrapperBase<T>(dim), pf_p(f) {} 00370 virtual ~WrapperData_TF() {} 00371 virtual T eval(typename Function<T>::FunctionArg x, 00372 const Vector<T> &par) const { 00373 if (pf_p) { 00374 return pf_p((*static_cast<const typename Function<T>::FunctionArg>(x))); 00375 } 00376 return T(0); } 00377 00378 protected: 00379 T (*pf_p)(const T&); 00380 00381 private: 00382 WrapperData_TF(const myData &other); 00383 myData &operator=(const myData &other); 00384 00385 protected: 00386 //# Make members of parent classes known. 00387 using WrapperBase<T>::ndim_p; 00388 using WrapperBase<T>::arg_p; 00389 }; 00390 00391 #undef WrapperData_TF 00392 00393 00394 #define WrapperData_VF WrapperData 00395 00396 // <summary> Specialization for calls with argument and no parameters 00397 // </summary> 00398 // <synopsis> Note that the actual name of the class is 00399 // <src>WrapperData</src>. The special name is only for the use of 00400 // cxx2html. 00401 // </synopsis> 00402 00403 template <class T> 00404 class WrapperData_VF<T,Vector<T>,T,True,False> : public WrapperBase<T> 00405 { 00406 typedef WrapperData_VF<T,Vector<T>,T,True,False> myData; 00407 00408 public: 00409 explicit WrapperData_VF(T(*f)(const Vector<T>&), uInt dim=1) : 00410 WrapperBase<T>(dim), pf_p(f) {} 00411 virtual ~WrapperData_VF() {} 00412 virtual T eval(typename Function<T>::FunctionArg x, 00413 const Vector<T> &) const { 00414 if (pf_p) { 00415 for (uInt i=0; i<ndim_p; ++i) arg_p[i] = x[i]; 00416 return pf_p(arg_p); } 00417 return T(0); } 00418 00419 protected: 00420 T (*pf_p)(const Vector<T>&); 00421 00422 private: 00423 WrapperData_VF(const myData &other); 00424 myData &operator=(const myData &other); 00425 00426 protected: 00427 //# Make members of parent classes known. 00428 using WrapperBase<T>::ndim_p; 00429 using WrapperBase<T>::arg_p; 00430 }; 00431 00432 #undef WrapperData_VF 00433 00434 00435 #define WrapperData_FF WrapperData 00436 00437 // <summary> Specialization for calls with no arguments and no parameters 00438 // </summary> 00439 // <synopsis> Note that the actual name of the class is 00440 // <src>WrapperData</src>. The special name is only for the use of 00441 // cxx2html. 00442 // </synopsis> 00443 00444 template <class T> 00445 class WrapperData_FF<T,T,T,False,False> : public WrapperBase<T> 00446 { 00447 typedef WrapperData_FF<T,T,T,True,False> myData; 00448 00449 public: 00450 explicit WrapperData_FF(T(*f)()) : 00451 WrapperBase<T>(0), pf_p(f) {} 00452 virtual ~WrapperData_FF() {} 00453 virtual T eval(typename Function<T>::FunctionArg x, 00454 const Vector<T> &par) const { 00455 if (pf_p) return pf_p(); 00456 return T(0); } 00457 00458 protected: 00459 T (*pf_p)(); 00460 00461 private: 00462 WrapperData_FF(const myData &other); 00463 myData &operator=(const myData &other); 00464 00465 protected: 00466 //# Make members of parent classes known. 00467 using WrapperBase<T>::ndim_p; 00468 using WrapperBase<T>::arg_p; 00469 }; 00470 00471 #undef WrapperData_FF 00472 00473 } //# NAMESPACE CASA - END 00474 00475 #endif