// 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 #include #include #include #include namespace libstrophe { template class type { private: T *_ptr; protected: typedef T* pointer_type; inline type(T *ptr) : _ptr(ptr) { } template...>>>> inline void call_checked(Args&&... args) { int ret = func(*this, std::forward(args)...); if (ret != success) throw std::runtime_error( fmt::format("Strophe Error: expected {}, was {}", success, ret)); } template...>>>> inline void call(Args&&... args) { func(*this, std::forward(args)...); } template>...>>>> inline typename std::invoke_result_t>...> call(Args&&... args) { return func(*this, std::forward(args)...); } public: inline explicit type() : _ptr(nullptr) { } template inline explicit type(Args&&... args) : type() { _ptr = f_create(std::forward(args)...); } inline ~type() { if (_ptr) f_destroy(reinterpret_cast(_ptr)); _ptr = nullptr; } type(const type &other) = delete; /* no copy construction */ type(type &&other) = default; template inline void create(Args&&... args) { if (_ptr) f_destroy(reinterpret_cast(_ptr)); _ptr = f_create(std::forward(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 context_type; class context : public context_type { public: using context_type::context_type; inline auto set_verbosity(auto &&...args) { return call(args...); } }; typedef type 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(args...); } inline auto get_flags(auto &&...args) { return call(args...); } inline auto set_keepalive(auto &&...args) { #pragma GCC diagnostic ignored "-Wdeprecated-declarations" return call(args...); #pragma GCC diagnostic pop } inline auto set_jid(auto &&...args) { return call(args...); } inline auto set_pass(auto &&...args) { return call(args...); } inline auto set_flags(auto &&...args) { return call(args...); } inline auto send(auto &&...args) { return call(args...); } inline auto connect_client(auto &&...args) { return call(args...); } inline auto handler_add(auto &&...args) { return call(args...); } }; typedef type 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(args...); } inline stanza get_ns(auto &&...args) { return call(args...); } inline stanza add_child(auto &&...args) { call(args...); return std::move(*this); } inline stanza set_name(auto &&...args) { call(args...); return std::move(*this); } inline stanza set_ns(auto &&...args) { call(args...); return std::move(*this); } inline stanza set_text(auto &&...args) { call(args...); return std::move(*this); } inline stanza set_type(auto &&...args) { call(args...); return std::move(*this); } }; }