Blaze 3.9
Archive.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_UTIL_SERIALIZATION_ARCHIVE_H_
36#define _BLAZE_UTIL_SERIALIZATION_ARCHIVE_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <memory>
44#include <blaze/util/EnableIf.h>
45#include <blaze/util/Types.h>
47
48
49namespace blaze {
50
51
52//=================================================================================================
53//
54// CLASS DEFINITION
55//
56//=================================================================================================
57
58//*************************************************************************************************
138template< typename Stream > // Type of the bound stream
140{
141 public:
142 //**Constructors********************************************************************************
145 template< typename... Args >
146 explicit inline Archive( Args&&... args );
147
148 explicit inline Archive( Stream& stream );
149
150 Archive( const Archive& ) = delete;
151 Archive( Archive&& ) = default;
153 //**********************************************************************************************
154
155 //**Destructor**********************************************************************************
158 ~Archive() = default;
160 //**********************************************************************************************
161
162 //**Assignment operators************************************************************************
165 Archive& operator=( const Archive& ) = delete;
166 Archive& operator=( Archive&& ) = default;
168 //**********************************************************************************************
169
170 //**Operators***********************************************************************************
173 inline operator bool() const;
174 inline bool operator!() const;
176 //**********************************************************************************************
177
178 //**Serialization functions*********************************************************************
181 template< typename T >
182 EnableIf_t< IsNumeric_v<T>, Archive& > operator<<( const T& value );
183
184 template< typename T >
185 DisableIf_t< IsNumeric_v<T>, Archive& > operator<<( const T& value );
186
187 template< typename T >
188 EnableIf_t< IsNumeric_v<T>, Archive& > operator>>( T& value );
189
190 template< typename T >
192
193 template< typename Type >
194 inline EnableIf_t< IsNumeric_v<Type>, Archive& > write( const Type* array, size_t count );
195
196 template< typename Type >
197 inline EnableIf_t< IsNumeric_v<Type>, Archive& > read ( Type* array, size_t count );
199 //**********************************************************************************************
200
201 //**Utility functions***************************************************************************
204 inline typename Stream::int_type peek() const;
205
206 inline bool good() const;
207 inline bool eof () const;
208 inline bool fail() const;
209 inline bool bad () const;
210
211 inline std::ios_base::iostate rdstate () const;
212 inline void setstate( std::ios_base::iostate state );
213 inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
215 //**********************************************************************************************
216
217 private:
218 //**Member variables****************************************************************************
221 std::unique_ptr<Stream> ptr_;
225 Stream& stream_;
227 //**********************************************************************************************
228};
229//*************************************************************************************************
230
231
232
233
234//=================================================================================================
235//
236// CONSTRUCTORS
237//
238//=================================================================================================
239
240//*************************************************************************************************
248template< typename Stream > // Type of the bound stream
249template< typename... Args > // Types of the optional arguments
250inline Archive<Stream>::Archive( Args&&... args )
251 : ptr_ ( new Stream( std::forward<Args>( args )... ) ) // The dynamically allocated stream resource
252 , stream_( *ptr_.get() ) // Reference to the bound stream
253{}
254//*************************************************************************************************
255
256
257//*************************************************************************************************
265template< typename Stream > // Type of the bound stream
267 : ptr_ () // The dynamically allocated stream resource
268 , stream_( stream ) // Reference to the bound stream
269{}
270//*************************************************************************************************
271
272
273
274
275//=================================================================================================
276//
277// OPERATORS
278//
279//=================================================================================================
280
281//*************************************************************************************************
290template< typename Stream > // Type of the bound stream
291inline Archive<Stream>::operator bool() const
292{
293 return !stream_.fail();
294}
295//*************************************************************************************************
296
297
298//*************************************************************************************************
307template< typename Stream > // Type of the bound stream
308inline bool Archive<Stream>::operator!() const
309{
310 return stream_.fail();
311}
312//*************************************************************************************************
313
314
315
316
317//=================================================================================================
318//
319// SERIALIZATION FUNCTIONS
320//
321//=================================================================================================
322
323//*************************************************************************************************
329template< typename Stream > // Type of the bound stream
330template< typename T > // Type of the value to be serialized
332{
333 using CharType = typename Stream::char_type;
334 stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
335 return *this;
336}
337//*************************************************************************************************
338
339
340//*************************************************************************************************
346template< typename Stream > // Type of the bound stream
347template< typename T > // Type of the object to be serialized
348DisableIf_t< IsNumeric_v<T>, Archive<Stream>& > Archive<Stream>::operator<<( const T& value )
349{
350 serialize( *this, value );
351 return *this;
352}
353//*************************************************************************************************
354
355
356//*************************************************************************************************
362template< typename Stream > // Type of the bound stream
363template< typename T > // Type of the value to be deserialized
365{
366 using CharType = typename Stream::char_type;
367 stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
368 return *this;
369}
370//*************************************************************************************************
371
372
373//*************************************************************************************************
379template< typename Stream > // Type of the bound stream
380template< typename T > // Type of the value to be deserialized
382{
383 deserialize( *this, value );
384 return *this;
385}
386//*************************************************************************************************
387
388
389//*************************************************************************************************
398template< typename Stream > // Type of the bound stream
399template< typename Type > // Type of the array elements
401 Archive<Stream>::write( const Type* array, size_t count )
402{
403 using CharType = typename Stream::char_type;
404 stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
405 return *this;
406}
407//*************************************************************************************************
408
409
410//*************************************************************************************************
421template< typename Stream > // Type of the bound stream
422template< typename Type > // Type of the array elements
424 Archive<Stream>::read( Type* array, size_t count )
425{
426 using CharType = typename Stream::char_type;
427 stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
428 return *this;
429}
430//*************************************************************************************************
431
432
433
434
435//=================================================================================================
436//
437// UTILITY FUNCTIONS
438//
439//=================================================================================================
440
441//*************************************************************************************************
446template< typename Stream > // Type of the bound stream
447inline typename Stream::int_type Archive<Stream>::peek() const
448{
449 return stream_.peek();
450}
451//*************************************************************************************************
452
453
454//*************************************************************************************************
459template< typename Stream > // Type of the bound stream
460inline bool Archive<Stream>::good() const
461{
462 return stream_.good();
463}
464//*************************************************************************************************
465
466
467//*************************************************************************************************
472template< typename Stream > // Type of the bound stream
473inline bool Archive<Stream>::eof() const
474{
475 return stream_.eof();
476}
477//*************************************************************************************************
478
479
480//*************************************************************************************************
485template< typename Stream > // Type of the bound stream
486inline bool Archive<Stream>::fail() const
487{
488 return stream_.fail();
489}
490//*************************************************************************************************
491
492
493//*************************************************************************************************
498template< typename Stream > // Type of the bound stream
499inline bool Archive<Stream>::bad() const
500{
501 return stream_.bad();
502}
503//*************************************************************************************************
504
505
506//*************************************************************************************************
511template< typename Stream > // Type of the bound stream
512inline std::ios_base::iostate Archive<Stream>::rdstate() const
513{
514 return stream_.rdstate();
515}
516//*************************************************************************************************
517
518
519//*************************************************************************************************
525template< typename Stream > // Type of the bound stream
526inline void Archive<Stream>::setstate( std::ios_base::iostate state )
527{
528 stream_.setstate( state );
529}
530//*************************************************************************************************
531
532
533//*************************************************************************************************
539template< typename Stream > // Type of the bound stream
540inline void Archive<Stream>::clear( std::ios_base::iostate state )
541{
542 return stream_.clear( state );
543}
544//*************************************************************************************************
545
546} // namespace blaze
547
548#endif
Header file for the EnableIf class template.
Header file for the IsNumeric type trait.
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1278
Binary archive for the portable serialization of data.
Definition: Archive.h:140
bool operator!() const
Returns the negated state of the archive.
Definition: Archive.h:308
bool good() const
Checks if no error has occurred, i.e. I/O operations are available.
Definition: Archive.h:460
void setstate(std::ios_base::iostate state)
Sets the state flags to a specific value.
Definition: Archive.h:526
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Clears error and eof flags.
Definition: Archive.h:540
Archive(Args &&... args)
Creating an archive with an internal stream resource.
Definition: Archive.h:250
std::unique_ptr< Stream > ptr_
The dynamically allocated stream resource.
Definition: Archive.h:221
bool fail() const
Checks if a recoverable error has occurred.
Definition: Archive.h:486
bool bad() const
Checks if a non-recoverable error has occurred.
Definition: Archive.h:499
Stream::int_type peek() const
Reads the next character from the input stream without extracting it.
Definition: Archive.h:447
std::ios_base::iostate rdstate() const
Returns the current state flags settings.
Definition: Archive.h:512
bool eof() const
Checks if end-of-file (EOF) has been reached.
Definition: Archive.h:473
Stream & stream_
Reference to the bound stream.
Definition: Archive.h:225
decltype(auto) operator<<(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Left-shift operator for the elementwise left-shift of a dense matrix.
Definition: DMatDMatMapExpr.h:1570
decltype(auto) operator>>(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Right-shift operator for the elementwise right-shift of a dense matrix.
Definition: DMatDMatMapExpr.h:1604
BLAZE_ALWAYS_INLINE EnableIf_t< IsIntegral_v< T1 > &&HasSize_v< T1, 1UL > > stream(T1 *address, const SIMDi8< T2 > &value) noexcept
Aligned, non-temporal store of a vector of 1-byte integral values.
Definition: Stream.h:74
constexpr Type & get(StaticVector< Type, N, TF, AF, PF, Tag > &v) noexcept
Tuple-like index-based access the contents of a static vector.
Definition: StaticVector.h:3052
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:138
typename EnableIf<!Condition, T >::Type DisableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:175
Header file for basic type definitions.