a2functional.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "common.h"
  38. #include <functional>
  39. #include <string>
  40. #include <algorithm>
  41. #include <memory>
  42. #include <chrono>
  43. #include "A2STR.h"
  44. namespace aria2 {
  45. class Deleter {
  46. public:
  47. template <class T> void operator()(T* ptr) { delete ptr; }
  48. };
  49. template <typename T, typename F> struct Defer {
  50. Defer(T t, F f) : t(t), f(std::move(f)) {}
  51. ~Defer() { f(t); }
  52. T t;
  53. F f;
  54. };
  55. template <typename T, typename F> Defer<T, F> defer(T&& t, F f)
  56. {
  57. return Defer<T, F>(std::forward<T>(t), std::forward<F>(f));
  58. }
  59. template <typename InputIterator, typename DelimiterType>
  60. std::string strjoin(InputIterator first, InputIterator last,
  61. const DelimiterType& delim)
  62. {
  63. std::string result;
  64. if (first == last) {
  65. return result;
  66. }
  67. InputIterator beforeLast = last - 1;
  68. for (; first != beforeLast; ++first) {
  69. result += *first;
  70. result += delim;
  71. }
  72. result += *beforeLast;
  73. return result;
  74. }
  75. // Applies unaryOp through first to last and joins the result with
  76. // delimiter delim.
  77. template <typename InputIterator, typename DelimiterType, typename UnaryOp>
  78. std::string strjoin(InputIterator first, InputIterator last,
  79. const DelimiterType& delim, const UnaryOp& unaryOp)
  80. {
  81. std::string result;
  82. if (first == last) {
  83. return result;
  84. }
  85. InputIterator beforeLast = last - 1;
  86. for (; first != beforeLast; ++first) {
  87. result += unaryOp(*first);
  88. result += delim;
  89. }
  90. result += unaryOp(*beforeLast);
  91. return result;
  92. }
  93. template <typename T>
  94. class LeastRecentAccess : public std::binary_function<T, T, bool> {
  95. public:
  96. bool operator()(const std::shared_ptr<T>& lhs,
  97. const std::shared_ptr<T>& rhs) const
  98. {
  99. return lhs->getLastAccessTime() < rhs->getLastAccessTime();
  100. }
  101. bool operator()(const T& lhs, const T& rhs) const
  102. {
  103. return lhs.getLastAccessTime() < rhs.getLastAccessTime();
  104. }
  105. };
  106. template <typename T, typename S> bool in(T x, S s, S t)
  107. {
  108. return s <= x && x <= t;
  109. }
  110. template <typename T> struct DerefLess {
  111. bool operator()(const T& lhs, const T& rhs) const { return *lhs < *rhs; }
  112. };
  113. template <typename T> struct DerefEqualTo {
  114. bool operator()(const T& lhs, const T& rhs) const { return *lhs == *rhs; }
  115. };
  116. template <typename T> struct DerefEqual {
  117. T target;
  118. DerefEqual(const T& t) : target(t) {}
  119. bool operator()(const T& other) const { return *target == *other; }
  120. };
  121. template <typename T> struct DerefEqual<T> derefEqual(const T& t) {
  122. return DerefEqual<T>(t);
  123. }
  124. template <typename T>
  125. struct RefLess {
  126. bool operator()(const std::shared_ptr<T>& lhs,
  127. const std::shared_ptr<T>& rhs) const
  128. {
  129. return lhs.get() < rhs.get();
  130. }
  131. };
  132. #if __cplusplus > 201103L
  133. using std::make_unique;
  134. #else // __cplusplus <= 201103L
  135. template <typename T, typename... U>
  136. typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
  137. make_unique(U&&... u)
  138. {
  139. return std::unique_ptr<T>(new T(std::forward<U>(u)...));
  140. }
  141. template <typename T>
  142. typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
  143. make_unique(size_t size)
  144. {
  145. return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
  146. }
  147. #endif // __cplusplus <= 201103L
  148. // User-defined literals for K, M, and G (powers of 1024)
  149. constexpr unsigned long long operator"" _k(unsigned long long k)
  150. {
  151. return k * 1024;
  152. }
  153. constexpr unsigned long long operator"" _m(unsigned long long m)
  154. {
  155. return m * 1024 * 1024;
  156. }
  157. constexpr unsigned long long operator"" _g(unsigned long long g)
  158. {
  159. return g * 1024 * 1024 * 1024;
  160. }
  161. constexpr std::chrono::hours operator"" _h(unsigned long long h)
  162. {
  163. return std::chrono::hours(h);
  164. }
  165. constexpr std::chrono::minutes operator"" _min(unsigned long long min)
  166. {
  167. return std::chrono::minutes(min);
  168. }
  169. constexpr std::chrono::seconds operator"" _s(unsigned long long s)
  170. {
  171. return std::chrono::seconds(s);
  172. }
  173. constexpr std::chrono::milliseconds operator"" _ms(unsigned long long ms)
  174. {
  175. return std::chrono::milliseconds(ms);
  176. }
  177. } // namespace aria2
  178. #endif // D_A2_FUNCTIONAL_H