2015-12-27 17:58:49 +01:00
|
|
|
#pragma once
|
|
|
|
|
2016-01-07 12:04:40 +01:00
|
|
|
// std
|
2015-12-27 17:58:49 +01:00
|
|
|
#include <algorithm>
|
2015-12-28 13:21:02 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <type_traits>
|
2016-01-30 13:40:25 +01:00
|
|
|
// Qt
|
|
|
|
#include <QtGlobal>
|
2015-12-27 17:58:49 +01:00
|
|
|
|
2016-02-27 15:07:23 +01:00
|
|
|
namespace DOS {
|
2015-12-27 17:58:49 +01:00
|
|
|
|
2016-03-28 17:14:11 +02:00
|
|
|
template<class Lambda>
|
2016-03-28 21:36:34 +02:00
|
|
|
struct DeferHelper {
|
2016-03-28 17:14:11 +02:00
|
|
|
DeferHelper(Lambda lambda)
|
|
|
|
: m_lambda(std::move(lambda))
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DeferHelper()
|
|
|
|
{
|
2016-03-28 21:36:34 +02:00
|
|
|
try {
|
|
|
|
m_lambda();
|
|
|
|
} catch (...) {}
|
2016-03-28 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Lambda m_lambda;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Lambda>
|
2016-03-28 21:36:34 +02:00
|
|
|
DeferHelper<Lambda> defer(Lambda l)
|
|
|
|
{
|
2016-03-28 17:14:11 +02:00
|
|
|
return DeferHelper<Lambda>(std::move(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 17:58:49 +01:00
|
|
|
template <typename T>
|
|
|
|
struct wrapped_array {
|
2016-02-27 15:07:23 +01:00
|
|
|
wrapped_array(T *first, T *last) : begin_ {first}, end_ {last} {}
|
|
|
|
wrapped_array(T *first, std::ptrdiff_t size)
|
2015-12-27 17:58:49 +01:00
|
|
|
: wrapped_array {first, first + size} {}
|
|
|
|
|
2016-02-27 15:07:23 +01:00
|
|
|
T *begin() const Q_DECL_NOEXCEPT
|
|
|
|
{
|
|
|
|
return begin_;
|
|
|
|
}
|
|
|
|
T *end() const Q_DECL_NOEXCEPT
|
|
|
|
{
|
|
|
|
return end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *begin_;
|
|
|
|
T *end_;
|
2015-12-27 17:58:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-02-27 15:07:23 +01:00
|
|
|
wrapped_array<T> wrap_array(T *first, std::ptrdiff_t size) Q_DECL_NOEXCEPT
|
2015-12-27 17:58:49 +01:00
|
|
|
{ return {first, size}; }
|
|
|
|
|
|
|
|
template <typename T, typename G>
|
2016-02-27 15:07:23 +01:00
|
|
|
std::vector<T> toVector(G *first, std::ptrdiff_t size) Q_DECL_NOEXCEPT {
|
2015-12-28 13:21:02 +01:00
|
|
|
const wrapped_array<G> array = wrap_array(first, size);
|
2015-12-27 17:58:49 +01:00
|
|
|
std::vector<T> result;
|
2016-01-02 16:48:16 +01:00
|
|
|
for (auto it = array.begin(); it != array.end(); ++it)
|
|
|
|
result.emplace_back(T(*it));
|
2015-12-27 17:58:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
2015-12-28 13:21:02 +01:00
|
|
|
|
|
|
|
template <typename T, typename K, typename R = typename std::result_of<K(T)>::type>
|
2016-02-27 15:07:23 +01:00
|
|
|
std::vector<R> toVector(T *first, std::ptrdiff_t size, K f) Q_DECL_NOEXCEPT {
|
2015-12-28 13:21:02 +01:00
|
|
|
wrapped_array<T> array = wrap_array<T>(first, size);
|
|
|
|
std::vector<R> result;
|
2016-01-02 16:48:16 +01:00
|
|
|
for (auto it = array.begin(); it != array.end(); ++it)
|
|
|
|
result.emplace_back(R(f(*it)));
|
2015-12-28 13:21:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2016-01-23 18:40:17 +01:00
|
|
|
|
2015-12-27 17:58:49 +01:00
|
|
|
}
|