前回の日記にAutoLinkSet.hを書きましたが、今回はBoost.Serializationを便利に使えるようにヘッダーファイルにマクロ定義を書いてそれを利用するプログラムをかいてみました。
► スポイラーを表示
CODE:
#pragma once
#include
#ifndef USE_BOOST_SERIALIZATION
#define USE_BOOST_SERIALIZATION
#include "AutoLinkSet.h"
#endif
//---------------------------------------------------------
// 以下STLコンテナを使う場合は 以下の#defineを書いてから
//このファイルをインクルードしてください。
// #define INCLUDE_ONLY_STRING
//---------------------------------------------------------
#ifdef INCLUDE_ONLY_ARRAY
#include // for std::array
#endif
#ifdef INCLUDE_ONLY_BITSET
#include // for std::bitset
#endif
#ifdef INCLUDE_ONLY_COMPLEX
#include // for std::complex
#endif
#ifdef INCLUDE_ONLY_DEQUE
#include // for std::deque
#endif
#ifdef INCLUDE_ONLY_LIST
#include // for std::list
#endif
#ifdef INCLUDE_ONLY_MAP
#include // for std::map
#endif
#ifdef INCLUDE_ONLY_PRIORITYQUE
#include // for std::priority_queue
#endif
#ifdef INCLUDE_ONLY_QUEUE
#include // for std::queue
#endif
#ifdef INCLUDE_ONLY_SET
#include // for std::set
#endif
#ifdef INCLUDE_ONLY_SHAREDPTR
#include // for std::shared_ptr
#endif
#ifdef INCLUDE_ONLY_STACK
#include // for std::stack
#endif
#ifdef INCLUDE_ONLY_STRING
#include // for std::string
#endif
#ifdef INCLUDE_ONLY_VECTOR
#include // for std::vector
#endif
//バイナリ形式でファイルの読み書きをしたい場合は以下のマクロを定義してください
#ifdef READWRITE_BINARY
#include
#include
#endif
//テキスト形式でファイルの読み書きをしたい場合は以下のマクロを定義してください
#ifdef READWRITE_TEXT
#include
#include
#endif
//XML形式でファイルの読み書きをしたい場合は以下のマクロを定義してください
#ifdef READWRITE_XML
#include
#include
#endif
CODE:
#include
#define INCLUDE_ONLY_STRING
#define READWRITE_TEXT
#include "Serialize_Header.h"
using namespace std:
struct PersonalData {
string name; //名前
double height; //身長
double weight; //体重
private:
friend class boost::serialization::accesss;
template
void serialize(Archive& ar, unsigned int ver ){
ar & name;
ar & height;
ar & weight;
}
};
int main() {
//保存
{
PersonalData data;
data.name = "山田太郎";
data.height = 162.9;
data.weight = 69.5;
ofstream file("save.dat");
boost::archive::text_oarchive oa(file);
oa > data;
}
}