a2functional.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_A2_FUNCTIONAL_H_
  36. #define _D_A2_FUNCTIONAL_H_
  37. #include <functional>
  38. #include "SharedHandle.h"
  39. #include "A2STR.h"
  40. #include <string>
  41. namespace aria2 {
  42. // mem_fun_t for SharedHandle
  43. template <class ReturnType, typename ClassType>
  44. class mem_fun_sh_t:public std::unary_function< SharedHandle<ClassType>, ReturnType>
  45. {
  46. private:
  47. ReturnType (ClassType::*f)();
  48. public:
  49. mem_fun_sh_t(ReturnType (ClassType::*f)()):f(f) {}
  50. ReturnType operator()(const SharedHandle<ClassType>& x) const
  51. {
  52. return (x.get()->*f)();
  53. }
  54. };
  55. // const_mem_fun_t for SharedHandle
  56. template <class ReturnType, typename ClassType>
  57. class const_mem_fun_sh_t:public std::unary_function< SharedHandle<ClassType>, ReturnType>
  58. {
  59. private:
  60. ReturnType (ClassType::*f)() const;
  61. public:
  62. const_mem_fun_sh_t(ReturnType (ClassType::*f)() const):f(f) {}
  63. ReturnType operator()(const SharedHandle<ClassType>& x) const
  64. {
  65. return (x.get()->*f)();
  66. }
  67. };
  68. template <class ReturnType, typename ClassType>
  69. mem_fun_sh_t<ReturnType, ClassType>
  70. mem_fun_sh(ReturnType (ClassType::*f)())
  71. {
  72. return mem_fun_sh_t<ReturnType, ClassType>(f);
  73. };
  74. template <class ReturnType, typename ClassType>
  75. const_mem_fun_sh_t<ReturnType, ClassType>
  76. mem_fun_sh(ReturnType (ClassType::*f)() const)
  77. {
  78. return const_mem_fun_sh_t<ReturnType, ClassType>(f);
  79. };
  80. template<class BinaryOp, class UnaryOp>
  81. class adopt2nd_t:public std::binary_function<typename BinaryOp::first_argument_type,
  82. typename UnaryOp::argument_type,
  83. typename BinaryOp::result_type> {
  84. private:
  85. BinaryOp _binaryOp;
  86. UnaryOp _unaryOp;
  87. public:
  88. adopt2nd_t(const BinaryOp& b, const UnaryOp& u):
  89. _binaryOp(b), _unaryOp(u) {}
  90. typename BinaryOp::result_type
  91. operator()(const typename BinaryOp::first_argument_type& x,
  92. const typename UnaryOp::argument_type& y)
  93. {
  94. return _binaryOp(x, _unaryOp(y));
  95. }
  96. };
  97. template <class BinaryOp, class UnaryOp>
  98. inline adopt2nd_t<BinaryOp, UnaryOp>
  99. adopt2nd(const BinaryOp& binaryOp, const UnaryOp& unaryOp)
  100. {
  101. return adopt2nd_t<BinaryOp, UnaryOp>(binaryOp, unaryOp);
  102. };
  103. template<typename Pair>
  104. class Ascend1st:public std::binary_function<Pair, Pair, bool>
  105. {
  106. public:
  107. bool operator()(const Pair& p1, const Pair& p2) const
  108. {
  109. return p1.first < p2.first;
  110. }
  111. };
  112. template<typename Pair>
  113. class select2nd
  114. {
  115. public:
  116. typename Pair::second_type operator()(const Pair& p) const
  117. {
  118. return p.second;
  119. }
  120. typename Pair::second_type operator()(Pair& p) const
  121. {
  122. return p.second;
  123. }
  124. };
  125. class Concat {
  126. private:
  127. std::string _delim;
  128. public:
  129. Concat(const std::string& delim = A2STR::NIL):_delim(delim) {}
  130. std::string operator()(const std::string& s1, const std::string& s2) const
  131. {
  132. return s1+_delim+s2;
  133. }
  134. };
  135. class Deleter {
  136. public:
  137. template<class T>
  138. void operator()(T* ptr) {
  139. delete ptr;
  140. }
  141. };
  142. template<typename T>
  143. class Append {
  144. private:
  145. T& _to;
  146. T _delim;
  147. public:
  148. template<typename S>
  149. Append(T& to, const S& delim):_to(to), _delim(delim) {}
  150. template<typename S>
  151. void operator()(const S& s) {
  152. _to += s+_delim;
  153. }
  154. };
  155. typedef Append<std::string> StringAppend;
  156. } // namespace aria2
  157. #endif // _D_A2_FUNCTIONAL_H_