Sparkplug B C++ Library 1.0.0
Modern C++-23 implementation of Eclipse Sparkplug B 2.2 specification
Loading...
Searching...
No Matches
mqtt_handle.hpp
1// include/sparkplug/mqtt_handle.hpp
2#pragma once
3
4typedef void* MQTTAsync;
5
6namespace sparkplug {
7
8// RAII wrapper for MQTTAsync client handle
10public:
11 MQTTAsyncHandle() noexcept : client_(nullptr) {
12 }
13 explicit MQTTAsyncHandle(MQTTAsync client) noexcept : client_(client) {
14 }
15
16 ~MQTTAsyncHandle() noexcept;
17
18 // Non-copyable
19 MQTTAsyncHandle(const MQTTAsyncHandle&) = delete;
20 MQTTAsyncHandle& operator=(const MQTTAsyncHandle&) = delete;
21
22 // Movable
23 MQTTAsyncHandle(MQTTAsyncHandle&& other) noexcept : client_(other.client_) {
24 other.client_ = nullptr;
25 }
26
27 MQTTAsyncHandle& operator=(MQTTAsyncHandle&& other) noexcept {
28 if (this != &other) {
29 reset();
30 client_ = other.client_;
31 other.client_ = nullptr;
32 }
33 return *this;
34 }
35
36 // Access
37 MQTTAsync get() const noexcept {
38 return client_;
39 }
40 MQTTAsync operator*() const noexcept {
41 return client_;
42 }
43 explicit operator bool() const noexcept {
44 return client_ != nullptr;
45 }
46
47 // Modifiers
48 void reset() noexcept;
49 MQTTAsync release() noexcept {
50 MQTTAsync tmp = client_;
51 client_ = nullptr;
52 return tmp;
53 }
54
55private:
56 MQTTAsync client_;
57};
58
59} // namespace sparkplug