forked from getml/reflect-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcepts.hpp
More file actions
65 lines (56 loc) · 2.73 KB
/
Copy pathconcepts.hpp
File metadata and controls
65 lines (56 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef RFL_CONCEPTS_HPP_
#define RFL_CONCEPTS_HPP_
#include <concepts>
#include <type_traits>
#include <cstdint>
#include <cstddef>
#include <iterator>
namespace rfl {
/// @brief Concept for byte-like types that can be used in contiguous containers
/// Includes char, signed char, unsigned char, std::byte, and uint8_t
template<typename T>
concept ByteLike = std::same_as<T, char> ||
std::same_as<T, signed char> ||
std::same_as<T, unsigned char> ||
std::same_as<T, std::uint8_t> ||
std::same_as<T, std::byte>;
/// @brief Concept for containers with a contiguous sequence of byte-like types
/// Requires:
/// - Container has a value_type that is byte-like
/// - Container provides data() method returning a pointer to contiguous memory
/// - Container provides size() method returning the number of elements
/// - Container supports range-based for loops (begin/end)
template<typename Container>
concept ContiguousByteContainer = requires(const Container& c) {
typename Container::value_type;
{ c.data() } -> std::convertible_to<const typename Container::value_type*>;
{ c.size() } -> std::convertible_to<std::size_t>;
{ c.begin() } -> std::input_iterator;
{ c.end() } -> std::input_iterator;
requires ByteLike<typename Container::value_type>;
requires std::contiguous_iterator<decltype(c.begin())>;
};
/// @brief Concept for mutable containers with a contiguous sequence of byte-like types
/// Extends ContiguousByteContainer with mutable access requirements
template<typename Container>
concept MutableContiguousByteContainer = ContiguousByteContainer<Container> && requires(Container& c) {
{ c.data() } -> std::convertible_to<typename Container::value_type*>;
{ c.begin() } -> std::output_iterator<typename Container::value_type>;
{ c.end() } -> std::output_iterator<typename Container::value_type>;
};
/// @brief Concept for back-insertable byte containers (like std::vector<uint8_t>)
/// Useful for containers that can grow dynamically during serialization
template<typename Container>
concept BackInsertableByteContainer = ContiguousByteContainer<Container> && requires(Container& c, typename Container::value_type v) {
c.push_back(v);
c.reserve(std::size_t{});
{ c.capacity() } -> std::convertible_to<std::size_t>;
};
/// @brief Concept for byte spans or views (read-only, non-owning containers)
/// Includes std::span<const uint8_t>, std::string_view when used with char data, etc.
template<typename Container>
concept ByteSpanLike = ContiguousByteContainer<Container> &&
std::is_trivially_copyable_v<Container> &&
std::is_trivially_destructible_v<Container>;
} // namespace rfl
#endif // RFL_CONCEPTS_HPP_