a2functional.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <functional>
  36. #include "SharedHandle.h"
  37. // mem_fun_t for SharedHandle
  38. template <class ReturnType, typename ClassType>
  39. class mem_fun_sh_t:public std::unary_function< SharedHandle<ClassType>, ReturnType>
  40. {
  41. private:
  42. ReturnType (ClassType::*f)();
  43. public:
  44. mem_fun_sh_t(ReturnType (ClassType::*f)()):f(f) {}
  45. ReturnType operator()(const SharedHandle<ClassType>& x) const
  46. {
  47. return (x.get()->*f)();
  48. }
  49. };
  50. // const_mem_fun_t for SharedHandle
  51. template <class ReturnType, typename ClassType>
  52. class const_mem_fun_sh_t:public std::unary_function< SharedHandle<ClassType>, ReturnType>
  53. {
  54. private:
  55. ReturnType (ClassType::*f)() const;
  56. public:
  57. const_mem_fun_sh_t(ReturnType (ClassType::*f)() const):f(f) {}
  58. ReturnType operator()(const SharedHandle<ClassType>& x) const
  59. {
  60. return (x.get()->*f)();
  61. }
  62. };
  63. template <class ReturnType, typename ClassType>
  64. mem_fun_sh_t<ReturnType, ClassType>
  65. mem_fun_sh(ReturnType (ClassType::*f)())
  66. {
  67. return mem_fun_sh_t<ReturnType, ClassType>(f);
  68. };
  69. template <class ReturnType, typename ClassType>
  70. const_mem_fun_sh_t<ReturnType, ClassType>
  71. mem_fun_sh(ReturnType (ClassType::*f)() const)
  72. {
  73. return const_mem_fun_sh_t<ReturnType, ClassType>(f);
  74. };
  75. template<class BinaryOp, class UnaryOp>
  76. class adopt2nd_t:public std::binary_function<typename BinaryOp::first_argument_type,
  77. typename UnaryOp::argument_type,
  78. typename BinaryOp::result_type> {
  79. private:
  80. BinaryOp _binaryOp;
  81. UnaryOp _unaryOp;
  82. public:
  83. adopt2nd_t(const BinaryOp& b, const UnaryOp& u):
  84. _binaryOp(b), _unaryOp(u) {}
  85. typename BinaryOp::result_type
  86. operator()(const typename BinaryOp::first_argument_type& x,
  87. const typename UnaryOp::argument_type& y)
  88. {
  89. return _binaryOp(x, _unaryOp(y));
  90. }
  91. };
  92. template <class BinaryOp, class UnaryOp>
  93. inline adopt2nd_t<BinaryOp, UnaryOp>
  94. adopt2nd(const BinaryOp& binaryOp, const UnaryOp& unaryOp)
  95. {
  96. return adopt2nd_t<BinaryOp, UnaryOp>(binaryOp, unaryOp);
  97. };
  98. template<typename T, std::size_t N>
  99. char (&char_array_ref(T (&)[N]))[N];
  100. template<typename T, std::size_t N>
  101. std::size_t
  102. arrayLength(T (&a)[N])
  103. {
  104. return sizeof(char_array_ref(a));
  105. }
  106. template<typename T>
  107. std::size_t
  108. arrayLength(T (&a)[0u])
  109. {
  110. return 0;
  111. }