All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Archive.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_SERIALIZATION_ARCHIVE_H_
23 #define _BLAZE_UTIL_SERIALIZATION_ARCHIVE_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <blaze/util/DisableIf.h>
31 #include <blaze/util/EnableIf.h>
32 #include <blaze/util/NonCopyable.h>
33 #include <blaze/util/Types.h>
35 #include <blaze/util/UniquePtr.h>
36 
37 
38 namespace blaze {
39 
40 
41 //=================================================================================================
42 //
43 // CLASS DEFINITION
44 //
45 //=================================================================================================
46 
47 //*************************************************************************************************
127 template< typename Stream > // Type of the bound stream
128 class Archive : private NonCopyable
129 {
130  public:
131  //**Constructors********************************************************************************
134  explicit inline Archive();
135 
136  template< typename A1 >
137  explicit inline Archive( const A1& a1 );
138 
139  template< typename A1, typename A2 >
140  explicit inline Archive( const A1& a1, const A2& a2 );
141 
142  template< typename A1, typename A2, typename A3 >
143  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3 );
144 
145  template< typename A1, typename A2, typename A3, typename A4 >
146  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
147 
148  template< typename A1, typename A2, typename A3, typename A4, typename A5 >
149  explicit inline Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
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  typename EnableIf< IsNumeric<T>, Archive& >::Type
172  operator<<( const T& value );
173 
174  template< typename T >
175  typename DisableIf< IsNumeric<T>, Archive& >::Type
176  operator<<( const T& value );
177 
178  template< typename T >
179  typename EnableIf< IsNumeric<T>, Archive& >::Type
180  operator>>( T& value );
181 
182  template< typename T >
183  typename DisableIf< IsNumeric<T>, Archive& >::Type
184  operator>>( T& value );
185 
186  template< typename Type >
187  inline typename EnableIf< IsNumeric<Type>, Archive& >::Type
188  write( const Type* array, size_t count );
189 
190  template< typename Type >
191  inline typename EnableIf< IsNumeric<Type>, Archive& >::Type
192  read ( Type* array, size_t count );
194  //**********************************************************************************************
195 
196  //**Utility functions***************************************************************************
199  inline bool good() const;
200  inline bool eof () const;
201  inline bool fail() const;
202  inline bool bad () const;
203 
204  inline std::ios_base::iostate rdstate () const;
205  inline void setstate( std::ios_base::iostate state );
206  inline void clear ( std::ios_base::iostate state = std::ios_base::goodbit );
208  //**********************************************************************************************
209 
210  private:
211 
212  //**Member variables****************************************************************************
216 
219  Stream& stream_;
220 
221  //**********************************************************************************************
222 };
223 //*************************************************************************************************
224 
225 
226 
227 
228 //=================================================================================================
229 //
230 // CONSTRUCTORS
231 //
232 //=================================================================================================
233 
234 //*************************************************************************************************
240 template< typename Stream > // Type of the bound stream
242  : ptr_ ( new Stream() ) // The dynamically allocated stream resource
243  , stream_( *ptr_.get() ) // Reference to the bound stream
244 {}
245 //*************************************************************************************************
246 
247 
248 //*************************************************************************************************
256 template< typename Stream > // Type of the bound stream
257 template< typename A1 > // Type of the first argument
258 inline Archive<Stream>::Archive( const A1& a1 )
259  : ptr_ ( new Stream( a1 ) ) // The dynamically allocated stream resource
260  , stream_( *ptr_.get() ) // Reference to the bound stream
261 {}
262 //*************************************************************************************************
263 
264 
265 //*************************************************************************************************
274 template< typename Stream > // Type of the bound stream
275 template< typename A1 // Type of the first argument
276  , typename A2 > // Type of the second argument
277 inline Archive<Stream>::Archive( const A1& a1, const A2& a2 )
278  : ptr_ ( new Stream( a1, a2 ) ) // The dynamically allocated stream resource
279  , stream_( *ptr_.get() ) // Reference to the bound stream
280 {}
281 //*************************************************************************************************
282 
283 
284 //*************************************************************************************************
294 template< typename Stream > // Type of the bound stream
295 template< typename A1 // Type of the first argument
296  , typename A2 // Type of the second argument
297  , typename A3 > // Type of the third argument
298 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3 )
299  : ptr_ ( new Stream( a1, a2, a3 ) ) // The dynamically allocated stream resource
300  , stream_( *ptr_.get() ) // Reference to the bound stream
301 {}
302 //*************************************************************************************************
303 
304 
305 //*************************************************************************************************
316 template< typename Stream > // Type of the bound stream
317 template< typename A1 // Type of the first argument
318  , typename A2 // Type of the second argument
319  , typename A3 // Type of the third argument
320  , typename A4 > // Type of the fourth argument
321 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
322  : ptr_ ( new Stream( a1, a2, a3, a4 ) ) // The dynamically allocated stream resource
323  , stream_( *ptr_.get() ) // Reference to the bound stream
324 {}
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
340 template< typename Stream > // Type of the bound stream
341 template< typename A1 // Type of the first argument
342  , typename A2 // Type of the second argument
343  , typename A3 // Type of the third argument
344  , typename A4 // Type of the fourth argument
345  , typename A5 > // Type of the fifth argument
346 inline Archive<Stream>::Archive( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
347  : ptr_ ( new Stream( a1, a2, a3, a4, a5 ) ) // The dynamically allocated stream resource
348  , stream_( *ptr_.get() ) // Reference to the bound stream
349 {}
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
361 template< typename Stream > // Type of the bound stream
363  : ptr_ () // The dynamically allocated stream resource
364  , stream_( stream ) // Reference to the bound stream
365 {}
366 //*************************************************************************************************
367 
368 
369 
370 
371 //=================================================================================================
372 //
373 // OPERATORS
374 //
375 //=================================================================================================
376 
377 //*************************************************************************************************
386 template< typename Stream > // Type of the bound stream
387 inline Archive<Stream>::operator bool() const
388 {
389  return !stream_.fail();
390 }
391 //*************************************************************************************************
392 
393 
394 //*************************************************************************************************
403 template< typename Stream > // Type of the bound stream
404 inline bool Archive<Stream>::operator!() const
405 {
406  return stream_.fail();
407 }
408 //*************************************************************************************************
409 
410 
411 
412 
413 //=================================================================================================
414 //
415 // SERIALIZATION FUNCTIONS
416 //
417 //=================================================================================================
418 
419 //*************************************************************************************************
425 template< typename Stream > // Type of the bound stream
426 template< typename T > // Type of the value to be serialized
427 typename EnableIf< IsNumeric<T>, Archive<Stream>& >::Type
428  Archive<Stream>::operator<<( const T& value )
429 {
430  typedef typename Stream::char_type CharType;
431  stream_.write( reinterpret_cast<const CharType*>( &value ), sizeof( T ) );
432  return *this;
433 }
434 //*************************************************************************************************
435 
436 
437 //*************************************************************************************************
443 template< typename Stream > // Type of the bound stream
444 template< typename T > // Type of the object to be serialized
445 typename DisableIf< IsNumeric<T>, Archive<Stream>& >::Type
446  Archive<Stream>::operator<<( const T& value )
447 {
448  serialize( *this, value );
449  return *this;
450 }
451 //*************************************************************************************************
452 
453 
454 //*************************************************************************************************
460 template< typename Stream > // Type of the bound stream
461 template< typename T > // Type of the value to be deserialized
462 typename EnableIf< IsNumeric<T>, Archive<Stream>& >::Type
464 {
465  typedef typename Stream::char_type CharType;
466  stream_.read( reinterpret_cast<CharType*>( &value ), sizeof( T ) );
467  return *this;
468 }
469 //*************************************************************************************************
470 
471 
472 //*************************************************************************************************
478 template< typename Stream > // Type of the bound stream
479 template< typename T > // Type of the value to be deserialized
480 typename DisableIf< IsNumeric<T>, Archive<Stream>& >::Type
482 {
483  deserialize( *this, value );
484  return *this;
485 }
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
498 template< typename Stream > // Type of the bound stream
499 template< typename Type > // Type of the array elements
500 inline typename EnableIf< IsNumeric<Type>, Archive<Stream>& >::Type
501  Archive<Stream>::write( const Type* array, size_t count )
502 {
503  typedef typename Stream::char_type CharType;
504  stream_.write( reinterpret_cast<const CharType*>( array ), count*sizeof(Type) );
505  return *this;
506 }
507 //*************************************************************************************************
508 
509 
510 //*************************************************************************************************
521 template< typename Stream > // Type of the bound stream
522 template< typename Type > // Type of the array elements
523 inline typename EnableIf< IsNumeric<Type>, Archive<Stream>& >::Type
524  Archive<Stream>::read( Type* array, size_t count )
525 {
526  typedef typename Stream::char_type CharType;
527  stream_.read( reinterpret_cast<CharType*>( array ), count*sizeof(Type) );
528  return *this;
529 }
530 //*************************************************************************************************
531 
532 
533 
534 
535 //=================================================================================================
536 //
537 // UTILITY FUNCTIONS
538 //
539 //=================================================================================================
540 
541 //*************************************************************************************************
546 template< typename Stream > // Type of the bound stream
547 inline bool Archive<Stream>::good() const
548 {
549  return stream_.good();
550 }
551 //*************************************************************************************************
552 
553 
554 //*************************************************************************************************
559 template< typename Stream > // Type of the bound stream
560 inline bool Archive<Stream>::eof() const
561 {
562  return stream_.eof();
563 }
564 //*************************************************************************************************
565 
566 
567 //*************************************************************************************************
572 template< typename Stream > // Type of the bound stream
573 inline bool Archive<Stream>::fail() const
574 {
575  return stream_.fail();
576 }
577 //*************************************************************************************************
578 
579 
580 //*************************************************************************************************
585 template< typename Stream > // Type of the bound stream
586 inline bool Archive<Stream>::bad() const
587 {
588  return stream_.bad();
589 }
590 //*************************************************************************************************
591 
592 
593 //*************************************************************************************************
598 template< typename Stream > // Type of the bound stream
599 inline std::ios_base::iostate Archive<Stream>::rdstate() const
600 {
601  return stream_.rdstate();
602 }
603 //*************************************************************************************************
604 
605 
606 //*************************************************************************************************
612 template< typename Stream > // Type of the bound stream
613 inline void Archive<Stream>::setstate( std::ios_base::iostate state )
614 {
615  stream_.setstate( state );
616 }
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
626 template< typename Stream > // Type of the bound stream
627 inline void Archive<Stream>::clear( std::ios_base::iostate state )
628 {
629  return stream_.clear( state );
630 }
631 //*************************************************************************************************
632 
633 } // namespace blaze
634 
635 #endif