IDS peak genericC++ v1.11.1
Loading...
Searching...
No Matches
peak::core::file::FileAdapter Class Reference

The FileAdapter is a utility class designed for accessing files stored in a RemoteDevice's persistent memory. More...

#include <peak/file/peak_fileadapter.hpp>

Public Member Functions

 FileAdapter (const std::shared_ptr< NodeMap > &nodeMap, const std::string &fileName)
 Constructs a FileAdapter for a specific file name in the RemoteDevice's NodeMap.
 
 ~FileAdapter ()
 
 FileAdapter (const FileAdapter &other)=delete
 
FileAdapter & operator= (const FileAdapter &other)=delete
 
 FileAdapter (FileAdapter &&other)=delete
 
FileAdapter & operator= (FileAdapter &&other)=delete
 
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.
 
uint64_t Read (uint8_t *buffer, uint64_t size, uint64_t offset=0) const
 Reads the contents of the file into the supplied buffer.
 
void Write (const std::vector< uint8_t > &data, uint64_t offset=0) const
 Writes the data to the file the adapter references.
 
void Write (const uint8_t *buffer, uint64_t bufferSize, uint64_t offset=0) const
 Writes the data supplied in the buffer to the file the adapter references.
 
void Delete () const
 Deletes the file the adapter references.
 
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 read.
 
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 written.
 

Static Public Member Functions

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 device.
 

Detailed Description

The FileAdapter is a utility class designed for accessing files stored in a RemoteDevice's persistent memory.

It provides convenient functions to Read, Write, and Delete files.

const std::string fileName = "UserData1";
const auto nodeMap = remoteDevice->NodeMaps().at(0);
peak::core::file::FileAdapter adapter(nodeMap, fileName);
// Write
std::vector<uint8_t> data{ dataSize };
adapter.Write(data);
// Read
const auto fileSize = adapter.Size();
data = adapter.Read(fileSize);
Note
The FileAdapter relies on certain nodes within the RemoteDevice. If these nodes are not present, the functions may not work properly. Check your device's manual to confirm the availability of these nodes.
  • FileSelector
  • FileOperationSelector
  • FileOperationExecute
  • FileSize
  • FileAccessBuffer
  • FileAccessLength
  • FileAccessOffset
  • ...

The FileAdapter class encapsulates the functions of the GenApi::FileProtocolAdapter.

Constructor & Destructor Documentation

◆ FileAdapter() [1/3]

peak::core::file::FileAdapter::FileAdapter ( const std::shared_ptr< NodeMap > & nodeMap,
const std::string & fileName )
inline

Constructs a FileAdapter for a specific file name in the RemoteDevice's NodeMap.

const std::string fileName = "UserData1";
const auto nodeMap = remoteDevice->NodeMaps().at(0);
peak::core::file::FileAdapter adapter(nodeMap, fileName);

File names may vary depending on the device. To obtain a list of valid file names, you can either iterate through the AvailableEntries of the 'FileSelector' EnumerationNode or use the static FileAdapter::AvailableFileNames function. Some files may be read-only.

Common file names include:

  • UserData1
  • UserData2
  • CriticalEventLog
Parameters
nodeMapThe NodeMap of the RemoteDevice
fileNameA valid file name
Since
1.10
Exceptions
InvalidArgumentExceptionThe nodeMap pointer is invalid.
NotAvailableExceptionThe 'FileSelector' node or it's entry with the given file name is not available.
InternalErrorExceptionAn internal error has occurred.

◆ ~FileAdapter()

peak::core::file::FileAdapter::~FileAdapter ( )
inline

◆ FileAdapter() [2/3]

peak::core::file::FileAdapter::FileAdapter ( const FileAdapter & other)
delete

◆ FileAdapter() [3/3]

peak::core::file::FileAdapter::FileAdapter ( FileAdapter && other)
delete

Member Function Documentation

◆ AvailableFileNames()

std::vector< std::string > peak::core::file::FileAdapter::AvailableFileNames ( const std::shared_ptr< NodeMap > & nodeMap)
inlinestatic

Returns a vector of available file names from the given NodeMap. File names may vary depending on the device.

