CVB++ 15.0
value_node.hpp
1#pragma once
2
3#include <typeinfo>
4#include <vector>
5
6#include "../_cexports/c_gev_server.h"
7
8#include "../global.hpp"
9
10#include "_decl/decl_node.hpp"
11
12namespace Cvb
13{
14 CVB_BEGIN_INLINE_NS
15 namespace GevServer
16 {
18
20 class ValueNode : public GevServer::Node
21 {
23 enum ReplyStatus
24 {
25 Success,
26 AccessDenied,
27 Busy,
28 NotImplemented,
29 DataOverrun,
30 InvalidParameter,
31 WrongConfig,
32 LocalProblem
33 };
34
35 public:
36 ValueNode(const ValueNode &other) = delete;
37 ValueNode &operator=(const ValueNode &other) = delete;
38 ValueNode(ValueNode &&other) = delete;
39 ValueNode &operator=(ValueNode &&other) = delete;
40 virtual ~ValueNode()
41 {
42 if (readCallbackID_)
43 CVB_CALL_CAPI(GSNUnregisterEvent(Handle(), CExports::TGSNodeEvent::GSNE_Read, readCallbackID_));
44
45 if (writeCallbackID_)
46 CVB_CALL_CAPI(GSNUnregisterEvent(Handle(), CExports::TGSNodeEvent::GSNE_Write, writeCallbackID_));
47 }
48
51
55 virtual bool IsStreamable() const
56 {
57 return GetInfoAsInt(NodeInfo::Streamable) != 0;
58 }
59
62
65 virtual void SetIsStreamable(const std::int64_t &value)
66 {
67 NativeCall([&]() {
68 return CVB_CALL_CAPI(GSNSetInfoAsInteger(Handle(), CExports::TGSNodeInfo::GSNI_Streamable, (value ? 1 : 0)));
69 });
70 }
71
73
79 template <class Rep, class Period>
81 {
82 auto timeMs = GetInfoAsInt(NodeInfo::PollingTime);
83 if (timeMs > 0)
85 else
87 }
88
90
94 virtual String ToString() const
95 {
96 auto bufferSize = NativeCall<size_t>(
97 [&](size_t &size) { return CExports::GSNGetAsStringTyped(Handle(), reinterpret_cast<Char *>(0), size); });
98 bufferSize += sizeof(Char);
99 std::vector<Char> buffer(bufferSize);
100 NativeCall([&]() { return CExports::GSNGetAsStringTyped(Handle(), buffer.data(), bufferSize); });
101 return buffer.data();
102 }
103
105
109 virtual void FromString(const String &value)
110 {
111 NativeCall([&]() { return CExports::GSNSetAsStringTyped(Handle(), value.data()); });
112 }
113
115
121 {
122 auto holder = Internal::CbCarrier<void(ValueNode &)>::Create(handler);
123 return updatedCarrierContainerWritten_.Register(holder);
124 }
125
127
132 {
133 updatedCarrierContainerWritten_.Unregister(eventCookie);
134 }
135
137
143 {
144 auto holder = Internal::CbCarrier<void(ValueNode &)>::Create(handler);
145 return updatedCarrierContainerOnRead_.Register(holder);
146 }
147
149
153 void UnregisterEventOnReadUpdated(EventCookie eventCookie) noexcept
154 {
155 updatedCarrierContainerOnRead_.Unregister(eventCookie);
156 }
157
158 protected:
159 explicit ValueNode(HandleGuard<Node> &&guard)
160 : Node(std::move(guard))
161 {
162 switch (NodeType(Handle()))
163 {
166 break; // No value events
167 default:
168 int resultRegister = CVB_CALL_CAPI(GSNRegisterEventWithStatus(
169 Handle(), CExports::TGSNodeEvent::GSNE_Read, &ValueNode::EventOnReadCallback, this, readCallbackID_));
170 if (resultRegister < 0)
171 std::rethrow_exception(CvbException::FromCvbResult(resultRegister, "failed to register updated handler"));
172
173 resultRegister =
174 CVB_CALL_CAPI(GSNRegisterEventWithStatus(Handle(), CExports::TGSNodeEvent::GSNE_Write,
175 &ValueNode::EventWrittenCallback, this, writeCallbackID_));
176 if (resultRegister < 0)
177 std::rethrow_exception(CvbException::FromCvbResult(resultRegister, "failed to register updated handler"));
178 break;
179 }
180 }
181
182 private:
183 static void __stdcall EventWrittenCallback(CExports::cvbres_t &status, void *pPrivate)
184 {
185 if (status == ReplyStatus::Success)
186 {
187 try
188 {
189 auto node = reinterpret_cast<ValueNode *>(pPrivate);
190 node->updatedCarrierContainerWritten_.Call<void(ValueNode &)>(*node);
191 }
192 catch (...)
193 {
194 }
195 }
196 switch (status)
197 {
198 case ReplyStatus::Success:
199 status = ErrorCodes::CVB_OK;
200 break;
201 case ReplyStatus::AccessDenied:
202 status = ErrorCodes::CVB_ACCESS;
203 break;
204 case ReplyStatus::Busy:
205 status = ErrorCodes::CVB_BUSY;
206 break;
207 case ReplyStatus::NotImplemented:
209 break;
210 case ReplyStatus::DataOverrun:
212 break;
213 case ReplyStatus::InvalidParameter:
215 break;
216 case ReplyStatus::WrongConfig:
218 break;
219 default:
220 case ReplyStatus::LocalProblem:
221 status = ErrorCodes::CVB_ERROR;
222 break;
223 }
224 }
225
226 static void __stdcall EventOnReadCallback(CExports::cvbres_t &status, void *pPrivate)
227 {
228 if (status == ReplyStatus::Success)
229 {
230 try
231 {
232 auto node = reinterpret_cast<ValueNode *>(pPrivate);
233 node->updatedCarrierContainerOnRead_.Call<void(ValueNode &)>(*node);
234 }
235 catch (...)
236 {
237 }
238 }
239
240 switch (status)
241 {
242 case ReplyStatus::Success:
243 status = ErrorCodes::CVB_OK;
244 break;
245 case ReplyStatus::AccessDenied:
246 status = ErrorCodes::CVB_ACCESS;
247 break;
248 case ReplyStatus::Busy:
249 status = ErrorCodes::CVB_BUSY;
250 break;
251 case ReplyStatus::NotImplemented:
253 break;
254 case ReplyStatus::DataOverrun:
256 break;
257 case ReplyStatus::InvalidParameter:
259 break;
260 case ReplyStatus::WrongConfig:
262 break;
263 default:
264 case ReplyStatus::LocalProblem:
265 status = ErrorCodes::CVB_ERROR;
266 break;
267 }
268 }
269
270 Internal::CarrierContainer updatedCarrierContainerWritten_;
271 Internal::CarrierContainer updatedCarrierContainerOnRead_;
272
273 std::size_t readCallbackID_{0};
274 std::size_t writeCallbackID_{0};
275 };
276 CVB_END_INLINE_NS
277 } // namespace GevServer
278} // namespace Cvb
Basic GevServer node for device feature access.
Definition decl_node.hpp:34
void * Handle() const noexcept
Classic API node handle.
Definition decl_node.hpp:102
Base class for all nodes that have a value.
Definition value_node.hpp:21
virtual String ToString() const
Returns this node's value as a string representation.
Definition value_node.hpp:94
void UnregisterEventOnReadUpdated(EventCookie eventCookie) noexcept
Manually unregister a listener to the node on read event.
Definition value_node.hpp:153
EventCookie RegisterEventOnReadUpdated(std::function< void(ValueNode &)> handler)
Register a listener to node on read event.
Definition value_node.hpp:142
virtual void SetIsStreamable(const std::int64_t &value)
Sets whether this node should be used when the camera settings are stored.
Definition value_node.hpp:65
virtual void FromString(const String &value)
Sets this node's value from the string value.
Definition value_node.hpp:109
virtual bool IsStreamable() const
Gets whether this node should be used when the camera settings are stored.
Definition value_node.hpp:55
void UnregisterEventWrittenUpdated(EventCookie eventCookie) noexcept
Manually unregister a listener to the node written event.
Definition value_node.hpp:131
EventCookie RegisterEventWrittenUpdated(std::function< void(ValueNode &)> handler)
Register a listener to node written event.
Definition value_node.hpp:120
std::chrono::duration< Rep, Period > PollingTime() const
Gets the polling time of this value.
Definition value_node.hpp:80
const int CVB_BUSY
Hardware busy.
Definition exception.hpp:57
const int CVB_ERROR
Generic unspecified error.
Definition exception.hpp:23
const int CVB_NOTENOUGHDATA
Too few data available for a calculation.
Definition exception.hpp:65
const int CVB_OK
No error occurred.
Definition exception.hpp:21
const int CVB_PARAMETER
Parameter error.
Definition exception.hpp:25
const int CVB_ACCESS
Access error.
Definition exception.hpp:111
const int CVB_NOTSUPPORTED
A certain feature is not supported.
Definition exception.hpp:59
const int CVB_OVERFLOW
Input value was too big or did lead to a too big result.
Definition exception.hpp:107
Describes a GenICam Pixel Format Naming Convention (PFNC) compatible image memory buffer with possibl...
Definition decl_int_swiss_knife_node.hpp:11
@ PollingTime
Gets the polling time in ms.
Definition gevserver.hpp:204
@ Streamable
Information on the streamability of the node.
Definition gevserver.hpp:199
NodeType
Available node types.
Definition gevserver.hpp:162
@ String
Node is a string node (no reg).
Definition gevserver.hpp:168
@ Category
Node is a category node.
Definition gevserver.hpp:165
@ EnumEntry
Node is an enumeration entry node (no reg).
Definition gevserver.hpp:173
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
T rethrow_exception(T... args)