forked from martinhaefner/simppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.cpp
109 lines (75 loc) · 1.85 KB
/
error.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
#include "simppl/error.h"
#include <cassert>
#include <cstring>
#include "simppl/string.h"
namespace simppl
{
namespace dbus
{
Error::Error(const char* name, const char* msg, uint32_t serial)
: name_and_message_(nullptr)
, message_(nullptr)
, serial_(SIMPPL_INVALID_SERIAL)
{
assert(name);
// TODO check name for valid dbus name (<atom>.<atom>)
set_members(name, msg, serial);
}
Error::Error()
: name_and_message_(nullptr)
, message_(nullptr)
, serial_(SIMPPL_INVALID_SERIAL)
{
// NOOP
}
Error::Error(Error&& rhs)
{
name_and_message_ = rhs.name_and_message_;
message_ = rhs.message_;
serial_ = rhs.serial_;
rhs.name_and_message_ = nullptr;
rhs.message_ = nullptr;
rhs.serial_ = 0; // FIXME INVALID
}
Error::~Error()
{
delete[] name_and_message_;
name_and_message_ = nullptr;
message_ = nullptr;
}
void Error::set_members(const char* name, const char* msg, uint32_t serial)
{
if (name_and_message_)
delete[] name_and_message_;
serial_ = serial;
size_t capacity = strlen(name) + 1;
if (msg && strlen(msg))
capacity += strlen(msg)+1;
name_and_message_ = new char[capacity];
strcpy(name_and_message_, name);
if (msg && strlen(msg))
{
message_ = name_and_message_ + strlen(name) + 1;
strcpy(message_, msg);
}
else
message_ = name_and_message_ + strlen(name_and_message_);
}
message_ptr_t Error::make_reply_for(DBusMessage& req, const char* class_name) const
{
return make_message(dbus_message_new_error(&req, class_name ? class_name : name(), message()));
}
const char* Error::what() const throw()
{
return name_and_message_;
}
const char* Error::name() const
{
return name_and_message_;
}
const char* Error::message() const
{
return message_ ? message_ : "";
}
} // namespace dbus
} // namespace simppl