CVB++ 15.0
decl_notify_observable.hpp
1#pragma once
2
3#include <cstring>
4#include <vector>
5
6#include "../../global.hpp"
7#include "../../string.hpp"
8
9#include "../driver.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 namespace Driver
17 {
18
20
21 class NotifyArgs
22 {
23 public:
24 NotifyArgs(void *data, int size, NotifyDataType dataType) noexcept
25 : data_(data)
26 , size_(size)
27 , dataType_(dataType)
28 {
29 }
30
32
36 void *Data() const noexcept
37 {
38 return data_;
39 }
40
42
46 int Size() const noexcept
47 {
48 return size_;
49 }
50
52
56 NotifyDataType DataType() const noexcept
57 {
58 return dataType_;
59 }
60
62
67 template <typename T>
68 T Data() const noexcept
69 {
73 "CVB: Unsupported notify data type!");
74 }
75
76 private:
77 mutable void *data_ = nullptr;
78 int size_ = 0;
80 };
81
82 template <>
83 inline std::int64_t NotifyArgs::Data<std::int64_t>() const noexcept
84 {
85 return *reinterpret_cast<std::int64_t *>(data_);
86 }
87
88 template <>
89 inline double NotifyArgs::Data<double>() const noexcept
90 {
91 return *reinterpret_cast<double *>(data_);
92 }
93
94 template <>
95 inline String NotifyArgs::Data<String>() const noexcept
96 {
97 std::string ansiString(reinterpret_cast<char *>(data_));
98 return String(ansiString.begin(), ansiString.end());
99 }
100
101 template <>
102 inline bool NotifyArgs::Data<bool>() const noexcept
103 {
104 return *reinterpret_cast<bool *>(data_);
105 }
106
107 template <>
108 inline std::vector<std::uint8_t> NotifyArgs::Data<std::vector<std::uint8_t>>() const noexcept
109 {
110 std::vector<std::uint8_t> data(static_cast<std::size_t>(size_));
111 std::memcpy(&data[0], data_, data.size());
112 return data;
113 }
114
116
117 class NotifyObservable
118 {
119
120 public:
121 NotifyObservable(const NotifyObservable &other) = delete;
122 NotifyObservable &operator=(const NotifyObservable &other) = delete;
123 NotifyObservable(NotifyObservable &&other) = delete;
124 NotifyObservable &operator=(NotifyObservable &&other) = delete;
125 ~NotifyObservable();
126
127 NotifyObservable(const DevicePtr &parent, int index);
128
130
134 int ID() const noexcept
135 {
136 return eventNumber_;
137 }
138
140
144 String Description() const noexcept
145 {
146 return description_;
147 }
148
150
154 bool IsAvailable() const;
155
157
163
165
169 void UnregisterEvent(EventCookie eventCookie) noexcept;
170
171 private:
172 static void __stdcall Callback(CExports::CVNotifyEvent_t, void *Buf, size_t Size,
173 CExports::CVNotifyDatatype_t DataType, void *UserData)
174 {
175 try
176 {
177 auto notifyObservable = reinterpret_cast<NotifyObservable *>(UserData);
178 notifyObservable->carrierContainer_.Call<void(NotifyArgs)>(
179 NotifyArgs(Buf, static_cast<int>(Size), static_cast<NotifyDataType>(DataType)));
180 }
181 catch (...)
182 {
183 // swallow exception so they don't propagate to native library.
184 }
185 }
186
187 DevicePtr parent_;
188
189 int eventNumber_ = 0;
190
191 String description_;
192
193 std::intptr_t cookie_ = 0;
194
195 Internal::CarrierContainer carrierContainer_;
196 };
197
198 } // namespace Driver
199
200 using Driver::NotifyArgs;
202
203 CVB_END_INLINE_NS
204
205} // namespace Cvb
Data type description for an image plane.
Definition data_type.hpp:23
Event argument for notification events.
Definition decl_notify_observable.hpp:22
T Data() const noexcept
The typed data of the event.
Definition decl_notify_observable.hpp:68
int Size() const noexcept
Size of the data delivered with the event.
Definition decl_notify_observable.hpp:46
NotifyDataType DataType() const noexcept
The data type of the data delivered with the event.
Definition decl_notify_observable.hpp:56
void * Data() const noexcept
The raw data of the event.
Definition decl_notify_observable.hpp:36
Single notify event observable.
Definition decl_notify_observable.hpp:118
String Description() const noexcept
The string identifier of this event.
Definition decl_notify_observable.hpp:144
bool IsAvailable() const
Gets whether the current device can actually handle this event.
Definition detail_notify_observable.hpp:51
int ID() const noexcept
Get the native ID.
Definition decl_notify_observable.hpp:134
void UnregisterEvent(EventCookie eventCookie) noexcept
Manually unregister a listener to this observable.
Definition detail_notify_observable.hpp:68
EventCookie RegisterEvent(std::function< void(NotifyArgs)> handler)
Register a listener to this observable.
Definition detail_notify_observable.hpp:62
void * Data() const noexcept
The raw data of the event.
Definition decl_notify_observable.hpp:36
Namespace for driver or device related operations.
Definition decl_composite.hpp:28
NotifyDataType
Data type delivered by the event.
Definition driver.hpp:358
@ String
String value.
Definition driver.hpp:366
@ Void
No data.
Definition driver.hpp:360
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98