functor.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_functor_h)
00024 #define octave_functor_h 1
00025 
00026 template <typename RT, typename PT>
00027 class fcn_ptr
00028 {
00029 public:
00030   typedef RT (*TYPE) (PT);
00031 };
00032 
00033 template <typename RT, typename PT>
00034 class functor
00035 {
00036 private:
00037   typedef typename fcn_ptr<RT, PT>::TYPE fcn_ptr_type;
00038   fcn_ptr_type fptr;
00039 
00040 public:
00041 
00042   functor (fcn_ptr_type p) : fptr (p) { }
00043 
00044   RT operator () (PT arg) { return fptr (arg); }
00045 };
00046 
00047 template <typename CT, typename RT, typename PT>
00048 class functor_with_conversion
00049 {
00050 private:
00051   typedef typename fcn_ptr<RT, PT>::TYPE fcn_ptr_type;
00052   fcn_ptr_type fptr;
00053 
00054 public:
00055 
00056   functor_with_conversion (fcn_ptr_type p) : fptr (p) { }
00057 
00058   CT operator () (PT arg) { return CT (fptr (arg)); }
00059 };
00060 
00061 template <typename RT, typename PT>
00062 functor<RT, PT>
00063 func_ptr (RT (*f) (PT))
00064 {
00065   return functor<RT, PT> (f);
00066 }
00067 
00068 template <typename CT, typename RT, typename PT>
00069 functor_with_conversion<CT, RT, PT>
00070 func_ptr_with_conversion (RT (*f) (PT))
00071 {
00072   return functor_with_conversion<CT, RT, PT> (f);
00073 }
00074 
00075 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines