/* */ #ifndef _D_A2_FUNCTIONAL_H_ #define _D_A2_FUNCTIONAL_H_ #include #include #include #include "SharedHandle.h" #include "A2STR.h" namespace aria2 { // mem_fun_t for SharedHandle template class mem_fun_sh_t:public std::unary_function< SharedHandle, ReturnType> { private: ReturnType (ClassType::*f)(); public: mem_fun_sh_t(ReturnType (ClassType::*f)()):f(f) {} ReturnType operator()(const SharedHandle& x) const { return (x.get()->*f)(); } }; // const_mem_fun_t for SharedHandle template class const_mem_fun_sh_t:public std::unary_function< SharedHandle, ReturnType> { private: ReturnType (ClassType::*f)() const; public: const_mem_fun_sh_t(ReturnType (ClassType::*f)() const):f(f) {} ReturnType operator()(const SharedHandle& x) const { return (x.get()->*f)(); } }; template mem_fun_sh_t mem_fun_sh(ReturnType (ClassType::*f)()) { return mem_fun_sh_t(f); }; template const_mem_fun_sh_t mem_fun_sh(ReturnType (ClassType::*f)() const) { return const_mem_fun_sh_t(f); }; // mem_fun1_t for SharedHandle template class mem_fun1_sh_t:public std::binary_function, ArgType, ReturnType> { private: ReturnType (ClassType::*f)(ArgType); public: mem_fun1_sh_t(ReturnType (ClassType::*f)(ArgType)):f(f) {} ReturnType operator()(const SharedHandle& x, ArgType a) const { return (x.get()->*f)(a); } }; template mem_fun1_sh_t mem_fun_sh(ReturnType (ClassType::*f)(ArgType)) { return mem_fun1_sh_t(f); }; template class adopt2nd_t:public std::binary_function { private: BinaryOp binaryOp_; UnaryOp unaryOp_; public: adopt2nd_t(const BinaryOp& b, const UnaryOp& u): binaryOp_(b), unaryOp_(u) {} typename BinaryOp::result_type operator()(const typename BinaryOp::first_argument_type& x, const typename UnaryOp::argument_type& y) { return binaryOp_(x, unaryOp_(y)); } }; template inline adopt2nd_t adopt2nd(const BinaryOp& binaryOp, const UnaryOp& unaryOp) { return adopt2nd_t(binaryOp, unaryOp); }; template class Ascend1st:public std::binary_function { public: bool operator()(const Pair& p1, const Pair& p2) const { return p1.first < p2.first; } }; template class select2nd { public: typename Pair::second_type operator()(const Pair& p) const { return p.second; } typename Pair::second_type operator()(Pair& p) const { return p.second; } }; class Deleter { public: template void operator()(T* ptr) { delete ptr; } }; template class Append { private: T& to_; T delim_; public: template Append(T& to, const S& delim):to_(to), delim_(delim) {} template void operator()(const S& s) { to_ += s+delim_; } }; typedef Append StringAppend; template class auto_delete { private: T obj_; void (*deleter_)(T); public: auto_delete(T obj, void (*deleter)(T)):obj_(obj), deleter_(deleter) {} ~auto_delete() { deleter_(obj_); } }; template class auto_delete_container { private: Container* c_; public: auto_delete_container(Container* c):c_(c) {} ~auto_delete_container() { std::for_each(c_->begin(), c_->end(), Deleter()); delete c_; } }; template std::string strjoin(InputIterator first, InputIterator last, const DelimiterType& delim) { std::string result; if(first == last) { return result; } InputIterator beforeLast = last-1; for(; first != beforeLast; ++first) { result += *first; result += delim; } result += *beforeLast; return result; } // Applies unaryOp through first to last and joins the result with // delimiter delim. template std::string strjoin(InputIterator first, InputIterator last, const DelimiterType& delim, const UnaryOp& unaryOp) { std::string result; if(first == last) { return result; } InputIterator beforeLast = last-1; for(; first != beforeLast; ++first) { result += unaryOp(*first); result += delim; } result += unaryOp(*beforeLast); return result; } template inline std::string strconcat(const T1& a1, const T2& a2) { std::string s(a1); s += a2; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3) { std::string s(a1); s += a2; s += a3; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4) { std::string s(a1); s += a2; s += a3; s += a4; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; s += a8; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; s += a8; s += a9; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9, const T10& a10) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; s += a8; s += a9; s += a10; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9, const T10& a10, const T11& a11) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; s += a8; s += a9; s += a10; s += a11; return s; } template inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9, const T10& a10, const T11& a11, const T12& a12) { std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7; s += a8; s += a9; s += a10; s += a11; s += a12; return s; } template inline void strappend(std::string& base, const T1& a1, const T2& a2) { base += a1; base += a2; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3) { base += a1; base += a2; base += a3; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3, const T4& a4) { base += a1; base += a2; base += a3; base += a4; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5) { base += a1; base += a2; base += a3; base += a4; base += a5; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6) { base += a1; base += a2; base += a3; base += a4; base += a5; base += a6; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7) { base += a1; base += a2; base += a3; base += a4; base += a5; base += a6; base += a7; } template inline void strappend(std::string& base, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8) { base += a1; base += a2; base += a3; base += a4; base += a5; base += a6; base += a7; base += a8; } template class LeastRecentAccess:public std::binary_function { public: bool operator()(const T& lhs, const T& rhs) const { return lhs.getLastAccess() < rhs.getLastAccess(); } }; } // namespace aria2 #endif // _D_A2_FUNCTIONAL_H_