-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol.h
More file actions
170 lines (139 loc) · 6.47 KB
/
Copy pathprotocol.h
File metadata and controls
170 lines (139 loc) · 6.47 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2025 The XYZ Protocol Authors. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==============================================================================*/
#ifndef XYZ_PROTOCOL_H_
#define XYZ_PROTOCOL_H_
#include <memory>
#include <mutex>
#include <unordered_map>
#include <utility>
namespace xyz {
template <typename T>
struct is_protocol : std::false_type {};
template <typename T, typename Alloc>
class protocol;
template <typename T, typename Alloc>
struct is_protocol<protocol<T, Alloc>> : std::true_type {};
template <typename T>
struct is_protocol_view : std::false_type {};
template <typename T>
class protocol_view;
template <typename T>
struct is_protocol_view<protocol_view<T>> : std::true_type {};
template <typename T>
concept not_protocol_or_view = !is_protocol<std::remove_cvref_t<T>>::value &&
!is_protocol_view<std::remove_cvref_t<T>>::value;
template <typename Protocol>
struct protocol_vtable_traits;
const void* get_mapped_vtable(const void* source_vtable_pointer,
const void* conversion_anchor,
std::size_t target_vtable_size,
void (*mapping_function)(const void* source,
void* target));
template <typename FromProtocol, typename ToProtocol>
const typename protocol_vtable_traits<ToProtocol>::const_vtable* get_vtable(
const typename protocol_vtable_traits<FromProtocol>::const_vtable*
source_vtable_pointer) {
using FromVtable =
typename protocol_vtable_traits<FromProtocol>::const_vtable;
using ToVtable = typename protocol_vtable_traits<ToProtocol>::const_vtable;
static const char conversion_anchor = 0;
auto mapping_function = [](const void* source, void* target) {
map_vtable_members(static_cast<const FromVtable*>(source),
static_cast<ToVtable*>(target));
};
return static_cast<const ToVtable*>(
get_mapped_vtable(source_vtable_pointer, &conversion_anchor,
sizeof(ToVtable), mapping_function));
}
template <typename FromProtocol, typename ToProtocol>
const typename protocol_vtable_traits<ToProtocol>::vtable* get_mutable_vtable(
const typename protocol_vtable_traits<FromProtocol>::vtable*
source_vtable_pointer) {
using FromVtable = typename protocol_vtable_traits<FromProtocol>::vtable;
using ToVtable = typename protocol_vtable_traits<ToProtocol>::vtable;
static const char conversion_anchor = 0;
auto mapping_function = [](const void* source, void* target) {
map_mutable_vtable_members(static_cast<const FromVtable*>(source),
static_cast<ToVtable*>(target));
};
return static_cast<const ToVtable*>(
get_mapped_vtable(source_vtable_pointer, &conversion_anchor,
sizeof(ToVtable), mapping_function));
}
template <typename Protocol, typename Allocator>
struct protocol_owning_vtable_traits;
template <typename FromProtocol, typename ToProtocol, typename Allocator>
const typename protocol_owning_vtable_traits<ToProtocol, Allocator>::vtable*
get_owning_vtable(const typename protocol_owning_vtable_traits<
FromProtocol, Allocator>::vtable* source_vtable_pointer) {
if (source_vtable_pointer == nullptr) {
return nullptr;
}
using FromVtable =
typename protocol_owning_vtable_traits<FromProtocol, Allocator>::vtable;
using ToVtable =
typename protocol_owning_vtable_traits<ToProtocol, Allocator>::vtable;
static const char conversion_anchor = 0;
auto mapping_function = [](const void* source, void* target) {
map_owning_vtable_members(static_cast<const FromVtable*>(source),
static_cast<ToVtable*>(target));
};
return static_cast<const ToVtable*>(
get_mapped_vtable(source_vtable_pointer, &conversion_anchor,
sizeof(ToVtable), mapping_function));
}
template <typename T, typename A = std::allocator<T>>
class protocol {
static_assert(
sizeof(T) == 0,
"The primary xyz::protocol template cannot be instantiated. "
"A partial specialization for T must be generated as a build "
"step. Use the xyz_generate_protocol CMake macro to produce "
"the required specialization.\n\n"
"Arguments:\n"
" CLASS_NAME: The name of the class to be generated.\n"
" INTERFACE: The path to the header file containing the interface "
"definition.\n"
" HEADER: The name of the header file to be included in the generated "
"file.\n"
" OUTPUT: The path where the generated header should be written.\n\n"
"Example usage:\n"
" xyz_generate_protocol(\n"
" CLASS_NAME MyInterface\n"
" INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/my_interface.h\n"
" HEADER my_interface.h\n"
" OUTPUT "
"${CMAKE_CURRENT_BINARY_DIR}/generated/protocol_MyInterface.h\n"
" )");
public:
constexpr bool valueless_after_move() const noexcept {
return false; // Placeholder implementation
}
};
template <typename T>
class protocol_view {
static_assert(
sizeof(T) == 0,
"The primary xyz::protocol_view template cannot be instantiated. "
"A partial specialization for T must be generated as a build "
"step. Use the xyz_generate_protocol CMake macro to produce "
"the required specialization.\n\n"
"Note: protocol_view specializations are automatically generated "
"alongside protocol specializations.");
};
} // namespace xyz
#endif // XYZ_PROTOCOL_H_