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/NonCopyable.h>
47 #include <blaze/util/Types.h>
49 
50 
51 namespace blaze {
52 
53 
54 //=================================================================================================
55 //
56 // CLASS DEFINITION
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
140 template< typename Stream > // Type of the bound stream
141 class Archive
142  : private NonCopyable
143 {
144  public:
145  //**Constructors********************************************************************************
148  template< typename... Args >
149  explicit inline Archive( Args&&... args );
150 
151  explicit inline Archive( Stream& stream );
153  //**********************************************************************************************
154 
155  //**Destructor**********************************************************************************
156  // No explicitly declared destructor.
157  //**********************************************************************************************
158 
159  //**Operators***********************************************************************************
162  inline operator bool() const;
163  inline bool operator!() const;
165  //**********************************************************************************************
166 
167  //**Serialization functions*********************************************************************
170  template< typename T >
171  EnableIf_< IsNumeric<T>, Archive& > operator<<( const T& value );
172 
173  template< typename T >
174  DisableIf_< IsNumeric<T>, Archive& > operator<<( const T& value );
175 
176  template< typename T >
177  EnableIf_< IsNumeric<T>, Archive& > operator>>( T& value );
178 
179  template< typename T >
180  DisableIf_< IsNumeric<T>, Archive& > operator>>( T& value );
181 
182  template< typename Type >
183  inline EnableIf_< IsNumeric<Type>, Archive& > write( const Type* array, size_t count );
184 
185  template< typename Type >
186  inline EnableIf_< IsNumeric<Type>, Archive& > read ( Type* array, size_t count );
188  //**********************************************************************************************
189 
190  //**Utility functions***************************************************************************
193  inline typename Stream::int_type peek() const;
194 
195  inline bool good() const;
196  inline bool eof () const;
197  inline bool fail() const;
198  inline bool bad () const;
199 
200  inline std::ios_base::iostate rdstate () const;
201  inline void setstate( std::ios_base::iostate state );
202  inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
204  //**********************************************************************************************
205 
206  private:
207 
208  //**Member variables****************************************************************************
211  std::unique_ptr<Stream> ptr_;
212 
215  Stream& stream_;
216 
217  //**********************************************************************************************
218 };
219 //*************************************************************************************************
220 
221 
222 
223 
224 //=================================================================================================
225 //
226 // CONSTRUCTORS
227 //
228 //=================================================================================================
229 
230 //*************************************************************************************************
238 template< typename Stream > // Type of the bound stream
239 template< typename... Args > // Types of the optional arguments
240 inline Archive<Stream>::Archive( Args&&... args )
241  : ptr_ ( new Stream( std::forward<Args>( args )... ) ) // The dynamically allocated stream resource
242  , stream_( *ptr_.get() ) // Reference to the bound stream
243 {}
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
255 template< typename Stream > // Type of the bound stream
257  : ptr_ () // The dynamically allocated stream resource
258  , stream_( stream ) // Reference to the bound stream
259 {}
260 //*************************************************************************************************
261 
262 
263 
264 
265 //=================================================================================================
266 //
267 // OPERATORS
268 //
269 //=================================================================================================
270 
271 //*************************************************************************************************
280 template< typename Stream > // Type of the bound stream
281 inline Archive<Stream>::operator bool() const
282 {
283  return !stream_.fail();
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
297 template< typename Stream > // Type of the bound stream
298 inline bool Archive<Stream>::operator!() const
299 {
300  return stream_.fail();
301 }
302 //*************************************************************************************************
303 
304 
305 
306 
307 //=================================================================================================
308 //
309 // SERIALIZATION FUNCTIONS
310 //
311 //=================================================================================================
312 
313 //*************************************************************************************************
319 template< typename Stream > // Type of the bound stream
320 template< typename T > // Type of the value to be serialized
322 {
323  using CharType = typename Stream::char_type;
324  stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
325  return *this;
326 }
327 //*************************************************************************************************
328 
329 
330 //*************************************************************************************************
336 template< typename Stream > // Type of the bound stream
337 template< typename T > // Type of the object to be serialized
339 {
340  serialize( *this, value );
341  return *this;
342 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
352 template< typename Stream > // Type of the bound stream
353 template< typename T > // Type of the value to be deserialized
355 {
356  using CharType = typename Stream::char_type;
357  stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
358  return *this;
359 }
360 //*************************************************************************************************
361 
362 
363 //*************************************************************************************************
369 template< typename Stream > // Type of the bound stream
370 template< typename T > // Type of the value to be deserialized
372 {
373  deserialize( *this, value );
374  return *this;
375 }
376 //*************************************************************************************************
377 
378 
379 //*************************************************************************************************
388 template< typename Stream > // Type of the bound stream
389 template< typename Type > // Type of the array elements
391  Archive<Stream>::write( const Type* array, size_t count )
392 {
393  using CharType = typename Stream::char_type;
394  stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
395  return *this;
396 }
397 //*************************************************************************************************
398 
399 
400 //*************************************************************************************************
411 template< typename Stream > // Type of the bound stream
412 template< typename Type > // Type of the array elements
414  Archive<Stream>::read( Type* array, size_t count )
415 {
416  using CharType = typename Stream::char_type;
417  stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
418  return *this;
419 }
420 //*************************************************************************************************
421 
422 
423 
424 
425 //=================================================================================================
426 //
427 // UTILITY FUNCTIONS
428 //
429 //=================================================================================================
430 
431 //*************************************************************************************************
436 template< typename Stream > // Type of the bound stream
437 inline typename Stream::int_type Archive<Stream>::peek() const
438 {
439  return stream_.peek();
440 }
441 //*************************************************************************************************
442 
443 
444 //*************************************************************************************************
449 template< typename Stream > // Type of the bound stream
450 inline bool Archive<Stream>::good() const
451 {
452  return stream_.good();
453 }
454 //*************************************************************************************************
455 
456 
457 //*************************************************************************************************
462 template< typename Stream > // Type of the bound stream
463 inline bool Archive<Stream>::eof() const
464 {
465  return stream_.eof();
466 }
467 //*************************************************************************************************
468 
469 
470 //*************************************************************************************************
475 template< typename Stream > // Type of the bound stream
476 inline bool Archive<Stream>::fail() const
477 {
478  return stream_.fail();
479 }
480 //*************************************************************************************************
481 
482 
483 //*************************************************************************************************
488 template< typename Stream > // Type of the bound stream
489 inline bool Archive<Stream>::bad() const
490 {
491  return stream_.bad();
492 }
493 //*************************************************************************************************
494 
495 
496 //*************************************************************************************************
501 template< typename Stream > // Type of the bound stream
502 inline std::ios_base::iostate Archive<Stream>::rdstate() const
503 {
504  return stream_.rdstate();
505 }
506 //*************************************************************************************************
507 
508 
509 //*************************************************************************************************
515 template< typename Stream > // Type of the bound stream
516 inline void Archive<Stream>::setstate( std::ios_base::iostate state )
517 {
518  stream_.setstate( state );
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
529 template< typename Stream > // Type of the bound stream
530 inline void Archive<Stream>::clear( std::ios_base::iostate state )
531 {
532  return stream_.clear( state );
533 }
534 //*************************************************************************************************
535 
536 } // namespace blaze
537 
538 #endif
Stream::int_type peek() const
Reads the next character from the input stream without extracting it.
Definition: Archive.h:437
Binary archive for the portable serialization of data.The Archive class implements the functionality ...
Definition: Archive.h:141
Header file for basic type definitions.
Base class for non-copyable class instances.
typename DisableIf< Condition, T >::Type DisableIf_
Auxiliary type for the DisableIf class template.The DisableIf_ alias declaration provides a convenien...
Definition: DisableIf.h:224
bool good() const
Checks if no error has occurred, i.e. I/O operations are available.
Definition: Archive.h:450
Stream & stream_
Reference to the bound stream.
Definition: Archive.h:215
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Clears error and eof flags.
Definition: Archive.h:530
STL namespace.
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Archive(Args &&... args)
Creating an archive with an internal stream resource.
Definition: Archive.h:240
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1268
bool eof() const
Checks if end-of-file (EOF) has been reached.
Definition: Archive.h:463
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
Header file for the EnableIf class template.
std::ios_base::iostate rdstate() const
Returns the current state flags settings.
Definition: Archive.h:502
std::unique_ptr< Stream > ptr_
The dynamically allocated stream resource.
Definition: Archive.h:211
Header file for the IsNumeric type trait.
BLAZE_ALWAYS_INLINE EnableIf_< And< IsIntegral< T1 >, HasSize< 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:75
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:224
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1290
bool fail() const
Checks if a recoverable error has occurred.
Definition: Archive.h:476
void setstate(std::ios_base::iostate state)
Sets the state flags to a specific value.
Definition: Archive.h:516
bool bad() const
Checks if a non-recoverable error has occurred.
Definition: Archive.h:489
bool operator!() const
Returns the negated state of the archive.
Definition: Archive.h:298