IDS peak genericC++ v1.13.0
Loading...
Searching...
No Matches
peak_fileadapter.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13
16#include <peak/dll_interface/peak_dll_interface_util.hpp>
17
18#include <cstdint>
19#include <vector>
20
21namespace peak
22{
23namespace core
24{
25class NodeMap;
26
27namespace file
28{
29
55{
56public:
83 FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName);
85
86 FileAdapter(const FileAdapter& other) = delete;
87 FileAdapter& operator=(const FileAdapter& other) = delete;
88 FileAdapter(FileAdapter&& other) = delete;
89 FileAdapter& operator=(FileAdapter&& other) = delete;
90
109 PEAK_NO_DISCARD std::vector<uint8_t> Read(uint64_t size, uint64_t offset = 0) const;
110
129 uint64_t Read(uint8_t* buffer, uint64_t size, uint64_t offset = 0) const;
130
150 void Write(const std::vector<uint8_t>& data, uint64_t offset = 0) const;
151
170 void Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset = 0) const;
171
179 void Delete() const;
180
191 PEAK_NO_DISCARD uint64_t Size() const;
192
203 PEAK_NO_DISCARD uint64_t MaxSize() const;
204
220 PEAK_NO_DISCARD static std::vector<std::string> AvailableFileNames(const std::shared_ptr<NodeMap>& nodeMap);
221
222private:
223 PEAK_FILE_ADAPTER_HANDLE m_backendHandle{};
224};
225
226inline FileAdapter::FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName)
227{
228 if (nodeMap == nullptr)
229 {
230 throw InvalidArgumentException("The given nodeMap is a nullptr!");
231 }
232
233 peak::core::ExecuteAndMapReturnCodes([&] {
234 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Construct(nodeMap->Handle(), fileName.c_str(), &m_backendHandle);
235 });
236}
237
239{
240 (void)PEAK_C_ABI_PREFIX PEAK_FileAdapter_Destruct(m_backendHandle);
241}
242
243inline std::vector<uint8_t> FileAdapter::Read(uint64_t size, uint64_t offset) const
244{
245 std::vector<uint8_t> array(static_cast<size_t>(size));
246 uint64_t readBytes{};
247
248 peak::core::ExecuteAndMapReturnCodes([&] {
249 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, array.data(), &readBytes);
250 });
251
252 array.resize(static_cast<size_t>(readBytes));
253
254 return array;
255}
256
257inline uint64_t FileAdapter::Read(uint8_t* buffer, uint64_t size, uint64_t offset) const
258{
259 uint64_t readBytes{};
260
261 peak::core::ExecuteAndMapReturnCodes([&] {
262 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, buffer, &readBytes);
263 });
264
265 return readBytes;
266}
267
268inline void FileAdapter::Write(const std::vector<uint8_t>& data, uint64_t offset) const
269{
270 peak::core::ExecuteAndMapReturnCodes([&] {
271 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, data.data(), data.size(), offset);
272 });
273}
274
275inline void FileAdapter::Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset) const
276{
277 peak::core::ExecuteAndMapReturnCodes([&] {
278 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, buffer, bufferSize, offset);
279 });
280}
281
282inline void FileAdapter::Delete() const
283{
284 peak::core::ExecuteAndMapReturnCodes([&] {
285 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Delete(m_backendHandle);
286 });
287}
288
289inline uint64_t FileAdapter::Size() const
290{
291 uint64_t size{};
292 peak::core::ExecuteAndMapReturnCodes([&] {
293 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetSize(m_backendHandle, &size);
294 });
295
296 return size;
297}
298
299inline uint64_t FileAdapter::MaxSize() const
300{
301 uint64_t size{};
302 peak::core::ExecuteAndMapReturnCodes([&] {
303 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetMaxSize(m_backendHandle, &size);
304 });
305
306 return size;
307}
308
309inline std::vector<std::string> FileAdapter::AvailableFileNames(const std::shared_ptr<NodeMap>& nodeMap)
310{
311 if (nodeMap == nullptr)
312 {
313 throw InvalidArgumentException("The given nodeMap is a nullptr!");
314 }
315
316 const auto fileSelectorNode = nodeMap->TryFindNode<nodes::EnumerationNode>("FileSelector");
317 if (fileSelectorNode == nullptr)
318 {
319 throw NotAvailableException("EnumerationNode with the name 'FileSelector' not found.");
320 }
321 std::vector<std::string> availableFileNames{};
322 const auto entries = fileSelectorNode->AvailableEntries();
323 availableFileNames.reserve(entries.size());
324
325 for (const auto& entry : entries)
326 {
327 availableFileNames.emplace_back(entry->SymbolicValue());
328 }
329 return availableFileNames;
330}
331
332} /* namespace file */
333} /* namespace core */
334} /* namespace peak */
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:116
Represents a GenAPI node map.
Definition peak_node_map.hpp:63
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:177
FileAdapter(FileAdapter &&other)=delete
void Delete() const
Deletes the file the adapter references.
Definition peak_fileadapter.hpp:282
FileAdapter(const FileAdapter &other)=delete
~FileAdapter()
Definition peak_fileadapter.hpp:238
PEAK_NO_DISCARD uint64_t Size() const
Returns the content size of the file the adapter references. This is the maximum size that can be rea...
Definition peak_fileadapter.hpp:289
FileAdapter & operator=(FileAdapter &&other)=delete
static PEAK_NO_DISCARD std::vector< std::string > AvailableFileNames(const std::shared_ptr< NodeMap > &nodeMap)
Returns a vector of available file names from the given NodeMap. File names may vary depending on the...
Definition peak_fileadapter.hpp:309
FileAdapter(const std::shared_ptr< NodeMap > &nodeMap, const std::string &fileName)
Constructs a FileAdapter for a specific file name in the RemoteDevice's NodeMap.
Definition peak_fileadapter.hpp:226
PEAK_NO_DISCARD std::vector< uint8_t > Read(uint64_t size, uint64_t offset=0) const
Returns the contents of the file that the adapter references.
Definition peak_fileadapter.hpp:243
FileAdapter & operator=(const FileAdapter &other)=delete
void Write(const std::vector< uint8_t > &data, uint64_t offset=0) const
Writes the data to the file the adapter references.
Definition peak_fileadapter.hpp:268
PEAK_NO_DISCARD uint64_t MaxSize() const
Returns the maximum size of the file the adapter references. This is the maximum size that can be wri...
Definition peak_fileadapter.hpp:299
Represents a GenAPI enumeration node.
Definition peak_enumeration_node.hpp:40
std::vector< std::shared_ptr< EnumerationEntryNode > > AvailableEntries() const
Returns the currently selectable entries.
Definition peak_enumeration_node.hpp:408
Definition peak_fileadapter.hpp:28
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:34
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
PEAK_C_API PEAK_FileAdapter_GetMaxSize(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle, uint64_t *maxSize)
Queries the maximum size of the given file.
PEAK_C_API PEAK_FileAdapter_Read(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle, uint64_t size, uint64_t offset, uint8_t *data, uint64_t *dataSize)
Reads from the given file.
#define PEAK_NO_DISCARD
Definition peak_backend.h:27
PEAK_C_API PEAK_FileAdapter_GetSize(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle, uint64_t *size)
Queries the size of the given file.
PEAK_C_API PEAK_FileAdapter_Delete(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle)
Deletes the given file.
struct PEAK_FILE_ADAPTER * PEAK_FILE_ADAPTER_HANDLE
Definition peak_backend.h:489
PEAK_C_API PEAK_FileAdapter_Write(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle, const uint8_t *data, uint64_t size, uint64_t offset)
Writes to the given file.
PEAK_C_API PEAK_FileAdapter_Construct(PEAK_NODE_MAP_HANDLE nodeMapHandle, const char *fileName, PEAK_FILE_ADAPTER_HANDLE *fileAdapterHandle)
Creates a file adapter.
PEAK_C_API PEAK_FileAdapter_Destruct(PEAK_FILE_ADAPTER_HANDLE fileAdapterHandle)
Destroys the given file adapter.
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:16