2017-04-01 01:09:34 -07:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
#include <algorithm>
|
2017-07-12 17:40:41 -07:00
|
|
|
#include <map>
|
2017-08-25 16:03:57 -07:00
|
|
|
#include <mutex>
|
2018-03-14 16:48:18 -07:00
|
|
|
#include <unordered_map>
|
2017-04-01 01:09:34 -07:00
|
|
|
#include <utility>
|
2017-04-27 17:56:06 -07:00
|
|
|
#include <vector>
|
2019-07-10 04:02:48 +08:00
|
|
|
#include <type_traits>
|
2017-04-01 01:09:34 -07:00
|
|
|
|
|
|
|
namespace vcpkg::Util
|
|
|
|
{
|
2019-07-10 04:02:48 +08:00
|
|
|
template <class T>
|
|
|
|
constexpr std::add_const_t<T>& as_const(T& t) noexcept
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void as_const(const T&&) = delete;
|
|
|
|
|
2017-10-15 23:53:35 -07:00
|
|
|
template<class Container>
|
2019-04-08 23:26:18 -07:00
|
|
|
using ElementT =
|
|
|
|
std::remove_reference_t<decltype(*std::declval<typename std::remove_reference_t<Container>::iterator>())>;
|
2017-10-15 23:53:35 -07:00
|
|
|
|
|
|
|
namespace Vectors
|
|
|
|
{
|
|
|
|
template<class Container, class T = ElementT<Container>>
|
|
|
|
void concatenate(std::vector<T>* augend, const Container& addend)
|
|
|
|
{
|
|
|
|
augend->insert(augend->end(), addend.begin(), addend.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 18:59:11 -07:00
|
|
|
namespace Sets
|
|
|
|
{
|
2018-03-14 10:30:27 -07:00
|
|
|
template<class Container, class Key>
|
|
|
|
bool contains(const Container& container, const Key& item)
|
2017-10-26 18:59:11 -07:00
|
|
|
{
|
2018-03-14 10:30:27 -07:00
|
|
|
return container.find(item) != container.end();
|
2017-10-26 18:59:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:48:18 -07:00
|
|
|
namespace Maps
|
|
|
|
{
|
|
|
|
template<class K, class V1, class V2, class Func>
|
|
|
|
void transform_values(const std::unordered_map<K, V1>& container, std::unordered_map<K, V2>& output, Func func)
|
|
|
|
{
|
|
|
|
std::for_each(container.cbegin(), container.cend(), [&](const std::pair<const K, V1>& p) {
|
|
|
|
output[p.first] = func(p.second);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class Range, class Func>
|
|
|
|
using FmapOut = std::remove_reference_t<decltype(std::declval<Func&>()(*std::declval<Range>().begin()))>;
|
2017-04-01 01:09:34 -07:00
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class Range, class Func, class Out = FmapOut<Range, Func>>
|
|
|
|
std::vector<Out> fmap(Range&& xs, Func&& f)
|
2017-04-01 01:09:34 -07:00
|
|
|
{
|
2017-08-23 15:47:42 -07:00
|
|
|
std::vector<Out> ret;
|
2017-04-01 01:09:34 -07:00
|
|
|
ret.reserve(xs.size());
|
|
|
|
|
|
|
|
for (auto&& x : xs)
|
|
|
|
ret.push_back(f(x));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-12 22:12:37 -07:00
|
|
|
|
2017-08-23 15:47:42 -07:00
|
|
|
template<class Cont, class Func>
|
|
|
|
using FmapFlattenOut = std::decay_t<decltype(*begin(std::declval<Func>()(*begin(std::declval<Cont>()))))>;
|
|
|
|
|
|
|
|
template<class Cont, class Func, class Out = FmapFlattenOut<Cont, Func>>
|
|
|
|
std::vector<Out> fmap_flatten(Cont&& xs, Func&& f)
|
|
|
|
{
|
|
|
|
std::vector<Out> ret;
|
|
|
|
|
|
|
|
for (auto&& x : xs)
|
|
|
|
for (auto&& y : f(x))
|
|
|
|
ret.push_back(std::move(y));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 22:12:37 -07:00
|
|
|
template<class Container, class Pred>
|
2017-04-13 15:37:24 -07:00
|
|
|
void erase_remove_if(Container& cont, Pred pred)
|
2017-04-12 22:12:37 -07:00
|
|
|
{
|
|
|
|
cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
|
|
|
|
}
|
2017-04-13 17:53:09 -07:00
|
|
|
|
2017-06-06 16:08:36 -07:00
|
|
|
template<class Container, class V>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find(Container&& cont, V&& v)
|
2017-06-06 16:08:36 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find(begin(cont), end(cont), v);
|
2017-06-06 16:08:36 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:53:09 -07:00
|
|
|
template<class Container, class Pred>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find_if(Container&& cont, Pred pred)
|
2017-04-13 17:53:09 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find_if(begin(cont), end(cont), pred);
|
2017-04-13 17:53:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Container, class Pred>
|
2017-10-13 18:37:41 -07:00
|
|
|
auto find_if_not(Container&& cont, Pred pred)
|
2017-04-13 17:53:09 -07:00
|
|
|
{
|
2017-10-13 18:37:41 -07:00
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find_if_not(begin(cont), end(cont), pred);
|
2017-04-13 17:53:09 -07:00
|
|
|
}
|
2017-04-14 14:31:17 -07:00
|
|
|
|
|
|
|
template<class K, class V, class Container, class Func>
|
2017-10-16 11:44:04 -07:00
|
|
|
void group_by(const Container& cont, std::map<K, std::vector<const V*>>* output, Func&& f)
|
2017-04-14 14:31:17 -07:00
|
|
|
{
|
|
|
|
for (const V& element : cont)
|
|
|
|
{
|
|
|
|
K key = f(element);
|
|
|
|
(*output)[key].push_back(&element);
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 20:06:47 -07:00
|
|
|
|
2017-10-30 23:06:04 -07:00
|
|
|
template<class Range>
|
|
|
|
void sort(Range& cont)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
std::sort(begin(cont), end(cont));
|
|
|
|
}
|
|
|
|
|
2018-02-25 10:13:57 -08:00
|
|
|
template<class Range>
|
|
|
|
void sort_unique_erase(Range& cont)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
std::sort(begin(cont), end(cont));
|
|
|
|
cont.erase(std::unique(begin(cont), end(cont)), end(cont));
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:06:04 -07:00
|
|
|
template<class Range1, class Range2>
|
|
|
|
bool all_equal(const Range1& r1, const Range2& r2)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::equal(begin(r1), end(r1), begin(r2), end(r2));
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
template<class AssocContainer, class K = std::decay_t<decltype(begin(std::declval<AssocContainer>())->first)>>
|
|
|
|
std::vector<K> extract_keys(AssocContainer&& input_map)
|
|
|
|
{
|
|
|
|
return fmap(input_map, [](auto&& p) { return p.first; });
|
|
|
|
}
|
|
|
|
|
2017-08-21 20:06:47 -07:00
|
|
|
struct MoveOnlyBase
|
|
|
|
{
|
|
|
|
MoveOnlyBase() = default;
|
|
|
|
MoveOnlyBase(const MoveOnlyBase&) = delete;
|
|
|
|
MoveOnlyBase(MoveOnlyBase&&) = default;
|
|
|
|
|
|
|
|
MoveOnlyBase& operator=(const MoveOnlyBase&) = delete;
|
|
|
|
MoveOnlyBase& operator=(MoveOnlyBase&&) = default;
|
2018-03-16 16:53:48 -07:00
|
|
|
|
|
|
|
~MoveOnlyBase() = default;
|
2017-08-21 20:06:47 -07:00
|
|
|
};
|
2017-08-22 15:14:15 -07:00
|
|
|
|
|
|
|
struct ResourceBase
|
|
|
|
{
|
|
|
|
ResourceBase() = default;
|
|
|
|
ResourceBase(const ResourceBase&) = delete;
|
|
|
|
ResourceBase(ResourceBase&&) = delete;
|
|
|
|
|
|
|
|
ResourceBase& operator=(const ResourceBase&) = delete;
|
|
|
|
ResourceBase& operator=(ResourceBase&&) = delete;
|
2018-03-16 16:53:48 -07:00
|
|
|
|
|
|
|
~ResourceBase() = default;
|
2017-08-22 15:14:15 -07:00
|
|
|
};
|
2017-08-25 16:03:57 -07:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuardPtr;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuarded
|
|
|
|
{
|
|
|
|
friend struct LockGuardPtr<T>;
|
|
|
|
|
|
|
|
LockGuardPtr<T> lock() { return *this; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex m_mutex;
|
|
|
|
T m_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct LockGuardPtr
|
|
|
|
{
|
|
|
|
T& operator*() { return m_ptr; }
|
|
|
|
T* operator->() { return &m_ptr; }
|
|
|
|
|
|
|
|
T* get() { return &m_ptr; }
|
|
|
|
|
|
|
|
LockGuardPtr(LockGuarded<T>& sync) : m_lock(sync.m_mutex), m_ptr(sync.m_t) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_lock<std::mutex> m_lock;
|
|
|
|
T& m_ptr;
|
|
|
|
};
|
2017-11-16 19:29:32 -08:00
|
|
|
|
|
|
|
namespace Enum
|
|
|
|
{
|
|
|
|
template<class E>
|
|
|
|
E to_enum(bool b)
|
|
|
|
{
|
|
|
|
return b ? E::YES : E::NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class E>
|
|
|
|
bool to_bool(E e)
|
|
|
|
{
|
|
|
|
return e == E::YES;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 16:53:48 -07:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void unused(T&& param)
|
|
|
|
{
|
|
|
|
(void)param;
|
|
|
|
}
|
2017-10-15 23:53:35 -07:00
|
|
|
}
|