codex
Loading...
Searching...
No Matches
mutable.hpp
Go to the documentation of this file.
1#ifndef CODEX_LAMBDA_MUTABLE_H
2#define CODEX_LAMBDA_MUTABLE_H
3
4#include <array>
5#include <cstddef>
6#include <cstring>
7#include <numeric>
8#include <ranges>
9#include <type_traits>
10
11namespace {
12
14template<typename T> concept arithmetic = std::is_arithmetic_v<T>;
15
17inline auto as_bytes(const void *p)
18 { return static_cast<const std::byte*>(p); }
19
21template<arithmetic T> inline auto arg_size(const T&) { return sizeof(T); }
22
24inline auto arg_ptr(const arithmetic auto &t) { return as_bytes(&t); }
25
27inline auto arg_size(const std::ranges::sized_range auto &r)
28 { return r.size() * sizeof(decltype(*begin(r))); }
29
31inline auto arg_ptr(const std::ranges::range auto &r)
32 { return as_bytes(&*begin(r)); }
33
36 std::byte *p;
37 const std::size_t *s;
38 void operator()(const auto &x) {
39 std::memcpy(this->p, arg_ptr(x), *this->s);
40 this->p += *this->s++;
41 }
42};
43
44}
45
46namespace codex {
47
51void copy_mutable_lambda(const auto &...ts, std::vector<std::byte> *v) {
52 const std::array sizes = {arg_size(ts)...};
53 v->resize(std::reduce(begin(sizes), end(sizes)));
54 auto copy = [p = v->data(), s = sizes.data()](const auto &x) mutable
55 { std::memcpy(p, arg_ptr(x), *s); p += *s++; };
56 (..., copy(ts));
57}
58
62void copy_function_object(const auto &...ts, std::vector<std::byte> *v) {
63 const std::array sizes = {arg_size(ts)...};
64 v->resize(std::reduce(begin(sizes), end(sizes)));
65 function_obj copy = {v->data(), sizes.data()};
66 (..., copy(ts));
67}
68
69}
70
71#endif
#define T(f, T, C)
Definition bench.cpp:136
Definition power.hpp:18
#define x
Definition gcc14.c:1
auto arg_ptr(const arithmetic auto &t)
Overload set that determines the address of its argument's content.
Definition mutable.hpp:24
auto as_bytes(const void *p)
Helper function to reduce the number of casts.
Definition mutable.hpp:17
auto arg_size(const T &)
Overload set that determines the size in bytes of its argument.
Definition mutable.hpp:21
constexpr codex::vec3 v
Definition rotate_test.cpp:12
Definition rotate.hpp:6
void copy_mutable_lambda(const auto &...ts, std::vector< std::byte > *v)
memcpyes each ts value into v (using a lambda expression).
Definition mutable.hpp:51
void copy_function_object(const auto &...ts, std::vector< std::byte > *v)
memcpyes each ts value into v (using a function object).
Definition mutable.hpp:62
codex::refl::field_tuple_t< E > T
Definition soa.cpp:9
#define r(x, y)
Definition std2.c:13
#define p()
Definition std2.c:11
#define t(a)
Definition std2.c:10
Equivalent to the lambda expression in codex::copy_mutable_lambda.
Definition mutable.hpp:35
void operator()(const auto &x)
Definition mutable.hpp:38
constexpr const char * data(void) const
Definition utils.hpp:18
constexpr fixed_string s
Definition test.cpp:6