00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_FUNCT_H 00024 #define O2SCL_FUNCT_H 00025 00026 #include <string> 00027 #include <o2scl/collection.h> 00028 00029 #ifndef DOXYGENP 00030 namespace o2scl { 00031 #endif 00032 00033 /** 00034 \brief One-dimensional function [abstract base] 00035 00036 This class generalizes a function y(x). 00037 00038 */ 00039 template<class param_t> 00040 class funct { 00041 00042 public: 00043 00044 funct() {} 00045 00046 virtual ~funct() {} 00047 00048 /** \brief The overloaded operator() 00049 */ 00050 virtual int operator()(double x, double &y, param_t &pa)=0; 00051 00052 /** \brief The overloaded operator() 00053 */ 00054 virtual double operator()(double x, param_t &pa) { 00055 double y; 00056 operator()(x,y,pa); 00057 return y; 00058 } 00059 00060 #ifndef DOXYGENP 00061 00062 private: 00063 00064 funct(const funct &); 00065 00066 funct& operator=(const funct&); 00067 00068 #endif 00069 00070 }; 00071 00072 /** \brief Function pointer to a function 00073 */ 00074 template<class param_t> 00075 class funct_fptr : public funct<param_t> { 00076 public: 00077 00078 /** \brief Specify the function pointer 00079 */ 00080 funct_fptr(int (*fp)(double x, double &y, param_t &pa)) { 00081 fptr=fp; 00082 } 00083 00084 virtual ~funct_fptr() {} 00085 00086 virtual int operator()(double x, double &y, param_t &pa) { 00087 return fptr(x,y,pa); 00088 } 00089 00090 // Unfortunately, if we overload operator()(double,double,void*), 00091 // we have to reimplement all operator()'s 00092 virtual double operator()(double x, param_t &pa) { 00093 double y; 00094 operator()(x,y,pa); 00095 return y; 00096 } 00097 00098 #ifndef DOXYGEN_INTERNAL 00099 00100 protected: 00101 00102 friend class io_tlate<funct_fptr>; 00103 00104 funct_fptr() {}; 00105 00106 /// Storage for the function pointer 00107 int (*fptr)(double x, double &y, param_t &pa); 00108 00109 #endif 00110 #ifndef DOXYGENP 00111 00112 private: 00113 00114 funct_fptr(const funct_fptr &); 00115 funct_fptr& operator=(const funct_fptr&); 00116 00117 #endif 00118 00119 }; 00120 00121 /** \brief Function pointer to a function 00122 */ 00123 template<class param_t> 00124 class funct_fptr_noerr : public funct<param_t> { 00125 public: 00126 00127 /** \brief Specify the function pointer 00128 */ 00129 funct_fptr_noerr(double (*fp)(double x, param_t &pa)) { 00130 fptr=fp; 00131 } 00132 00133 virtual ~funct_fptr_noerr() {} 00134 00135 virtual int operator()(double x, double &y, param_t &pa) { 00136 y=fptr(x,pa); 00137 return 0; 00138 } 00139 00140 // Unfortunately, if we overload operator()(double,double,void*), 00141 // we have to reimplement all operator()'s 00142 virtual double operator()(double x, param_t &pa) { 00143 double y; 00144 operator()(x,y,pa); 00145 return y; 00146 } 00147 00148 #ifndef DOXYGEN_INTERNAL 00149 00150 protected: 00151 00152 /// Storage for the function pointer 00153 double (*fptr)(double x, param_t &pa); 00154 00155 #endif 00156 #ifndef DOXYGENP 00157 00158 private: 00159 00160 funct_fptr_noerr(const funct_fptr_noerr &); 00161 funct_fptr_noerr& operator=(const funct_fptr_noerr&); 00162 00163 #endif 00164 00165 }; 00166 00167 /** \brief Function pointer to a function 00168 */ 00169 template<class param_t> 00170 class funct_fptr_nopar : public funct<param_t> { 00171 public: 00172 00173 /** \brief Specify the function pointer 00174 */ 00175 funct_fptr_nopar(double (*fp)(double x)) { 00176 fptr=fp; 00177 } 00178 00179 virtual ~funct_fptr_nopar() {} 00180 00181 virtual int operator()(double x, double &y, param_t &pa) { 00182 y=fptr(x); 00183 return 0; 00184 } 00185 00186 // Unfortunately, if we overload operator()(double,double,void*), 00187 // we have to reimplement all operator()'s 00188 virtual double operator()(double x, param_t &pa) { 00189 double y; 00190 operator()(x,y,pa); 00191 return y; 00192 } 00193 00194 #ifndef DOXYGEN_INTERNAL 00195 00196 protected: 00197 00198 /// Storage for the function pointer 00199 double (*fptr)(double x); 00200 00201 #endif 00202 #ifndef DOXYGENP 00203 00204 private: 00205 00206 funct_fptr_nopar(const funct_fptr_nopar &); 00207 funct_fptr_nopar& operator=(const funct_fptr_nopar&); 00208 00209 #endif 00210 00211 }; 00212 00213 /** 00214 \brief Member function pointer to a one-dimensional function 00215 */ 00216 template <class tclass, class param_t> class funct_mfptr : 00217 public funct<param_t> { 00218 public: 00219 00220 /** \brief Specify the member function pointer 00221 */ 00222 funct_mfptr(tclass *tp, int (tclass::*fp)(double x, double &y, 00223 param_t &pa)) { 00224 tptr=tp; 00225 fptr=fp; 00226 } 00227 00228 virtual ~funct_mfptr() {}; 00229 00230 virtual int operator()(double x, double &y, param_t &pa) { 00231 return (*tptr.*fptr)(x,y,pa); 00232 } 00233 00234 // Unfortunately, if we overload operator()(double,double,void*), 00235 // we have to reimplement all operator()'s 00236 virtual double operator()(double x, param_t &pa) { 00237 double y; 00238 operator()(x,y,pa); 00239 return y; 00240 } 00241 00242 #ifndef DOXYGEN_INTERNAL 00243 00244 protected: 00245 00246 /// Storage for the member function pointer 00247 int (tclass::*fptr)(double x, double &y, param_t &pa); 00248 00249 /// Store the pointer to the class instance 00250 tclass *tptr; 00251 00252 #endif 00253 #ifndef DOXYGENP 00254 00255 private: 00256 00257 funct_mfptr(const funct_mfptr &); 00258 funct_mfptr& operator=(const funct_mfptr&); 00259 00260 #endif 00261 00262 }; 00263 00264 /** \brief Member function pointer to a one-dimensional function 00265 */ 00266 template <class tclass, class param_t> class funct_mfptr_noerr : 00267 public funct<param_t> { 00268 public: 00269 00270 /** \brief Specify the member function pointer 00271 */ 00272 funct_mfptr_noerr(tclass *tp, 00273 double (tclass::*fp)(double x, param_t &pa)) { 00274 tptr=tp; 00275 fptr=fp; 00276 } 00277 00278 virtual ~funct_mfptr_noerr() {}; 00279 00280 virtual int operator()(double x, double &y, param_t &pa) { 00281 y=(*tptr.*fptr)(x,pa); 00282 return 0; 00283 } 00284 00285 // Unfortunately, if we overload operator()(double,double,void*), 00286 // we have to reimplement all operator()'s 00287 virtual double operator()(double x, param_t &pa) { 00288 double y; 00289 operator()(x,y,pa); 00290 return y; 00291 } 00292 00293 #ifndef DOXYGEN_INTERNAL 00294 00295 protected: 00296 00297 /// Storage for the member function pointer 00298 double (tclass::*fptr)(double x, param_t &pa); 00299 00300 /// Store the pointer to the class instance 00301 tclass *tptr; 00302 00303 #endif 00304 #ifndef DOXYGENP 00305 00306 private: 00307 00308 funct_mfptr_noerr(const funct_mfptr_noerr &); 00309 funct_mfptr_noerr& operator=(const funct_mfptr_noerr&); 00310 00311 #endif 00312 00313 }; 00314 00315 /** \brief Member function pointer to a one-dimensional function 00316 */ 00317 template <class tclass, class param_t> class funct_mfptr_nopar : 00318 public funct<param_t> { 00319 public: 00320 00321 /** \brief Specify the member function pointer 00322 */ 00323 funct_mfptr_nopar(tclass *tp, double (tclass::*fp)(double x)) { 00324 tptr=tp; 00325 fptr=fp; 00326 } 00327 00328 virtual ~funct_mfptr_nopar() {}; 00329 00330 virtual int operator()(double x, double &y, param_t &pa) { 00331 y=(*tptr.*fptr)(x); 00332 return 0; 00333 } 00334 00335 // Unfortunately, if we overload operator()(double,double,void*), 00336 // we have to reimplement all operator()'s 00337 virtual double operator()(double x, param_t &pa) { 00338 double y; 00339 operator()(x,y,pa); 00340 return y; 00341 } 00342 00343 #ifndef DOXYGEN_INTERNAL 00344 00345 protected: 00346 00347 /// Storage for the member function pointer 00348 double (tclass::*fptr)(double x); 00349 /// Store the pointer to the class instance 00350 tclass *tptr; 00351 00352 #endif 00353 #ifndef DOXYGENP 00354 00355 private: 00356 00357 funct_mfptr_nopar(const funct_mfptr_nopar &); 00358 funct_mfptr_nopar& operator=(const funct_mfptr_nopar&); 00359 00360 #endif 00361 00362 }; 00363 00364 #ifndef DOXYGENP 00365 } 00366 #endif 00367 00368 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page