// rTorrent - BitTorrent client // Copyright (C) 2005, Jari Sundell // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // In addition, as a special exception, the copyright holders give // permission to link the code of portions of this program with the // OpenSSL library under certain conditions as described in each // individual source file, and distribute linked combinations // including the two. // // You must obey the GNU General Public License in all respects for // all of the code used other than OpenSSL. If you modify file(s) // with this exception, you may extend this exception to your version // of the file(s), but you are not obligated to do so. If you do not // wish to do so, delete this exception statement from your version. // If you delete this exception statement from all source files in the // program, then also delete it here. // // Contact: Jari Sundell // // Skomakerveien 33 // 3185 Skoppum, NORWAY #ifndef RTORRENT_UTILS_FUNCTIONAL_H #define RTORRENT_UTILS_FUNCTIONAL_H #include namespace rak { template struct reference_fix { typedef Type type; }; template struct reference_fix { typedef Type type; }; template struct _accumulate { _accumulate(Type& t, Ftor f) : m_t(t), m_f(f) {} template void operator () (Arg& a) { m_t += m_f(a); } Type& m_t; Ftor m_f; }; template inline _accumulate accumulate(Type& t, Ftor f) { return _accumulate(t, f); } // Operators: template struct _equal { _equal(Type t, Ftor f) : m_t(t), m_f(f) {} template bool operator () (Arg& a) { return m_t == m_f(a); } Type m_t; Ftor m_f; }; template inline _equal equal(Type t, Ftor f) { return _equal(t, f); } template struct _less { _less(Type t, Ftor f) : m_t(t), m_f(f) {} template bool operator () (Arg& a) { return m_t < m_f(a); } Type m_t; Ftor m_f; }; template inline _less less(Type t, Ftor f) { return _less(t, f); } template struct _greater { _greater(Type t, Ftor f) : m_t(t), m_f(f) {} template bool operator () (Arg& a) { return m_t > m_f(a); } Type m_t; Ftor m_f; }; template inline _greater greater(Type t, Ftor f) { return _greater(t, f); } template struct _less_equal { _less_equal(Type t, Ftor f) : m_t(t), m_f(f) {} template bool operator () (Arg& a) { return m_t <= m_f(a); } Type m_t; Ftor m_f; }; template inline _less_equal less_equal(Type t, Ftor f) { return _less_equal(t, f); } template struct _greater_equal { _greater_equal(Type t, Ftor f) : m_t(t), m_f(f) {} template bool operator () (Arg& a) { return m_t >= m_f(a); } Type m_t; Ftor m_f; }; template inline _greater_equal greater_equal(Type t, Ftor f) { return _greater_equal(t, f); } template struct _on : public std::unary_function { _on(Src s, Dest d) : m_dest(d), m_src(s) {} typename Dest::result_type operator () (typename reference_fix::type arg) { return m_dest(m_src(arg)); } Dest m_dest; Src m_src; }; template inline _on on(Src s, Dest d) { return _on(s, d); } // Creates a functor for accessing a member. template struct _mem_ptr_ref : public std::unary_function { _mem_ptr_ref(Member Class::*m) : m_member(m) {} Member& operator () (Class& c) { return c.*m_member; } Member Class::*m_member; }; template inline _mem_ptr_ref mem_ptr_ref(Member Class::*m) { return _mem_ptr_ref(m); } template struct _if_then { _if_then(Cond c, Then t) : m_cond(c), m_then(t) {} template void operator () (Arg& a) { if (m_cond(a)) m_then(a); } Cond m_cond; Then m_then; }; template inline _if_then if_then(Cond c, Then t) { return _if_then(c, t); } template struct call_delete : public std::unary_function { void operator () (T* t) { delete t; } }; template inline void call_delete_func(T* t) { delete t; } template class bind1st_t : public std::unary_function { protected: typedef typename reference_fix::type value_type; typedef typename reference_fix::type argument_type; Operation m_op; value_type m_value; public: bind1st_t(const Operation& op, const value_type v) : m_op(op), m_value(v) {} typename Operation::result_type operator () (const argument_type arg) { return m_op(m_value, arg); } }; template inline bind1st_t bind1st(const Operation& op, const Type& val) { return bind1st_t(op, val); } } #endif