-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageAppend.cpp
More file actions
234 lines (207 loc) · 7.96 KB
/
MessageAppend.cpp
File metadata and controls
234 lines (207 loc) · 7.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "DBusUtils.hpp"
#include <iostream>
UDBus::MessageBuilder::MessageBuilder(Message& msg) noexcept
{
setMessage(msg);
}
void UDBus::MessageBuilder::setMessage(Message& msg) noexcept
{
this->message = &msg;
if (nodeStack.empty())
nodeStack.push(&node);
}
void UDBus::MessageBuilder::appendGenericBasic(char type, void* data) const noexcept
{
const auto f = [type, data, this](const AppendNode&) -> void
{
// If the iterator stack is empty it means that we can freely append to a generic iterator
if (message->iteratorStack.empty())
{
DBusMessageIter iter;
dbus_message_iter_init_append(message->get(), &iter);
// Wondering why strings are handled like this? Check the comment in DBusUtils.hpp L156
if (type == DBUS_TYPE_STRING)
{
auto* ptr = tempStrings[reinterpret_cast<uintptr_t>(data)].data();
dbus_message_iter_append_basic(&iter, type, &ptr);
}
else
dbus_message_iter_append_basic(&iter, type, data);
return;
}
// Else append to the deepest iterator always
// Wondering why strings are handled like this? Check the comment in DBusUtils.hpp L156
if (type == DBUS_TYPE_STRING)
{
auto* ptr = tempStrings[reinterpret_cast<uintptr_t>(data)].data();
message->iteratorStack.back().append_basic(type, &ptr);
}
else
message->iteratorStack.back().append_basic(type, data);
};
const char signature[2] = { type, '\0' };
if (layerDepth > 0)
{
nodeStack.top()->children.emplace_back(AppendNode{
.children = {},
.event = f,
.signature = signature
});
}
else
f(*nodeStack.top());
}
void UDBus::MessageBuilder::appendArrayBasic(char type, void* data, size_t n, size_t size) const noexcept
{
const auto f = [type, data, n, size, this](const AppendNode&) -> void
{
// If the iterator stack is empty we can append the array directly
if (message->iteratorStack.empty())
{
dbus_message_append_args(message->get(), DBUS_TYPE_ARRAY, type, &data, n, DBUS_TYPE_INVALID);
return;
}
// Otherwise, get the parent iterator and create a child iterator
auto& parent = message->iteratorStack.back();
auto& child = message->iteratorStack.emplace_back();
const char signature[] = { type, '\0' };
// Assign the child to the parent
parent.setAppend(*message, DBUS_TYPE_ARRAY, signature, child, false);
for (size_t i = 0; i < n; i++)
{
// Evil pointer magic to iterate a typeless array without template arguments
const auto tmp = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(data) + (i * size));
child.append_basic(type, tmp);
}
closeContainers();
};
const char signature[] = { DBUS_TYPE_ARRAY, type, '\0' };
if (layerDepth > 0)
{
nodeStack.top()->children.emplace_back(AppendNode{
.children = {},
.event = f,
.signature = signature
});
}
else
f(*nodeStack.top());
}
void UDBus::MessageBuilder::appendStructureEvent(const char type, const char* containedSignature) const noexcept
{
bool bRootIterator = false;
auto& stack = message->iteratorStack;
if (stack.empty())
bRootIterator = true;
auto& parent = bRootIterator ? stack.emplace_back() : stack.back();
auto& child = stack.emplace_back();
parent.setAppend(*message, type, containedSignature, child, bRootIterator);
}
void UDBus::MessageBuilder::sendMessage(AppendNode& node) noexcept
{
node.event(node);
for (auto& a : node.children)
sendMessage(a);
}
void UDBus::MessageBuilder::getSignature(AppendNode& node, std::string& signature) noexcept
{
if (node.bIgnore && node.signature.empty())
return;
// Append the type to the signature, except for structs where the actual signatures are wrapped in () or for dict
// entries where they are wrapped in {}
if (node.signature == DBUS_TYPE_STRUCT_AS_STRING)
signature += DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
else if (node.signature == DBUS_TYPE_DICT_ENTRY_AS_STRING)
signature += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
else
signature += node.signature;
// Variants need to get their own inner signature at any level
for (auto& a : node.children)
getSignature(a, node.signature == DBUS_TYPE_VARIANT_AS_STRING ? node.innerSignature : signature);
// Close the signature string
if (node.signature == DBUS_TYPE_STRUCT_AS_STRING)
signature += DBUS_STRUCT_END_CHAR_AS_STRING;
else if (node.signature == DBUS_TYPE_DICT_ENTRY_AS_STRING)
signature += DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
else if (node.signature == DBUS_TYPE_ARRAY_AS_STRING)
signature += node.innerSignature;
}
#define BEGIN_GENERIC_STRUCTURE(type, typeString, containedSignature) \
{ \
auto& a = nodeStack.top()->children.emplace_back(AppendNode{ \
.children = {}, \
.event = [this](const AppendNode& n) -> void \
{ \
appendStructureEvent(type, containedSignature); \
}, \
.signature = typeString, \
}); \
nodeStack.push(&a); \
layerDepth++; \
}
template <>
UDBus::MessageBuilder& UDBus::MessageBuilder::append<UDBus::MessageManipulators>(const MessageManipulators& op) noexcept
{
switch (op)
{
case BeginStruct:
BEGIN_GENERIC_STRUCTURE(DBUS_TYPE_STRUCT, DBUS_TYPE_STRUCT_AS_STRING, nullptr);
break;
case EndStruct:
endStructure();
break;
case BeginVariant:
BEGIN_GENERIC_STRUCTURE(DBUS_TYPE_VARIANT, DBUS_TYPE_VARIANT_AS_STRING, n.innerSignature.c_str());
break;
case EndVariant:
for (auto& a : nodeStack.top()->children)
getSignature(a, nodeStack.top()->innerSignature);
endStructure();
break;
case BeginArray:
BEGIN_GENERIC_STRUCTURE(DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING, n.innerSignature.c_str());
break;
case Next:
nodeStack.top()->innerSignature.clear();
break;
case EndArray:
for (auto& a : nodeStack.top()->children)
getSignature(a, nodeStack.top()->innerSignature);
endStructure();
break;
case BeginDictEntry:
BEGIN_GENERIC_STRUCTURE(DBUS_TYPE_DICT_ENTRY, DBUS_TYPE_DICT_ENTRY_AS_STRING, nullptr);
break;
case EndDictEntry:
endStructure();
break;
case EndMessage:
sendMessage(node);
break;
default:
break;
}
return *this;
}
void UDBus::MessageBuilder::closeContainers() const noexcept
{
(message->iteratorStack.end() - 2)->close_container(); // Close parent first
message->iteratorStack.pop_back(); // Pop child, this will close it too
if (message->iteratorStack.size() == 1)
message->iteratorStack.pop_back(); // Pop parent only if it's the last iterator
}
void UDBus::MessageBuilder::endStructure() noexcept
{
nodeStack.top()->children.emplace_back(AppendNode
{
.children = {},
.event = [this](const AppendNode&) -> void
{
closeContainers();
},
.signature = {},
.bIgnore = true
});
nodeStack.pop();
layerDepth--;
}