70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
//
|
|
// Created by erki on 10/12/22.
|
|
//
|
|
|
|
#ifndef SKULLC_UTILITY_BYTES_HPP_
|
|
#define SKULLC_UTILITY_BYTES_HPP_
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <type_traits>
|
|
|
|
namespace Utility
|
|
{
|
|
|
|
template<typename T, std::size_t N>
|
|
T arrayToType(const std::uint8_t raw[N])
|
|
{
|
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
static_assert(sizeof(T) == N, "The raw data array is not the same size as T.");
|
|
|
|
T t;
|
|
std::memcpy(&t, raw, sizeof(T));
|
|
|
|
return t;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
T arrayToType(const std::array<std::uint8_t, N> raw)
|
|
{
|
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
static_assert(sizeof(T) == N, "The raw data array is not the same size as T.");
|
|
|
|
T t;
|
|
std::memcpy(&t, raw.data(), sizeof(T));
|
|
|
|
return t;
|
|
}
|
|
|
|
template<typename T>
|
|
constexpr T zeroInitialized()
|
|
{
|
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
|
|
T t;
|
|
std::memset(&t, 0, sizeof(T));
|
|
|
|
return t;
|
|
}
|
|
|
|
template<typename T>
|
|
void zeroInitialized(T& t)
|
|
{
|
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
|
|
std::memset(&t, 0, sizeof(T));
|
|
}
|
|
|
|
template<typename T>
|
|
void zeroInitialized(T* t)
|
|
{
|
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
|
|
std::memset(t, 0, sizeof(T));
|
|
}
|
|
|
|
}// namespace Utility
|
|
|
|
#endif//SKULLC_UTILITY_BYTES_HPP_
|