LDMX Software
Public Member Functions | Private Attributes | List of all members
packing::utility::Reader Class Reference

Reading a raw data file. More...

#include <Reader.h>

Public Member Functions

 Reader ()
 default constructor
 
void open (const std::string &file_name)
 Open a file with this reader.
 
 Reader (const std::string &file_name)
 Constructor that also opens the input file.
 
 ~Reader ()=default
 destructor, close the input file stream
 
void seek (int off, std::ios_base::seekdir dir=std::ios::beg)
 Go ("seek") a specific position in the file.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
void seek (int off, std::ios_base::seekdir dir=std::ios::beg)
 Seek by number of words.
 
int tell ()
 Tell us where the reader is.
 
template<typename WordType >
int tell ()
 Tell by number of words.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Readerread (WordType *w, std::size_t count)
 Read the next 'count' words into the input handle.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Readeroperator>> (WordType &w)
 Stream the next word into the input handle.
 
template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Readeroperator>> (ObjectType &o)
 Stream into a class object.
 
template<typename ContentType >
Readerread (std::vector< ContentType > &vec, std::size_t count)
 Read the next 'count' objects into the input vector.
 
bool operator! () const
 Check if reader is in a fail state.
 
 operator bool () const
 Check if reader is in good/bad state.
 
bool eof ()
 check if file is done
 

Private Attributes

std::ifstream file_
 file stream we are reading from
 
std::size_t file_size_
 file size in bytes
 

Detailed Description

Reading a raw data file.

We wrap a basic std::ifstream in order to make the retrieving of specific-width words easier for ourselves.

Definition at line 19 of file Reader.h.

Constructor & Destructor Documentation

◆ Reader() [1/2]

packing::utility::Reader::Reader ( )
inline

default constructor

We make sure that our input file stream will not skip white space.

Definition at line 26 of file Reader.h.

26{ file_.unsetf(std::ios::skipws); }
std::ifstream file_
file stream we are reading from
Definition Reader.h:218

References file_.

◆ Reader() [2/2]

packing::utility::Reader::Reader ( const std::string &  file_name)
inline

Constructor that also opens the input file.

See also
open
Parameters
[in]file_namefull path to the file we are going to open

Definition at line 47 of file Reader.h.

47: Reader() { this->open(file_name); }
void open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:35
Reader()
default constructor
Definition Reader.h:26

References open().

Member Function Documentation

◆ eof()

bool packing::utility::Reader::eof ( )
inline

check if file is done

Just calls the underlying ifstream eof.

Returns
true if we have reached the end of file.

Definition at line 214 of file Reader.h.

214{ return file_.eof() or file_.tellg() == file_size_; }
std::size_t file_size_
file size in bytes
Definition Reader.h:220

References file_, and file_size_.

Referenced by hcal::HcalRawDecoder::produce(), and packing::SingleSubsystemUnpacker::produce().

◆ open()

void packing::utility::Reader::open ( const std::string &  file_name)
inline

Open a file with this reader.

We open the file as an input, binary file.

Parameters
[in]file_namefull path to the file we are going to open

Definition at line 35 of file Reader.h.

35 {
36 file_.open(file_name, std::ios::in | std::ios::binary);
37 file_.seekg(0, std::ios::end);
38 file_size_ = file_.tellg();
39 file_.seekg(0);
40 }

References file_, and file_size_.

Referenced by packing::FiberTrackerRawDecoder::configure(), packing::WRRawDecoder::configure(), hcal::HcalRawDecoder::configure(), packing::SingleSubsystemUnpacker::configure(), packing::rawdatafile::File::File(), and Reader().

◆ operator bool()

packing::utility::Reader::operator bool ( ) const
inline

Check if reader is in good/bad state.

Following the C++ reference, we pass-along the check on if our ifstream is in a fail state.

Defining this operator allows us to do the following.

Reader r('dummy.raw') if (r) { std::cout << "dummy.raw was opened good" << std::endl; }

Returns
bool true if ifstream is in good state

Definition at line 205 of file Reader.h.

205{ return !file_.fail(); }

References file_.

◆ operator!()

bool packing::utility::Reader::operator! ( ) const
inline

Check if reader is in a fail state.

Following the C++ reference, we pass-along the check on if our ifstream is in a fail state.

Returns
bool true if ifstream is in fail state

Definition at line 188 of file Reader.h.

188{ return file_.fail(); }

References file_.

◆ operator>>() [1/2]

template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Reader & packing::utility::Reader::operator>> ( ObjectType &  o)
inline

Stream into a class object.

We assume that the class we are streaming to has a specific method defined.

Reader& read(Reader&)

This can be satisfied by the classes that we own and operate.

