Blaze  3.6
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/DisableIf.h>
45 #include <blaze/util/EnableIf.h>
46 #include <blaze/util/Types.h>
48 
49 
50 namespace blaze {
51 
52 
53 //=================================================================================================
54 //
55 // CLASS DEFINITION
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
139 template< typename Stream > // Type of the bound stream
140 class Archive
141 {
142  public:
143  //**Constructors********************************************************************************
146  template< typename... Args >
147  explicit inline Archive( Args&&... args );
148 
149  explicit inline Archive( Stream& stream );
150 
151  Archive( const Archive& ) = delete;
152  Archive( Archive&& ) = default;
154  //**********************************************************************************************
155 
156  //**Destructor**********************************************************************************
159  ~Archive() = default;
161  //**********************************************************************************************
162 
163  //**Assignment operators************************************************************************
166  Archive& operator=( const Archive& ) = delete;
167  Archive& operator=( Archive&& ) = default;
169  //**********************************************************************************************
170 
171  //**Operators***********************************************************************************
174  inline operator bool() const;
175  inline bool operator!() const;
177  //**********************************************************************************************
178 
179  //**Serialization functions*********************************************************************
182  template< typename T >
183  EnableIf_t< IsNumeric_v<T>, Archive& > operator<<( const T& value );
184 
185  template< typename T >
186  DisableIf_t< IsNumeric_v<T>, Archive& > operator<<( const T& value );
187 
188  template< typename T >
189  EnableIf_t< IsNumeric_v<T>, Archive& > operator>>( T& value );
190 
191  template< typename T >
193 
194  template< typename Type >
195  inline EnableIf_t< IsNumeric_v<Type>, Archive& > write( const Type* array, size_t count );
196 
197  template< typename Type >
198  inline EnableIf_t< IsNumeric_v<Type>, Archive& > read ( Type* array, size_t count );
200  //**********************************************************************************************
201 
202  //**Utility functions***************************************************************************
205  inline typename Stream::int_type peek() const;
206 
207  inline bool good() const;
208  inline bool eof () const;
209  inline bool fail() const;
210  inline bool bad () const;
211 
212  inline std::ios_base::iostate rdstate () const;
213  inline void setstate( std::ios_base::iostate state );
214  inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
216  //**********************************************************************************************
217 
218  private:
219  //**Member variables****************************************************************************
222  std::unique_ptr<Stream> ptr_;
223 
226  Stream& stream_;
227 
228  //**********************************************************************************************
229 };
230 //*************************************************************************************************
231 
232 
233 
234 
235 //=================================================================================================
236 //
237 // CONSTRUCTORS
238 //
239 //=================================================================================================
240 
241 //*************************************************************************************************
249 template< typename Stream > // Type of the bound stream
250 template< typename... Args > // Types of the optional arguments
251 inline Archive<Stream>::Archive( Args&&... args )
252  : ptr_ ( new Stream( std::forward<Args>( args )... ) ) // The dynamically allocated stream resource
253  , stream_( *ptr_.get() ) // Reference to the bound stream
254 {}
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
266 template< typename Stream > // Type of the bound stream
268  : ptr_ () // The dynamically allocated stream resource
269  , stream_( stream ) // Reference to the bound stream
270 {}
271 //*************************************************************************************************
272 
273 
274 
275 
276 //=================================================================================================
277 //
278 // OPERATORS
279 //
280 //=================================================================================================
281 
282 //*************************************************************************************************
291 template< typename Stream > // Type of the bound stream
292 inline Archive<Stream>::operator bool() const
293 {
294  return !stream_.fail();
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
308 template< typename Stream > // Type of the bound stream
309 inline bool Archive<Stream>::operator!() const
310 {
311  return stream_.fail();
312 }
313 //*************************************************************************************************
314 
315 
316 
317 
318 //=================================================================================================
319 //
320 // SERIALIZATION FUNCTIONS
321 //
322 //=================================================================================================
323 
324 //*************************************************************************************************
330 template< typename Stream > // Type of the bound stream
331 template< typename T > // Type of the value to be serialized
333 {
334  using CharType = typename Stream::char_type;
335  stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
336  return *this;
337 }
338 //*************************************************************************************************
339 
340 
341 //*************************************************************************************************
347 template< typename Stream > // Type of the bound stream
348 template< typename T > // Type of the object to be serialized
350 {
351  serialize( *this, value );
352  return *this;
353 }
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
363 template< typename Stream > // Type of the bound stream
364 template< typename T > // Type of the value to be deserialized
366 {
367  using CharType = typename Stream::char_type;
368  stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
369  return *this;
370 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
380 template< typename Stream > // Type of the bound stream
381 template< typename T > // Type of the value to be deserialized
383 {
384  deserialize( *this, value );
385  return *this;
386 }
387 //*************************************************************************************************
388 
389 
390 //*************************************************************************************************
399 template< typename Stream > // Type of the bound stream
400 template< typename Type > // Type of the array elements
402  Archive<Stream>::write( const Type* array, size_t count )
403 {
404  using CharType = typename Stream::char_type;
405  stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
406  return *this;
407 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
422 template< typename Stream > // Type of the bound stream
423 template< typename Type > // Type of the array elements
425  Archive<Stream>::read( Type* array, size_t count )
426 {
427  using CharType = typename Stream::char_type;
428  stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
429  return *this;
430 }
431 //*************************************************************************************************
432 
433 
434 
435 
436 //=================================================================================================
437 //
438 // UTILITY FUNCTIONS
439 //
440 //=================================================================================================
441 
442 //*************************************************************************************************
447 template< typename Stream > // Type of the bound stream
448 inline typename Stream::int_type Archive<Stream>::peek() const
449 {
450  return stream_.peek();
451 }
452 //*************************************************************************************************
453 
454 
455 //*************************************************************************************************
460 template< typename Stream > // Type of the bound stream
461 inline bool Archive<Stream>::good() const
462 {
463  return stream_.good();
464 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
473 template< typename Stream > // Type of the bound stream
474 inline bool Archive<Stream>::eof() const
475 {
476  return stream_.eof();
477 }
478 //*************************************************************************************************
479 
480 
481 //*************************************************************************************************
486 template< typename Stream > // Type of the bound stream
487 inline bool Archive<Stream>::fail() const
488 {
489  return stream_.fail();
490 }
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
499 template< typename Stream > // Type of the bound stream
500 inline bool Archive<Stream>::bad() const
501 {
502  return stream_.bad();
503 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
512 template< typename Stream > // Type of the bound stream
513 inline std::ios_base::iostate Archive<Stream>::rdstate() const
514 {
515  return stream_.rdstate();
516 }
517 //*************************************************************************************************
518 
519 
520 //*************************************************************************************************
526 template< typename Stream > // Type of the bound stream
527 inline void Archive<Stream>::setstate( std::ios_base::iostate state )
528 {
529  stream_.setstate( state );
530 }
531 //*************************************************************************************************
532 
533 
534 //*************************************************************************************************
540 template< typename Stream > // Type of the bound stream
541 inline void Archive<Stream>::clear( std::ios_base::iostate state )
542 {
543  return stream_.clear( state );
544 }
545 //*************************************************************************************************
546 
547 } // namespace blaze
548 
549 #endif
Stream::int_type peek() const
Reads the next character from the input stream without extracting it.
Definition: Archive.h:448
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:140
Header file for basic type definitions.
bool good() const
Checks if no error has occurred, i.e. I/O operations are available.
Definition: Archive.h:461
Stream & stream_
Reference to the bound stream.
Definition: Archive.h:226
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Clears error and eof flags.
Definition: Archive.h:541
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.The EnableIf_t alias declaration provides a convenient...
Definition: EnableIf.h:138
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Archive(Args &&... args)
Creating an archive with an internal stream resource.
Definition: Archive.h:251
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1257
bool eof() const
Checks if end-of-file (EOF) has been reached.
Definition: Archive.h:474
Header file for the EnableIf class template.
std::ios_base::iostate rdstate() const
Returns the current state flags settings.
Definition: Archive.h:513
std::unique_ptr< Stream > ptr_
The dynamically allocated stream resource.
Definition: Archive.h:222
Header file for the IsNumeric type trait.
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:1349
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:1383
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1279
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
bool fail() const
Checks if a recoverable error has occurred.
Definition: Archive.h:487
void setstate(std::ios_base::iostate state)
Sets the state flags to a specific value.
Definition: Archive.h:527
bool bad() const
Checks if a non-recoverable error has occurred.
Definition: Archive.h:500
typename DisableIf< Condition, T >::Type DisableIf_t
Auxiliary type for the DisableIf class template.The DisableIf_t alias declaration provides a convenie...
Definition: DisableIf.h:138
bool operator!() const
Returns the negated state of the archive.
Definition: Archive.h:309
constexpr Type & get(StaticVector< Type, N, TF > &v) noexcept
Tuple-like index-based access the contents of a static vector.
Definition: StaticVector.h:2704