src/utils/Archive.h
3b232069
 #ifndef __COGAPS_ARCHIVE_H__
 #define __COGAPS_ARCHIVE_H__
 
bb4e2b6f
 #include "GapsAssert.h"
 
3b232069
 #include <fstream>
552f115a
 #include <stdint.h>
dd3a9441
 
1de64512
 // flags for opening an archive
3b232069
 #define ARCHIVE_READ  std::ios::in
74377719
 #define ARCHIVE_WRITE (std::ios::out | std::ios::trunc)
3b232069
 
1de64512
 // magic number written to beginning of archive files
bb4e2b6f
 // needs to be updated everytime the method of checkpointing changes
b962ecf5
 //#define ARCHIVE_MAGIC_NUM 0xCE45D32B // v3.3.22
 #define ARCHIVE_MAGIC_NUM 0xB123AA4D // v3.3.30
1de64512
 
3b232069
 class Archive
 {
 public:
 
     Archive(const std::string &path, std::ios_base::openmode flags)
d6b46a33
         :
     mStream(path.c_str(), std::ios::binary | flags)
bb4e2b6f
     {
         if (flags == ARCHIVE_WRITE)
         {
             *this << static_cast<uint32_t>(ARCHIVE_MAGIC_NUM);
         }
         else // read
         {
             uint32_t magic = 0;
             *this >> magic;
             if (magic != ARCHIVE_MAGIC_NUM)
             {
                 GAPS_ERROR("incompatible checkpoint file\n");
             }            
         }
     }
 
7bbb1f81
     ~Archive()
bb4e2b6f
     {
         mStream.close();
     }
3b232069
 
dd3a9441
     template<typename T>
     friend Archive& writeToArchive(Archive &ar, T val)
     {
         ar.mStream.write(reinterpret_cast<char*>(&val), sizeof(T)); // NOLINT
         return ar;
     }
 
     template<typename T>
     friend Archive& readFromArchive(Archive &ar, T &val)
     {
         ar.mStream.read(reinterpret_cast<char*>(&val), sizeof(T)); // NOLINT
         return ar;
     }    
 
     // explicitly define which types can be automatically written/read
     // don't have C++11 and don't want to add another dependency on boost,
     // so no template tricks
 
2cdb0256
     friend Archive& operator<<(Archive &ar, char val)     { return writeToArchive(ar, val); }
dd3a9441
     friend Archive& operator<<(Archive &ar, bool val)     { return writeToArchive(ar, val); }
     friend Archive& operator<<(Archive &ar, int val)      { return writeToArchive(ar, val); }
     friend Archive& operator<<(Archive &ar, unsigned val) { return writeToArchive(ar, val); }
d6b46a33
     friend Archive& operator<<(Archive &ar, unsigned long val) { return writeToArchive(ar, val); } //NOLINT
     friend Archive& operator<<(Archive &ar, unsigned long long val) { return writeToArchive(ar, val); } //NOLINT
dd3a9441
     friend Archive& operator<<(Archive &ar, int64_t val)  { return writeToArchive(ar, val); }
     friend Archive& operator<<(Archive &ar, float val)    { return writeToArchive(ar, val); }
     friend Archive& operator<<(Archive &ar, double val)   { return writeToArchive(ar, val); }
 
2cdb0256
     friend Archive& operator>>(Archive &ar, char &val)     { return readFromArchive(ar, val); }
dd3a9441
     friend Archive& operator>>(Archive &ar, bool &val)     { return readFromArchive(ar, val); }
     friend Archive& operator>>(Archive &ar, int &val)      { return readFromArchive(ar, val); }
     friend Archive& operator>>(Archive &ar, unsigned &val) { return readFromArchive(ar, val); }
d6b46a33
     friend Archive& operator>>(Archive &ar, unsigned long &val) { return readFromArchive(ar, val); } //NOLINT
     friend Archive& operator>>(Archive &ar, unsigned long long &val) { return readFromArchive(ar, val); } //NOLINT
dd3a9441
     friend Archive& operator>>(Archive &ar, int64_t &val)  { return readFromArchive(ar, val); }
     friend Archive& operator>>(Archive &ar, float &val)    { return readFromArchive(ar, val); }
     friend Archive& operator>>(Archive &ar, double &val)   { return readFromArchive(ar, val); }
9c705994
 
 private:
 
     std::fstream mStream;
3b232069
 };
 
b525c989
 #endif // __COGAPS_ARCHIVE_H__