Mantid
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Mantid::Kernel::BinaryFile< T > Class Template Reference

The BinaryFile template is a helper function for loading simple binary files. More...

#include <BinaryFile.h>

Public Member Functions

 BinaryFile ()
 Empty constructor. More...
 
 BinaryFile (std::string filename)
 Constructor - open a file. More...
 
void close ()
 Close the file. More...
 
size_t getBufferSize (const size_t num_items) const
 Get a buffer size for loading blocks of data. More...
 
size_t getNumElements () const
 Returns the # of elements in the file (cached result of getFileSize) More...
 
size_t getOffset () const
 Returns the current offset into the file. More...
 
std::vector< T > loadAll ()
 Loads the entire contents of the file into a pointer to a std::vector. More...
 
std::vector< T > loadAllIntoVector ()
 Loads the entire contents of the file into a std::vector. More...
 
size_t loadBlock (T *buffer, size_t block_size)
 Loads a single block from file and returns a pointer to a vector containing it. More...
 
size_t loadBlockAt (T *buffer, size_t newOffset, size_t block_size)
 Loads a single block from file and returns a pointer to a vector containing it. More...
 
void open (const std::string &filename)
 Open a file and keep a handle to the file. More...
 
 ~BinaryFile ()
 Destructor, close the file if needed. More...
 

Private Member Functions

size_t getFileSize ()
 Get the size of a file as a multiple of a particular data type. More...
 

Private Attributes

std::unique_ptr< std::ifstream > handle
 File stream. More...
 
size_t num_elements
 Number of elements of size T in the file. More...
 
size_t obj_size
 Size of each object. More...
 
size_t offset
 Offset into the file, if loading in blocks. More...
 

Detailed Description

template<typename T>
class Mantid::Kernel::BinaryFile< T >

The BinaryFile template is a helper function for loading simple binary files.

NOTE: Data saving and loading is little-endian (on Win, Linux, and Intel Mac). Converting from a byte buffer loaded from disk to the T type is done with a reinterpret_cast<T>.

Definition at line 44 of file BinaryFile.h.

Constructor & Destructor Documentation

◆ BinaryFile() [1/2]

template<typename T >
Mantid::Kernel::BinaryFile< T >::BinaryFile ( )
inline

Empty constructor.

Definition at line 48 of file BinaryFile.h.

◆ BinaryFile() [2/2]

template<typename T >
Mantid::Kernel::BinaryFile< T >::BinaryFile ( std::string  filename)
inline

Constructor - open a file.

Definition at line 52 of file BinaryFile.h.

◆ ~BinaryFile()

template<typename T >
Mantid::Kernel::BinaryFile< T >::~BinaryFile ( )
inline

Destructor, close the file if needed.

Definition at line 55 of file BinaryFile.h.

Member Function Documentation

◆ close()

template<typename T >
void Mantid::Kernel::BinaryFile< T >::close ( )
inline

Close the file.

Definition at line 82 of file BinaryFile.h.

◆ getBufferSize()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::getBufferSize ( const size_t  num_items) const
inline

Get a buffer size for loading blocks of data.

Parameters
num_items
Returns
the buffer size

Definition at line 96 of file BinaryFile.h.

References Mantid::Kernel::DEFAULT_BLOCK_SIZE.

◆ getFileSize()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::getFileSize ( )
inlineprivate

Get the size of a file as a multiple of a particular data type.

Returns
the size of the file normalized to the data type
Exceptions
runtime_errorif the file size is not compatible
runtime_errorif the handle is not open.

Definition at line 231 of file BinaryFile.h.

◆ getNumElements()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::getNumElements ( ) const
inline

◆ getOffset()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::getOffset ( ) const
inline

Returns the current offset into the file.

Definition at line 89 of file BinaryFile.h.

◆ loadAll()

template<typename T >
std::vector< T > Mantid::Kernel::BinaryFile< T >::loadAll ( )
inline

Loads the entire contents of the file into a pointer to a std::vector.

The file is closed once done.

Returns
file contents in a vector

Definition at line 109 of file BinaryFile.h.

Referenced by Mantid::DataHandling::FilterEventsByLogValuePreNexus::readPulseidFile(), Mantid::DataHandling::LoadEventPreNexus2::readPulseidFile(), and Mantid::DataHandling::LoadDspacemap::readVulcanBinaryFile().

◆ loadAllIntoVector()

template<typename T >
std::vector< T > Mantid::Kernel::BinaryFile< T >::loadAllIntoVector ( )
inline

Loads the entire contents of the file into a std::vector.

The file is closed once done.

Returns
:: vector with contents of the file

Definition at line 145 of file BinaryFile.h.

Referenced by Mantid::DataHandling::LoadPreNexusMonitors::exec(), Mantid::DataHandling::FilterEventsByLogValuePreNexus::loadPixelMap(), and Mantid::DataHandling::LoadEventPreNexus2::loadPixelMap().

◆ loadBlock()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::loadBlock ( T *  buffer,
size_t  block_size 
)
inline

Loads a single block from file and returns a pointer to a vector containing it.

This can be called repeatedly to load an entire file.

Parameters
block_sizehow many elements to load in the block. If there are not enough elements, the vector returned is smaller than block_size
bufferarray of block_size[] of T; must have been allocated before.
Returns
loaded_size, actually how many elements were loaded.

Definition at line 186 of file BinaryFile.h.

◆ loadBlockAt()

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::loadBlockAt ( T *  buffer,
size_t  newOffset,
size_t  block_size 
)
inline

Loads a single block from file and returns a pointer to a vector containing it.

This can be called repeatedly to load an entire file.

Parameters
bufferarray of block_size[] of T; must have been allocated before.
newOffsetoffset (in # of elements) of where to start loading in the file.
block_sizehow many elements to load in the block. If there are not enough elements, the vector returned is smaller than block_size
Returns
loaded_size, actually how many elements were loaded.

Definition at line 215 of file BinaryFile.h.

◆ open()

template<typename T >
void Mantid::Kernel::BinaryFile< T >::open ( const std::string &  filename)
inline

Open a file and keep a handle to the file.

Parameters
filename:: full path to open
Exceptions
runtime_errorif the file size is not an even multiple of the type size
invalid_argumentif the file does not exist

Definition at line 64 of file BinaryFile.h.

References Mantid::DataHandling::exists().

Member Data Documentation

◆ handle

template<typename T >
std::unique_ptr<std::ifstream> Mantid::Kernel::BinaryFile< T >::handle
private

File stream.

Definition at line 259 of file BinaryFile.h.

◆ num_elements

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::num_elements
private

Number of elements of size T in the file.

Definition at line 263 of file BinaryFile.h.

◆ obj_size

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::obj_size
private

Size of each object.

Definition at line 261 of file BinaryFile.h.

◆ offset

template<typename T >
size_t Mantid::Kernel::BinaryFile< T >::offset
private

Offset into the file, if loading in blocks.

Definition at line 265 of file BinaryFile.h.


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