-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrophe.hh
208 lines (166 loc) · 6.75 KB
/
strophe.hh
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <fmt/core.h>
#include <memory>
#include <functional>
#include <type_traits>
#include <strophe.h>
namespace libstrophe {
template<typename T, typename CFun, typename DFun,
CFun &f_create, DFun &f_destroy, typename Base = T>
class type {
private:
T *_ptr;
protected:
typedef T* pointer_type;
inline type(T *ptr) : _ptr(ptr) {
}
template<typename Fun, Fun &func, int success = 0, typename... Args,
typename = std::enable_if_t<std::is_same_v<int, std::invoke_result_t<Fun, pointer_type, std::decay_t<Args>...>>>>
inline void call_checked(Args&&... args) {
int ret = func(*this, std::forward<Args>(args)...);
if (ret != success) throw std::runtime_error(
fmt::format("Strophe Error: expected {}, was {}", success, ret));
}
template<typename Fun, Fun &func, int success = 0, typename... Args,
typename = std::enable_if_t<std::is_same_v<void, std::invoke_result_t<Fun, pointer_type, std::decay_t<Args>...>>>>
inline void call(Args&&... args) {
func(*this, std::forward<Args>(args)...);
}
template<typename Fun, Fun &func, typename... Args,
typename = std::enable_if_t<!std::is_same_v<void, std::invoke_result_t<Fun, pointer_type, std::decay_t<std::decay_t<Args>>...>>>>
inline typename std::invoke_result_t<Fun, pointer_type, std::decay_t<std::decay_t<Args>>...>
call(Args&&... args) {
return func(*this, std::forward<Args>(args)...);
}
public:
inline explicit type() : _ptr(nullptr) {
}
template<typename... Args>
inline explicit type(Args&&... args) : type() {
_ptr = f_create(std::forward<Args>(args)...);
}
inline ~type() {
if (_ptr)
f_destroy(reinterpret_cast<Base*>(_ptr));
_ptr = nullptr;
}
type(const type &other) = delete; /* no copy construction */
type(type &&other) = default;
template<typename... Args>
inline void create(Args&&... args) {
if (_ptr)
f_destroy(reinterpret_cast<Base*>(_ptr));
_ptr = f_create(std::forward<Args>(args)...);
}
type& operator =(const type &other) = delete; /* no copy assignment */
type& operator =(type &&other) = default;
inline operator bool() const { return _ptr; }
inline T* operator *() { return _ptr; }
inline operator T*() { return _ptr; }
inline operator const T*() const { return _ptr; }
};
inline auto initialize = xmpp_initialize;
inline auto shutdown = xmpp_shutdown;
typedef type<xmpp_ctx_t,
decltype(xmpp_ctx_new), decltype(xmpp_ctx_free),
xmpp_ctx_new, xmpp_ctx_free> context_type;
class context : public context_type {
public:
using context_type::context_type;
inline auto set_verbosity(auto &&...args) {
return call<decltype(xmpp_ctx_set_verbosity),
xmpp_ctx_set_verbosity>(args...);
}
};
typedef type<xmpp_conn_t,
decltype(xmpp_conn_new), decltype(xmpp_conn_release),
xmpp_conn_new, xmpp_conn_release> connection_type;
class connection : public connection_type {
public:
using connection_type::connection_type;
inline connection(context& ctx) {
create(*ctx);
}
inline auto get_context(auto &&...args) {
return call<decltype(xmpp_conn_get_context),
xmpp_conn_get_context>(args...);
}
inline auto get_flags(auto &&...args) {
return call<decltype(xmpp_conn_get_flags),
xmpp_conn_get_flags>(args...);
}
inline auto set_keepalive(auto &&...args) {
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return call<decltype(xmpp_conn_set_keepalive),
xmpp_conn_set_keepalive>(args...);
#pragma GCC diagnostic pop
}
inline auto set_jid(auto &&...args) {
return call<decltype(xmpp_conn_set_jid),
xmpp_conn_set_jid>(args...);
}
inline auto set_pass(auto &&...args) {
return call<decltype(xmpp_conn_set_pass),
xmpp_conn_set_pass>(args...);
}
inline auto set_flags(auto &&...args) {
return call<decltype(xmpp_conn_set_flags),
xmpp_conn_set_flags>(args...);
}
inline auto send(auto &&...args) {
return call<decltype(xmpp_send), xmpp_send>(args...);
}
inline auto connect_client(auto &&...args) {
return call<decltype(xmpp_connect_client), xmpp_connect_client>(args...);
}
inline auto handler_add(auto &&...args) {
return call<decltype(xmpp_handler_add), xmpp_handler_add>(args...);
}
};
typedef type<xmpp_stanza_t,
decltype(xmpp_stanza_new), decltype(xmpp_stanza_release),
xmpp_stanza_new, xmpp_stanza_release> stanza_type;
class stanza : public stanza_type {
public:
using stanza_type::stanza_type;
inline static stanza reply(auto &&...args) {
return stanza(xmpp_stanza_reply(args...));
}
inline stanza get_name(auto &&...args) {
return call<decltype(xmpp_stanza_get_name),
xmpp_stanza_get_name>(args...);
}
inline stanza get_ns(auto &&...args) {
return call<decltype(xmpp_stanza_get_ns),
xmpp_stanza_get_ns>(args...);
}
inline stanza add_child(auto &&...args) {
call<decltype(xmpp_stanza_add_child),
xmpp_stanza_add_child>(args...);
return std::move(*this);
}
inline stanza set_name(auto &&...args) {
call<decltype(xmpp_stanza_set_name),
xmpp_stanza_set_name>(args...);
return std::move(*this);
}
inline stanza set_ns(auto &&...args) {
call<decltype(xmpp_stanza_set_ns),
xmpp_stanza_set_ns>(args...);
return std::move(*this);
}
inline stanza set_text(auto &&...args) {
call<decltype(xmpp_stanza_set_text),
xmpp_stanza_set_text>(args...);
return std::move(*this);
}
inline stanza set_type(auto &&...args) {
call<decltype(xmpp_stanza_set_type),
xmpp_stanza_set_type>(args...);
return std::move(*this);
}
};
}