a2functional.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. // mem_fun1_t for SharedHandle
  81. template<typename ReturnType, typename ClassType, typename ArgType>
  82. class mem_fun1_sh_t:public std::binary_function<SharedHandle<ClassType>,
  83. ArgType,
  84. ReturnType>
  85. {
  86. private:
  87. ReturnType (ClassType::*f)(ArgType);
  88. public:
  89. mem_fun1_sh_t(ReturnType (ClassType::*f)(ArgType)):f(f) {}
  90. ReturnType operator()(const SharedHandle<ClassType>& x, ArgType a) const
  91. {
  92. return (x.get()->*f)(a);
  93. }
  94. };
  95. template<typename ReturnType, typename ClassType, typename ArgType>
  96. mem_fun1_sh_t<ReturnType, ClassType, ArgType>
  97. mem_fun_sh(ReturnType (ClassType::*f)(ArgType))
  98. {
  99. return mem_fun1_sh_t<ReturnType, ClassType, ArgType>(f);
  100. };
  101. template<class BinaryOp, class UnaryOp>
  102. class adopt2nd_t:public std::binary_function<typename BinaryOp::first_argument_type,
  103. typename UnaryOp::argument_type,
  104. typename BinaryOp::result_type> {
  105. private:
  106. BinaryOp _binaryOp;
  107. UnaryOp _unaryOp;
  108. public:
  109. adopt2nd_t(const BinaryOp& b, const UnaryOp& u):
  110. _binaryOp(b), _unaryOp(u) {}
  111. typename BinaryOp::result_type
  112. operator()(const typename BinaryOp::first_argument_type& x,
  113. const typename UnaryOp::argument_type& y)
  114. {
  115. return _binaryOp(x, _unaryOp(y));
  116. }
  117. };
  118. template <class BinaryOp, class UnaryOp>
  119. inline adopt2nd_t<BinaryOp, UnaryOp>
  120. adopt2nd(const BinaryOp& binaryOp, const UnaryOp& unaryOp)
  121. {
  122. return adopt2nd_t<BinaryOp, UnaryOp>(binaryOp, unaryOp);
  123. };
  124. template<typename Pair>
  125. class Ascend1st:public std::binary_function<Pair, Pair, bool>
  126. {
  127. public:
  128. bool operator()(const Pair& p1, const Pair& p2) const
  129. {
  130. return p1.first < p2.first;
  131. }
  132. };
  133. template<typename Pair>
  134. class select2nd
  135. {
  136. public:
  137. typename Pair::second_type operator()(const Pair& p) const
  138. {
  139. return p.second;
  140. }
  141. typename Pair::second_type operator()(Pair& p) const
  142. {
  143. return p.second;
  144. }
  145. };
  146. class Deleter {
  147. public:
  148. template<class T>
  149. void operator()(T* ptr) {
  150. delete ptr;
  151. }
  152. };
  153. template<typename T>
  154. class Append {
  155. private:
  156. T& _to;
  157. T _delim;
  158. public:
  159. template<typename S>
  160. Append(T& to, const S& delim):_to(to), _delim(delim) {}
  161. template<typename S>
  162. void operator()(const S& s) {
  163. _to += s+_delim;
  164. }
  165. };
  166. typedef Append<std::string> StringAppend;
  167. template<typename T>
  168. class auto_delete {
  169. private:
  170. T _obj;
  171. void (*_deleter)(T);
  172. public:
  173. auto_delete(T obj, void (*deleter)(T)):_obj(obj), _deleter(deleter) {}
  174. ~auto_delete()
  175. {
  176. _deleter(_obj);
  177. }
  178. };
  179. template<typename InputIterator, typename DelimiterType>
  180. std::string strjoin(InputIterator first, InputIterator last,
  181. const DelimiterType& delim)
  182. {
  183. std::string result;
  184. if(first == last) {
  185. return result;
  186. }
  187. InputIterator beforeLast = last-1;
  188. for(; first != beforeLast; ++first) {
  189. result += *first;
  190. result += delim;
  191. }
  192. result += *beforeLast;
  193. return result;
  194. }
  195. // Applies unaryOp through first to last and joins the result with
  196. // delimiter delim.
  197. template<typename InputIterator, typename DelimiterType, typename UnaryOp>
  198. std::string strjoin(InputIterator first, InputIterator last,
  199. const DelimiterType& delim, const UnaryOp& unaryOp)
  200. {
  201. std::string result;
  202. if(first == last) {
  203. return result;
  204. }
  205. InputIterator beforeLast = last-1;
  206. for(; first != beforeLast; ++first) {
  207. result += unaryOp(*first);
  208. result += delim;
  209. }
  210. result += unaryOp(*beforeLast);
  211. return result;
  212. }
  213. template<typename T1, typename T2>
  214. inline std::string strconcat(const T1& a1, const T2& a2)
  215. {
  216. std::string s(a1); s += a2; return s;
  217. }
  218. template<typename T1, typename T2, typename T3>
  219. inline std::string strconcat(const T1& a1, const T2& a2, const T3& a3)
  220. {
  221. std::string s(a1); s += a2; s += a3; return s;
  222. }
  223. template<typename T1, typename T2, typename T3, typename T4>
  224. inline std::string
  225. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4)
  226. {
  227. std::string s(a1); s += a2; s += a3; s += a4; return s;
  228. }
  229. template<typename T1, typename T2, typename T3, typename T4, typename T5>
  230. inline std::string
  231. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  232. const T5& a5)
  233. {
  234. std::string s(a1); s += a2; s += a3; s += a4; s += a5; return s;
  235. }
  236. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  237. typename T6>
  238. inline std::string
  239. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  240. const T5& a5, const T6& a6)
  241. {
  242. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; return s;
  243. }
  244. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  245. typename T6, typename T7>
  246. inline std::string
  247. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  248. const T5& a5, const T6& a6, const T7& a7)
  249. {
  250. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  251. return s;
  252. }
  253. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  254. typename T6, typename T7, typename T8>
  255. inline std::string
  256. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  257. const T5& a5, const T6& a6, const T7& a7, const T8& a8)
  258. {
  259. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  260. s += a8; return s;
  261. }
  262. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  263. typename T6, typename T7, typename T8, typename T9>
  264. inline std::string
  265. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  266. const T5& a5, const T6& a6, const T7& a7, const T8& a8,
  267. const T9& a9)
  268. {
  269. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  270. s += a8; s += a9; return s;
  271. }
  272. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  273. typename T6, typename T7, typename T8, typename T9, typename T10>
  274. inline std::string
  275. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  276. const T5& a5, const T6& a6, const T7& a7, const T8& a8,
  277. const T9& a9, const T10& a10)
  278. {
  279. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  280. s += a8; s += a9; s += a10; return s;
  281. }
  282. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  283. typename T6, typename T7, typename T8, typename T9, typename T10,
  284. typename T11>
  285. inline std::string
  286. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  287. const T5& a5, const T6& a6, const T7& a7, const T8& a8,
  288. const T9& a9, const T10& a10, const T11& a11)
  289. {
  290. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  291. s += a8; s += a9; s += a10; s += a11; return s;
  292. }
  293. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  294. typename T6, typename T7, typename T8, typename T9, typename T10,
  295. typename T11, typename T12>
  296. inline std::string
  297. strconcat(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  298. const T5& a5, const T6& a6, const T7& a7, const T8& a8,
  299. const T9& a9, const T10& a10, const T11& a11,
  300. const T12& a12)
  301. {
  302. std::string s(a1); s += a2; s += a3; s += a4; s += a5; s += a6; s += a7;
  303. s += a8; s += a9; s += a10; s += a11; s += a12; return s;
  304. }
  305. template<typename T1, typename T2>
  306. inline void strappend(std::string& base, const T1& a1, const T2& a2)
  307. {
  308. base += a1; base += a2;
  309. }
  310. template<typename T1, typename T2, typename T3>
  311. inline void strappend(std::string& base,
  312. const T1& a1, const T2& a2, const T3& a3)
  313. {
  314. base += a1; base += a2; base += a3;
  315. }
  316. template<typename T1, typename T2, typename T3, typename T4>
  317. inline void strappend(std::string& base,
  318. const T1& a1, const T2& a2, const T3& a3, const T4& a4)
  319. {
  320. base += a1; base += a2; base += a3; base += a4;
  321. }
  322. template<typename T1, typename T2, typename T3, typename T4, typename T5>
  323. inline void strappend(std::string& base,
  324. const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  325. const T5& a5)
  326. {
  327. base += a1; base += a2; base += a3; base += a4; base += a5;
  328. }
  329. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  330. typename T6>
  331. inline void strappend(std::string& base,
  332. const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  333. const T5& a5, const T6& a6)
  334. {
  335. base += a1; base += a2; base += a3; base += a4; base += a5; base += a6;
  336. }
  337. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  338. typename T6, typename T7>
  339. inline void strappend(std::string& base,
  340. const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  341. const T5& a5, const T6& a6, const T7& a7)
  342. {
  343. base += a1; base += a2; base += a3; base += a4; base += a5; base += a6;
  344. base += a7;
  345. }
  346. template<typename T1, typename T2, typename T3, typename T4, typename T5,
  347. typename T6, typename T7, typename T8>
  348. inline void strappend(std::string& base,
  349. const T1& a1, const T2& a2, const T3& a3, const T4& a4,
  350. const T5& a5, const T6& a6, const T7& a7, const T8& a8)
  351. {
  352. base += a1; base += a2; base += a3; base += a4; base += a5; base += a6;
  353. base += a7; base += a8;
  354. }
  355. template<typename T>
  356. class LeastRecentAccess:public std::binary_function<T, T, bool> {
  357. public:
  358. bool operator()(const T& lhs, const T& rhs) const
  359. {
  360. return lhs.getLastAccess() < rhs.getLastAccess();
  361. }
  362. };
  363. } // namespace aria2
  364. #endif // _D_A2_FUNCTIONAL_H_