daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 58 in 1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <boost/iostreams/device/mapped_file.hpp>
  2.  
  3. class State {
  4. public:
  5.     template<typename Path>
  6.     State(Path&& p) : path(p) {
  7.         if (access(path.c_str(), F_OK) == -1) {
  8.             std::ofstream f(path, f.binary | f.trunc);
  9.             char buf[sizeof(int)] = {0};
  10.             f.write(buf, sizeof(int));
  11.         }
  12.         backend.open(path, backend.readwrite, sizeof(int));
  13.     }
  14.  
  15.     int get_state() const {
  16.         int result;
  17.         memcpy(&result, backend.data(), sizeof(result));
  18.         return result;
  19.     }
  20.  
  21.     void set_state(int state) {
  22.         memcpy(backend.data(), &state, sizeof(state));
  23.     }
  24.  
  25. private:
  26.     const std::string path;
  27.     boost::iostreams::mapped_file backend;
  28. };
  29.  
  30. int main() {
  31.     State s("state");
  32.     int i = s.get_state();
  33.     std::cout << i << std::endl;
  34.     s.set_state(i + 1);
  35.     return 0;
  36. }
RAW Paste Data
Top