-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwstring.cpp
123 lines (89 loc) · 2.63 KB
/
wstring.cpp
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
#include "simppl/wstring.h"
#include <cstring>
#include <cassert>
namespace simppl
{
namespace dbus
{
/*static*/
void WStringCodec::encode(DBusMessageIter& iter, const std::wstring& str)
{
static_assert(sizeof(uint32_t) == sizeof(wchar_t), "data types mapping does not match");
DBusMessageIter _iter;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32_AS_STRING, &_iter);
for (auto& t : str) {
Codec<uint32_t>::encode(_iter, (uint32_t)t);
}
dbus_message_iter_close_container(&iter, &_iter);
}
/*static*/
void WStringCodec::decode(DBusMessageIter& iter, std::wstring& str)
{
str.clear();
DBusMessageIter _iter;
simppl_dbus_message_iter_recurse(&iter, &_iter, DBUS_TYPE_ARRAY);
int count =
#if DBUS_MAJOR_VERSION == 1 && DBUS_MINOR_VERSION < 9
dbus_message_iter_get_array_len(&_iter) / sizeof(uint32_t);
#else
dbus_message_iter_get_element_count(&iter);
#endif
if (count > 0)
str.reserve(count);
while(dbus_message_iter_get_arg_type(&_iter) != 0)
{
uint32_t t;
Codec<uint32_t>::decode(_iter, t);
str.push_back((wchar_t)t);
}
// advance to next element
dbus_message_iter_next(&iter);
}
/*static*/
void WStringCodec::encode(DBusMessageIter& iter, const wchar_t* str)
{
DBusMessageIter _iter;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32_AS_STRING, &_iter);
while(str && *str) {
Codec<uint32_t>::encode(_iter, (uint32_t)*str++);
}
dbus_message_iter_close_container(&iter, &_iter);
}
/*static*/
void WStringCodec::decode(DBusMessageIter& iter, wchar_t*& str)
{
wchar_t* c_str = nullptr;
//assert(str == nullptr); // we allocate the string via Deserializer::alloc -> free with Deserializer::free
DBusMessageIter _iter;
simppl_dbus_message_iter_recurse(&iter, &_iter, DBUS_TYPE_ARRAY);
int count =
#if DBUS_MAJOR_VERSION == 1 && DBUS_MINOR_VERSION < 9
dbus_message_iter_get_array_len(&_iter) / sizeof(uint32_t);
#else
dbus_message_iter_get_element_count(&iter);
#endif
if (count > 0)
{
c_str = new wchar_t[count+1];
c_str[count] = 0;
int i = 0;
while(dbus_message_iter_get_arg_type(&_iter) != 0)
{
uint32_t t;
Codec<uint32_t>::decode(_iter, t);
c_str[i++] = (wchar_t)t;
}
str = c_str;
}
else
str = nullptr;
// advance to next element
dbus_message_iter_next(&iter);
}
/*static*/
std::ostream& WStringCodec::make_type_signature(std::ostream& os)
{
return Codec<uint32_t>::make_type_signature(os << DBUS_TYPE_ARRAY_AS_STRING);
}
} // namespace dbus
} // namespace simppl