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 : private NonCopyable
142 {
143  public:
144  //**Constructors********************************************************************************
147  template< typename... Args >
148  explicit inline Archive( Args&&... args );
149 
150  explicit inline Archive( Stream& stream );
152  //**********************************************************************************************
153 
154  //**Destructor**********************************************************************************
155  // No explicitly declared destructor.
156  //**********************************************************************************************
157 
158  //**Operators***********************************************************************************
161  inline operator bool() const;
162  inline bool operator!() const;
164  //**********************************************************************************************
165 
166  //**Serialization functions*********************************************************************
169  template< typename T >
170  EnableIf_< IsNumeric<T>, Archive& > operator<<( const T& value );
171 
172  template< typename T >
173  DisableIf_< IsNumeric<T>, Archive& > operator<<( const T& value );
174 
175  template< typename T >
176  EnableIf_< IsNumeric<T>, Archive& > operator>>( T& value );
177 
178  template< typename T >
179  DisableIf_< IsNumeric<T>, Archive& > operator>>( T& value );
180 
181  template< typename Type >
182  inline EnableIf_< IsNumeric<Type>, Archive& > write( const Type* array, size_t count );
183 
184  template< typename Type >
185  inline EnableIf_< IsNumeric<Type>, Archive& > read ( Type* array, size_t count );
187  //**********************************************************************************************
188 
189  //**Utility functions***************************************************************************
192  inline typename Stream::int_type peek() const;
193 
194  inline bool good() const;
195  inline bool eof () const;
196  inline bool fail() const;
197  inline bool bad () const;
198 
199  inline std::ios_base::iostate rdstate () const;
200  inline void setstate( std::ios_base::iostate state );
201  inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
203  //**********************************************************************************************
204 
205  private:
206 
207  //**Member variables****************************************************************************
210  std::unique_ptr<Stream> ptr_;
211 
214  Stream& stream_;
215 
216  //**********************************************************************************************
217 };
218 //*************************************************************************************************
219 
220 
221 
222 
223 //=================================================================================================
224 //
225 // CONSTRUCTORS
226 //
227 //=================================================================================================
228 
229 //*************************************************************************************************
237 template< typename Stream > // Type of the bound stream
238 template< typename... Args > // Types of the optional arguments
239 inline Archive<Stream>::Archive( Args&&... args )
240  : ptr_ ( new Stream( std::forward<Args>( args )... ) ) // The dynamically allocated stream resource
241  , stream_( *ptr_.get() ) // Reference to the bound stream
242 {}
243 //*************************************************************************************************
244 
245 
246 //*************************************************************************************************
254 template< typename Stream > // Type of the bound stream
256  : ptr_ () // The dynamically allocated stream resource
257  , stream_( stream ) // Reference to the bound stream
258 {}
259 //*************************************************************************************************
260 
261 
262 
263 
264 //=================================================================================================
265 //
266 // OPERATORS
267 //
268 //=================================================================================================
269 
270 //*************************************************************************************************
279 template< typename Stream > // Type of the bound stream
280 inline Archive<Stream>::operator bool() const
281 {
282  return !stream_.fail();
283 }
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
296 template< typename Stream > // Type of the bound stream
297 inline bool Archive<Stream>::operator!() const
298 {
299  return stream_.fail();
300 }
301 //*************************************************************************************************
302 
303 
304 
305 
306 //=================================================================================================
307 //
308 // SERIALIZATION FUNCTIONS
309 //
310 //=================================================================================================
311 
312 //*************************************************************************************************
318 template< typename Stream > // Type of the bound stream
319 template< typename T > // Type of the value to be serialized
321 {
322  typedef typename Stream::char_type CharType;
323  stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
324  return *this;
325 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
335 template< typename Stream > // Type of the bound stream
336 template< typename T > // Type of the object to be serialized
338 {
339  serialize( *this, value );
340  return *this;
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
351 template< typename Stream > // Type of the bound stream
352 template< typename T > // Type of the value to be deserialized
354 {
355  typedef typename Stream::char_type CharType;
356  stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
357  return *this;
358 }
359 //*************************************************************************************************
360 
361 
362 //*************************************************************************************************
368 template< typename Stream > // Type of the bound stream
369 template< typename T > // Type of the value to be deserialized
371 {
372  deserialize( *this, value );
373  return *this;
374 }
375 //*************************************************************************************************
376 
377 
378 //*************************************************************************************************
387 template< typename Stream > // Type of the bound stream
388 template< typename Type > // Type of the array elements
390  Archive<Stream>::write( const Type* array, size_t count )
391 {
392  typedef typename Stream::char_type CharType;
393  stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
394  return *this;
395 }
396 //*************************************************************************************************
397 
398 
399 //*************************************************************************************************
410 template< typename Stream > // Type of the bound stream
411 template< typename Type > // Type of the array elements
413  Archive<Stream>::read( Type* array, size_t count )
414 {
415  typedef typename Stream::char_type CharType;
416  stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
417  return *this;
418 }
419 //*************************************************************************************************
420 
421 
422 
423 
424 //=================================================================================================
425 //
426 // UTILITY FUNCTIONS
427 //
428 //=================================================================================================
429 
430 //*************************************************************************************************
435 template< typename Stream > // Type of the bound stream
436 inline typename Stream::int_type Archive<Stream>::peek() const
437 {
438  return stream_.peek();
439 }
440 //*************************************************************************************************
441 
442 
443 //*************************************************************************************************
448 template< typename Stream > // Type of the bound stream
449 inline bool Archive<Stream>::good() const
450 {
451  return stream_.good();
452 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
461 template< typename Stream > // Type of the bound stream
462 inline bool Archive<Stream>::eof() const
463 {
464  return stream_.eof();
465 }
466 //*************************************************************************************************
467 
468 
469 //*************************************************************************************************
474 template< typename Stream > // Type of the bound stream
475 inline bool Archive<Stream>::fail() const
476 {
477  return stream_.fail();
478 }
479 //*************************************************************************************************
480 
481 
482 //*************************************************************************************************
487 template< typename Stream > // Type of the bound stream
488 inline bool Archive<Stream>::bad() const
489 {
490  return stream_.bad();
491 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
500 template< typename Stream > // Type of the bound stream
501 inline std::ios_base::iostate Archive<Stream>::rdstate() const
502 {
503  return stream_.rdstate();
504 }
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
514 template< typename Stream > // Type of the bound stream
515 inline void Archive<Stream>::setstate( std::ios_base::iostate state )
516 {
517  stream_.setstate( state );
518 }
519 //*************************************************************************************************
520 
521 
522 //*************************************************************************************************
528 template< typename Stream > // Type of the bound stream
529 inline void Archive<Stream>::clear( std::ios_base::iostate state )
530 {
531  return stream_.clear( state );
532 }
533 //*************************************************************************************************
534 
535 } // namespace blaze
536 
537 #endif
Archive(Args &&...args)
Creating an archive with an internal stream resource.
Definition: Archive.h:239
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:223
Stream & stream_
Reference to the bound stream.
Definition: Archive.h:214
void clear(std::ios_base::iostate state=std::ios_base::goodbit)
Clears error and eof flags.
Definition: Archive.h:529
STL namespace.
std::ios_base::iostate rdstate() const
Returns the current state flags settings.
Definition: Archive.h:501
bool operator!() const
Returns the negated state of the archive.
Definition: Archive.h:297
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void serialize(Archive &archive, const Matrix< MT, SO > &mat)
Serializes the given matrix and writes it to the archive.
Definition: MatrixSerializer.h:1268
bool good() const
Checks if no error has occurred, i.e. I/O operations are available.
Definition: Archive.h:449
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:462
Header file for the EnableIf class template.
std::unique_ptr< Stream > ptr_
The dynamically allocated stream resource.
Definition: Archive.h:210
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
Stream::int_type peek() const
Reads the next character from the input stream without extracting it.
Definition: Archive.h:436
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:223
void deserialize(Archive &archive, Matrix< MT, SO > &mat)
Deserializes a matrix from the given archive.
Definition: MatrixSerializer.h:1290
bool bad() const
Checks if a non-recoverable error has occurred.
Definition: Archive.h:488
void setstate(std::ios_base::iostate state)
Sets the state flags to a specific value.
Definition: Archive.h:515
bool fail() const
Checks if a recoverable error has occurred.
Definition: Archive.h:475