CVB++ 15.1
Loading...
Searching...
No Matches
detail_node.hpp
1#pragma once
2
3#include "../../_cexports/c_gen_api.h"
4
5#include "../../global.hpp"
6#include "../../exception.hpp"
7
8#include "../_decl/decl_node.hpp"
9#include "../_decl/decl_node_map.hpp"
10#include "../boolean_node.hpp"
11#include "../category_node.hpp"
12#include "../command_node.hpp"
13#include "../enumeration_node.hpp"
14#include "../enum_entry_node.hpp"
15#include "../float_reg_node.hpp"
16#include "../float_node.hpp"
17#include "../int_reg_node.hpp"
18#include "../integer_node.hpp"
19#include "../string_reg_node.hpp"
20#include "../string_node.hpp"
21#include "../port_node.hpp"
22#include "../register_node.hpp"
23
24namespace Cvb
25{
26
27 CVB_BEGIN_INLINE_NS
28
29 namespace GenApi
30 {
31
32 inline NodePtr Node::FromName(const NodeMapPtr &nodeMap, const String &name)
33 {
34
35 CExports::NODEMAP handle = nullptr;
36 auto resultGet = CExports::NMGetNodeTyped(nodeMap->Handle(), name.c_str(), handle);
37 if (resultGet < 0)
38 std::rethrow_exception(CvbException::FromCvbResult(resultGet, "failed to get node"));
39
40 HandleGuard<Node> guard(handle);
41 return FromHandle(nodeMap, std::move(guard));
42 }
43
44 inline NodePtr Node::FromHandle(const NodeMapPtr &nodeMap, HandleGuard<Node> &&guard)
45 {
46 auto handle = guard.Handle();
47
48 CExports::TNodeType nodeType = CExports::TNodeType::NT_Invalid;
49 auto resultType = CExports::NType(handle, nodeType);
50 if (resultType < 0)
51 std::rethrow_exception(CvbException::FromCvbResult(resultType, "failed to get node type"));
52
53 CExports::cvbint64_t value = 0;
54 bool isRegister =
55 CExports::NInfoAsInteger(guard.Handle(), static_cast<CExports::TNodeInfo>(NodeInfo::RegisterLength), value)
56 >= 0
57 && value > 0;
58
59 NodePtr node;
60 switch (nodeType)
61 {
62
63 case CExports::TNodeType::NT_Invalid:
64 case CExports::TNodeType::NT_Unknown:
65 throw std::runtime_error("invalid node type");
66
67 case CExports::TNodeType::NT_Boolean:
68 node = std::make_shared<BooleanNode>(std::move(guard));
69 break;
70
71 case CExports::TNodeType::NT_Category:
72 node = std::make_shared<CategoryNode>(std::move(guard));
73 break;
74
75 case CExports::TNodeType::NT_Command:
76 node = std::make_shared<CommandNode>(std::move(guard));
77 break;
78
79 case CExports::TNodeType::NT_Enumeration:
80 node = std::make_shared<EnumerationNode>(std::move(guard));
81 break;
82
83 case CExports::TNodeType::NT_EnumEntry:
84 node = std::make_shared<EnumEntryNode>(std::move(guard));
85 break;
86
87 case CExports::TNodeType::NT_Float:
88 if (isRegister)
89 node = std::make_shared<FloatRegNode>(std::move(guard));
90 else
91 node = std::make_shared<FloatNode>(std::move(guard));
92 break;
93
94 case CExports::TNodeType::NT_Integer:
95 if (isRegister)
96 node = std::make_shared<IntRegNode>(std::move(guard));
97 else
98 node = std::make_shared<IntegerNode>(std::move(guard));
99 break;
100
101 case CExports::TNodeType::NT_String:
102 if (isRegister)
103 node = std::make_shared<StringRegNode>(std::move(guard));
104 else
105 node = std::make_shared<StringNode>(std::move(guard));
106 break;
107
108 case CExports::TNodeType::NT_Port:
109 node = std::make_shared<PortNode>(std::move(guard));
110 break;
111
112 default:
113 if (isRegister)
114 node = std::make_shared<RegisterNode>(std::move(guard));
115 else
116 node = std::shared_ptr<Node>(new Node(std::move(guard)));
117 break;
118 }
119
120 node->nodeMap_ = nodeMap;
121 return node;
122 }
123
125 {
126 auto aliasName = GetInfoAsString(NodeInfo::AliasName);
127 if (aliasName.empty())
128 return NodePtr();
129
130 return nodeMap_->Node(aliasName);
131 }
132
134 {
135 auto holder = Internal::CbCarrier<void(Node &)>::Create(handler);
136 auto eventCookie = updatedCarrierContainer_.Register(holder);
137
138 std::lock_guard<std::mutex> guard(nativeUpdateRegistrationMutex_);
139 if (nativeUpdateRegistered_)
140 return eventCookie;
141
142 const auto result = CExports::NRegisterUpdate(Handle(), UpdatedCallback, this, cbID_);
143 if (result < 0)
144 {
145 updatedCarrierContainer_.Unregister(eventCookie);
146 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to register updated handler"));
147 }
148
149 nativeUpdateRegistered_ = true;
150 return eventCookie;
151 }
152
153 inline void Node::UnregisterEventUpdated(EventCookie eventCookie) noexcept
154 {
155 updatedCarrierContainer_.Unregister(eventCookie);
156 }
157
158 template <class T>
159 inline std::vector<std::shared_ptr<T>> Node::GetDependentNodes(NodeList type) const
160 {
161 int numNodes = GetDependentNodeCount(type);
162 std::vector<std::shared_ptr<T>> dependentNodes(static_cast<std::size_t>(numNodes));
163
164 for (int i = 0; i < numNodes; ++i)
165 {
166 auto nodeName = NativeCall<String>([&](String &value) {
167 std::size_t bufferSize = 0;
168 auto bufferSizeResult = CExports::NListTyped(Handle(), static_cast<CExports::TNodeList>(type), i,
169 reinterpret_cast<Char *>(0), bufferSize);
170 if (bufferSizeResult < 0)
171 return bufferSizeResult;
172
173 std::vector<Char> buffer(bufferSize);
174 auto bufferResult =
175 CExports::NListTyped(Handle(), static_cast<CExports::TNodeList>(type), i, buffer.data(), bufferSize);
176 if (bufferResult < 0)
177 return bufferResult;
178
179 value = String(buffer.data());
180 return bufferResult;
181 });
182 dependentNodes[i] = nodeMap_->Node<T>(nodeName);
183 }
184 return dependentNodes;
185 }
186
187 } // namespace GenApi
188
189 CVB_END_INLINE_NS
190
191} // namespace Cvb
EventCookie RegisterEventUpdated(std::function< void(Node &)> handler)
Register a listener to node updated event.
Definition detail_node.hpp:133
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
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
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition genapi.hpp:27
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
T rethrow_exception(T... args)