CVB++ 15.0
Loading...
Searching...
No Matches
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
33
35 class Node
36 {
37 public:
38 using GuardType = HandleGuard<Node>;
39
40 template <class T>
41 static std::shared_ptr<T> FromHandle(HandleGuard<Node> &&guard)
42 {
43 if (!guard.Handle())
44 throw std::runtime_error("handle must not be null");
45
46 static_assert(std::is_base_of<Node, T>::value, "CVB: Type must be derived from Node");
47 return std::make_shared<T>(std::move(guard));
48 }
49
50 static NodePtr FromName(const NodeMapPtr &nodeMap, const String &name);
51
52 Node(const Node &other) = delete;
53 Node &operator=(const Node &other) = delete;
54 Node(Node &&other) = delete;
55 Node &&operator=(Node &&other) = delete;
56
57 virtual ~Node()
58 {
59 CExports::NDeregisterUpdate(Handle(), cbID_);
60 }
61
63
69 void *Handle() const noexcept
70 {
71 return handle_.Handle();
72 }
73
75
82 String Name() const
83 {
84 return GetInfoAsString(NodeInfo::Name);
85 }
86
88
95 {
96 return GetInfoAsString(NodeInfo::DisplayName);
97 }
98
100
105 {
106 return GetInfoAsString(NodeInfo::ToolTip);
107 }
108
110
117 {
118 return GetInfoAsString(NodeInfo::Description);
119 }
120
122
127 {
128 return static_cast<Cvb::GenApi::AccessMode>(GetInfoAsInt(NodeInfo::AccessMode));
129 }
130
132
136 bool IsImplemented() const
137 {
139 }
140
142
151
153
161
163
171
173
181 {
182 return static_cast<Cvb::GenApi::CacheMode>(GetInfoAsInt(NodeInfo::CachingMode));
183 }
184
187
192 {
193 return GetInfoAsString(NodeInfo::EventID);
194 }
195
197
205 bool IsFeature() const
206 {
207 return GetInfoAsBool(NodeInfo::Feature);
208 }
209
211
218 bool IsDeprecated() const
219 {
220 return GetInfoAsBool(NodeInfo::Deprecated);
221 }
222
224
229 {
230 return static_cast<Cvb::GenApi::Visibility>(GetInfoAsInt(NodeInfo::Visibility));
231 }
232
234
241 NodePtr AliasNode() const;
242
244
250
252
256 void UnregisterEventUpdated(EventCookie eventCookie) noexcept;
257
259
263 NodeMapPtr NodeMap() const noexcept
264 {
265 return nodeMap_;
266 }
267
268 protected:
269 void NativeCall(std::function<CExports::cvbres_t()> fn) const
270 {
271 auto result = fn();
272 if (result < 0)
273 Utilities::SystemInfo::ThrowLastError(result);
274 }
275
276 template <class T>
277 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
278 {
279 T value;
280 auto result = fn(value);
281 if (result < 0)
282 Utilities::SystemInfo::ThrowLastError(result);
283 return value;
284 }
285
286 bool GetInfoAsBool(NodeInfo command) const
287 {
288 return (NativeCall<CExports::cvbbool_t>([&](CExports::cvbbool_t &value) {
289 return CExports::NInfoAsBoolean(Handle(), static_cast<CExports::TNodeInfo>(command), value);
290 }))
291 ? true
292 : false;
293 }
294
295 std::int64_t GetInfoAsInt(NodeInfo command) const
296 {
297 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
298 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(command), value);
299 }));
300 }
301
302 String GetInfoAsString(NodeInfo command) const
303 {
304 return NativeCall<String>([&](String &value) {
305 std::size_t bufferSize = 0;
306 auto bufferSizeResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
307 reinterpret_cast<Char *>(0), bufferSize);
308 if (bufferSizeResult < 0)
309 return bufferSizeResult;
310
311 std::vector<Char> buffer(bufferSize);
312 auto bufferResult = CExports::NInfoAsStringTyped(Handle(), static_cast<CExports::TNodeInfo>(command),
313 buffer.data(), bufferSize);
314 if (bufferResult < 0)
315 return bufferResult;
316
317 value = String(buffer.data());
318 return bufferResult;
319 });
320 }
321
322 int GetDependentNodeCount(NodeList type) const
323 {
324 CExports::cvbdim_t numNodes = 0;
325 auto result = CExports::NListCount(Handle(), static_cast<CExports::TNodeList>(type), numNodes);
326 if (result < 0 && result != ErrorCodes::CVB_WRONGOBJECT)
327 std::rethrow_exception(CvbException::FromCvbResult(result, "failed calling NListCount"));
328
329 return numNodes;
330 }
331
332 template <class T>
333 std::vector<std::shared_ptr<T>> GetDependentNodes(NodeList type) const;
334
335 explicit Node(HandleGuard<Node> &&guard) noexcept
336 : handle_(std::move(guard))
337 {
338 }
339
340 private:
341 static void __stdcall UpdatedCallback(CExports::NODE, void *pPrivate)
342 {
343 try
344 {
345 auto node = reinterpret_cast<Node *>(pPrivate);
346 node->updatedCarrierContainer_.Call<void(Node &)>(*node);
347 }
348 catch (...)
349 {
350 // swallow exception so they don't propagate to native library.
351 }
352 }
353
354 bool IsRegisterNode() const noexcept
355 {
356 CExports::cvbint64_t value = 0;
357 return CExports::NInfoAsInteger(Handle(), static_cast<CExports::TNodeInfo>(NodeInfo::RegisterLength), value)
358 >= 0
359 && value > 0;
360 }
361
362 HandleGuard<Node> handle_;
363
364 NodeMapPtr nodeMap_;
365
366 Internal::CarrierContainer updatedCarrierContainer_;
367
368 void *cbID_ = nullptr;
369 };
370
371 } // namespace GenApi
372
373 using GenApi::Node;
374
375 CVB_END_INLINE_NS
376
377} // namespace Cvb
Basic GenApi node for device feature access.
Definition decl_node.hpp:36
String DisplayName() const
Gets the display name of this node.
Definition decl_node.hpp:94
Cvb::GenApi::Visibility Visibility() const
Gets the complexity level of this node.
Definition decl_node.hpp:228
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:205
bool IsWriteable() const
Helper to check whether this node is writable.
Definition decl_node.hpp:167
bool IsReadable() const
Helper to check whether this node is readable.
Definition decl_node.hpp:157
Cvb::GenApi::CacheMode CacheMode() const
Gets the cache mode of this node.
Definition decl_node.hpp:180
bool IsImplemented() const
Helper to check whether this node is implemented.
Definition decl_node.hpp:136
Cvb::GenApi::AccessMode AccessMode() const
Gets the access mode of this node.
Definition decl_node.hpp:126
String EventID() const
Nodes with an event identifier may become invalidated, if an event / message is delivered from the de...
Definition decl_node.hpp:191
NodeMapPtr NodeMap() const noexcept
Gets the node map this node resides in.
Definition decl_node.hpp:263
String Name() const
Gets the full name of this node.
Definition decl_node.hpp:82
bool IsAvailable() const
Helper to check whether this node is available.
Definition decl_node.hpp:146
bool IsDeprecated() const
Gets whether this node is considered deprecated.
Definition decl_node.hpp:218
String Description() const
Gets the long descriptive text of this node.
Definition decl_node.hpp:116
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:69
String ToolTip() const
Gets the short descriptive text of this node.
Definition decl_node.hpp:104
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:66
Visibility
Feature complexity level.
Definition genapi.hpp:233
CacheMode
Defines how the value is cached.
Definition genapi.hpp:218
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition genapi.hpp:22
AccessMode
Access possibility of the node.
Definition genapi.hpp:183
@ ReadOnly
Node can only be read.
Definition genapi.hpp:209
@ NotAvailable
Definition genapi.hpp:197
@ WriteOnly
Node can only be written to.
Definition genapi.hpp:211
@ ReadWrite
Node can be read and written to.
Definition genapi.hpp:213
@ NotImplemented
Definition genapi.hpp:191
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