CVB++ 15.0
Loading...
Searching...
No Matches
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
34
36 class NodeMap : public std::enable_shared_from_this<NodeMap>
37 {
38
39 public:
40 using GuardType = HandleGuard<GenApi::NodeMap>;
41
43
53 class GenApiVersion final
54 {
55 public:
57
60 GenApiVersion() noexcept = default;
61
63
69 GenApiVersion(std::uint16_t major, std::uint16_t minor, std::uint16_t subMinor) noexcept
70 : major_(major)
71 , minor_(minor)
72 , subMinor_(subMinor)
73 {
74 }
75
77
82 {
83 StringStream stream;
84 stream << major_ << "." << minor_ << "." << subMinor_;
85 return stream.str();
86 }
87
89
93 std::uint16_t Major() const noexcept
94 {
95 return major_;
96 }
97
99
103 void SetMajor(std::uint16_t major) noexcept
104 {
105 major_ = major;
106 }
107
109
113 std::uint16_t Minor() const noexcept
114 {
115 return minor_;
116 }
117
119
123 void SetMinor(std::uint16_t minor) noexcept
124 {
125 minor_ = minor;
126 }
127
129
133 std::uint16_t SubMinor() const noexcept
134 {
135 return subMinor_;
136 }
137
139
143 void SetSubMinor(std::uint16_t subMinor) noexcept
144 {
145 subMinor_ = subMinor;
146 }
147
149
154 bool operator==(const GenApiVersion &version) const noexcept
155 {
156 return major_ == version.major_ && minor_ == version.minor_ && subMinor_ == version.subMinor_;
157 }
158
160
165 bool operator!=(const GenApiVersion &version) const noexcept
166 {
167 return !(*this == version);
168 }
169
171
176 bool operator<(const GenApiVersion &version) const noexcept
177 {
178 return (major_ < version.major_) ? true
179 : (major_ != version.major_) ? false
180 : (minor_ < version.minor_) ? true
181 : (minor_ != version.minor_) ? false
182 : subMinor_ < version.subMinor_;
183 }
184
186
191 bool operator<=(const GenApiVersion &version) const noexcept
192 {
193 return (*this == version) || (*this < version);
194 }
195
197
202 bool operator>(const GenApiVersion &version) const noexcept
203 {
204 return (*this != version) && !(*this < version);
205 }
206
208
213 bool operator>=(const GenApiVersion &version) const noexcept
214 {
215 return !(*this < version);
216 }
217
218 private:
219 std::uint16_t major_ = 0;
220 std::uint16_t minor_ = 0;
221 std::uint16_t subMinor_ = 0;
222 };
223
224 static NodeMapPtr Create(const Device &device, const String &name);
225
227
236 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, const Device &device);
237
238 template <class T>
239 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, const Device &device)
240 {
241 return FromHandle(std::move(guard), name, device);
242 }
243
244 template <class T>
245 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard)
246 {
247 return FromHandle(std::move(guard), String(), nullptr);
248 }
249
251
263 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, void *provider)
264 {
265 if (!guard.Handle())
266 throw std::runtime_error("handle must not be null");
267
268 return NodeMapPtr(new NodeMap(std::move(guard), name, provider));
269 }
270
271 template <class T>
272 static NodeMapPtr FromHandle(HandleGuard<NodeMap> &&guard, const String &name, void *provider)
273 {
274 return FromHandle(std::move(guard), name, provider);
275 }
276
278
284 void *Handle() const noexcept
285 {
286 return handle_.Handle();
287 }
288
290
294 String Name() const noexcept
295 {
296 return name_;
297 }
298
300
304 String Description() const noexcept
305 {
306 return description_;
307 }
308
310
318 {
319 return GetInfoAsString(NodeMapInfo::Vendor);
320 }
321
323
331 {
332 return GetInfoAsString(NodeMapInfo::Model);
333 }
334
336
341 {
342 return GetInfoAsString(NodeMapInfo::Namespace);
343 }
344
346
351 {
352 return GetInfoAsString(NodeMapInfo::Tooltip);
353 }
354
356
361 {
362 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMajor)),
363 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMinor)),
364 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionSubMinor)));
365 }
366
368
373 {
374 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMajor)),
375 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMinor)),
376 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionSubMinor)));
377 }
378
380
384 {
385 auto lastPoll = lastPoll_;
386 lastPoll_ = std::chrono::system_clock::now();
387 auto delta = lastPoll_ - lastPoll;
388
389 NativeCall([&]() {
390 return CExports::NMPollNodes(
391 Handle(),
392 static_cast<CExports::cvbval_t>(std::chrono::duration_cast<std::chrono::milliseconds>(delta).count()));
393 });
394 }
395
397
401 void LoadSettings(const String &fileName)
402 {
403 NativeCall([&]() { return CExports::NMLoadSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
404 }
405
407
411 void SaveSettings(const String &fileName) const
412 {
413 NativeCall([&]() { return CExports::NMSaveSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
414 }
415
417
422 template <class RANGE>
423 typename TypedRange<void, String, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
424 {
425 NativeCall([&]() {
426 return CExports::NMSaveSetExTyped(Handle(), fileName.c_str(), BuildNodeList(range).c_str(), nullptr, nullptr);
427 });
428 }
429
431
436 template <class RANGE>
437 typename TypedRange<void, NodePtr, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
438 {
439 auto rangeAdapter = MakeRangeAdapter<NodePtr>(range);
441 names.reserve(rangeAdapter.Size());
442 for (const auto &node : range)
443 names.emplace_back(node->Name());
444 SaveSettings(fileName, names);
445 }
446
448
453 template <class... NAMES>
454 typename VarArgRange<void, const String &, NAMES...>::type SaveSettings(const String &fileName,
455 const NAMES &...names) const
456 {
457 std::vector<String> range = {names...};
458 SaveSettings(fileName, range);
459 }
460
462
467 template <class... NODES>
468 typename VarArgRange<void, const Node &, NODES...>::type SaveSettings(const String &fileName,
469 const NODES &...nodes) const
470 {
471 SaveSettings(fileName, nodes.Name()...);
472 }
473
475
483
485
492 void DownloadFile(const String &fileName, const String &fileSelectorEntryName) const
493 {
494 NativeCall(
495 [&]() { return CExports::NMDownloadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
496 }
497
499
506 std::vector<uint8_t> DownloadFile(const String &fileSelectorEntryName) const
507 {
508 size_t size = 0;
509 NativeCall([&]() {
510 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), nullptr, size);
511 });
512 std::vector<uint8_t> buffer(size);
513 NativeCall([&]() {
514 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), buffer.data(), size);
515 });
516 return buffer;
517 }
518
520
527 void UploadFile(const String &fileName, const String &fileSelectorEntryName)
528 {
529 NativeCall(
530 [&]() { return CExports::NMUploadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
531 }
532
534
540 Cvb::String ToJson(std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr)
541 {
542 std::vector<NodePtr> nodes = {};
543 auto nodesMap = Nodes();
544 for (auto &kvp : nodesMap)
545 {
546 nodes.push_back(kvp.second);
547 }
548
549 return ToJson(nodes, serializationErrorCallback);
550 }
551
553
561 std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr);
562
564
571 template <class T>
572 std::shared_ptr<T> Node(const String &name) const
573 {
574 static_assert(std::is_base_of<Cvb::GenApi::Node, T>::value, "requested node type must be derived from Node");
575 auto ptr = std::dynamic_pointer_cast<T>(Node(name));
576 if (!ptr)
577 throw std::runtime_error("requested node can not be casted to the given type");
578
579 return ptr;
580 }
581
583
606 NodePtr Node(const String &name) const;
607
609
638 template <class T>
639 std::shared_ptr<T> TryGetNode(const String &name) const noexcept
640 {
641 try
642 {
643 return Node<T>(name);
644 }
645 catch (...)
646 {
647 return nullptr;
648 }
649 }
650
652
680 NodePtr TryGetNode(const String &name) const noexcept;
681
683
688
689 private:
690 NodeMap(HandleGuard<NodeMap> &&guard, const String &name, void *provider);
691
692 void ReadDescription(void *provider);
693
694 void FillNodeKeys();
695
696 void NativeCall(std::function<CExports::cvbres_t()> fn) const
697 {
698 auto result = fn();
699 if (result < 0)
700 {
702 if (message.empty())
703 message = "Error";
704
705 std::stringstream stream;
706 auto name = Name();
707 std::string asciName(name.begin(), name.end());
708 stream << "NodeMap[" << asciName << "]: " << message;
709 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
710 }
711 }
712
713 template <class T>
714 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
715 {
716 T value;
717 auto result = fn(value);
718 if (result < 0)
719 {
721 if (message.empty())
722 message = "Error";
723
724 std::stringstream stream;
725 auto name = Name();
726 std::string asciName(name.begin(), name.end());
727 stream << "NodeMap[" << asciName << "]: " << message;
728 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
729 }
730 return value;
731 }
732
733 std::int64_t GetInfoAsInt(NodeMapInfo command) const
734 {
735 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
736 return CExports::NMInfoAsInteger(Handle(), static_cast<CExports::TNodeMapInfo>(command), value);
737 }));
738 }
739
740 String GetInfoAsString(NodeMapInfo command) const
741 {
742 return NativeCall<String>([&](String &value) {
743 std::size_t bufferSize = 0;
744 auto bufferSizeResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
745 reinterpret_cast<Char *>(0), bufferSize);
746 if (bufferSizeResult < 0)
747 return bufferSizeResult;
748
749 std::vector<Char> buffer(bufferSize);
750 auto bufferResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
751 reinterpret_cast<Char *>(&buffer[0]), bufferSize);
752 if (bufferResult < 0)
753 return bufferResult;
754
755 value = String(reinterpret_cast<Char *>(&buffer[0]));
756 return bufferResult;
757 });
758 }
759
760 template <class RANGE>
761 typename TypedRange<String, String, RANGE>::type BuildNodeList(const RANGE &range) const
762 {
763 StringStream nameStream;
764 for (const auto &name : range)
765 nameStream << name << CVB_LIT("|");
766
767 return nameStream.str();
768 }
769
770 HandleGuard<NodeMap> handle_;
771
772 String name_;
773
774 String description_;
775
776 mutable std::map<String, std::weak_ptr<class Node>> nodes_;
777
778 std::chrono::system_clock::time_point lastPoll_;
779 };
780
781 } // namespace GenApi
782
783 using GenApi::NodeMap;
784
785 CVB_END_INLINE_NS
786
787} // namespace Cvb
788
789#ifdef _MSC_VER
790# pragma warning(pop)
791#endif
Generic CVB physical device.
Definition decl_device.hpp:39
Version information for GenICam related objects.
Definition decl_node_map.hpp:54
void SetMinor(std::uint16_t minor) noexcept
Sets the minor version number.
Definition decl_node_map.hpp:123
bool operator>=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:213
bool operator<(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:176
void SetMajor(std::uint16_t major) noexcept
Sets the major version number.
Definition decl_node_map.hpp:103
std::uint16_t Minor() const noexcept
Gets the minor version number.
Definition decl_node_map.hpp:113
std::uint16_t Major() const noexcept
Gets the major version number.
Definition decl_node_map.hpp:93
String ToString() const
Gets the string representation of this version.
Definition decl_node_map.hpp:81
std::uint16_t SubMinor() const noexcept
Gets the sub-minor version number.
Definition decl_node_map.hpp:133
bool operator==(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:154
bool operator!=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:165
bool operator<=(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:191
bool operator>(const GenApiVersion &version) const noexcept
Compares to an other version.
Definition decl_node_map.hpp:202
void SetSubMinor(std::uint16_t subMinor) noexcept
Sets the sub-minor version number.
Definition decl_node_map.hpp:143
GenApiVersion() noexcept=default
Creates a default version object (0.0.0)
Contains all nodes of a device or module.
Definition decl_node_map.hpp:37
void PollNodes()
Polls all nodes of this node map that have a polling time.
Definition decl_node_map.hpp:383
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:423
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:639
void LoadSettings(const String &fileName)
Loads the node values from the gcs file and sets the node values accordingly.
Definition decl_node_map.hpp:401
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:572
String Description() const noexcept
Gets the descriptive text of this node map.
Definition decl_node_map.hpp:304
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:437
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:454
void UploadFile(const String &fileName, const String &fileSelectorEntryName)
Uploads a file to the camera via the GenApi file upload.
Definition decl_node_map.hpp:527
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:506
String ModelName() const
Gets the name of the model, that the XML description is for.
Definition decl_node_map.hpp:330
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:411
String TransportLayerNamespace() const
Gets the transport layer type of the device.
Definition decl_node_map.hpp:340
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:540
GenApiVersion XmlFileSchemaVersion() const
Gets the XML schema version.
Definition decl_node_map.hpp:372
GenApiVersion XmlFileVersion() const
Gets the XML version.
Definition decl_node_map.hpp:360
String VendorName() const
Gets the name of the vendor, who created the XML description.
Definition decl_node_map.hpp:317
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:263
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:284
String ToolTip() const
Gets the short descriptive text, if any is available.
Definition decl_node_map.hpp:350
String Name() const noexcept
Name used to access this node map.
Definition decl_node_map.hpp:294
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:492
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:468
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:66
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition genapi.hpp:22
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)