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

Writing a raw data file. More...

#include <Writer.h>

Public Member Functions

 Writer ()
 default constructor
 
void open (const std::string &file_name)
 Open a file with this writer.
 
 Writer (const std::string &file_name)
 Open the input file name upon construction of this writer.
 
 ~Writer ()=default
 destructor, close the input file stream
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Writerwrite (const WordType *w, std::size_t num)
 Write a certain number of words from the input array to the output file stream.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Writeroperator<< (const WordType &w)
 Write a single integral-type word to the output file stream.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Writeroperator<< (const std::vector< WordType > &vec)
 Write out a vector of integral-type objects.
 
template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Writeroperator<< (const ObjectType &o)
 Write out a class object.
 
template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Writeroperator<< (const std::vector< ObjectType > &vec)
 Write out a vector fo class objects.
 
bool operator! () const
 Check if writer is in a fail state.
 
 operator bool () const
 Check if writer is in a good/bad state.
 

Private Attributes

std::ofstream file_
 file stream we are writing to
 

Detailed Description

Writing a raw data file.

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

Definition at line 19 of file Writer.h.

Constructor & Destructor Documentation

◆ Writer() [1/2]

packing::utility::Writer::Writer ( )
inline

default constructor

make sure we don't skip "whitespace"

Definition at line 26 of file Writer.h.

26{ file_.unsetf(std::ios::skipws); }
std::ofstream file_
file stream we are writing to
Definition Writer.h:156

References file_.

◆ Writer() [2/2]

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

Open the input file name upon construction of this writer.

Parameters
[in]file_namename of file to open

Definition at line 44 of file Writer.h.

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

References open().

Member Function Documentation

◆ open()

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

Open a file with this writer.

We open the file stream in output, binary mode.

Parameters
[in]file_namename of file to open

Definition at line 35 of file Writer.h.

35 {
36 file_.open(file_name, std::ios::out | std::ios::binary);
37 }

References file_.

Referenced by packing::SingleSubsystemPacker::configure(), packing::rawdatafile::File::File(), and Writer().

◆ operator bool()

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

Check if writer is in a good/bad state.

Uses std::ofstream::fail to check on stream.

Returns
true if stream is in good state

Definition at line 152 of file Writer.h.

152{ return !file_.fail(); }

References file_.

◆ operator!()

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

Check if writer is in a fail state.

Uses std::ofstream::fail to check on stream.

Returns
true if stream is in fail state

Definition at line 143 of file Writer.h.

143{ return file_.fail(); }

References file_.

◆ operator<<() [1/4]

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

Write out a class object.

We assume that the input class object has a specific method defined.

Writer& write(Writer&) const;

With this method defined, then the class can be streamed out through this object.

Template Parameters

in] ObjectType class-type to write out

Parameters
[in]oobject to write out
Returns
*this

Definition at line 114 of file Writer.h.

114 {
115 return o.write(*this);
116 }

References write().

◆ operator<<() [2/4]

template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Writer & packing::utility::Writer::operator<< ( const std::vector< ObjectType > &  vec)
inline

Write out a vector fo class objects.

We make the same assumptions as when streaming out a single class object. We leave early if any write action changes the file to a fail state.

Template Parameters

in] ObjectType class-type inside vector to write out

Parameters
[in]vecvector of objects to write out
Returns
*this

Definition at line 130 of file Writer.h.

130 {
131 for (auto const& o : vec)
132 if (!o.write(*this)) return *this;
133 return *this;
134 }
Writer & write(const WordType *w, std::size_t num)
Write a certain number of words from the input array to the output file stream.
Definition Writer.h:63

◆ operator<<() [3/4]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Writer & packing::utility::Writer::operator<< ( const std::vector< WordType > &  vec)
inline

Write out a vector of integral-type objects.

This is a short-cut in order to help speed up writing.

Template Parameters

in] WordType integral-type to write out

Parameters
[in]vecvector of integral words to write out
Returns
*this

Definition at line 94 of file Writer.h.

94 {
95 return write(vec.data(), vec.size());
96 }

References write().

◆ operator<<() [4/4]

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

Write a single integral-type word to the output file stream.

See also
write
Template Parameters

in] WordType integral-type to write out

Parameters
[in]wsingle word to write out
Returns
*this

Definition at line 79 of file Writer.h.

79 {
80 return write(&w, 1);
81 }

References write().

◆ write()

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Writer & packing::utility::Writer::write ( const WordType *  w,
std::size_t  num 
)
inline

Write a certain number of words from the input array to the output file stream.

This method is only enabled for integral types so we can safely reinterpret the underlying data as an array of characters.

Template Parameters

in] WordType integral-type to write out

Parameters
[in]wpointer to array of words to write
[in]numnumber of words in array
Returns
*this

Definition at line 63 of file Writer.h.

63 {
64 file_.write(reinterpret_cast<const char*>(w), sizeof(WordType) * num);
65 return *this;
66 }

References file_, and write().

Referenced by operator<<(), operator<<(), operator<<(), and write().

Member Data Documentation

◆ file_

std::ofstream packing::utility::Writer::file_
private

file stream we are writing to

Definition at line 156 of file Writer.h.

Referenced by open(), operator bool(), operator!(), write(), and Writer().


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