/* */ #ifndef D_A2_FUNCTIONAL_H #define D_A2_FUNCTIONAL_H #include "common.h" #include #include #include #include #include #include "A2STR.h" namespace aria2 { class Deleter { public: template void operator()(T* ptr) { delete ptr; } }; template struct Defer { Defer(T t, F f) : t(t), f(std::move(f)) {} ~Defer() { f(t); } T t; F f; }; template Defer defer(T&& t, F f) { return Defer(std::forward(t), std::forward(f)); } 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 class LeastRecentAccess : public std::binary_function { public: bool operator()(const std::shared_ptr& lhs, const std::shared_ptr& rhs) const { return lhs->getLastAccessTime() < rhs->getLastAccessTime(); } bool operator()(const T& lhs, const T& rhs) const { return lhs.getLastAccessTime() < rhs.getLastAccessTime(); } }; template bool in(T x, S s, S t) { return s <= x && x <= t; } template struct DerefLess { bool operator()(const T& lhs, const T& rhs) const { return *lhs < *rhs; } }; template struct DerefEqualTo { bool operator()(const T& lhs, const T& rhs) const { return *lhs == *rhs; } }; template struct DerefEqual { T target; DerefEqual(const T& t) : target(t) {} bool operator()(const T& other) const { return *target == *other; } }; template struct DerefEqual derefEqual(const T& t) { return DerefEqual(t); } template struct RefLess { bool operator()(const std::shared_ptr& lhs, const std::shared_ptr& rhs) const { return lhs.get() < rhs.get(); } }; #if __cplusplus > 201103L using std::make_unique; #else // __cplusplus <= 201103L template typename std::enable_if::value, std::unique_ptr>::type make_unique(U&&... u) { return std::unique_ptr(new T(std::forward(u)...)); } template typename std::enable_if::value, std::unique_ptr>::type make_unique(size_t size) { return std::unique_ptr(new typename std::remove_extent::type[size]()); } #endif // __cplusplus <= 201103L // User-defined literals for K, M, and G (powers of 1024) constexpr unsigned long long operator"" _k(unsigned long long k) { return k * 1024; } constexpr unsigned long long operator"" _m(unsigned long long m) { return m * 1024 * 1024; } constexpr unsigned long long operator"" _g(unsigned long long g) { return g * 1024 * 1024 * 1024; } constexpr std::chrono::hours operator"" _h(unsigned long long h) { return std::chrono::hours(h); } constexpr std::chrono::minutes operator"" _min(unsigned long long min) { return std::chrono::minutes(min); } constexpr std::chrono::seconds operator"" _s(unsigned long long s) { return std::chrono::seconds(s); } constexpr std::chrono::milliseconds operator"" _ms(unsigned long long ms) { return std::chrono::milliseconds(ms); } } // namespace aria2 #endif // D_A2_FUNCTIONAL_H