CVB++ 15.0
detail_node_map.hpp
1#pragma once
2
3#include <memory>
4#include <stdexcept>
5#include <vector>
6
7#include "../../_cexports/c_gen_api.h"
8
9#include "../../global.hpp"
10
11#include "../_decl/decl_node.hpp"
12#include "../_decl/decl_node_map.hpp"
13
14#include "../../_decl/decl_device.hpp"
15
16#include "../value_node.hpp"
17
18namespace Cvb
19{
20
21 CVB_BEGIN_INLINE_NS
22
23 namespace GenApi
24 {
25
26 inline NodeMapPtr NodeMap::FromHandle(HandleGuard<NodeMap> &&guard, const String &name, const Device &device)
27 {
28 return FromHandle(std::move(guard), name, device.Handle());
29 }
30
31 inline NodeMapPtr NodeMap::Create(const Device &device, const String &name)
32 {
33 NodeMapPtr nodeMap;
34
35 if (CExports::CanNodeMapHandle2(device.Handle()))
36 {
37 std::string cname(Internal::CastToAscii(name));
38 nodeMap = Internal::DoResCallShareOut<NodeMap>(
39 [&](void *&handle) { return CVB_CALL_CAPI(NMH2GetNodeMap(device.Handle(), cname.c_str(), handle)); }, name,
40 device);
41 }
42 else if (name == CVB_LIT("Device"))
43 nodeMap = Internal::DoResCallShareOut<NodeMap>(
44 [&](void *&handle) { return CVB_CALL_CAPI(NMHGetNodeMap(device.Handle(), handle)); }, name, device);
45 else
46 throw std::runtime_error("node map not found");
47
48 return nodeMap;
49 }
50
52 {
53 if (nodes_.find(CVB_LIT("Cust::FileSelector")) == nodes_.end()
54 && nodes_.find(CVB_LIT("Std::FileSelector")) == nodes_.end())
55 return std::vector<String>();
56
57 auto enumerationNode = Node<EnumerationNode>(CVB_LIT("FileSelector"));
58 auto entries = enumerationNode->Entries();
59
60 std::vector<String> availableFiles;
61 for (auto entry : entries)
62 {
63 if (entry->IsAvailable())
64 availableFiles.push_back(entry->SymbolicValue());
65 }
66 return availableFiles;
67 }
68
69 inline NodePtr NodeMap::Node(const String &name) const
70 {
71 auto element = nodes_.find(name);
72 String localName = name;
73 if (element == nodes_.end())
74 {
75 if (nodes_.find(CVB_LIT("Cust::") + name) != nodes_.end())
76 localName = String(CVB_LIT("Cust::")) + name;
77 else if (nodes_.find(CVB_LIT("Std::") + name) != nodes_.end())
78 localName = String(CVB_LIT("Std::")) + name;
79 else
80 throw std::out_of_range("no node for name");
81 }
82
83 auto node = nodes_[localName].lock();
84 if (!node)
85 {
86 node = Node::FromName(std::const_pointer_cast<NodeMap>(shared_from_this()), localName);
87 nodes_[localName] = node;
88 }
89
90 return node;
91 }
92
93 inline NodePtr NodeMap::TryGetNode(const String &name) const noexcept
94 {
95 try
96 {
97 return Node(name);
98 }
99 catch (...)
100 {
101 return nullptr;
102 }
103 }
104
106 {
108 for (const auto &entry : nodes_)
109 nodes[entry.first] = Node(entry.first);
110 return nodes;
111 }
112
113 inline NodeMap::NodeMap(HandleGuard<NodeMap> &&guard, const String &name, void *provider)
114 : handle_(std::move(guard))
115 , name_(name)
116 , lastPoll_(std::chrono::system_clock::now())
117 {
118 if (CExports::CanNodeMapHandle2(provider))
119 ReadDescription(provider);
120 FillNodeKeys();
121 }
122
123 inline void NodeMap::ReadDescription(void *provider)
124 {
125 std::string cname(name_.begin(), name_.end());
126 size_t bufferLength = 0;
127 if (CExports::NMH2GetDescription(provider, cname.c_str(), nullptr, bufferLength) >= 0 && bufferLength > 1)
128 {
129 std::vector<char> buffer(bufferLength);
130 auto resultDescription = CExports::NMH2GetDescription(provider, cname.c_str(), &buffer[0], bufferLength);
131 if (resultDescription < 0)
132 std::rethrow_exception(CvbException::FromCvbResult(resultDescription, "failed to get node map description"));
133 description_ = String(buffer.begin(), buffer.end() - 1);
134 }
135 }
136
137 inline void NodeMap::FillNodeKeys()
138 {
139 CExports::cvbdim_t numNodes = 0;
140 auto resultNum = CExports::NMNodeCount(Handle(), numNodes);
141 if (resultNum < 0)
142 std::rethrow_exception(CvbException::FromCvbResult(resultNum, "failed to get node count"));
143
144 for (CExports::cvbdim_t i = 0; i < numNodes; ++i)
145 {
146 size_t nameLength = 0;
147 auto resultNameLength = CExports::NMListNode(Handle(), i, nullptr, nameLength);
148 if (resultNameLength < 0)
149 std::rethrow_exception(CvbException::FromCvbResult(resultNameLength, "failed to get node name length"));
150 std::vector<char> buffer(static_cast<size_t>(nameLength));
151 auto resultBuffer = CExports::NMListNode(Handle(), i, &buffer[0], nameLength);
152 if (resultBuffer < 0)
153 std::rethrow_exception(CvbException::FromCvbResult(resultBuffer, "failed to get node name"));
154 String keyString(buffer.begin(), buffer.end() - 1);
155
156 nodes_[keyString] = std::weak_ptr<class Node>();
157 }
158 }
159
161 std::function<void(NodePtr, const std::string &)> serializationErrorCallback)
162 {
163 CExports::NODEMAPSTREAM nodeMapStream;
164 auto res = CExports::NMCreateStream(Handle(), CExports::TStreamType::ST_JSON, nodeMapStream);
165 if (res < 0)
166 Utilities::SystemInfo::ThrowLastError(res);
167 ReleaseObjectGuard guard(nodeMapStream);
168
169 for (const auto &node : nodes)
170 {
171 res = CExports::NMStreamPush(nodeMapStream, node->Handle());
172 if (res < 0)
173 {
174 if (serializationErrorCallback)
175 serializationErrorCallback(node, Internal::CastToAscii(node->Name()) + ": "
177 else
178 Utilities::SystemInfo::ThrowLastError(res);
179 }
180 }
181
182 size_t settingsSize;
183 res = CExports::NMStreamDumpTyped(nodeMapStream, reinterpret_cast<Char *>(0), settingsSize);
184 if (res < 0)
185 Utilities::SystemInfo::ThrowLastError(res);
186 settingsSize += sizeof(Char);
187 std::vector<Char> settings(settingsSize);
188 res = CExports::NMStreamDumpTyped(nodeMapStream, &settings[0], settingsSize);
189 if (res < 0)
190 Utilities::SystemInfo::ThrowLastError(res);
191
192 return String(&settings[0]);
193 }
194
195 } // namespace GenApi
196
197 CVB_END_INLINE_NS
198
199} // namespace Cvb
Generic CVB physical device.
Definition decl_device.hpp:78
void * Handle() const noexcept
Classic API device handle.
Definition decl_device.hpp:122
std::shared_ptr< T > TryGetNode(const String &name) const noexcept
Tries to get the node with the given name from the node map.
Definition decl_node_map.hpp:641
std::shared_ptr< T > Node(const String &name) const
Get the node with the given name from the node map.
Definition decl_node_map.hpp:574
std::vector< String > AvailableFiles() const
Gets the currently available file identifiers, which can be downloaded or uploaded.
Definition detail_node_map.hpp:51
static NodeMapPtr FromHandle(HandleGuard< NodeMap > &&guard, const String &name, const Device &device)
Creates a node map from a classic API handle.
Definition detail_node_map.hpp:26
Cvb::String ToJson(std::function< void(NodePtr, const std::string &)> serializationErrorCallback=nullptr)
Serializes all nodes of this nodemap to a json string.
Definition decl_node_map.hpp:542
std::map< String, NodePtr > Nodes() const
Get a dictionary contain all nodes of this node map.
Definition detail_node_map.hpp:105
void * Handle() const noexcept
Classic API node map handle.
Definition decl_node_map.hpp:286
T move(T... args)
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
std::string GetLastErrorMessage(int &errorCode)
Returns the last error message and its code.
Definition system_info.hpp:141
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
T const_pointer_cast(T... args)
T rethrow_exception(T... args)