CVB++ 14.0
node_map_enumerator.hpp
1#pragma once
2
3#include <map>
4#include <memory>
5
6#include "genapi.hpp"
7#include "node_map.hpp"
8
9
10namespace Cvb
11{
12
13 CVB_BEGIN_INLINE_NS
14
15 template <>
16 inline HandleGuard<GenApi::NodeMapEnumerator>::HandleGuard(void* handle) noexcept
17 : HandleGuard<GenApi::NodeMapEnumerator>(handle, [](void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
18 {
19 }
20
21 namespace GenApi
22 {
23
25
31 {
32 struct PrivateTag {};
33
34 public:
35
36 NodeMapEnumerator() noexcept
37 : handle_(nullptr) // not an invalid object just no node maps to enumerate
38 {
39 }
40
41 NodeMapEnumerator(HandleGuard<NodeMapEnumerator>&& guard, PrivateTag) noexcept
42 : handle_(std::move(guard))
43 {
44 }
45
47
52
54
60
62
70 template<class T>
71 static NodeMapEnumerator FromObject(const T& object)
72 {
73 CVB_CALL_CAPI_CHECKED(ShareObject(object.Handle()));
74 return NodeMapEnumerator(HandleGuard<NodeMapEnumerator>(object.Handle()), PrivateTag{});
75 }
76
78
84 void* Handle() const noexcept
85 {
86 return handle_.Handle();
87 }
88
90
97 {
99 auto nodeMapCount = NodeMapCount();
100 names.reserve(nodeMapCount);
101 std::generate_n(std::back_inserter(names), nodeMapCount, [this, i = 0]() mutable {
102 return NameAt(i++); });
103 return names;
104 }
105
107
114 NodeMapPtr operator[](const String& name) const
115 {
116 return NodeMap(name);
117 }
118
120
127 NodeMapPtr NodeMap(const String& name) const
128 {
129 auto iter = nodeMaps_.find(name);
130 if (iter != nodeMaps_.end())
131 {
132 if (auto nodeMap = iter->second.lock())
133 return nodeMap;
134 }
135
136 CExports::NODEMAP handle = nullptr;
137 std::string cname(Internal::CastToAscii(name));
138 CVB_CALL_CAPI_CHECKED(NMH2GetNodeMap(Handle(), cname.c_str(), handle));
139
140 auto nodeMap = NodeMap::FromHandle(HandleGuard<class NodeMap>(handle), name, Handle());
141 nodeMaps_[name] = nodeMap;
142 return nodeMap;
143 }
144
145 private:
146
147 int NodeMapCount() const noexcept
148 {
149 size_t numNodeMaps = 0;
150 CExports::NMH2GetNum(Handle(), numNodeMaps);
151 return static_cast<int>(numNodeMaps);
152 }
153
154 String NameAt(int index) const
155 {
156 size_t bufferSize = 0;
157 CVB_CALL_CAPI_CHECKED(NMH2GetID(Handle(), index, nullptr, bufferSize));
158
159 std::vector<char> buffer(bufferSize);
160 CVB_CALL_CAPI_CHECKED(NMH2GetID(Handle(), index, buffer.data(), bufferSize));
161
162 return String(buffer.begin(), buffer.end() - 1); // safe as we define the node map names
163 }
164
166
167 HandleGuard<NodeMapEnumerator> handle_;
168
169
170
171 };
172 }
173
174 CVB_END_INLINE_NS
175}
Lazy enumeration of node maps.
Definition: node_map_enumerator.hpp:31
NodeMapPtr operator[](const String &name) const
Name based node map access.
Definition: node_map_enumerator.hpp:114
static NodeMapEnumerator FromObject(const T &object)
Create a node map enumerator for a given object.
Definition: node_map_enumerator.hpp:71
NodeMapEnumerator(NodeMapEnumerator &&other)=default
Move constructor.
NodeMapPtr NodeMap(const String &name) const
Name based node map access.
Definition: node_map_enumerator.hpp:127
std::vector< String > Names() const
Get the names of all available node maps.
Definition: node_map_enumerator.hpp:96
NodeMapEnumerator & operator=(NodeMapEnumerator &&other)=default
Move operator.
void * Handle() const noexcept
Classic API device handle.
Definition: node_map_enumerator.hpp:84
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:27
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24