IDS peak genericC++ v1.11.1
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
54{
55public:
82 FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName);
84
85 FileAdapter(const FileAdapter& other) = delete;
86 FileAdapter& operator=(const FileAdapter& other) = delete;
87 FileAdapter(FileAdapter&& other) = delete;
88 FileAdapter& operator=(FileAdapter&& other) = delete;
89
108 PEAK_NO_DISCARD std::vector<uint8_t> Read(uint64_t size, uint64_t offset = 0) const;
109
128 uint64_t Read(uint8_t* buffer, uint64_t size, uint64_t offset = 0) const;
129
149 void Write(const std::vector<uint8_t>& data, uint64_t offset = 0) const;
150
169 void Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset = 0) const;
170
178 void Delete() const;
179
190 PEAK_NO_DISCARD uint64_t Size() const;
191
202 PEAK_NO_DISCARD uint64_t MaxSize() const;
203
219 PEAK_NO_DISCARD static std::vector<std::string> AvailableFileNames(const std::shared_ptr<NodeMap>& nodeMap);
220
221private:
222 PEAK_FILE_ADAPTER_HANDLE m_backendHandle{};
223};
224
225inline FileAdapter::FileAdapter(const std::shared_ptr<NodeMap>& nodeMap, const std::string& fileName)
226{
227 if (nodeMap == nullptr)
228 {
229 throw InvalidArgumentException("The given nodeMap is a nullptr!");
230 }
231
232 peak::core::ExecuteAndMapReturnCodes([&] {
233 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Construct(nodeMap->Handle(), fileName.c_str(), &m_backendHandle);
234 });
235}
236
238{
239 (void)PEAK_C_ABI_PREFIX PEAK_FileAdapter_Destruct(m_backendHandle);
240}
241
242inline std::vector<uint8_t> FileAdapter::Read(uint64_t size, uint64_t offset) const
243{
244 std::vector<uint8_t> array(static_cast<size_t>(size));
245 uint64_t readBytes{};
246
247 peak::core::ExecuteAndMapReturnCodes([&] {
248 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, array.data(), &readBytes);
249 });
250
251 array.resize(static_cast<size_t>(readBytes));
252
253 return array;
254}
255
256inline uint64_t FileAdapter::Read(uint8_t* buffer, uint64_t size, uint64_t offset) const
257{
258 uint64_t readBytes{};
259
260 peak::core::ExecuteAndMapReturnCodes([&] {
261 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Read(m_backendHandle, size, offset, buffer, &readBytes);
262 });
263
264 return readBytes;
265}
266
267inline void FileAdapter::Write(const std::vector<uint8_t>& data, uint64_t offset) const
268{
269 peak::core::ExecuteAndMapReturnCodes([&] {
270 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, data.data(), data.size(), offset);
271 });
272}
273
274inline void FileAdapter::Write(const uint8_t* buffer, uint64_t bufferSize, uint64_t offset) const
275{
276 peak::core::ExecuteAndMapReturnCodes([&] {
277 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Write(m_backendHandle, buffer, bufferSize, offset);
278 });
279}
280
281inline void FileAdapter::Delete() const
282{
283 peak::core::ExecuteAndMapReturnCodes([&] {
284 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_Delete(m_backendHandle);
285 });
286}
287
288inline uint64_t FileAdapter::Size() const
289{
290 uint64_t size{};
291 peak::core::ExecuteAndMapReturnCodes([&] {
292 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetSize(m_backendHandle, &size);
293 });
294
295 return size;
296}
297
298inline uint64_t FileAdapter::MaxSize() const
299{
300 uint64_t size{};
301 peak::core::ExecuteAndMapReturnCodes([&] {
302 return PEAK_C_ABI_PREFIX PEAK_FileAdapter_GetMaxSize(m_backendHandle, &size);
303 });
304
305 return size;
306}
307
308inline std::vector<std::string> FileAdapter::AvailableFileNames(const std::shared_ptr<NodeMap>& nodeMap)
309{
310 if (nodeMap == nullptr)
311 {
312 throw InvalidArgumentException("The given nodeMap is a nullptr!");
313 }
314
315 const auto fileSelectorNode = nodeMap->TryFindNode<nodes::EnumerationNode>("FileSelector");
316 if (fileSelectorNode == nullptr)
317 {
318 throw NotAvailableException("EnumerationNode with the name 'FileSelector' not found.");
319 }
320 std::vector<std::string> availableFileNames{};
321 const auto entries = fileSelectorNode->AvailableEntries();
322 availableFileNames.reserve(entries.size());
323
324 for (const auto& entry : entries)
325 {
326 availableFileNames.emplace_back(entry->SymbolicValue());
327 }
328 return availableFileNames;
329}
330
331} /* namespace file */
332} /* namespace core */
333} /* namespace peak */
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:74
Represents a GenAPI node map.
Definition peak_node_map.hpp:62
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:98
FileAdapter(FileAdapter &&other)=delete
void Delete() const
Deletes the file the adapter references.
Definition peak_fileadapter.hpp:281
FileAdapter(const FileAdapter &other)=delete
~FileAdapter()
Definition peak_fileadapter.hpp:237
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:288
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:308
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:225
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:242
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:267
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:298
Represents a GenAPI enumeration node.
Definition peak_enumeration_node.hpp:39
std::vector< std::shared_ptr< EnumerationEntryNode > > AvailableEntries() const
Returns the currently selectable entries.
Definition peak_enumeration_node.hpp:400
Definition peak_fileadapter.hpp:28
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:31
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