File utility improved

+ minor fixes
This commit is contained in:
Nekotekina 2016-01-06 02:52:48 +03:00
parent 5029dff73a
commit b3e3c68f15
75 changed files with 839 additions and 964 deletions

View file

@ -25,6 +25,7 @@
#include <cstdint>
#include <climits>
#include <cmath>
#include <cerrno>
#include <atomic>
#include <mutex>
#include <thread>
@ -78,7 +79,7 @@ public:
{
}
operator bool() const //template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>> operator T() const
operator bool() const
{
return m_value != 0;
}
@ -86,13 +87,15 @@ public:
CHECK_SIZE_ALIGN(b8, 1, 1);
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>> inline T align(const T& value, u64 align)
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
inline T align(const T& value, u64 align)
{
return static_cast<T>((value + (align - 1)) & ~(align - 1));
}
// copy null-terminated string from std::string to char array with truncation
template<std::size_t N> inline void strcpy_trunc(char(&dst)[N], const std::string& src)
template<std::size_t N>
inline void strcpy_trunc(char(&dst)[N], const std::string& src)
{
const std::size_t count = src.size() >= N ? N - 1 : src.size();
std::memcpy(dst, src.c_str(), count);
@ -100,7 +103,8 @@ template<std::size_t N> inline void strcpy_trunc(char(&dst)[N], const std::strin
}
// copy null-terminated string from char array to char array with truncation
template<std::size_t N, std::size_t N2> inline void strcpy_trunc(char(&dst)[N], const char(&src)[N2])
template<std::size_t N, std::size_t N2>
inline void strcpy_trunc(char(&dst)[N], const char(&src)[N2])
{
const std::size_t count = N2 >= N ? N - 1 : N2;
std::memcpy(dst, src, count);
@ -108,13 +112,15 @@ template<std::size_t N, std::size_t N2> inline void strcpy_trunc(char(&dst)[N],
}
// returns true if all array elements are unique and sorted in ascending order
template<typename T, std::size_t N> constexpr bool is_ascending(const T(&array)[N], std::size_t from = 0)
template<typename T, std::size_t N>
constexpr bool is_ascending(const T(&array)[N], std::size_t from = 0)
{
return from >= N - 1 ? true : array[from] < array[from + 1] ? is_ascending(array, from + 1) : false;
}
// get (first) array element equal to `value` or nullptr if not found
template<typename T, std::size_t N, typename T2> constexpr const T* static_search(const T(&array)[N], const T2& value, std::size_t from = 0)
template<typename T, std::size_t N, typename T2>
constexpr const T* static_search(const T(&array)[N], const T2& value, std::size_t from = 0)
{
return from >= N ? nullptr : array[from] == value ? array + from : static_search(array, value, from + 1);
}
@ -135,7 +141,8 @@ struct explicit_bool_t
}
};
template<typename T1, typename T2, typename T3 = const char*> struct triplet_t
template<typename T1, typename T2, typename T3 = const char*>
struct triplet_t
{
T1 first;
T2 second;
@ -154,18 +161,28 @@ template<typename T1, typename T2, typename T3 = const char*> struct triplet_t
#define alignof32(type) static_cast<u32>(alignof(type))
// return 32 bit .size() for container
template<typename T> inline auto size32(const T& container) -> decltype(static_cast<u32>(container.size()))
template<typename T>
inline auto size32(const T& container) -> decltype(static_cast<u32>(container.size()))
{
const auto size = container.size();
return size >= 0 && size <= UINT32_MAX ? static_cast<u32>(size) : throw std::length_error(__FUNCTION__);
}
// return 32 bit size for an array
template<typename T, std::size_t Size> constexpr u32 size32(const T(&)[Size])
template<typename T, std::size_t Size>
constexpr u32 size32(const T(&)[Size])
{
return Size >= 0 && Size <= UINT32_MAX ? static_cast<u32>(Size) : throw std::length_error(__FUNCTION__);
}
#define CONCATENATE_DETAIL(x, y) x ## y
#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)
#define SWITCH(expr) for (const auto& CONCATENATE(_switch_, __LINE__) = (expr);;) { const auto& _switch_ = CONCATENATE(_switch_, __LINE__);
#define CCASE(value) } constexpr std::remove_reference_t<decltype(_switch_)> CONCATENATE(_value_, __LINE__) = value; if (_switch_ == CONCATENATE(_value_, __LINE__)) {
#define VCASE(value) } if (_switch_ == (value)) {
#define DEFAULT } /* must be the last one */
#define WRAP_EXPR(expr) [&]{ return expr; }
#define COPY_EXPR(expr) [=]{ return expr; }
#define PURE_EXPR(expr) [] { return expr; }
@ -174,8 +191,8 @@ template<typename T, std::size_t Size> constexpr u32 size32(const T(&)[Size])
#define IS_INTEGRAL(t) (std::is_integral<t>::value || std::is_same<std::decay_t<t>, u128>::value)
#define IS_INTEGER(t) (std::is_integral<t>::value || std::is_enum<t>::value || std::is_same<std::decay_t<t>, u128>::value)
#define IS_BINARY_COMPARABLE(t1, t2) (IS_INTEGER(t1) && IS_INTEGER(t2) && sizeof(t1) == sizeof(t2))
#define CHECK_ASSERTION(expr) if (expr) {} else throw EXCEPTION("Assertion failed: " #expr)
#define CHECK_SUCCESS(expr) if (s32 _r = (expr)) throw EXCEPTION(#expr " failed (0x%x)", _r)
#define CHECK_ASSERTION(expr) if (expr) {} else throw EXCEPTION("Assertion failed: %s", #expr)
#define CHECK_SUCCESS(expr) if (s32 _r = (expr)) throw EXCEPTION("Failure: %s -> 0x%x", #expr, _r)
// Some forward declarations for the ID manager
template<typename T> struct id_traits;