CVB++ 15.1
Loading...
Searching...
No Matches
decl_node.hpp
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <mutex>
6#include <vector>
7
8#include "../../global.hpp"
9#include "../../exception.hpp"
10#include "../../string.hpp"
11#include "../../utilities/system_info.hpp"
12
13#include "../genapi.hpp"
14
15namespace Cvb
16{
17
18 CVB_BEGIN_INLINE_NS
19
20 template <>
21 inline HandleGuard<GenApi::Node>::HandleGuard(void *handle) noexcept
22 : HandleGuard<GenApi::Node>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
23 {
24 }
25
26 namespace GenApi
27 {
28
30
36
38 class Node
39 {
40 public:
41 using GuardType = HandleGuard<Node>;
42
43 template <class T>
44 static std::shared_ptr<T> FromHandle(HandleGuard<Node> &&guard)
45 {
46 if (!guard.Handle())
47 throw std::runtime_error("handle must not be null");
48
49 static_assert(std::is_base_of<Node, T>::value, "CVB: Type must be derived from Node");
50 return std::make_shared<T>(std::move(guard));
51 }
52
53 static NodePtr FromName(const NodeMapPtr &nodeMap, const String &name);
54
55 Node(const Node &other) = delete;
56 Node &operator=(const Node &other) = delete;
57 Node(Node &&other) = delete;
58 Node &&operator=(Node &&other) = delete;
59
60 virtual ~Node()
61 {
62 if (nativeUpdateRegistered_)
63 CExports::NDeregisterUpdate(Handle(), cbID_);
64 }
65
67
73 void *Handle() const noexcept
74 {
75 return handle_.Handle();
76 }
77
79
86 String Name() const
87 {
88 return GetInfoAsString(NodeInfo::Name);
89 }
90
92
99 {
100 return GetInfoAsString(NodeInfo::DisplayName);
101 }
102
104
109 {
110 return GetInfoAsString(NodeInfo::ToolTip);
111 }
112
114
121 {
122 return GetInfoAsString(NodeInfo::Description);
123 }
124
126
131 {
132 return static_cast<Cvb::GenApi::AccessMode>(GetInfoAsInt(NodeInfo::AccessMode));
133 }
134
136
140 bool IsImplemented() const
141 {
143 }
144
146
155
157
165
167
175
177
185 {
186 return static_cast<Cvb::GenApi::CacheMode>(GetInfoAsInt(NodeInfo::CachingMode));
187 }
188
191
196 {
197 return GetInfoAsString(NodeInfo::EventID);
198 }
199
201
209 bool IsFeature() const
210 {
211 return GetInfoAsBool(NodeInfo::Feature);
212 }
213
215
222 bool IsDeprecated() const
223 {
224 return GetInfoAsBool(NodeInfo::Deprecated);
225 }
226
228
233 {
234 return static_cast<Cvb::GenApi::Visibility>(GetInfoAsInt(NodeInfo::Visibility));
235 }
236
238
245 NodePtr AliasNode() const;
246
248
254
256
260 void UnregisterEventUpdated(EventCookie eventCookie) noexcept;
261
263
267 NodeMapPtr NodeMap() const noexcept
268 {
269 return nodeMap_;
270 }
271
272 protected:
273 void NativeCall(std::function<CExports::cvbres_t()> fn) const
274 {
275 auto result = fn();
276 if (result < 0)
277 Utilities::SystemInfo::ThrowLastError(result);
278 }
279
280 template <class T>
281 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
282 {
283 T value;
284 auto result = fn(value);
285 if (result < 0)
286 Utilities::SystemInfo::ThrowLastError(result);
287 return value;
288 }
289
290 bool GetInfoAsBool(NodeInfo command) const
291 {
292 return (NativeCall<CExports::cvbbool_t>([&](CExports::cvbbool_t &value) {
293 return CExports::NInfoAsBoolean(Handle(), static_cast<CExports::TNodeInfo>(command), value);
294 }))
295 ? true
296 : false;
297 }
298
299 std::int64_t GetInfoAsInt(NodeInfo command) const
300 {
301 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
302 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(command), value);
303 }));
304 }
305
306 String GetInfoAsString(NodeInfo command) const
307 {
308 return NativeCall<String>([&](String &value) {
309 std::size_t bufferSize = 0;
310 auto bufferSizeResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
311 reinterpret_cast<Char *>(0), bufferSize);
312 if (bufferSizeResult < 0)
313 return bufferSizeResult;
314
315 std::vector<Char> buffer(bufferSize);
316 auto bufferResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
317 buffer.data(), bufferSize);
318 if (bufferResult < 0)
319 return bufferResult;
320
321 value = String(buffer.data());
322 return bufferResult;
323 });
324 }
325
326 int GetDependentNodeCount(NodeList type) const
327 {
328 CExports::cvbdim_t numNodes = 0;
329 auto result = CExports::NListCount(Handle(), static_cast<CExports::TNodeList>(type), numNodes);
330 if (result < 0 && result != ErrorCodes::CVB_WRONGOBJECT)
331 std::rethrow_exception(CvbException::FromCvbResult(result, "failed calling NListCount"));
332
333 return numNodes;
334 }
335
336 template <class T>
337 std::vector<std::shared_ptr<T>> GetDependentNodes(NodeList type) const;
338
339 explicit Node(HandleGuard<Node> &&guard) noexcept
340 : handle_(std::move(guard))
341 {
342 }
343
344 private:
345 friend class NodeMap;
346
347 static NodePtr FromHandle(const NodeMapPtr &nodeMap, HandleGuard<Node> &&guard);
348
349 static void __stdcall UpdatedCallback(CExports::NODE, void *pPrivate)
350 {
351 try
352 {
353 auto node = reinterpret_cast<Node *>(pPrivate);
354 node->updatedCarrierContainer_.Call<void(Node &)>(*node);
355 }
356 catch (...)
357 {
358 // swallow exception so they don't propagate to native library.
359 }
360 }
361
362 bool IsRegisterNode() const noexcept
363 {
364 CExports::cvbint64_t value = 0;
365 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(NodeInfo::RegisterLength), value)
366 >= 0
367 && value > 0;
368 }
369
370 HandleGuard<Node> handle_;
371
372 NodeMapPtr nodeMap_;
373
374 Internal::CarrierContainer updatedCarrierContainer_;
375
376 std::mutex nativeUpdateRegistrationMutex_;
377
378 void *cbID_ = nullptr;
379
380 bool nativeUpdateRegistered_ = false;
381 };
382
383 } // namespace GenApi
384
385 using GenApi::Node;
386
387 CVB_END_INLINE_NS
388
389} // namespace Cvb
Basic GenApi node for device feature access.
Definition decl_node.hpp:39
String DisplayName() const
Gets the display name of this node.
Definition decl_node.hpp:98
Cvb::GenApi::Visibility Visibility() const
Gets the complexity level of this node.
Definition decl_node.hpp:232
EventCookie RegisterEventUpdated(std::function< void(Node &)> handler)
Register a listener to node updated event.
Definition detail_node.hpp:133
bool IsFeature() const
Returns whether this node is considered a feature node.
Definition decl_node.hpp:209
bool IsWriteable() const
Helper to check whether this node is writable.
Definition decl_node.hpp:171
bool IsReadable() const
Helper to check whether this node is readable.
Definition decl_node.hpp:161
Cvb::GenApi::CacheMode CacheMode() const
Gets the cache mode of this node.
Definition decl_node.hpp:184
bool IsImplemented() const
Helper to check whether this node is implemented.
Definition decl_node.hpp:140
Cvb::GenApi::AccessMode AccessMode() const
Gets the access mode of this node.
Definition decl_node.hpp:130
String EventID() const
Nodes with an event identifier may become invalidated, if an event / message is delivered from the de...
Definition decl_node.hpp:195
NodeMapPtr NodeMap() const noexcept
Gets the node map this node resides in.
Definition decl_node.hpp:267
String Name() const
Gets the full name of this node.
Definition decl_node.hpp:86
bool IsAvailable() const
Helper to check whether this node is available.
Definition decl_node.hpp:150
bool IsDeprecated() const
Gets whether this node is considered deprecated.
Definition decl_node.hpp:222
String Description() const
Gets the long descriptive text of this node.
Definition decl_node.hpp:120
void UnregisterEventUpdated(EventCookie eventCookie) noexcept
Manually unregister a listener to the node updated event.
Definition detail_node.hpp:153
NodePtr AliasNode() const
Gets the node, that is an alias value for this node.
Definition detail_node.hpp:124
void * Handle() const noexcept
Classic API node handle.
Definition decl_node.hpp:73
String ToolTip() const
Gets the short descriptive text of this node.
Definition decl_node.hpp:108
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 version.hpp:11
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