WrappedFunction.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: WrappedFunction.h 36369 2010-10-19 16:14:31Z moneta $
00002 // Authors: L. Moneta, A. Zsenei   08/2005 
00003 
00004  /**********************************************************************
00005   *                                                                    *
00006   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
00007   *                                                                    *
00008   * This library is free software; you can redistribute it and/or      *
00009   * modify it under the terms of the GNU General Public License        *
00010   * as published by the Free Software Foundation; either version 2     *
00011   * of the License, or (at your option) any later version.             *
00012   *                                                                    *
00013   * This library 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 GNU   *
00016   * General Public License for more details.                           *
00017   *                                                                    *
00018   * You should have received a copy of the GNU General Public License  *
00019   * along with this library (see file COPYING); if not, write          *
00020   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022   *                                                                    *
00023   **********************************************************************/
00024 
00025 #ifndef ROOT_Math_WrappedFunction
00026 #define ROOT_Math_WrappedFunction
00027 
00028 #ifndef ROOT_Math_IFunction
00029 #include "IFunction.h"
00030 #endif
00031 
00032 
00033 namespace ROOT {
00034 namespace Math {
00035 
00036 
00037 
00038 
00039 struct NullTypeFunc1D {}; 
00040 
00041 typedef double(*FreeFunctionPtr)(double);
00042 
00043 typedef double(*FreeMultiFunctionPtr)(const double*); 
00044 
00045 /**
00046    Template class to wrap any C++ callable object which takes one argument 
00047    i.e. implementing operator() (double x) in a One-dimensional function interface.
00048    It provides a ROOT::Math::IGenFunction-like signature
00049 
00050    Note: If you want to wrap just the reference (to avoid copying) you need to use 
00051    Func& or const Func & as template parameter.  The former should be used when the 
00052    operator() is not a const method of Func
00053 
00054    @ingroup  GenFunc
00055 
00056  */
00057 template< typename Func =  FreeFunctionPtr   >
00058 class WrappedFunction : public IGenFunction {
00059 
00060 
00061  public:
00062 
00063    /**
00064       construct from the pointer to the object and the member function
00065     */
00066    WrappedFunction( Func f ) : 
00067       fFunc( f ) 
00068    { /* no op */ }
00069 
00070    // use default  copy contructor and assignment operator
00071 
00072    /// clone (required by the interface)
00073    WrappedFunction * Clone() const {
00074       return new WrappedFunction(fFunc);
00075    }
00076    
00077    //  virtual ~WrappedFunction() { /**/ }
00078 
00079 private:
00080 
00081    virtual double DoEval (double x) const {
00082       return fFunc( x );
00083    }
00084 
00085 
00086    Func fFunc; 
00087 
00088 
00089 }; // WrappedFunction
00090 
00091 
00092 /**
00093    Template class to wrap any member function of a class 
00094    taking a double and returning a double in a 1D function interface
00095    For example, if you have a class like: 
00096    struct X { 
00097        double Eval(double x); 
00098    };
00099    you can wrapped in the following way: 
00100    WrappedMemFunction<X, double ( X::* ) (double) > f; 
00101 
00102 
00103    @ingroup  GenFunc
00104 
00105  */
00106 
00107 template<typename FuncObj, typename MemFuncPtr >
00108 class WrappedMemFunction : public IGenFunction {
00109 
00110 
00111  public:
00112 
00113    /**
00114       construct from the pointer to the object and the member function
00115     */
00116    WrappedMemFunction( FuncObj & obj, MemFuncPtr memFn ) : 
00117       fObj(&obj), 
00118       fMemFunc( memFn ) 
00119    { /* no op */ }
00120 
00121    // use default  copy contructor and assignment operator
00122 
00123    /// clone (required by the interface)
00124    WrappedMemFunction * Clone() const {
00125       return new WrappedMemFunction(*fObj,fMemFunc);
00126    }
00127    
00128 
00129 private:
00130 
00131    virtual double DoEval (double x) const {
00132       return ((*fObj).*fMemFunc)( x );
00133    }
00134 
00135 
00136    FuncObj * fObj; 
00137    MemFuncPtr fMemFunc;
00138 
00139 
00140 }; // WrappedMemFunction
00141 
00142 
00143 /**
00144    Template class to wrap any C++ callable object 
00145    implementing operator() (const double * x) in a multi-dimensional function interface.
00146    It provides a ROOT::Math::IGenMultiFunction-like signature
00147 
00148    Note: If you want to wrap just the reference (to avoid copying) you need to use 
00149    Func& or const Func & as template parameter. The former should be used when the 
00150    operator() is not a const method of Func
00151 
00152    @ingroup  GenFunc
00153 
00154  */
00155 template< typename Func =  FreeMultiFunctionPtr   >
00156 class WrappedMultiFunction : public IMultiGenFunction {
00157 
00158 
00159  public:
00160 
00161    /**
00162       construct from the pointer to the object and the member function
00163     */
00164    WrappedMultiFunction( Func f , unsigned int dim = 1) : 
00165       fFunc( f ), 
00166       fDim( dim)
00167    { /* no op */ }
00168 
00169    // use default  copy contructor and assignment operator
00170 
00171    /// clone (required by the interface)
00172    WrappedMultiFunction * Clone() const {
00173       return new WrappedMultiFunction(fFunc,fDim);
00174    }
00175 
00176    unsigned int NDim() const { return fDim; }
00177    
00178    //  virtual ~WrappedFunction() { /**/ }
00179 
00180 private:
00181 
00182    virtual double DoEval (const double * x) const {
00183       return fFunc( x );
00184    }
00185 
00186 
00187    Func fFunc; 
00188    unsigned int fDim; 
00189 
00190 
00191 }; // WrappedMultiFunction
00192 
00193 
00194 template<typename FuncObj, typename MemFuncPtr >
00195 class WrappedMemMultiFunction : public IMultiGenFunction {
00196 
00197 
00198  public:
00199 
00200    /**
00201       construct from the pointer to the object and the member function
00202     */
00203    WrappedMemMultiFunction( FuncObj & obj, MemFuncPtr memFn, unsigned int dim = 1 ) : 
00204       fObj(&obj), 
00205       fMemFunc( memFn ),
00206       fDim(dim)
00207    { /* no op */ }
00208 
00209    // use default  copy contructor and assignment operator
00210 
00211    /// clone (required by the interface)
00212    WrappedMemMultiFunction * Clone() const {
00213       return new WrappedMemMultiFunction(*fObj,fMemFunc,fDim);
00214    }
00215    
00216 
00217    unsigned int NDim() const { return fDim; }
00218 
00219 private:
00220 
00221    virtual double DoEval (const double * x) const {
00222       return ((*fObj).*fMemFunc)( x );
00223    }
00224 
00225 
00226    FuncObj * fObj; 
00227    MemFuncPtr fMemFunc;
00228    unsigned int fDim;
00229 
00230 
00231 }; // WrappedMemMultiFunction
00232 
00233 
00234 } // namespace Math
00235 } // namespace ROOT
00236 
00237 
00238 
00239 #endif // ROOT_Math_WrappedFunction

Generated on Tue Jul 5 14:25:39 2011 for ROOT_528-00b_version by  doxygen 1.5.1