CVB++ 15.1
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
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
297 {
298 return name_;
299 }
300
302
307 {
308 ReadDescription();
309 return description_;
310 }
311
313
321 {
322 return GetInfoAsString(NodeMapInfo::Vendor);
323 }
324
326
334 {
335 return GetInfoAsString(NodeMapInfo::Model);
336 }
337
339
344 {
345 return GetInfoAsString(NodeMapInfo::Namespace);
346 }
347
349
354 {
355 return GetInfoAsString(NodeMapInfo::Tooltip);
356 }
357
359
364 {
365 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMajor)),
366 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionMinor)),
367 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::DeviceVersionSubMinor)));
368 }
369
371
376 {
377 return GenApiVersion(static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMajor)),
378 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionMinor)),
379 static_cast<std::uint16_t>(GetInfoAsInt(NodeMapInfo::SchemaVersionSubMinor)));
380 }
381
383
387 {
388 auto lastPoll = lastPoll_;
389 lastPoll_ = std::chrono::system_clock::now();
390 auto delta = lastPoll_ - lastPoll;
391
392 NativeCall([&]() {
393 return CExports::NMPollNodes(
394 Handle(),
395 static_cast<CExports::cvbval_t>(std::chrono::duration_cast<std::chrono::milliseconds>(delta).count()));
396 });
397 }
398
400
404 void LoadSettings(const String &fileName)
405 {
406 NativeCall([&]() { return CExports::NMLoadSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
407 }
408
410
414 void SaveSettings(const String &fileName) const
415 {
416 NativeCall([&]() { return CExports::NMSaveSetTyped(Handle(), fileName.c_str(), nullptr, nullptr); });
417 }
418
420
425 template <class RANGE>
426 typename TypedRange<void, String, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
427 {
428 NativeCall([&]() {
429 return CExports::NMSaveSetExTyped(Handle(), fileName.c_str(), BuildNodeList(range).c_str(), nullptr, nullptr);
430 });
431 }
432
434
439 template <class RANGE>
440 typename TypedRange<void, NodePtr, RANGE>::type SaveSettings(const String &fileName, const RANGE &range) const
441 {
442 auto rangeAdapter = MakeRangeAdapter<NodePtr>(range);
444 names.reserve(rangeAdapter.Size());
445 for (const auto &node : range)
446 names.emplace_back(node->Name());
447 SaveSettings(fileName, names);
448 }
449
451
456 template <class... NAMES>
457 typename VarArgRange<void, const String &, NAMES...>::type SaveSettings(const String &fileName,
458 const NAMES &...names) const
459 {
460 std::vector<String> range = {names...};
461 SaveSettings(fileName, range);
462 }
463
465
470 template <class... NODES>
471 typename VarArgRange<void, const Node &, NODES...>::type SaveSettings(const String &fileName,
472 const NODES &...nodes) const
473 {
474 SaveSettings(fileName, nodes.Name()...);
475 }
476
478
486
488
495 void DownloadFile(const String &fileName, const String &fileSelectorEntryName) const
496 {
497 NativeCall(
498 [&]() { return CExports::NMDownloadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
499 }
500
502
509 std::vector<uint8_t> DownloadFile(const String &fileSelectorEntryName) const
510 {
511 size_t size = 0;
512 NativeCall([&]() {
513 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), nullptr, size);
514 });
515 std::vector<uint8_t> buffer(size);
516 NativeCall([&]() {
517 return CExports::NMDownloadFileMemoryTyped(Handle(), fileSelectorEntryName.c_str(), buffer.data(), size);
518 });
519 return buffer;
520 }
521
523
530 void UploadFile(const String &fileName, const String &fileSelectorEntryName)
531 {
532 NativeCall(
533 [&]() { return CExports::NMUploadFileTyped(Handle(), fileSelectorEntryName.c_str(), fileName.c_str()); });
534 }
535
537
543 Cvb::String ToJson(std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr)
544 {
545 std::vector<NodePtr> nodes = {};
546 auto nodesMap = Nodes();
547 for (auto &kvp : nodesMap)
548 {
549 nodes.push_back(kvp.second);
550 }
551
552 return ToJson(nodes, serializationErrorCallback);
553 }
554
556
564 std::function<void(NodePtr, const std::string &)> serializationErrorCallback = nullptr);
565
567
574 template <class T>
575 std::shared_ptr<T> Node(const String &name) const
576 {
577 static_assert(std::is_base_of<Cvb::GenApi::Node, T>::value, "requested node type must be derived from Node");
578 auto ptr = std::dynamic_pointer_cast<T>(Node(name));
579 if (!ptr)
580 throw std::runtime_error("requested node can not be casted to the given type");
581
582 return ptr;
583 }
584
586
609 NodePtr Node(const String &name) const;
610
612
641 template <class T>
642 std::shared_ptr<T> TryGetNode(const String &name) const noexcept
643 {
644 try
645 {
646 return Node<T>(name);
647 }
648 catch (...)
649 {
650 return nullptr;
651 }
652 }
653
655
683 NodePtr TryGetNode(const String &name) const noexcept;
684
686
691
692 private:
693 NodeMap(HandleGuard<NodeMap> &&guard, const String &name, void *provider);
694
695 void ReadDescription() const;
696
697 void FillNodeKeys() const;
698
699 void NativeCall(std::function<CExports::cvbres_t()> fn) const
700 {
701 auto result = fn();
702 if (result < 0)
703 {
705 if (message.empty())
706 message = "Error";
707
708 std::stringstream stream;
709 auto name = Name();
710 std::string asciName(name.begin(), name.end());
711 stream << "NodeMap[" << asciName << "]: " << message;
712 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
713 }
714 }
715
716 template <class T>
717 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
718 {
719 T value;
720 auto result = fn(value);
721 if (result < 0)
722 {
724 if (message.empty())
725 message = "Error";
726
727 std::stringstream stream;
728 auto name = Name();
729 std::string asciName(name.begin(), name.end());
730 stream << "NodeMap[" << asciName << "]: " << message;
731 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
732 }
733 return value;
734 }
735
736 std::int64_t GetInfoAsInt(NodeMapInfo command) const
737 {
738 return static_cast<std::int64_t>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
739 return CExports::NMInfoAsInteger(Handle(), static_cast<CExports::TNodeMapInfo>(command), value);
740 }));
741 }
742
743 String GetInfoAsString(NodeMapInfo command) const
744 {
745 return NativeCall<String>([&](String &value) {
746 std::size_t bufferSize = 0;
747 auto bufferSizeResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
748 reinterpret_cast<Char *>(0), bufferSize);
749 if (bufferSizeResult < 0)
750 return bufferSizeResult;
751
752 std::vector<Char> buffer(bufferSize);
753 auto bufferResult = CExports::NMInfoAsStringTyped(Handle(), static_cast<CExports::TNodeMapInfo>(command),
754 reinterpret_cast<Char *>(&buffer[0]), bufferSize);
755 if (bufferResult < 0)
756 return bufferResult;
757
758 value = String(reinterpret_cast<Char *>(&buffer[0]));
759 return bufferResult;
760 });
761 }
762
763 template <class RANGE>
764 typename TypedRange<String, String, RANGE>::type BuildNodeList(const RANGE &range) const
765 {
766 StringStream nameStream;
767 for (const auto &name : range)
768 nameStream << name << CVB_LIT("|");
769
770 return nameStream.str();
771 }
772
773 HandleGuard<NodeMap> handle_;
774
775 String name_;
776
777 mutable ReleaseObjectGuard provider_{nullptr};
778
779 mutable String description_;
780
781 mutable bool descriptionRead_ = false;
782
783 mutable std::map<String, std::weak_ptr<class Node>> nodes_;
784
785 mutable std::map<String, bool> nodeKeys_;
786
787 mutable bool nodeKeysFilled_ = false;
788
789 std::chrono::system_clock::time_point lastPoll_;
790 };
791
792 } // namespace GenApi
793
794 using GenApi::NodeMap;
795
796 CVB_END_INLINE_NS
797
798} // namespace Cvb
799
800#ifdef _MSC_VER
801# pragma warning(pop)
802#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:386
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:426
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:642
void LoadSettings(const String &fileName)
Loads the node values from the gcs file and sets the node values accordingly.
Definition decl_node_map.hpp:404
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:575
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:440
std::vector< String > AvailableFiles() const
Gets the currently available file identifiers, which can be downloaded or uploaded.
Definition detail_node_map.hpp:52
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:457
void UploadFile(const String &fileName, const String &fileSelectorEntryName)
Uploads a file to the camera via the GenApi file upload.
Definition decl_node_map.hpp:530
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:509
String ModelName() const
Gets the name of the model, that the XML description is for.
Definition decl_node_map.hpp:333
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
String Name() const
Name used to access this node map.
Definition decl_node_map.hpp:296
void SaveSettings(const String &fileName) const
Saves all nodes which are streamable.
Definition decl_node_map.hpp:414
String TransportLayerNamespace() const
Gets the transport layer type of the device.
Definition decl_node_map.hpp:343
String Description() const
Gets the descriptive text of this node map.
Definition decl_node_map.hpp:306
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:543
GenApiVersion XmlFileSchemaVersion() const
Gets the XML schema version.
Definition decl_node_map.hpp:375
GenApiVersion XmlFileVersion() const
Gets the XML version.
Definition decl_node_map.hpp:363
String VendorName() const
Gets the name of the vendor, who created the XML description.
Definition decl_node_map.hpp:320
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:125
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:353
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:495
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:471
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 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
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)