Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
memory.hpp
1#ifndef TOPAZ_CORE_MEMORY_HPP
2#define TOPAZ_CORE_MEMORY_HPP
3#include <span>
4#include <ranges>
5#include <type_traits>
6
7namespace tz
8{
9 template<typename T>
10 constexpr auto view_bytes(T&& t) -> std::conditional_t<std::is_const_v<T>, std::span<const std::byte>, std::span<std::byte>>
11 {
12 if constexpr(requires{std::ranges::contiguous_range<T>;})
13 {
14 // we are a range
15 using V = std::ranges::range_value_t<T>;
16 std::span<V> range{std::begin(t), std::end(t)};
17 if constexpr(std::is_const_v<V>)
18 {
19 return std::as_bytes(range);
20 }
21 else
22 {
23 return std::as_writable_bytes(range);
24 }
25 }
26 else
27 {
28 // we are not a range
29 static_assert(std::is_standard_layout_v<T>, "view_bytes must be provided a contiguous range or a standard-layout-type. You have provided neither.");
30 // we are a scalar-y value
31 std::span<T, 1> span{&t};
32 if constexpr(std::is_const_v<T>)
33 {
34 return std::as_bytes(span);
35 }
36 else
37 {
38 return std::as_writable_bytes(span);
39 }
40 }
41 }
42}
43
44#endif // TOPAZ_CORE_MEMORY_HPP