forked from martinhaefner/simppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.cpp
71 lines (51 loc) · 1.37 KB
/
string.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
#include "simppl/string.h"
#include <cstring>
#include <cassert>
namespace simppl
{
namespace dbus
{
/*static*/
void StringCodec::encode(DBusMessageIter& iter, const std::string& str)
{
char* c_str = const_cast<char*>(str.c_str());
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &c_str);
}
/*static*/
void StringCodec::decode(DBusMessageIter& iter, std::string& str)
{
char* c_str = nullptr;
simppl_dbus_message_iter_get_basic(&iter, &c_str, DBUS_TYPE_STRING);
if (c_str)
{
str.assign(c_str);
}
else
str.clear();
}
/*static*/
void StringCodec::encode(DBusMessageIter& iter, const char* str)
{
char* c_str = const_cast<char*>(str);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &c_str);
}
/*static*/
void StringCodec::decode(DBusMessageIter& iter, char*& str)
{
assert(str == nullptr); // we allocate the string via Deserializer::alloc -> free with Deserializer::free
char* c_str = nullptr;
simppl_dbus_message_iter_get_basic(&iter, &c_str, DBUS_TYPE_STRING);
// FIXME trouble with allocated memory in case of exception
if (c_str)
{
str = (char*)new char[strlen(c_str)+1];
strcpy(str, c_str);
}
}
/*static*/
std::ostream& StringCodec::make_type_signature(std::ostream& os)
{
return os << DBUS_TYPE_STRING_AS_STRING;
}
} // namespace dbus
} // namespace simppl