IDS peak genericC++ v1.17.0
Loading...
Searching...
No Matches
peak_fileadapter.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
10
13#include <peak/dll_interface/peak_dll_interface_util.hpp>
15
16#include <cstdint>
17#include <vector>
18
19namespace peak
20{
21namespace core
22{
23namespace file
24{
25
51{
52public:
79 FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName);
81
82 FileAdapter(const FileAdapter& other) = delete;
83 FileAdapter& operator=(const FileAdapter& other) = delete;
84 FileAdapter(FileAdapter&& other) = delete;
85 FileAdapter& operator=(FileAdapter&& other) = delete;
86
105 PEAK_COMMON_NO_DISCARD std::vector<uint8_t> Read(uint64_t size, uint64_t offset = 0) const;
106
117 PEAK_COMMON_NO_DISCARD std::vector<uint8_t> ReadAll() const;
118
137 PEAK_COMMON_NO_DISCARD uint64_t Read(uint8_t* buffer, uint64_t size, uint64_t offset = 0) const;
138
158 void Write(const std::vector<uint8_t>& data, uint64_t offset = 0) const;
159
178 void Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset = 0) const;
179
187 void Delete() const;
188
199 PEAK_COMMON_NO_DISCARD uint64_t Size() const;
200
211 PEAK_COMMON_NO_DISCARD uint64_t MaxSize() const;
212
228 PEAK_COMMON_NO_DISCARD static std::vector<std::string> AvailableFileNames(
229 const std::shared_ptr<NodeMap>& nodeMap);
230
231private:
232 PEAK_FILE_ADAPTER_HANDLE m_backendHandle{};
233};
234
235inline FileAdapter::FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName)
236{
237 if (nodeMap == nullptr)
238 {
239 throw InvalidArgumentException("The given nodeMap is a nullptr!");
240 }
241
242 peak::core::ExecuteAndMapReturnCodes([&] {
244 nodeMap->Handle(), fileName.c_str(), &m_backendHandle);
245 });
246}
247
249{
250 (void)PEAK_C_ABI_PREFIX PEAK_FileAdapter_Destruct(m_backendHandle);
251}
252
253inline std::vector<uint8_t> FileAdapter::Read(uint64_t size, uint64_t offset) const
254{
255 std::vector<uint8_t> array(static_cast<size_t>(size));
256 uint64_t readBytes{};
257
258 peak::core::ExecuteAndMapReturnCodes([&] {
259 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, array.data(), &readBytes);
260 });
261
262 array.resize(static_cast<size_t>(readBytes));
263
264 return array;
265}
266
267inline std::vector<uint8_t> FileAdapter::ReadAll() const
268{
269 return Read(MaxSize());
270}
271
272inline uint64_t FileAdapter::Read(uint8_t* buffer, uint64_t size, uint64_t offset) const
273{
274 uint64_t readBytes{};
275
276 peak::core::ExecuteAndMapReturnCodes([&] {
277 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, buffer, &readBytes);
278 });
279
280 return readBytes;
281}
282
283inline void FileAdapter::Write(const std::vector<uint8_t>& data, uint64_t offset) const
284{
285 peak::core::ExecuteAndMapReturnCodes([&] {
286 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, data.data(), data.size(), offset);
287 });
288}
289
290inline void FileAdapter::Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset) const
291{
292 peak::core::ExecuteAndMapReturnCodes(
293 [&] { return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, buffer, bufferSize, offset); });
294}
295
296inline void FileAdapter::Delete() const
297{
298 peak::core::ExecuteAndMapReturnCodes(
299 [&] { return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Delete(m_backendHandle); });
300}
301
302inline uint64_t FileAdapter::Size() const
303{
304 uint64_t size{};
305 peak::core::ExecuteAndMapReturnCodes(
306 [&] { return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetSize(m_backendHandle, &size); });
307
308 return size;
309}
310
311inline uint64_t FileAdapter::MaxSize() const
312{
313 uint64_t size{};
314 peak::core::ExecuteAndMapReturnCodes(
315 [&] { return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetMaxSize(m_backendHandle, &size); });
316
317 return size;
318}
319
320inline std::vector<std::string> FileAdapter::AvailableFileNames(const std::shared_ptr<NodeMap>& nodeMap)
321{
322 if (nodeMap == nullptr)
323 {
324 throw InvalidArgumentException("The given nodeMap is a nullptr!");
325 }
326
327 const auto fileSelectorNode = nodeMap->TryFindNode<nodes::EnumerationNode>("FileSelector");
328 if (fileSelectorNode == nullptr)
329 {
330 throw NotAvailableException("EnumerationNode with the name 'FileSelector' not found.");
331 }
332 std::vector<std::string> availableFileNames{};
333 const auto entries = fileSelectorNode->AvailableEntries();
334 availableFileNames.reserve(entries.size());
335
336 for (const auto& entry : entries)
337 {
338 availableFileNames.emplace_back(entry->SymbolicValue());
339 }
340 return availableFileNames;
341}
342
343} /* namespace file */
344} /* namespace core */
345} /* namespace peak */
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:118
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:181
FileAdapter(FileAdapter &&other)=delete
void Delete() const
Deletes the file the adapter references.
Definition peak_fileadapter.hpp:296
FileAdapter(const FileAdapter &other)=delete
~FileAdapter()
Definition peak_fileadapter.hpp:248
PEAK_COMMON_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:302
FileAdapter & operator=(FileAdapter &&other)=delete
static PEAK_COMMON_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:320
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:235
PEAK_COMMON_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:253
PEAK_COMMON_NO_DISCARD std::vector< uint8_t > ReadAll() const
Returns the entire content of the file that the adapter references.
Definition peak_fileadapter.hpp:267
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:283
PEAK_COMMON_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:311
Represents a GenAPI enumeration node.
Definition peak_enumeration_node.hpp:37
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< EnumerationEntryNode > > AvailableEntries() const
Returns the currently selectable entries.
Definition peak_enumeration_node.hpp:439
Definition peak_fileadapter.hpp:24
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:32
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_buffer.hpp:29
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.
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:487
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_COMMON_NO_DISCARD
Definition peak_common_defines.h:89
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:13