Template Numerical Library version\ main:bb09b17
|
This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-the-fly data type conversion. More...
#include <TNL/File.h>
Public Member Functions | |
File ()=default | |
Basic constructor. | |
File (const File &)=delete | |
File (const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) | |
Constructor which opens given file. | |
File (File &&)=default | |
void | close () |
Closes the file. | |
const std::string & | getFileName () const |
Returns name of the file. | |
template<typename SourceType > | |
void | ignore (std::streamsize elements=1) |
Extracts and discards characters from the file. | |
template<typename Type , typename SourceType = Type, typename Allocator = Allocators::Host< Type >> | |
void | load (Type *destination, std::streamsize elements=1) |
Method for loading data from the file. | |
void | open (const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) |
Open given file. | |
File & | operator= (const File &)=delete |
File & | operator= (File &&)=default |
template<typename Type , typename TargetType = Type, typename Allocator = Allocators::Host< Type >> | |
void | save (const Type *source, std::streamsize elements=1) |
Method for saving data to the file. | |
Protected Member Functions | |
template<typename Type , typename SourceType , typename Allocator , typename = std::enable_if_t< allocates_host_accessible_data_v< Allocator > >> | |
void | load_impl (Type *destination, std::streamsize elements) |
template<typename Type , typename SourceType , typename Allocator , typename = std::enable_if_t< ! allocates_host_accessible_data_v< Allocator > >, typename = void> | |
void | load_impl (Type *destination, std::streamsize elements) |
template<typename Type , typename TargetType , typename Allocator , typename = std::enable_if_t< allocates_host_accessible_data_v< Allocator > >> | |
void | save_impl (const Type *source, std::streamsize elements) |
template<typename Type , typename TargetType , typename Allocator , typename = std::enable_if_t< ! allocates_host_accessible_data_v< Allocator > >, typename = void> | |
void | save_impl (const Type *source, std::streamsize elements) |
Protected Attributes | |
std::fstream | file |
std::string | fileName |
This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-the-fly data type conversion.
|
inline |
Constructor which opens given file.
All parameters are passed to the open method.
|
inline |
Closes the file.
Throws std::ios_base::failure on failure.
void TNL::File::ignore | ( | std::streamsize | elements = 1 | ) |
Extracts and discards characters from the file.
Throws std::ios_base::failure on failure.
SourceType | type of data stored on the file, |
elements | number of elements to be read and ignored. |
void TNL::File::load | ( | Type * | destination, |
std::streamsize | elements = 1 ) |
Method for loading data from the file.
The data will be stored in buffer which was allocated using the allocator of type Allocator. The data type of the buffer is given by the template parameter Type. The second template parameter SourceType defines the type of data in the source file. If both types are different, on-the-fly conversion takes place during the data loading.
Throws std::ios_base::failure on failure.
Type | type of data to be loaded to the destination. |
SourceType | type of data stored on the file, |
Allocator | type of the allocator which was used to allocate destination. |
destination | Pointer in memory where the elements are loaded and stored after reading. |
elements | number of elements to be loaded from the file. |
The following example shows how to load data directly to GPU.
The following example shows how to do on-the-fly data conversion.
|
inline |
Open given file.
Opens file with given fileName in some mode from std::ios_base::openmode. Note that the file is always opened in binary mode, i.e. std::ios_base::binary is always added to mode.
Throws std::ios_base::failure on failure.
fileName | String which indicates file name. |
mode | Indicates in what mode the file will be opened - see std::ios_base::openmode. |
void TNL::File::save | ( | const Type * | source, |
std::streamsize | elements = 1 ) |
Method for saving data to the file.
The data from the buffer (with type Type) which was allocated using an allocator of type Allocator. TargetType defines as what data type the buffer shall be saved. If the type is different from the data type, on-the-fly data type conversion takes place during the data saving.
Throws std::ios_base::failure on failure.
Type | type of data in the source. |
TargetType | tells as what type data the buffer shall be saved. |
Allocator | type of the allocator which was used to allocate source. |
Index | type of index by which the elements are indexed. |
source | buffer that is going to be saved to the file. |
elements | number of elements saved to the file. |
See File::load for examples.