const auto nodeMap = remoteDevice->NodeMaps().at(0);
Parameters
nodeMapThe NodeMap of the RemoteDevice
Returns
Vector of available file names
Since
1.10
Exceptions
InvalidArgumentExceptionThe NodeMap pointer is invalid.
NotAvailableExceptionThe 'FileSelector' node is not available.
InternalErrorExceptionAn internal error has occurred.

◆ Delete()

void peak::core::file::FileAdapter::Delete ( ) const
inline

Deletes the file the adapter references.

Since
1.10
Exceptions
InternalErrorExceptionAn internal error has occurred.

◆ MaxSize()

uint64_t peak::core::file::FileAdapter::MaxSize ( ) const
inline

Returns the maximum size of the file the adapter references. This is the maximum size that can be written.

Returns
The maximum size
Since
1.10
Exceptions
InternalErrorExceptionAn internal error has occurred.

◆ operator=() [1/2]

FileAdapter & peak::core::file::FileAdapter::operator= ( const FileAdapter & other)
delete

◆ operator=() [2/2]

FileAdapter & peak::core::file::FileAdapter::operator= ( FileAdapter && other)
delete

◆ Read() [1/2]

std::vector< uint8_t > peak::core::file::FileAdapter::Read ( uint64_t size,
uint64_t offset = 0 ) const
inline

Returns the contents of the file that the adapter references.

To read the entire file, use FileAdapter::Size as the size argument.

const auto fileSize = adapter.Size();
auto data = adapter.Read(fileSize);

Alternatively, you can specify an offset and a smaller size to read only a part of the file content.

Parameters
sizeThe amount of data to be read
offsetThe position from the beginning of the file where data reading should start
Returns
File content
Since
1.10
Exceptions
OutOfRangeExceptionSize + offset exceeds the file's size.
InternalErrorExceptionAn internal error has occurred.

◆ Read() [2/2]

uint64_t peak::core::file::FileAdapter::Read ( uint8_t * buffer,
uint64_t size,
uint64_t offset = 0 ) const
inline

Reads the contents of the file into the supplied buffer.

To read the entire file, use FileAdapter::Size as the size argument.

Alternatively, you can specify an offset and a smaller size to read only a part of the file content.

Parameters
bufferThe buffer to write to. The buffer needs to be at least the size supplied in size.
sizeThe amount of data to be read
offsetThe position from the beginning of the file where data reading should start
Returns
the number of bytes read
Since
1.10
Exceptions
OutOfRangeExceptionSize + offset exceeds the file's size.
InternalErrorExceptionAn internal error has occurred.

◆ Size()

uint64_t peak::core::file::FileAdapter::Size ( ) const
inline

Returns the content size of the file the adapter references. This is the maximum size that can be read.

Returns
The content size
Since
1.10
Exceptions
InternalErrorExceptionAn internal error has occurred.

◆ Write() [1/2]

void peak::core::file::FileAdapter::Write ( const std::vector< uint8_t > & data,
uint64_t offset = 0 ) const
inline

Writes the data to the file the adapter references.

std::vector<uint8_t> data{ dataSize };
// fill the data buffer...
adapter.Write(data);

Ensure that the data size does not exceed FileAdapter::MaxSize.

Note
If you don't want to write from the start of the file, add an offset in the function call. This might fail if the RemoteDevice's FileAccessOffset node cannot set the specified offset.
Parameters
dataThe data to be written
offsetThe position from the beginning of the file where data writing should start
Since
1.10
Exceptions
OutOfRangeExceptionData size + offset exceeds the file's MaxSize or offset cannot be set.
InternalErrorExceptionAn internal error has occurred.

◆ Write() [2/2]

void peak::core::file::FileAdapter::Write ( const uint8_t * buffer,
uint64_t bufferSize,
uint64_t offset = 0 ) const
inline

Writes the data supplied in the buffer to the file the adapter references.

Ensure that the data size does not exceed FileAdapter::MaxSize.

Note
If you don't want to write from the start of the file, add an offset in the function call. This might fail if the RemoteDevice's FileAccessOffset node cannot set the specified offset.
Parameters
bufferThe buffer containing the bytes to write to the file.
bufferSizeThe size of the buffer supplied in buffer
offsetThe position from the beginning of the file where data writing should start
Since
1.10
Exceptions
OutOfRangeExceptionData size + offset exceeds the file's MaxSize or offset cannot be set.
InternalErrorExceptionAn internal error has occurred.

The documentation for this class was generated from the following file: