clang-format

This commit is contained in:
Guillaume Meunier
2024-03-30 17:00:34 +01:00
parent bb660cfb3f
commit 9cceaac648
122 changed files with 2904 additions and 2783 deletions
+6 -6
View File
@@ -23,10 +23,10 @@
namespace utils
{
template<typename T>
requires std::integral<T>
T align_up(T alignment, T value)
{
return ((value + alignment - 1) / alignment) * alignment;
}
template <typename T>
requires std::integral<T>
T align_up(T alignment, T value)
{
return ((value + alignment - 1) / alignment) * alignment;
}
} // namespace utils
+191 -189
View File
@@ -19,213 +19,215 @@
#pragma once
#include <atomic>
#include <chrono>
// #include <chrono>
#include <condition_variable>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <mutex>
#include <condition_variable>
#include <optional>
#include <thread>
namespace utils
{
enum class future_status
enum class future_status
{
ready,
timeout
};
template <typename Result, typename Progress>
struct async_token;
template <typename Result, typename Progress>
struct future;
template <typename Result, typename Progress, typename F, typename... Args>
future<Result, Progress> async(F && f, Args &&... args);
template <typename Result, typename Progress>
struct future
{
friend struct async_token<Result, Progress>;
template <typename F, typename... Args>
friend future<Result, Progress> async(F && f, Args &&... args);
struct state
{
ready,
timeout
};
std::mutex lock;
std::condition_variable cv;
template<typename Result, typename Progress>
struct async_token;
std::optional<Result> result;
Progress progress;
std::exception_ptr exception;
std::atomic<bool> cancelled = false;
template<typename Result, typename Progress>
struct future;
std::thread thread;
template<typename Result, typename Progress, typename F, typename... Args>
future<Result, Progress> async(F&& f, Args&&...args);
template<typename Result, typename Progress>
struct future
{
friend struct async_token<Result, Progress>;
template<typename F, typename... Args> friend future<Result, Progress> async(F&& f, Args&&...args);
struct state
~state()
{
std::mutex lock;
std::condition_variable cv;
std::optional<Result> result;
Progress progress;
std::exception_ptr exception;
std::atomic<bool> cancelled = false;
std::thread thread;
~state()
if (thread.joinable())
{
if (thread.joinable())
{
if (thread.get_id() == std::this_thread::get_id())
thread.detach();
else
thread.join();
}
if (thread.get_id() == std::this_thread::get_id())
thread.detach();
else
thread.join();
}
};
std::shared_ptr<state> shared_state;
public:
bool valid() const
{
return (bool)shared_state;
}
future_status poll() const
{
assert(valid());
std::unique_lock _{shared_state->lock};
if (shared_state->result || shared_state->exception)
return future_status::ready;
else
return future_status::timeout;
}
// future_status wait_until(std::chrono::steady_clock::time_point deadline) const
// {
// assert(valid());
//
// std::unique_lock _{shared_state->lock};
// auto now = std::chrono::steady_clock::now();
// while(now < deadline)
// {
// if (shared_state->result || shared_state->exception)
// return future_status::ready;
//
// shared_state->cv.wait_until(_, deadline);
// }
// return future_status::timeout;
// }
//
// future_status wait_for(std::chrono::steady_clock::duration duration) const
// {
// return wait_until(std::chrono::steady_clock::now() + duration);
// }
Result get()
{
assert(valid());
std::unique_lock _{shared_state->lock};
shared_state->cv.wait(_, [&]{
return shared_state->result || shared_state->exception;
});
if (shared_state->thread.joinable())
shared_state->thread.join();
if (shared_state->result)
return std::move(*shared_state->result);
std::rethrow_exception(shared_state->exception);
}
Progress get_progress()
{
assert(valid());
std::unique_lock _{shared_state->lock};
return shared_state->progress;
}
void cancel()
{
if (!valid())
return;
shared_state->cancelled = true;
shared_state.reset();
}
void reset()
{
shared_state.reset();
}
};
template<typename Result, typename Progress>
struct async_token
std::shared_ptr<state> shared_state;
public:
bool valid() const
{
template<typename R, typename P, typename F, typename... Args> friend future<R, P> async(F&& f, Args&&...args);
using state = typename future<Result, Progress>::state;
std::shared_ptr<state> shared_state;
async_token(std::shared_ptr<state> shared_state) : shared_state(shared_state)
{
}
async_token(const async_token&) = default;
async_token(async_token&&) = default;
async_token& operator=(const async_token&) = default;
async_token& operator=(async_token&&) = default;
void set_result(Result&& r)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->result = std::move(r);
shared_state->cv.notify_all();
}
void set_exception(std::exception_ptr&& e)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->exception = std::move(e);
shared_state->cv.notify_all();
}
public:
void set_progress(Progress&& p)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->progress = std::move(p);
shared_state->cv.notify_all();
}
bool is_cancelled()
{
return shared_state->cancelled;
}
};
template<typename Result, typename Progress, typename F, typename... Args>
future<Result, Progress> async(F&& f, Args&&...args)
{
future<Result, Progress> fut;
fut.shared_state = std::make_shared<typename future<Result, Progress>::state>();
fut.shared_state->thread = std::thread([f, args...](auto token)
{
try
{
token.set_result(std::invoke(f, token, args...));
}
catch(...)
{
token.set_exception(std::current_exception());
}
}, async_token<Result, Progress>(fut.shared_state));
return fut;
return (bool)shared_state;
}
future_status poll() const
{
assert(valid());
std::unique_lock _{shared_state->lock};
if (shared_state->result || shared_state->exception)
return future_status::ready;
else
return future_status::timeout;
}
// future_status wait_until(std::chrono::steady_clock::time_point deadline) const
// {
// assert(valid());
//
// std::unique_lock _{shared_state->lock};
// auto now = std::chrono::steady_clock::now();
// while(now < deadline)
// {
// if (shared_state->result || shared_state->exception)
// return future_status::ready;
//
// shared_state->cv.wait_until(_, deadline);
// }
// return future_status::timeout;
// }
//
// future_status wait_for(std::chrono::steady_clock::duration duration) const
// {
// return wait_until(std::chrono::steady_clock::now() + duration);
// }
Result get()
{
assert(valid());
std::unique_lock _{shared_state->lock};
shared_state->cv.wait(_, [&] {
return shared_state->result || shared_state->exception;
});
if (shared_state->thread.joinable())
shared_state->thread.join();
if (shared_state->result)
return std::move(*shared_state->result);
std::rethrow_exception(shared_state->exception);
}
Progress get_progress()
{
assert(valid());
std::unique_lock _{shared_state->lock};
return shared_state->progress;
}
void cancel()
{
if (!valid())
return;
shared_state->cancelled = true;
shared_state.reset();
}
void reset()
{
shared_state.reset();
}
};
template <typename Result, typename Progress>
struct async_token
{
template <typename R, typename P, typename F, typename... Args>
friend future<R, P> async(F && f, Args &&... args);
using state = typename future<Result, Progress>::state;
std::shared_ptr<state> shared_state;
async_token(std::shared_ptr<state> shared_state) :
shared_state(shared_state)
{
}
async_token(const async_token &) = default;
async_token(async_token &&) = default;
async_token & operator=(const async_token &) = default;
async_token & operator=(async_token &&) = default;
void set_result(Result && r)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->result = std::move(r);
shared_state->cv.notify_all();
}
void set_exception(std::exception_ptr && e)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->exception = std::move(e);
shared_state->cv.notify_all();
}
public:
void set_progress(Progress && p)
{
assert(shared_state);
std::unique_lock _{shared_state->lock};
shared_state->progress = std::move(p);
shared_state->cv.notify_all();
}
bool is_cancelled()
{
return shared_state->cancelled;
}
};
template <typename Result, typename Progress, typename F, typename... Args>
future<Result, Progress> async(F && f, Args &&... args)
{
future<Result, Progress> fut;
fut.shared_state = std::make_shared<typename future<Result, Progress>::state>();
fut.shared_state->thread = std::thread([f, args...](auto token) {
try
{
token.set_result(std::invoke(f, token, args...));
}
catch (...)
{
token.set_exception(std::current_exception());
}
},
async_token<Result, Progress>(fut.shared_state));
return fut;
}
} // namespace utils
+1 -1
View File
@@ -19,9 +19,9 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include <cstdint>
namespace utils
{
+6 -6
View File
@@ -23,8 +23,8 @@
namespace utils
{
template<typename Rng, typename T>
bool contains(Rng&& range, const T& value)
template <typename Rng, typename T>
bool contains(Rng && range, const T & value)
{
auto it = std::find(range.begin(), range.end(), value);
@@ -32,10 +32,10 @@ bool contains(Rng&& range, const T& value)
}
// Check if all values of range2 are in range1
template<typename Rng1, typename Rng2>
bool contains_all(Rng1&& range1, Rng2&& range2)
template <typename Rng1, typename Rng2>
bool contains_all(Rng1 && range1, Rng2 && range2)
{
for(const auto& i: range2)
for (const auto & i: range2)
{
if (!contains(range1, i))
return false;
@@ -44,4 +44,4 @@ bool contains_all(Rng1&& range1, Rng2&& range2)
return true;
}
}
} // namespace utils
+5 -7
View File
@@ -20,12 +20,10 @@
#include <fstream>
#include <vector>
#include <span>
#include <concepts>
namespace utils
{
template<typename T>
template <typename T>
static std::vector<T> read_whole_file(std::filesystem::path filename)
{
std::ifstream file(filename, std::ios::binary | std::ios::ate);
@@ -34,7 +32,7 @@ static std::vector<T> read_whole_file(std::filesystem::path filename)
size_t size = file.tellg();
file.seekg(0);
if constexpr(sizeof(T) > 1)
if constexpr (sizeof(T) > 1)
size = size - size % sizeof(T);
std::vector<T> bytes(size / sizeof(T));
@@ -44,8 +42,8 @@ static std::vector<T> read_whole_file(std::filesystem::path filename)
return bytes;
}
template<typename T>
requires std::contiguous_iterator<typename T::iterator>
template <typename T>
requires std::contiguous_iterator<typename T::iterator>
static void write_whole_file(std::filesystem::path filename, T bytes)
{
std::ofstream file(filename, std::ios::binary);
@@ -53,4 +51,4 @@ static void write_whole_file(std::filesystem::path filename, T bytes)
file.write(reinterpret_cast<const char *>(bytes.data()), bytes.size() * sizeof(typename T::value_type));
}
}
} // namespace utils
+7 -16
View File
@@ -18,13 +18,12 @@
#pragma once
#include <spdlog/fmt/fmt.h>
#include <glm/fwd.hpp>
#include <spdlog/fmt/fmt.h>
// /usr/include/fmt/chrono.h:2017
template<glm::length_t L, typename T, glm::qualifier Q, typename Char>
template <glm::length_t L, typename T, glm::qualifier Q, typename Char>
struct fmt::formatter<glm::vec<L, T, Q>, Char> //: fmt::formatter<T>
{
// auto parse(basic_format_parse_context<Char>& ctx) const -> decltype(ctx.begin())
@@ -37,14 +36,13 @@ struct fmt::formatter<glm::vec<L, T, Q>, Char> //: fmt::formatter<T>
// return begin;
// }
auto parse(basic_format_parse_context<Char>& ctx) const // -> decltype(ctx.begin())
auto parse(basic_format_parse_context<Char> & ctx) const // -> decltype(ctx.begin())
{
return ctx.begin();
}
template <typename FormatContext>
auto format(const glm::vec<L, T, Q>& v, FormatContext& ctx) const -> decltype(ctx.out())
auto format(const glm::vec<L, T, Q> & v, FormatContext & ctx) const -> decltype(ctx.out())
{
if constexpr (L == 1)
{
@@ -66,11 +64,8 @@ struct fmt::formatter<glm::vec<L, T, Q>, Char> //: fmt::formatter<T>
return fmt::format_to(ctx.out(), "[ {}, {}, {}, {} ]", v.x, v.y, v.z, v.w);
}
// return formatter<std::string_view>(
// auto time = std::tm();
// time.tm_wday = static_cast<int>(wd.c_encoding());
// detail::get_locale loc(localized, ctx.locale());
@@ -78,25 +73,21 @@ struct fmt::formatter<glm::vec<L, T, Q>, Char> //: fmt::formatter<T>
// w.on_abbr_weekday();
// return w.out();
}
};
template<typename T, glm::qualifier Q, typename Char>
template <typename T, glm::qualifier Q, typename Char>
struct fmt::formatter<glm::qua<T, Q>, Char>
{
auto parse(basic_format_parse_context<Char>& ctx) const // -> decltype(ctx.begin())
auto parse(basic_format_parse_context<Char> & ctx) const // -> decltype(ctx.begin())
{
return ctx.begin();
}
template <typename FormatContext>
auto format(const glm::qua<T, Q>& q, FormatContext& ctx) const -> decltype(ctx.out())
auto format(const glm::qua<T, Q> & q, FormatContext & ctx) const -> decltype(ctx.out())
{
return fmt::format_to(ctx.out(), "[ {}, {}, {}, {} ]", q.w, q.x, q.y, q.z);
}
};
/*
+9 -9
View File
@@ -19,22 +19,23 @@
#pragma once
#include <bit>
#include <boost/pfr/core.hpp>
#include <type_traits>
#include <vulkan/vulkan.hpp>
#include <bit>
namespace utils
{
template<typename T>
struct is_vk_flags : std::false_type {};
template <typename T>
struct is_vk_flags : std::false_type
{};
template<typename T>
struct is_vk_flags<vk::Flags<T>> : std::true_type {};
template <typename T>
struct is_vk_flags<vk::Flags<T>> : std::true_type
{};
template<typename T>
template <typename T>
struct magic_hash
{
std::size_t operator()(const T & info) const noexcept
@@ -42,7 +43,6 @@ struct magic_hash
size_t h = 0;
boost::pfr::for_each_field(info, [&](const auto & member) {
using U = std::remove_cvref_t<decltype(member)>;
if constexpr (is_vk_flags<U>::value)
@@ -61,4 +61,4 @@ struct magic_hash
return h;
}
};
}
} // namespace utils
+19 -17
View File
@@ -26,33 +26,35 @@
namespace utils
{
template<typename T>
concept has_const_iterator = requires {
std::declval<typename T::const_iterator>();
};
template <typename T>
concept has_const_iterator = requires {
std::declval<typename T::const_iterator>();
};
template<typename T, bool> struct get_const_iterator_aux;
template <typename T, bool>
struct get_const_iterator_aux;
template<typename T> struct get_const_iterator_aux<T, true>
{
using const_iterator = T::const_iterator;
};
template <typename T>
struct get_const_iterator_aux<T, true>
{
using const_iterator = T::const_iterator;
};
template<typename T> struct get_const_iterator_aux<T, false>
{
using const_iterator = T::iterator;
};
template<typename T>
using get_const_iterator = get_const_iterator_aux<T, has_const_iterator<T>>::const_iterator;
template <typename T>
struct get_const_iterator_aux<T, false>
{
using const_iterator = T::iterator;
};
template <typename T>
using get_const_iterator = get_const_iterator_aux<T, has_const_iterator<T>>::const_iterator;
template <typename T>
struct enumerate_range
{
struct iterator
{
using underlying_iterator = std::conditional_t<std::is_const_v<T>, get_const_iterator<T>, typename T::iterator>;
using underlying_iterator = std::conditional_t<std::is_const_v<T>, get_const_iterator<T>, typename T::iterator>;
using underlying_value_type = std::conditional_t<std::is_const_v<T>, const typename T::value_type, typename T::value_type>;
size_t index;