Template Parameters

in] ObjectType class type to read

Parameters
[in]oinstance of object to read into
Returns
*this

Definition at line 152 of file Reader.h.

152 {
153 return o.read(*this);
154 }

References read().

◆ operator>>() [2/2]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Reader & packing::utility::Reader::operator>> ( WordType &  w)
inline

Stream the next word into the input handle.

This implementation of the stream operator is only available to handles of integral types. Helps for shorthand of only grabbing a single word from the reader.

See also
read
Template Parameters

in] WordType integral-type word to read out

Parameters
[in]wreference to word to read into
Returns
handle to modified reader

Definition at line 133 of file Reader.h.

133 {
134 return read(&w, 1);
135 }
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
Definition Reader.h:113

References read().

◆ read() [1/2]

template<typename ContentType >
Reader & packing::utility::Reader::read ( std::vector< ContentType > &  vec,
std::size_t  count 
)
inline

Read the next 'count' objects into the input vector.

This is common enough, I wanted to specialize the read function.

The std::vector::resize is helpful for avoiding additional copies while the vector is being expanded. After allocating the space for each entry in the vector, we call the stream operator from *this into each entry in order, leaving early if a failure occurs.

Template Parameters

in] ContentType type of object inside the vector

Parameters
[in]vecobject vector to read into
[in]countnumber of objects to read
Returns
*this

Definition at line 172 of file Reader.h.

172 {
173 vec.resize(count);
174 for (auto& w : vec) {
175 if (!(*this >> w)) return *this;
176 }
177 return *this;
178 }

◆ read() [2/2]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Reader & packing::utility::Reader::read ( WordType *  w,
std::size_t  count 
)
inline

Read the next 'count' words into the input handle.

This implementation of read is only available to pointers to integral types. We assume that whatever space pointed to by w already has the space reserved necessary for the input words.

Template Parameters

in] WordType integral-type word to read out

Parameters
[in]wpointer to WordType array to write data to
[in]countnumber of words to read
Returns
(*this)

Definition at line 113 of file Reader.h.

113 {
114 file_.read(reinterpret_cast<char*>(w), sizeof(WordType) * count);
115 return *this;
116 }

References file_, and read().

Referenced by packing::FiberTrackerField::FiberTrackerField(), operator>>(), operator>>(), packing::SingleSubsystemUnpacker::produce(), packing::rawdatafile::EventPacket::read(), packing::rawdatafile::SubsystemPacket::read(), and read().

◆ seek() [1/2]

void packing::utility::Reader::seek ( int  off,
std::ios_base::seekdir  dir = std::ios::beg 
)
inline

Go ("seek") a specific position in the file.

This non-template version of seek uses the default meaning of the "off" parameter in which it counts bytes.

Parameters
[in]offnumber of bytes to move relative to dir
[in]dirlocation flag for the file, default is beginning

Definition at line 61 of file Reader.h.

61 {
62 file_.seekg(off, dir);
63 }

References file_.

Referenced by packing::rawdatafile::File::File(), and seek().

◆ seek() [2/2]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
void packing::utility::Reader::seek ( int  off,
std::ios_base::seekdir  dir = std::ios::beg 
)
inline

Seek by number of words.

This template version of seek uses the input word type to move around by the count of the input words rather than the count of bytes.

Template Parameters

in] WordType Integral-type to count by

Parameters
[in]offnumber of words to move relative to dir
[in]dirlocation flag for the file, default is beginning

Definition at line 78 of file Reader.h.

78 {
79 seek(off * sizeof(WordType), dir);
80 }
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.
Definition Reader.h:61

References seek().

◆ tell() [1/2]

int packing::utility::Reader::tell ( )
inline

Tell us where the reader is.

Returns
int number of bytes relative to beginning of file

Definition at line 87 of file Reader.h.

87{ return file_.tellg(); }

References file_.

Referenced by packing::rawdatafile::File::File(), and tell().

◆ tell() [2/2]

template<typename WordType >
int packing::utility::Reader::tell ( )
inline

Tell by number of words.

Returns
int number of words relative to beginning of file

Definition at line 95 of file Reader.h.

95 {
96 return tell() / sizeof(WordType);
97 }
int tell()
Tell us where the reader is.
Definition Reader.h:87

References tell().

Member Data Documentation

◆ file_

std::ifstream packing::utility::Reader::file_
private

file stream we are reading from

Definition at line 218 of file Reader.h.

Referenced by eof(), open(), operator bool(), operator!(), read(), Reader(), seek(), and tell().

◆ file_size_

std::size_t packing::utility::Reader::file_size_
private

file size in bytes

Definition at line 220 of file Reader.h.

Referenced by eof(), and open().


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