![]() |
Binary archive for the portable serialization of data.The Archive class implements the functionality to create platform independent, portable, representations of arbitrary C++ data structures. The resulting binary data structures can be used to reconstitute the data structures in a different context, on another platform, etc. More...
#include <Archive.h>
Inherits blaze::NonCopyable.
Public Member Functions | |
template<typename T > | |
EnableIf< IsNumeric< T > , Archive< Stream > & >::Type | operator<< (const T &value) |
Serializes the given built-in data value and writes it to the archive. More... | |
template<typename T > | |
DisableIf< IsNumeric< T > , Archive< Stream > & >::Type | operator<< (const T &value) |
Serializes the user-defined object and writes it to the archive. More... | |
template<typename T > | |
EnableIf< IsNumeric< T > , Archive< Stream > & >::Type | operator>> (T &value) |
Deserializes a value of built-in data type and reads it from the archive. More... | |
template<typename T > | |
DisableIf< IsNumeric< T > , Archive< Stream > & >::Type | operator>> (T &value) |
Deserializes an object of user-defined data type and reads it from the archive. More... | |
template<typename Type > | |
EnableIf< IsNumeric< Type > , Archive< Stream > & >::Type | write (const Type *array, size_t count) |
Writing an array of values to the stream. More... | |
template<typename Type > | |
EnableIf< IsNumeric< Type > , Archive< Stream > & >::Type | read (Type *array, size_t count) |
Reading an array of values from the stream. More... | |
Constructors | |
Archive () | |
Creating an archive with an internal stream resource. More... | |
template<typename A1 > | |
Archive (const A1 &a1) | |
Creating an archive with an internal stream resource. More... | |
template<typename A1 , typename A2 > | |
Archive (const A1 &a1, const A2 &a2) | |
Creating an archive with an internal stream resource. More... | |
template<typename A1 , typename A2 , typename A3 > | |
Archive (const A1 &a1, const A2 &a2, const A3 &a3) | |
Creating an archive with an internal stream resource. More... | |
template<typename A1 , typename A2 , typename A3 , typename A4 > | |
Archive (const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) | |
Creating an archive with an internal stream resource. More... | |
template<typename A1 , typename A2 , typename A3 , typename A4 , typename A5 > | |
Archive (const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) | |
Creating an archive with an internal stream resource. More... | |
Archive (Stream &stream) | |
Creating an archive with an external stream resource. More... | |
Operators | |
operator bool () const | |
Returns the current state of the archive. More... | |
bool | operator! () const |
Returns the negated state of the archive. More... | |
Serialization functions | |
template<typename T > | |
EnableIf< IsNumeric< T > , Archive & >::Type | operator<< (const T &value) |
template<typename T > | |
DisableIf< IsNumeric< T > , Archive & >::Type | operator<< (const T &value) |
template<typename T > | |
EnableIf< IsNumeric< T > , Archive & >::Type | operator>> (T &value) |
template<typename T > | |
DisableIf< IsNumeric< T > , Archive & >::Type | operator>> (T &value) |
template<typename Type > | |
EnableIf< IsNumeric< Type > , Archive & >::Type | write (const Type *array, size_t count) |
template<typename Type > | |
EnableIf< IsNumeric< Type > , Archive & >::Type | read (Type *array, size_t count) |
Utility functions | |
bool | good () const |
Checks if no error has occurred, i.e. I/O operations are available. More... | |
bool | eof () const |
Checks if end-of-file (EOF) has been reached. More... | |
bool | fail () const |
Checks if a recoverable error has occurred. More... | |
bool | bad () const |
Checks if a non-recoverable error has occurred. More... | |
std::ios_base::iostate | rdstate () const |
Returns the current state flags settings. More... | |
void | setstate (std::ios_base::iostate state) |
Sets the state flags to a specific value. More... | |
void | clear (std::ios_base::iostate state=std::ios_base::goodbit) |
Clears error and eof flags. More... | |
Private Attributes | |
Member variables | |
UniquePtr< Stream > | ptr_ |
The dynamically allocated stream resource. More... | |
Stream & | stream_ |
Reference to the bound stream. | |
Binary archive for the portable serialization of data.
The Archive class implements the functionality to create platform independent, portable, representations of arbitrary C++ data structures. The resulting binary data structures can be used to reconstitute the data structures in a different context, on another platform, etc.
The following example demonstrates the Archive class by means of a C-style POD data structure:
The archive already supports the (de-)serialization of built-in data types. In order to be able to serialize the POD data structure, i.e. to convert it into a binary representation that can stored or transfered to other machines, the according serialize
and deserialize
function have to be implemented:
The serialize
function implements the conversion from the POD to a binary representation, the deserialize
function implements the reverse conversion from a binary representation to a POD. Note that it is important to write the values to the archive in exactly the same order as the values are read from the archive!
With these two functions it is already possible to serialize a POD object:
Each archive has to be bound to either an output or input stream. In the example, the archive is bound to a file output stream that is created by passing the file name and the according flags to the archive. Subsequently, the archive can be used like an output stream to write the POD data structure to the file called 'filename'.
The reverse conversion from the binary representation contained in the file to the POD data structure can be achieved by binding an archive to a file input stream. Subsequently, the archive can be used as an input stream to read the POD from file.
Note that the Archive class can be bound to any kind of input or output stream (or also iostream) that supports the standard write or read functions, respectively. Therefore the serialization of a C++ data structure is not restricted to binary files, but allows for any possible destination.
|
inlineexplicit |
Creating an archive with an internal stream resource.
This function creates a new archive with an internal stream resource, which is created based on the given argument a1.
|
inlineexplicit |
Creating an archive with an internal stream resource.
a1 | The first stream argument. |
This function creates a new archive with an internal stream resource, which is created based on the given argument a1.
|
inlineexplicit |
Creating an archive with an internal stream resource.
a1 | The first stream argument. |
a2 | The second stream argument. |
This function creates a new archive with an internal stream resource, which is created based on the given arguments a1 and a2.
|
inlineexplicit |
Creating an archive with an internal stream resource.
a1 | The first stream argument. |
a2 | The second stream argument. |
a3 | The third stream argument. |
This function creates a new archive with an internal stream resource, which is created based on the given arguments a1, a2, and a3.
|
inlineexplicit |
Creating an archive with an internal stream resource.
a1 | The first stream argument. |
a2 | The second stream argument. |
a3 | The third stream argument. |
a4 | The fourth stream argument. |
This function creates a new archive with an internal stream resource, which is created based on the given arguments a1, a2, a3, and a4.
|
inlineexplicit |
Creating an archive with an internal stream resource.
a1 | The first stream argument. |
a2 | The second stream argument. |
a3 | The third stream argument. |
a4 | The fourth stream argument. |
a5 | The fifth stream argument. |
This function creates a new archive with an internal stream resource, which is created based on the given arguments a1, a2, a3, a4, and a5.
|
inlineexplicit |
Creating an archive with an external stream resource.
stream | The stream to be bound to the archive. |
This function creates a new archive with an external stream resource, which is bound to the archive. Note that the stream is NOT automatically closed when the archive is destroyed.
|
inline |
Checks if a non-recoverable error has occurred.
|
inline |
Clears error and eof flags.
state | The new error state flags setting. |
|
inline |
Checks if end-of-file (EOF) has been reached.
|
inline |
Checks if a recoverable error has occurred.
|
inline |
Checks if no error has occurred, i.e. I/O operations are available.
|
inline |
Returns the current state of the archive.
This operator returns the current state of the Archive based on the state of the bound stream. In case an input/output error has occurred, the operator returns false, otherwise it returns true.
|
inline |
Returns the negated state of the archive.
This operator returns the negated state of the Archive based on the state of the bound stream. In case an input/output error has occurred, the operator returns true, otherwise it returns false.
EnableIf< IsNumeric<T>, Archive<Stream>& >::Type blaze::Archive< Stream >::operator<< | ( | const T & | value | ) |
Serializes the given built-in data value and writes it to the archive.
value | The built-in data value to be serialized. |
DisableIf< IsNumeric<T>, Archive<Stream>& >::Type blaze::Archive< Stream >::operator<< | ( | const T & | value | ) |
Serializes the user-defined object and writes it to the archive.
value | The user-defined object to be serialized. |
EnableIf< IsNumeric<T>, Archive<Stream>& >::Type blaze::Archive< Stream >::operator>> | ( | T & | value | ) |
Deserializes a value of built-in data type and reads it from the archive.
value | The built-in data value to be read from the archive. |
DisableIf< IsNumeric<T>, Archive<Stream>& >::Type blaze::Archive< Stream >::operator>> | ( | T & | value | ) |
Deserializes an object of user-defined data type and reads it from the archive.
value | The user-defined object to be read from the archive. |
|
inline |
Returns the current state flags settings.
|
inline |
Reading an array of values from the stream.
array | Pointer to the first element of the array. |
count | The number of elements in the array. |
This function reads count elements of the numeric type Type from the archive and writes them to the array. Note that the function can only be used for arrays of numeric type. Also note that the read function does not allocate memory, but expects that array provides storage for at least count elements of type Type!
|
inline |
Sets the state flags to a specific value.
state | The new error state flags setting. |
|
inline |
Writing an array of values to the stream.
array | Pointer to the first element of the array. |
count | The number of elements in the array. |
This function writes count elements of the numeric type Type from the given array to the archive. Note that the function can only be used for arrays of numeric type!
|
private |
The dynamically allocated stream resource.
In case no stream is bound to the archive from the outside, this smart pointer handles the internally allocated stream resource.