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 <blaze/util/DisableIf.h>
44 #include <blaze/util/EnableIf.h>
45 #include <blaze/util/NonCopyable.h>
46 #include <blaze/util/Types.h>
48 #include <blaze/util/UniquePtr.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 : private NonCopyable
142 {
143  public:
144  //**Constructors********************************************************************************
147  explicit inline Archive();
148 
149  template< typename A1 >
150  explicit inline Archive( const A1& a1 );
151 
152  template< typename A1, typename A2 >
153  explicit inline Archive( const A1& a1, const A2& a2 );
154 
155  template< typename A1, typename A2, typename A3 >
156  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3 );
157 
158  template< typename A1, typename A2, typename A3, typename A4 >
159  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
160 
161  template< typename A1, typename A2, typename A3, typename A4, typename A5 >
162  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
163 
164  explicit inline Archive( Stream& stream );
166  //**********************************************************************************************
167 
168  //**Destructor**********************************************************************************
169  // No explicitly declared destructor.
170  //**********************************************************************************************
171 
172  //**Operators***********************************************************************************
175  inline operator bool() const;
176  inline bool operator!() const;
178  //**********************************************************************************************
179 
180  //**Serialization functions*********************************************************************
183  template< typename T >
184  typename EnableIf< IsNumeric<T>, Archive& >::Type
185  operator<<( const T& value );
186 
187  template< typename T >
188  typename DisableIf< IsNumeric<T>, Archive& >::Type
189  operator<<( const T& value );
190 
191  template< typename T >
192  typename EnableIf< IsNumeric<T>, Archive& >::Type
193  operator>>( T& value );
194 
195  template< typename T >
196  typename DisableIf< IsNumeric<T>, Archive& >::Type
197  operator>>( T& value );
198 
199  template< typename Type >
200  inline typename EnableIf< IsNumeric<Type>, Archive& >::Type
201  write( const Type* array, size_t count );
202 
203  template< typename Type >
204  inline typename EnableIf< IsNumeric<Type>, Archive& >::Type
205  read ( Type* array, size_t count );
207  //**********************************************************************************************
208 
209  //**Utility functions***************************************************************************
212  inline bool good() const;
213  inline bool eof () const;
214  inline bool fail() const;
215  inline bool bad () const;
216 
217  inline std::ios_base::iostate rdstate () const;
218  inline void setstate( std::ios_base::iostate state );
219  inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
221  //**********************************************************************************************
222 
223  private:
224 
225  //**Member variables****************************************************************************
229 
232  Stream& stream_;
233 
234  //**********************************************************************************************
235 };
236 //*************************************************************************************************
237 
238 
239 
240 
241 //=================================================================================================
242 //
243 // CONSTRUCTORS
244 //
245 //=================================================================================================
246 
247 //*************************************************************************************************
253 template< typename Stream > // Type of the bound stream
255  : ptr_ ( new Stream() ) // The dynamically allocated stream resource
256  , stream_( *ptr_.get() ) // Reference to the bound stream
257 {}
258 //*************************************************************************************************
259 
260 
261 //*************************************************************************************************
269 template< typename Stream > // Type of the bound stream
270 template< typename A1 > // Type of the first argument
271 inline Archive<Stream>::Archive( const A1& a1 )
272  : ptr_ ( new Stream( a1 ) ) // The dynamically allocated stream resource
273  , stream_( *ptr_.get() ) // Reference to the bound stream
274 {}
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
287 template< typename Stream > // Type of the bound stream
288 template< typename A1 // Type of the first argument
289  , typename A2 > // Type of the second argument
290 inline Archive<Stream>::Archive( const A1& a1, const A2& a2 )
291  : ptr_ ( new Stream( a1, a2 ) ) // The dynamically allocated stream resource
292  , stream_( *ptr_.get() ) // Reference to the bound stream
293 {}
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
307 template< typename Stream > // Type of the bound stream
308 template< typename A1 // Type of the first argument
309  , typename A2 // Type of the second argument
310  , typename A3 > // Type of the third argument
311 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3 )
312  : ptr_ ( new Stream( a1, a2, a3 ) ) // The dynamically allocated stream resource
313  , stream_( *ptr_.get() ) // Reference to the bound stream
314 {}
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
329 template< typename Stream > // Type of the bound stream
330 template< typename A1 // Type of the first argument
331  , typename A2 // Type of the second argument
332  , typename A3 // Type of the third argument
333  , typename A4 > // Type of the fourth argument
334 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
335  : ptr_ ( new Stream( a1, a2, a3, a4 ) ) // The dynamically allocated stream resource
336  , stream_( *ptr_.get() ) // Reference to the bound stream
337 {}
338 //*************************************************************************************************
339 
340 
341 //*************************************************************************************************
353 template< typename Stream > // Type of the bound stream
354 template< typename A1 // Type of the first argument
355  , typename A2 // Type of the second argument
356  , typename A3 // Type of the third argument
357  , typename A4 // Type of the fourth argument
358  , typename A5 > // Type of the fifth argument
359 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
360  : ptr_ ( new Stream( a1, a2, a3, a4, a5 ) ) // The dynamically allocated stream resource
361  , stream_( *ptr_.get() ) // Reference to the bound stream
362 {}
363 //*************************************************************************************************
364 
365 
366 //*************************************************************************************************
374 template< typename Stream > // Type of the bound stream
376  : ptr_ () // The dynamically allocated stream resource
377  , stream_( stream ) // Reference to the bound stream
378 {}
379 //*************************************************************************************************
380 
381 
382 
383 
384 //=================================================================================================
385 //
386 // OPERATORS
387 //
388 //=================================================================================================
389 
390 //*************************************************************************************************
399 template< typename Stream > // Type of the bound stream
400 inline Archive<Stream>::operator bool() const
401 {
402  return !stream_.fail();
403 }
404 //*************************************************************************************************
405 
406 
407 //*************************************************************************************************
416 template< typename Stream > // Type of the bound stream
417 inline bool Archive<Stream>::operator!() const
418 {
419  return stream_.fail();
420 }
421 //*************************************************************************************************
422 
423 
424 
425 
426 //=================================================================================================
427 //
428 // SERIALIZATION FUNCTIONS
429 //
430 //=================================================================================================
431 
432 //*************************************************************************************************
438 template< typename Stream > // Type of the bound stream
439 template< typename T > // Type of the value to be serialized
440 typename EnableIf< IsNumeric<T>, Archive<Stream>& >::Type
441  Archive<Stream>::operator<<( const T& value )
442 {
443  typedef typename Stream::char_type CharType;
444  stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
445  return *this;
446 }
447 //*************************************************************************************************
448 
449 
450 //*************************************************************************************************
456 template< typename Stream > // Type of the bound stream
457 template< typename T > // Type of the object to be serialized
458 typename DisableIf< IsNumeric<T>, Archive<Stream>& >::Type
459  Archive<Stream>::operator<<( const T& value )
460 {
461  serialize( *this, value );
462  return *this;
463 }
464 //*************************************************************************************************
465 
466 
467 //*************************************************************************************************
473 template< typename Stream > // Type of the bound stream
474 template< typename T > // Type of the value to be deserialized
475 typename EnableIf< IsNumeric<T>, Archive<Stream>& >::Type
477 {
478  typedef typename Stream::char_type CharType;
479  stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
480  return *this;
481 }
482 //*************************************************************************************************
483 
484 
485 //*************************************************************************************************
491 template< typename Stream > // Type of the bound stream
492 template< typename T > // Type of the value to be deserialized
493 typename DisableIf< IsNumeric<T>, Archive<Stream>& >::Type
495 {
496  deserialize( *this, value );
497  return *this;
498 }
499 //*************************************************************************************************
500 
501 
502 //*************************************************************************************************
511 template< typename Stream > // Type of the bound stream
512 template< typename Type > // Type of the array elements
513 inline typename EnableIf< IsNumeric<Type>, Archive<Stream>& >::Type
514  Archive<Stream>::write( const Type* array, size_t count )
515 {
516  typedef typename Stream::char_type CharType;
517  stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
518  return *this;
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
534 template< typename Stream > // Type of the bound stream
535 template< typename Type > // Type of the array elements
536 inline typename EnableIf< IsNumeric<Type>, Archive<Stream>& >::Type
537  Archive<Stream>::read( Type* array, size_t count )
538 {
539  typedef typename Stream::char_type CharType;
540  stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
541  return *this;
542 }
543 //*************************************************************************************************
544 
545 
546 
547 
548 //=================================================================================================
549 //
550 // UTILITY FUNCTIONS
551 //
552 //=================================================================================================
553 
554 //*************************************************************************************************
559 template< typename Stream > // Type of the bound stream
560 inline bool Archive<Stream>::good() const
561 {
562  return stream_.good();
563 }
564 //*************************************************************************************************
565 
566 
567 //*************************************************************************************************
572 template< typename Stream > // Type of the bound stream
573 inline bool Archive<Stream>::eof() const
574 {
575  return stream_.eof();
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
585 template< typename Stream > // Type of the bound stream
586 inline bool Archive<Stream>::fail() const
587 {
588  return stream_.fail();
589 }
590 //*************************************************************************************************
591 
592 
593 //*************************************************************************************************
598 template< typename Stream > // Type of the bound stream
599 inline bool Archive<Stream>::bad() const
600 {
601  return stream_.bad();
602 }
603 //*************************************************************************************************
604 
605 
606 //*************************************************************************************************
611 template< typename Stream > // Type of the bound stream
612 inline std::ios_base::iostate Archive<Stream>::rdstate() const
613 {
614  return stream_.rdstate();
615 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
625 template< typename Stream > // Type of the bound stream
626 inline void Archive<Stream>::setstate( std::ios_base::iostate state )
627 {
628  stream_.setstate( state );
629 }
630 //*************************************************************************************************
631 
632 
633 //*************************************************************************************************
639 template< typename Stream > // Type of the bound stream
640 inline void Archive<Stream>::clear( std::ios_base::iostate state )
641 {
642  return stream_.clear( state );
643 }
644 //*************************************************************************************************
645 
646 } // namespace blaze
647 
648 #endif
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.
Stream & stream_
Reference to the bound stream.
Definition: Archive.h:232
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Clears error and eof flags.
Definition: Archive.h:640
Header file for the UniquePtr smart pointer class.
std::ios_base::iostate rdstate() const
Returns the current state flags settings.
Definition: Archive.h:612
bool operator!() const
Returns the negated state of the archive.
Definition: Archive.h:417
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
UniquePtr< Stream > ptr_
The dynamically allocated stream resource.
Definition: Archive.h:228
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1265
bool good() const
Checks if no error has occurred, i.e. I/O operations are available.
Definition: Archive.h:560
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
bool eof() const
Checks if end-of-file (EOF) has been reached.
Definition: Archive.h:573
Header file for the EnableIf class template.
Header file for the IsNumeric type trait.
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
Substitution Failure Is Not An Error (SFINAE) class.The DisableIf class template is an auxiliary tool...
Definition: DisableIf.h:184
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1287
Archive()
Creating an archive with an internal stream resource.
Definition: Archive.h:254
bool bad() const
Checks if a non-recoverable error has occurred.
Definition: Archive.h:599
BLAZE_ALWAYS_INLINE EnableIf< And< IsIntegral< T >, HasSize< T, 2UL > > >::Type stream(T *address, const simd_int16_t &value)
Aligned, non-temporal store of a vector of 2-byte integral values.
Definition: Stream.h:74
void setstate(std::ios_base::iostate state)
Sets the state flags to a specific value.
Definition: Archive.h:626
bool fail() const
Checks if a recoverable error has occurred.
Definition: Archive.h:586