Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Algo/Bind.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00029 #ifndef SAF_ALGO_BIND_H
00030 #define SAF_ALGO_BIND_H
00031 
00032 namespace Saf
00033 {
00034     namespace Algo
00035     {
00041         template <class Func, class Arg1Type>
00042         class Binder1st
00043             : public UnaryFunction<typename Func::Arg2Type, typename Func::ResType>
00044         {
00045         private:
00047             Func m_func;
00049             Arg1Type m_arg1;
00050 
00051         public:
00053             Binder1st(const Func& func, const Arg1Type& arg1)
00054                 : m_func(func), m_arg1(arg1)
00055             {}
00056 
00058             typename Func::ResType operator()(const typename Func::Arg2Type& arg2) const
00059             {
00060                 return m_func(m_arg1, arg2);
00061             }
00062         };
00063 
00068         template <class Func, class Arg1Type>
00069         inline Binder1st<Func,Arg1Type> Bind1st(const Func& func, const Arg1Type& arg1)
00070         {
00071             return Binder1st<Func,Arg1Type>(func, arg1);
00072         }
00073 
00079         template <class Func, class Arg2Type>
00080         class Binder2nd
00081             : public UnaryFunction<typename Func::Arg1Type, typename Func::ResType>
00082         {
00083         private:
00085             Func m_func;
00087             Arg2Type m_arg2;
00088 
00089         public:
00091             Binder2nd(const Func& func, const Arg2Type& arg2)
00092                 : m_func(func), m_arg2(arg2)
00093             {}
00094 
00096             typename Func::ResType operator()(const typename Func::Arg1Type& arg1) const
00097             {
00098                 return m_func(arg1, m_arg2);
00099             }
00100         };
00101 
00106         template <class Func, class Arg2Type>
00107         inline Binder2nd<Func,Arg2Type> Bind2nd(const Func& func, const Arg2Type& arg2)
00108         {
00109             return Binder2nd<Func,Arg2Type>(func, arg2);
00110         }
00111     }
00112 }
00113 
00114 #endif