CVB++ 15.0
decl_node_map.hpp
1#pragma once
2
3#include <map>
4#include <memory>
5
6#include "../../global.hpp"
7#include "../../string.hpp"
8
9#include "../genapi.hpp"
10
11#ifdef _MSC_VER
12# pragma warning(push, 1)
13# pragma warning(disable : 4244)
14#endif
15
16namespace Cvb
17{
18
19 CVB_BEGIN_INLINE_NS
20
21 template <>
22 inline HandleGuard<GenApi::NodeMap>::HandleGuard(void *handle) noexcept
23 : HandleGuard<GenApi::NodeMap>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
24 {
25 }
26
27 namespace GenApi
28 {
30
36
38 class NodeMap : public std::enable_shared_from_this<NodeMap>
39 {
40
41 public:
42 using GuardType = HandleGuard<GenApi::NodeMap>;
43
45
55 class GenApiVersion final
56 {
57 public:
59
62 GenApiVersion() noexcept = default;
63
65
71 GenApiVersion(std::uint16_t major, std::uint16_t minor, std::uint16_t subMinor) noexcept
72 : major_(major)
73 , minor_(minor)
74 , subMinor_(subMinor)
75 {
76 }
77
79
84 {
85 StringStream stream;
86 stream << major_ << "." << minor_ << "." << subMinor_;
87 return stream.str();
88 }
89
91
95 std::uint16_t Major() const noexcept
96 {
97 return major_;
98 }
99
101
105 void SetMajor(std::uint16_t major) noexcept
106 {
107 major_ = major;
108 }
109
111
115 std::uint16_t Minor() const noexcept
116 {
117 return minor_;
118 }
119
121
125 void SetMinor(std::uint16_t minor) noexcept
126 {
127 minor_ = minor;
128 }
129
131
135 std::uint16_t SubMinor() const noexcept
136 {
137 return subMinor_;
138 }
139
141
145 void SetSubMinor(std::uint16_t subMinor) noexcept
146 {
147 subMinor_ = subMinor;
148 }
149
151
156 bool operator==(const GenApiVersion &version) const noexcept
157 {
158 return major_ == version.major_ && minor_ == version.minor_ && subMinor_ == version.subMinor_;
159 }
160
162
167 bool operator!=(const GenApiVersion &version) const noexcept
168 {
169 return !(*this == version);
170 }
171
173
178 bool operator<(const GenApiVersion &version) const noexcept
179 {
180 return (major_ < version.major_) ? true
181 : (major_ != version.major_) ? false
182 : (minor_ < version.minor_) ? true
183 : (minor_ != version.minor_) ? false
184 : subMinor_ < version.subMinor_;
185 }
186
188
193 bool operator<=(const GenApiVersion &version) const noexcept
194 {
195 return (*this == version) || (*this < version);
196 }
197
199
204 bool operator>(const GenApiVersion &version) const noexcept
205 {
206 return (*this != version) && !(*this < version);
207 }
208
210
215 bool operator>=(const GenApiVersion &version) const noexcept
216 {
217 return !(*this < version);
218 }
219
220 private:
221 std::uint16_t major_ = 0;
222 std::uint16_t minor_ = 0;
223 std::uint16_t subMinor_ = 0;
224 };
225
226 static NodeMapPtr Create(const Device &device, const String &name);
227
229
238 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, const Device &device);
239
240 template <class T>
241 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, const Device &device)
242 {
243 return FromHandle(std::move(guard), name, device);
244 }
245
246 template <class T>
247 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard)
248 {
249 return FromHandle(std::move(guard), String(), nullptr);
250 }
251
253
265 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, void *provider)
266 {
267 if (!guard.Handle())
268 throw std::runtime_error("handle must not be null");
269
270 return NodeMapPtr(new NodeMap(std::move(guard), name, provider));
271 }
272
273 template <class T>
274 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, void *provider)
275 {
276 return FromHandle(std::move(guard), name, provider);
277 }
278
280
286 void *Handle() const noexcept
287 {
288 return handle_.Handle();
289 }
290
292
296 String Name() const noexcept
297 {
298 return name_;
299 }
300
302
306 String Description() const noexcept
307 {
308 return description_;
309 }
310
312
320 {
321 return GetInfoAsString(NodeMapInfo::Vendor);
322 }
323
325
333 {
334 return GetInfoAsString(NodeMapInfo::Model);
335 }
336
338
343 {
344 return GetInfoAsString(NodeMapInfo::Namespace);
345 }
346
348
353 {
354 return GetInfoAsString(NodeMapInfo::Tooltip);
355 }
356
358
363 {
364 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMajor)),
365 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMinor)),
366 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionSubMinor)));
367 }
368
370
375 {
376 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMajor)),
377 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMinor)),
378 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionSubMinor)));
379 }
380
382
386 {
387 auto lastPoll = lastPoll_;
388 lastPoll_ = std::chrono::system_clock::now();
389 auto delta = lastPoll_ - lastPoll;
390
391 NativeCall([&]() {
392 return CExports::NMPollNodes(
393 Handle(),
394 static_cast<CExports::cvbval_t>(std::chrono::duration_cast<std::chrono::milliseconds>(delta).count()));
395 });
396 }
397
399
403 void LoadSettings(const String &fileName)
404 {
405 NativeCall([&]() { return CExports::NMLoadSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
406 }
407
409
413 void SaveSettings(const String &fileName) const
414 {
415 NativeCall([&]() { return CExports::NMSaveSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
416 }
417
419
424 template <class RANGE>
425 typename TypedRange<void, String, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
426 {
427 NativeCall([&]() {
428 return CExports::NMSaveSetExTyped(Handle(), fileName.c_str(), BuildNodeList(range).c_str(), nullptr, nullptr);
429 });
430 }
431
433
438 template <class RANGE>
439 typename TypedRange<void, NodePtr, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
440 {
441 auto rangeAdapter = MakeRangeAdapter<NodePtr>(range);
443 names.reserve(rangeAdapter.Size());
444 for (const auto &node : range)
445 names.emplace_back(node->Name());
446 SaveSettings(fileName, names);
447 }
448
450
455 template <class... NAMES>
456 typename VarArgRange<void, const String &, NAMES...>::type SaveSettings(const String &fileName,
457 const NAMES &...names) const
458 {
459 std::vector<String> range = {names...};
460 SaveSettings(fileName, range);
461 }
462
464
469 template <class... NODES>
470 typename VarArgRange<void, const Node &, NODES...>::type SaveSettings(const String &fileName,
471 const NODES &...nodes) const
472 {
473 SaveSettings(fileName, nodes.Name()...);
474 }
475
477
485
487
494 void DownloadFile(const String &fileName, const String &fileSelectorEntryName) const
495 {
496 NativeCall(
497 [&]() { return CExports::NMDownloadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
498 }
499
501
508 std::vector<uint8_t> DownloadFile(const String &fileSelectorEntryName) const
509 {
510 size_t size = 0;
511 NativeCall([&]() {
512 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), nullptr, size);
513 });
514 std::vector<uint8_t> buffer(size);
515 NativeCall([&]() {
516 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), buffer.data(), size);
517 });
518 return buffer;
519 }
520
522
529 void UploadFile(const String &fileName, const String &fileSelectorEntryName)
530 {
531 NativeCall(
532 [&]() { return CExports::NMUploadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
533 }
534
536
542 Cvb::String ToJson(std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr)
543 {
544 std::vector<NodePtr> nodes = {};
545 auto nodesMap = Nodes();
546 for (auto &kvp : nodesMap)
547 {
548 nodes.push_back(kvp.second);
549 }
550
551 return ToJson(nodes, serializationErrorCallback);
552 }
553
555
563 std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr);
564
566
573 template <class T>
574 std::shared_ptr<T> Node(const String &name) const
575 {
576 static_assert(std::is_base_of<Cvb::GenApi::Node, T>::value, "requested node type must be derived from Node");
577 auto ptr = std::dynamic_pointer_cast<T>(Node(name));
578 if (!ptr)
579 throw std::runtime_error("requested node can not be casted to the given type");
580
581 return ptr;
582 }
583
585
608 NodePtr Node(const String &name) const;
609
611
640 template <class T>
641 std::shared_ptr<T> TryGetNode(const String &name) const noexcept
642 {
643 try
644 {
645 return Node<T>(name);
646 }
647 catch (...)
648 {
649 return nullptr;
650 }
651 }
652
654
682 NodePtr TryGetNode(const String &name) const noexcept;
683
685
690
691 private:
692 NodeMap(HandleGuard<NodeMap> &&guard, const String &name, void *provider);
693
694 void ReadDescription(void *provider);
695
696 void FillNodeKeys();
697
698 void NativeCall(std::function<CExports::cvbres_t()> fn) const
699 {
700 auto result = fn();
701 if (result < 0)
702 {
704 if (message.empty())
705 message = "Error";
706
707 std::stringstream stream;
708 auto name = Name();
709 std::string asciName(name.begin(), name.end());
710 stream << "NodeMap[" << asciName << "]: " << message;
711 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
712 }
713 }
714
715 template <class T>
716 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
717 {
718 T value;
719 auto result = fn(value);
720 if (result < 0)
721 {
723 if (message.empty())
724 message = "Error";
725
726 std::stringstream stream;
727 auto name = Name();
728 std::string asciName(name.begin(), name.end());
729 stream << "NodeMap[" << asciName << "]: " << message;
730 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
731 }
732 return value;
733 }
734
735 std::int64_t GetInfoAsInt(NodeMapInfo command) const
736 {
737 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
738 return CExports::NMInfoAsInteger(Handle(), static_cast<CExports::TNodeMapInfo>(command), value);
739 }));
740 }
741
742 String GetInfoAsString(NodeMapInfo command) const
743 {
744 return NativeCall<String>([&](String &value) {
745 std::size_t bufferSize = 0;
746 auto bufferSizeResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
747 reinterpret_cast<Char *>(0), bufferSize);
748 if (bufferSizeResult < 0)
749 return bufferSizeResult;
750
751 std::vector<Char> buffer(bufferSize);
752 auto bufferResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
753 reinterpret_cast<Char *>(&buffer[0]), bufferSize);
754 if (bufferResult < 0)
755 return bufferResult;
756
757 value = String(reinterpret_cast<Char *>(&buffer[0]));
758 return bufferResult;
759 });
760 }
761
762 template <class RANGE>
763 typename TypedRange<String, String, RANGE>::type BuildNodeList(const RANGE &range) const
764 {
765 StringStream nameStream;
766 for (const auto &name : range)
767 nameStream << name << CVB_LIT("|");
768
769 return nameStream.str();
770 }
771
772 HandleGuard<NodeMap> handle_;
773
774 String name_;
775
776 String description_;
777
778 mutable std::map<String, std::weak_ptr<class Node>> nodes_;
779
780 std::chrono::system_clock::time_point lastPoll_;
781 };
782
783 } // namespace GenApi
784
785 using GenApi::NodeMap;
786
787 CVB_END_INLINE_NS
788
789} // namespace Cvb
790
791#ifdef _MSC_VER
792# pragma warning(pop)
793#endif
Generic CVB physical device.
Definition decl_device.hpp:78
Version information for GenICam related objects.
Definition decl_node_map.hpp:56
void SetMinor(std::uint16_t minor) noexcept
Sets the minor version number.
Definition decl_node_map.hpp:125
bool operator>=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:215
bool operator<(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:178
void SetMajor(std::uint16_t major) noexcept
Sets the major version number.
Definition decl_node_map.hpp:105
std::uint16_t Minor() const noexcept
Gets the minor version number.
Definition decl_node_map.hpp:115
std::uint16_t Major() const noexcept
Gets the major version number.
Definition decl_node_map.hpp:95
String ToString() const
Gets the string representation of this version.
Definition decl_node_map.hpp:83
std::uint16_t SubMinor() const noexcept
Gets the sub-minor version number.
Definition decl_node_map.hpp:135
bool operator==(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:156
bool operator!=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:167
bool operator<=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:193
bool operator>(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:204
void SetSubMinor(std::uint16_t subMinor) noexcept
Sets the sub-minor version number.
Definition decl_node_map.hpp:145
GenApiVersion() noexcept=default
Creates a default version object (0.0.0)
Contains all nodes of a device or module.
Definition decl_node_map.hpp:39
void PollNodes()
Polls all nodes of this node map that have a polling time.
Definition decl_node_map.hpp:385
TypedRange< void, String, RANGE >::type SaveSettings(const String &fileName, const RANGE &range) const
Saves all nodes that are in a specific container.
Definition decl_node_map.hpp:425
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
void LoadSettings(const String &fileName)
Loads the node values from the gcs file and sets the node values accordingly.
Definition decl_node_map.hpp:403
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
String Description() const noexcept
Gets the descriptive text of this node map.
Definition decl_node_map.hpp:306
TypedRange< void, NodePtr, RANGE >::type SaveSettings(const String &fileName, const RANGE &range) const
Saves all nodes which are in a given container.
Definition decl_node_map.hpp:439
std::vector< String > AvailableFiles() const
Gets the currently available file identifiers, which can be downloaded or uploaded.
Definition detail_node_map.hpp:51
VarArgRange< void, constString &, NAMES... >::type SaveSettings(const String &fileName, const NAMES &...names) const
Saves all nodes which are in a given as argument.
Definition decl_node_map.hpp:456
void UploadFile(const String &fileName, const String &fileSelectorEntryName)
Uploads a file to the camera via the GenApi file upload.
Definition decl_node_map.hpp:529
std::vector< uint8_t > DownloadFile(const String &fileSelectorEntryName) const
Downloads a file from the camera into memory via the GenApi file download.
Definition decl_node_map.hpp:508
String ModelName() const
Gets the name of the model, that the XML description is for.
Definition decl_node_map.hpp:332
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
void SaveSettings(const String &fileName) const
Saves all nodes which are streamable.
Definition decl_node_map.hpp:413
String TransportLayerNamespace() const
Gets the transport layer type of the device.
Definition decl_node_map.hpp:342
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
GenApiVersion XmlFileSchemaVersion() const
Gets the XML schema version.
Definition decl_node_map.hpp:374
GenApiVersion XmlFileVersion() const
Gets the XML version.
Definition decl_node_map.hpp:362
String VendorName() const
Gets the name of the vendor, who created the XML description.
Definition decl_node_map.hpp:319
static NodeMapPtr FromHandle(HandleGuard< NodeMap > &&guard, const String &name, void *provider)
Creates a node map from a classic API handle.
Definition decl_node_map.hpp:265
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
String ToolTip() const
Gets the short descriptive text, if any is available.
Definition decl_node_map.hpp:352
String Name() const noexcept
Name used to access this node map.
Definition decl_node_map.hpp:296
void DownloadFile(const String &fileName, const String &fileSelectorEntryName) const
Downloads a file from the camera via the GenApi file download.
Definition decl_node_map.hpp:494
VarArgRange< void, constNode &, NODES... >::type SaveSettings(const String &fileName, const NODES &...nodes) const
Saves all nodes which are in a given as argument.
Definition decl_node_map.hpp:470
T duration_cast(T... args)
cvbbool_t ReleaseObject(OBJ &Object)
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()
Returns the last error message.
Definition system_info.hpp:167
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
std::stringstream StringStream
String stream for wide characters or unicode characters.
Definition string.hpp:56
T dynamic_pointer_cast(T... args)
T rethrow_exception(T... args)