DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tpglibs::testapp::BinarySignalReader< T > Class Template Reference

Simple binary file reader for integer types. More...

#include <BinarySignalReader.hpp>

Public Member Functions

 BinarySignalReader (const std::string &filepath)
 Constructor - opens the file for reading.
 
 ~BinarySignalReader ()=default
 Destructor - closes the file.
 
std::vector< T > next (size_t n)
 Read next chunk of data.
 
bool eof ()
 Check if we've reached end of file.
 
std::streampos tellg ()
 Get current file position.
 
void seekg (std::streampos pos)
 Seek to specific position.
 

Private Attributes

std::ifstream m_file
 
bool m_eof_reached
 

Detailed Description

template<typename T>
class tpglibs::testapp::BinarySignalReader< T >

Simple binary file reader for integer types.

Reads binary data from a file and returns it as the requested integer type. Used for reading int16_t test data.

Definition at line 27 of file BinarySignalReader.hpp.

Constructor & Destructor Documentation

◆ BinarySignalReader()

template<typename T >
tpglibs::testapp::BinarySignalReader< T >::BinarySignalReader ( const std::string & filepath)
explicit

Constructor - opens the file for reading.

Parameters
filepathPath to the binary file
Exceptions
std::runtime_errorif file cannot be opened

Definition at line 19 of file BinarySignalReader.hxx.

20 : m_file(filepath, std::ios::binary), m_eof_reached(false) {
21 if (!m_file.is_open()) {
22 throw std::runtime_error("Failed to open file: " + filepath);
23 }
24}

◆ ~BinarySignalReader()

template<typename T >
tpglibs::testapp::BinarySignalReader< T >::~BinarySignalReader ( )
default

Destructor - closes the file.

Member Function Documentation

◆ eof()

template<typename T >
bool tpglibs::testapp::BinarySignalReader< T >::eof ( )

Check if we've reached end of file.

Returns
true if EOF reached

Definition at line 51 of file BinarySignalReader.hxx.

51 {
52 // Check if we're at the end of the file by comparing position with file size
53 std::streampos current_pos = m_file.tellg();
54 m_file.seekg(0, std::ios::end);
55 std::streampos end_pos = m_file.tellg();
56 m_file.seekg(current_pos);
57
58 if (current_pos >= end_pos) {
59 m_eof_reached = true;
60 return true;
61 }
62
63 m_eof_reached = false;
64 return false;
65}

◆ next()

template<typename T >
std::vector< T > tpglibs::testapp::BinarySignalReader< T >::next ( size_t n)

Read next chunk of data.

Parameters
nNumber of elements to read (actual bytes = n * sizeof(T))
Returns
Vector containing the read data
Exceptions
std::runtime_errorif read fails

Definition at line 27 of file BinarySignalReader.hxx.

27 {
28 if (m_eof_reached || n == 0) {
29 return std::vector<T>();
30 }
31
32 std::vector<T> result;
33 result.reserve(n);
34
35 for (size_t i = 0; i < n; ++i) {
36 T value;
37 m_file.read(reinterpret_cast<char*>(&value), sizeof(T));
38
39 if (m_file.gcount() != sizeof(T)) {
40 m_eof_reached = true;
41 break;
42 }
43
44 result.push_back(value);
45 }
46
47 return result;
48}

◆ seekg()

template<typename T >
void tpglibs::testapp::BinarySignalReader< T >::seekg ( std::streampos pos)

Seek to specific position.

Parameters
posPosition to seek to

Definition at line 73 of file BinarySignalReader.hxx.

73 {
74 m_file.seekg(pos);
75 eof(); // Update m_eof_reached based on actual position
76}
bool eof()
Check if we've reached end of file.

◆ tellg()

template<typename T >
std::streampos tpglibs::testapp::BinarySignalReader< T >::tellg ( )

Get current file position.

Returns
Current position in file

Definition at line 68 of file BinarySignalReader.hxx.

68 {
69 return m_file.tellg();
70}

Member Data Documentation

◆ m_eof_reached

template<typename T >
bool tpglibs::testapp::BinarySignalReader< T >::m_eof_reached
private

Definition at line 69 of file BinarySignalReader.hpp.

◆ m_file

template<typename T >
std::ifstream tpglibs::testapp::BinarySignalReader< T >::m_file
private

Definition at line 68 of file BinarySignalReader.hpp.


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