2007-11-07 12:36:33 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-11-07 12:36:33 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
2010-10-31 07:23:53 +00:00
|
|
|
#ifndef D_A2_FUNCTIONAL_H
|
|
|
|
#define D_A2_FUNCTIONAL_H
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2012-04-08 09:59:06 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2007-11-07 12:36:33 +00:00
|
|
|
#include <functional>
|
2010-06-20 11:37:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
2013-06-21 16:10:38 +00:00
|
|
|
#include <memory>
|
2015-06-21 06:17:00 +00:00
|
|
|
#include <chrono>
|
2010-06-20 11:37:47 +00:00
|
|
|
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2007-11-07 12:36:33 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2008-05-11 09:36:39 +00:00
|
|
|
class Deleter {
|
|
|
|
public:
|
2015-12-27 09:39:47 +00:00
|
|
|
template <class T> void operator()(T* ptr) { delete ptr; }
|
2008-05-11 09:36:39 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T, typename F> struct Defer {
|
|
|
|
Defer(T t, F f) : t(t), f(std::move(f)) {}
|
2011-08-05 09:34:07 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
~Defer() { f(t); }
|
2011-08-05 09:34:07 +00:00
|
|
|
|
2014-09-16 14:49:50 +00:00
|
|
|
T t;
|
|
|
|
F f;
|
2010-06-20 11:37:47 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T, typename F> Defer<T, F> defer(T&& t, F f)
|
2014-09-16 14:49:50 +00:00
|
|
|
{
|
|
|
|
return Defer<T, F>(std::forward<T>(t), std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename InputIterator, typename DelimiterType>
|
2009-06-06 12:48:05 +00:00
|
|
|
std::string strjoin(InputIterator first, InputIterator last,
|
2010-01-05 16:01:46 +00:00
|
|
|
const DelimiterType& delim)
|
2009-06-06 12:48:05 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (first == last) {
|
2009-06-06 12:48:05 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
InputIterator beforeLast = last - 1;
|
|
|
|
for (; first != beforeLast; ++first) {
|
2009-06-06 12:48:05 +00:00
|
|
|
result += *first;
|
|
|
|
result += delim;
|
|
|
|
}
|
|
|
|
result += *beforeLast;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-04-03 04:02:14 +00:00
|
|
|
// Applies unaryOp through first to last and joins the result with
|
|
|
|
// delimiter delim.
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename InputIterator, typename DelimiterType, typename UnaryOp>
|
2010-04-03 04:02:14 +00:00
|
|
|
std::string strjoin(InputIterator first, InputIterator last,
|
|
|
|
const DelimiterType& delim, const UnaryOp& unaryOp)
|
|
|
|
{
|
|
|
|
std::string result;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (first == last) {
|
2010-04-03 04:02:14 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
InputIterator beforeLast = last - 1;
|
|
|
|
for (; first != beforeLast; ++first) {
|
2010-04-03 04:02:14 +00:00
|
|
|
result += unaryOp(*first);
|
|
|
|
result += delim;
|
|
|
|
}
|
|
|
|
result += unaryOp(*beforeLast);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T>
|
|
|
|
class LeastRecentAccess : public std::binary_function<T, T, bool> {
|
2010-01-28 14:01:50 +00:00
|
|
|
public:
|
2013-06-21 16:10:38 +00:00
|
|
|
bool operator()(const std::shared_ptr<T>& lhs,
|
|
|
|
const std::shared_ptr<T>& rhs) const
|
2012-03-22 16:34:37 +00:00
|
|
|
{
|
|
|
|
return lhs->getLastAccessTime() < rhs->getLastAccessTime();
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
bool operator()(const T& lhs, const T& rhs) const
|
|
|
|
{
|
2010-10-09 14:22:49 +00:00
|
|
|
return lhs.getLastAccessTime() < rhs.getLastAccessTime();
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T, typename S> bool in(T x, S s, S t)
|
2010-10-09 14:22:49 +00:00
|
|
|
{
|
|
|
|
return s <= x && x <= t;
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T> struct DerefLess {
|
|
|
|
bool operator()(const T& lhs, const T& rhs) const { return *lhs < *rhs; }
|
2010-11-11 07:33:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T> struct DerefEqualTo {
|
|
|
|
bool operator()(const T& lhs, const T& rhs) const { return *lhs == *rhs; }
|
2010-11-11 07:33:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T> struct DerefEqual {
|
2010-11-11 07:33:43 +00:00
|
|
|
T target;
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
DerefEqual(const T& t) : target(t) {}
|
|
|
|
bool operator()(const T& other) const { return *target == *other; }
|
2010-11-11 07:33:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T> struct DerefEqual<T> derefEqual(const T& t) {
|
2010-11-11 07:33:43 +00:00
|
|
|
return DerefEqual<T>(t);
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T>
|
2012-03-20 12:42:09 +00:00
|
|
|
struct RefLess {
|
2015-12-27 09:39:47 +00:00
|
|
|
bool operator()(const std::shared_ptr<T>& lhs,
|
|
|
|
const std::shared_ptr<T>& rhs) const
|
2012-03-20 12:42:09 +00:00
|
|
|
{
|
|
|
|
return lhs.get() < rhs.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-30 10:13:41 +00:00
|
|
|
#if __cplusplus > 201103L
|
|
|
|
using std::make_unique;
|
|
|
|
#else // __cplusplus <= 201103L
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T, typename... U>
|
2013-07-03 18:02:33 +00:00
|
|
|
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
|
|
|
|
make_unique(U&&... u)
|
2013-06-22 06:49:41 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename T>
|
2013-07-03 18:02:33 +00:00
|
|
|
typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
|
|
|
|
make_unique(size_t size)
|
2013-07-03 14:23:22 +00:00
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
|
|
|
|
}
|
2016-01-30 10:13:41 +00:00
|
|
|
#endif // __cplusplus <= 201103L
|
2013-07-03 14:23:22 +00:00
|
|
|
|
2015-06-21 09:04:30 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-06-21 06:17:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_A2_FUNCTIONAL_H
|