Newer
Older
Import / research / match3 / state.cpp
//  DescriptionHere - state.cpp
//  Created by John Ryland (jryland@xiaofrog.com), 08/11/2017
//  Copyright (c) 2017 InvertedLogic
//  All rights reserved.
#include "state.h"
#include <sstream>
#include <iostream>
#include <fstream>

const char* saveFileName = "save.txt";

struct SaveFile
{
  SaveFile(std::ios_base::openmode mode) : ios(&fb) { fb.open(saveFileName, mode); }
  ~SaveFile()                            { fb.close(); }
  std::filebuf fb;
  std::iostream ios;
};

#define SERIALIZE_OUT(type, var) \
  if (f.ios) { f.ios << #var " = " << a_state.m_##var << std::endl; }

bool saveState(const SavedState& a_state)
{
  SaveFile f(std::ios::out);
  ALL_STATE_MEMBERS(SERIALIZE_OUT)
  return f.fb.is_open();
}

#define SERIALIZE_IN(type, arg) \
  else if (var == #arg) { f.ios >> a_state.m_##arg; }

bool loadState(SavedState& a_state)
{
  SaveFile f(std::ios::in);
  while (f.ios) {
    std::string var, eq;
    f.ios >> var >> eq;
    if (false) {}
    ALL_STATE_MEMBERS(SERIALIZE_IN)
  }
  return f.fb.is_open();
}