Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - checkpoint.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 13 15 86.7 %
Date: 2012-04-09 Functions: 2 2 100.0 %
Branches: 13 32 40.6 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines utility functions for checkpointing data structures.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_CHECKPOINT_H
      23                 :            : #define __SSRC_WSPR_UTILITY_CHECKPOINT_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers-packages.h>
      26                 :            : 
      27                 :            : #include <boost/archive/binary_iarchive.hpp>
      28                 :            : #include <boost/archive/binary_oarchive.hpp>
      29                 :            : #include <boost/filesystem/convenience.hpp>
      30                 :            : #include <fstream>
      31                 :            : 
      32                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      33                 :            : 
      34                 :            : template<typename T>
      35                 :          5 : void save_checkpoint(const std::string & filename, const T & data)
      36                 :            :   SSRC_DECL_THROW(std::runtime_error, boost::archive::archive_exception)
      37                 :            : {
      38                 :            :   std::ofstream ofs(filename.c_str(), std::ios_base::out |
      39                 :         10 :                     std::ios_base::binary | std::ios_base::trunc);
      40   [ +  -  +  - ]:          5 :   if(ofs.is_open()) {
      41   [ +  -  +  - ]:         10 :     boost::archive::binary_oarchive oa(ofs);
      42         [ +  - ]:          5 :     oa << data;
      43                 :            :   } else {
      44   [ #  #  #  # ]:          0 :     throw std::runtime_error("checkpoint file cannot be opened for writing");
      45                 :            :   }
      46                 :          5 : }
      47                 :            : 
      48                 :            : template<typename T>
      49                 :          5 : void load_checkpoint(const std::string & filename, T & data)
      50                 :            :   SSRC_DECL_THROW(std::runtime_error, boost::archive::archive_exception)
      51                 :            : {
      52   [ +  -  +  + ]:          5 :   if(boost::filesystem::exists(filename)) {
      53                 :            :     std::ifstream ifs(filename.c_str(),
      54                 :          8 :                       std::ios_base::in | std::ios_base::binary);
      55                 :            : 
      56   [ +  -  +  - ]:          4 :     if(ifs.is_open()) {
      57   [ +  -  +  - ]:          8 :       boost::archive::binary_iarchive ia(ifs);
      58         [ +  - ]:          4 :       ia >> data;
      59                 :            :     } else {
      60   [ #  #  #  # ]:          0 :       throw std::runtime_error("checkpoint file exists but cannot be opened");
      61                 :            :     }
      62                 :            :   }
      63                 :          5 : }
      64                 :            : 
      65                 :            : __END_NS_SSRC_WSPR_UTILITY
      66                 :            : 
      67                 :            : #endif