CVB++ 15.0
decl_node.hpp
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <vector>
6
7#include "../../global.hpp"
8#include "../../exception.hpp"
9#include "../../string.hpp"
10#include "../../utilities/system_info.hpp"
11
12#include "../genapi.hpp"
13
14namespace Cvb
15{
16
17 CVB_BEGIN_INLINE_NS
18
19 template <>
20 inline HandleGuard<GenApi::Node>::HandleGuard(void *handle) noexcept
21 : HandleGuard<GenApi::Node>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
22 {
23 }
24
25 namespace GenApi
26 {
27
29
35
37 class Node
38 {
39 public:
40 using GuardType = HandleGuard<Node>;
41
42 template <class T>
43 static std::shared_ptr<T> FromHandle(HandleGuard<Node> &&guard)
44 {
45 if (!guard.Handle())
46 throw std::runtime_error("handle must not be null");
47
48 static_assert(std::is_base_of<Node, T>::value, "CVB: Type must be derived from Node");
49 return std::make_shared<T>(std::move(guard));
50 }
51
52 static NodePtr FromName(const NodeMapPtr &nodeMap, const String &name);
53
54 Node(const Node &other) = delete;
55 Node &operator=(const Node &other) = delete;
56 Node(Node &&other) = delete;
57 Node &&operator=(Node &&other) = delete;
58
59 virtual ~Node()
60 {
61 CExports::NDeregisterUpdate(Handle(), cbID_);
62 }
63
65
71 void *Handle() const noexcept
72 {
73 return handle_.Handle();
74 }
75
77
84 String Name() const
85 {
86 return GetInfoAsString(NodeInfo::Name);
87 }
88
90
97 {
98 return GetInfoAsString(NodeInfo::DisplayName);
99 }
100
102
107 {
108 return GetInfoAsString(NodeInfo::ToolTip);
109 }
110
112
119 {
120 return GetInfoAsString(NodeInfo::Description);
121 }
122
124
129 {
130 return static_cast<Cvb::GenApi::AccessMode>(GetInfoAsInt(NodeInfo::AccessMode));
131 }
132
134
138 bool IsImplemented() const
139 {
141 }
142
144
153
155
163
165
173
175
183 {
184 return static_cast<Cvb::GenApi::CacheMode>(GetInfoAsInt(NodeInfo::CachingMode));
185 }
186
189
194 {
195 return GetInfoAsString(NodeInfo::EventID);
196 }
197
199
207 bool IsFeature() const
208 {
209 return GetInfoAsBool(NodeInfo::Feature);
210 }
211
213
220 bool IsDeprecated() const
221 {
222 return GetInfoAsBool(NodeInfo::Deprecated);
223 }
224
226
231 {
232 return static_cast<Cvb::GenApi::Visibility>(GetInfoAsInt(NodeInfo::Visibility));
233 }
234
236
243 NodePtr AliasNode() const;
244
246
252
254
258 void UnregisterEventUpdated(EventCookie eventCookie) noexcept;
259
261
265 NodeMapPtr NodeMap() const noexcept
266 {
267 return nodeMap_;
268 }
269
270 protected:
271 void NativeCall(std::function<CExports::cvbres_t()> fn) const
272 {
273 auto result = fn();
274 if (result < 0)
275 Utilities::SystemInfo::ThrowLastError(result);
276 }
277
278 template <class T>
279 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
280 {
281 T value;
282 auto result = fn(value);
283 if (result < 0)
284 Utilities::SystemInfo::ThrowLastError(result);
285 return value;
286 }
287
288 bool GetInfoAsBool(NodeInfo command) const
289 {
290 return (NativeCall<CExports::cvbbool_t>([&](CExports::cvbbool_t &value) {
291 return CExports::NInfoAsBoolean(Handle(), static_cast<CExports::TNodeInfo>(command), value);
292 }))
293 ? true
294 : false;
295 }
296
297 std::int64_t GetInfoAsInt(NodeInfo command) const
298 {
299 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
300 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(command), value);
301 }));
302 }
303
304 String GetInfoAsString(NodeInfo command) const
305 {
306 return NativeCall<String>([&](String &value) {
307 std::size_t bufferSize = 0;
308 auto bufferSizeResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
309 reinterpret_cast<Char *>(0), bufferSize);
310 if (bufferSizeResult < 0)
311 return bufferSizeResult;
312
313 std::vector<Char> buffer(bufferSize);
314 auto bufferResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
315 buffer.data(), bufferSize);
316 if (bufferResult < 0)
317 return bufferResult;
318
319 value = String(buffer.data());
320 return bufferResult;
321 });
322 }
323
324 int GetDependentNodeCount(NodeList type) const
325 {
326 CExports::cvbdim_t numNodes = 0;
327 auto result = CExports::NListCount(Handle(), static_cast<CExports::TNodeList>(type), numNodes);
328 if (result < 0 && result != ErrorCodes::CVB_WRONGOBJECT)
329 std::rethrow_exception(CvbException::FromCvbResult(result, "failed calling NListCount"));
330
331 return numNodes;
332 }
333
334 template <class T>
335 std::vector<std::shared_ptr<T>> GetDependentNodes(NodeList type) const;
336
337 explicit Node(HandleGuard<Node> &&guard) noexcept
338 : handle_(std::move(guard))
339 {
340 }
341
342 private:
343 static void __stdcall UpdatedCallback(CExports::NODE, void *pPrivate)
344 {
345 try
346 {
347 auto node = reinterpret_cast<Node *>(pPrivate);
348 node->updatedCarrierContainer_.Call<void(Node &)>(*node);
349 }
350 catch (...)
351 {
352 // swallow exception so they don't propagate to native library.
353 }
354 }
355
356 bool IsRegisterNode() const noexcept
357 {
358 CExports::cvbint64_t value = 0;
359 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(NodeInfo::RegisterLength), value)
360 >= 0
361 && value > 0;
362 }
363
364 HandleGuard<Node> handle_;
365
366 NodeMapPtr nodeMap_;
367
368 Internal::CarrierContainer updatedCarrierContainer_;
369
370 void *cbID_ = nullptr;
371 };
372
373 } // namespace GenApi
374
375 using GenApi::Node;
376
377 CVB_END_INLINE_NS
378
379} // namespace Cvb
Basic GenApi node for device feature access.
Definition decl_node.hpp:38
String DisplayName() const
Gets the display name of this node.
Definition decl_node.hpp:96
Cvb::GenApi::Visibility Visibility() const
Gets the complexity level of this node.
Definition decl_node.hpp:230
EventCookie RegisterEventUpdated(std::function< void(Node &)> handler)
Register a listener to node updated event.
Definition detail_node.hpp:132
bool IsFeature() const
Returns whether this node is considered a feature node.
Definition decl_node.hpp:207
bool IsWriteable() const
Helper to check whether this node is writable.
Definition decl_node.hpp:169
bool IsReadable() const
Helper to check whether this node is readable.
Definition decl_node.hpp:159
Cvb::GenApi::CacheMode CacheMode() const
Gets the cache mode of this node.
Definition decl_node.hpp:182
bool IsImplemented() const
Helper to check whether this node is implemented.
Definition decl_node.hpp:138
Cvb::GenApi::AccessMode AccessMode() const
Gets the access mode of this node.
Definition decl_node.hpp:128
String EventID() const
Nodes with an event identifier may become invalidated, if an event / message is delivered from the de...
Definition decl_node.hpp:193
NodeMapPtr NodeMap() const noexcept
Gets the node map this node resides in.
Definition decl_node.hpp:265
String Name() const
Gets the full name of this node.
Definition decl_node.hpp:84
bool IsAvailable() const
Helper to check whether this node is available.
Definition decl_node.hpp:148
bool IsDeprecated() const
Gets whether this node is considered deprecated.
Definition decl_node.hpp:220
String Description() const
Gets the long descriptive text of this node.
Definition decl_node.hpp:118
void UnregisterEventUpdated(EventCookie eventCookie) noexcept
Manually unregister a listener to the node updated event.
Definition detail_node.hpp:138
NodePtr AliasNode() const
Gets the node, that is an alias value for this node.
Definition detail_node.hpp:123
void * Handle() const noexcept
Classic API node handle.
Definition decl_node.hpp:71
String ToolTip() const
Gets the short descriptive text of this node.
Definition decl_node.hpp:106
cvbbool_t ReleaseObject(OBJ &Object)
T make_shared(T... args)
T move(T... args)
const int CVB_WRONGOBJECT
Wrong object.
Definition exception.hpp:79
Namespace for GenApi based device configuration.
Definition decl_fw_updater.hpp:29
std::shared_ptr< Node > NodePtr
Convenience shared pointer for Node.
Definition genapi.hpp:71
Visibility
Feature complexity level.
Definition genapi.hpp:238
CacheMode
Defines how the value is cached.
Definition genapi.hpp:223
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition genapi.hpp:27
AccessMode
Access possibility of the node.
Definition genapi.hpp:188
@ ReadOnly
Node can only be read.
Definition genapi.hpp:214
@ NotAvailable
Definition genapi.hpp:202
@ WriteOnly
Node can only be written to.
Definition genapi.hpp:216
@ ReadWrite
Node can be read and written to.
Definition genapi.hpp:218
@ NotImplemented
Definition genapi.hpp:196
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
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49