AsmGrader 0.0.0
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <concepts>
8#include <cstddef>
9#include <cstdint>
10#include <string>
11#include <string_view>
12#include <type_traits>
13
14namespace asmgrader {
15
16class MemoryIOBase;
17template <typename T>
18struct MemoryIOSerde;
19
20// TODO: If efficiency with ptrace becomes an issue, consider an implementation with mmap
21
22template <typename T>
23concept MemoryReadSupported = requires(std::uintptr_t address, MemoryIOBase& mio) {
24 { MemoryIOSerde<T>::read(address, mio) } -> std::same_as<Result<T>>;
25};
26template <typename T>
27concept MemoryWriteSupported = requires(const T& data) {
28 { MemoryIOSerde<T>::to_bytes(data) } -> std::same_as<ByteVector>;
29};
30template <typename T>
32
33namespace detail {
34
35// TODO: Ranges & byte array compat
36// All string types are compatible
37template <typename T, typename U>
38struct MemoryIOCompatImpl : std::false_type
39{
40};
41
42template <typename T>
43concept CharLike = sizeof(T) == 1 &&
44 (std::is_fundamental_v<std::remove_cvref_t<T>> || std::same_as<std::remove_cvref_t<T>, std::byte>);
45
46template <CharLike U>
47struct MemoryIOCompatImpl<std::string, U*> : std::true_type
48{
49};
50
51template <CharLike U>
52struct MemoryIOCompatImpl<std::string_view, U*> : std::true_type
53{
54};
55
56// Pointers and std::uintptr_t are compatible
57template <typename U>
58struct MemoryIOCompatImpl<std::uintptr_t, U*> : std::true_type
59{
60};
61
62template <typename T, typename U>
64{
65 using NormT = std::decay_t<T>;
66 using NormU = std::decay_t<U>;
67
68 static constexpr bool value =
69 (std::is_trivially_constructible_v<NormT, NormU> && std::is_trivially_constructible_v<NormU, NormT>) ||
71};
72
73} // namespace detail
74
79template <typename T, typename U>
81
84static_assert(MemoryIOCompatible<std::string_view, decltype("Hello")>);
85
88
89} // namespace asmgrader
A trait for types that are compatible in how they are read and written to memory.
Definition concepts.hpp:80
Definition concepts.hpp:31
Definition concepts.hpp:23
Definition concepts.hpp:27
Definition concepts.hpp:43
Definition asm_buffer.hpp:19
static Result< T > read(std::uintptr_t address, MemoryIOBase &mio)
Definition memory_io_serde.hpp:79
static ByteVector to_bytes(const T &data)
Definition memory_io_serde.hpp:85
Definition concepts.hpp:39
Definition concepts.hpp:64
static constexpr bool value
Definition concepts.hpp:68
std::decay_t< U > NormU
Definition concepts.hpp:66
std::decay_t< T > NormT
Definition concepts.hpp:65