2022-05-13 17:01:19 +02:00
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
template<class T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2022-06-05 18:35:52 +02:00
|
|
|
inline typename std::unique_ptr<T> make_unique_nothrow(size_t num) noexcept {
|
2022-05-13 17:01:19 +02:00
|
|
|
return std::unique_ptr<T>(new (std::nothrow) std::remove_extent_t<T>[num]());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class... Args>
|
|
|
|
std::shared_ptr<T> make_shared_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
|
|
|
|
return std::shared_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
|
|
|
|
}
|
2023-06-14 18:46:06 +02:00
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
std::string string_format(const std::string &format, Args... args) {
|
|
|
|
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
|
|
|
auto size = static_cast<size_t>(size_s);
|
|
|
|
auto buf = std::make_unique<char[]>(size);
|
|
|
|
std::snprintf(buf.get(), size, format.c_str(), args...);
|
|
|
|
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
|
|
|
|
}
|
2024-05-08 17:32:15 +02:00
|
|
|
|
|
|
|
// those work only in powers of 2
|
|
|
|
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
|
|
|
|
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)
|