[189] | 1 | #pragma once
|
---|
| 2 |
|
---|
| 3 | #include <vector>
|
---|
| 4 | #include <string>
|
---|
| 5 | #include <fstream>
|
---|
| 6 | #include <sstream>
|
---|
| 7 |
|
---|
| 8 | #include <boost/archive/xml_oarchive.hpp>
|
---|
| 9 | #include <boost/archive/xml_iarchive.hpp>
|
---|
| 10 |
|
---|
| 11 | namespace Jenga{
|
---|
| 12 | namespace Common{
|
---|
| 13 |
|
---|
| 14 | using namespace std;
|
---|
| 15 |
|
---|
| 16 | template<class T_xml_schema> class BoostXmlSupport{
|
---|
| 17 | virtual const char *RootTagName() const = 0;
|
---|
| 18 |
|
---|
| 19 | public:
|
---|
| 20 | bool Read( istream& ifs )
|
---|
| 21 | {
|
---|
| 22 | bool isSuccessful = false;
|
---|
| 23 |
|
---|
| 24 | try{
|
---|
| 25 | boost::archive::xml_iarchive ia(ifs);
|
---|
| 26 |
|
---|
| 27 | // ファイルから読込
|
---|
| 28 | ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 29 |
|
---|
| 30 | isSuccessful = true;
|
---|
| 31 | }
|
---|
| 32 | catch(...){
|
---|
| 33 | // 失敗
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if( !isSuccessful )
|
---|
| 37 | {
|
---|
| 38 | return false;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return true;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | bool Write( ostream& ofs ) const
|
---|
| 45 | {
|
---|
| 46 | bool isSuccessful = false;
|
---|
| 47 |
|
---|
| 48 | try{
|
---|
| 49 | boost::archive::xml_oarchive oa(ofs);
|
---|
| 50 |
|
---|
| 51 | // ファイルに書き出し
|
---|
| 52 | oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 53 |
|
---|
| 54 | isSuccessful = true;
|
---|
| 55 | }
|
---|
| 56 | catch( ... ){
|
---|
| 57 | // 失敗
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if( !isSuccessful )
|
---|
| 61 | {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | bool Read( const string &xmlFilePath )
|
---|
| 69 | {
|
---|
| 70 | bool isSuccessful = false;
|
---|
| 71 |
|
---|
| 72 | // 入力アーカイブの作成
|
---|
| 73 | std::ifstream ifs( xmlFilePath.c_str() );
|
---|
| 74 |
|
---|
| 75 | bool result = Read(ifs);
|
---|
| 76 |
|
---|
| 77 | // 入力を閉じる
|
---|
| 78 | ifs.close();
|
---|
| 79 |
|
---|
| 80 | return result;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | bool Write( const string &xmlFilePath ) const
|
---|
| 84 | {
|
---|
| 85 | // 出力アーカイブの作成
|
---|
| 86 | std::ofstream ofs( xmlFilePath.c_str() );
|
---|
| 87 |
|
---|
| 88 | bool result = Write(ofs);
|
---|
| 89 |
|
---|
| 90 | // 出力を閉じる
|
---|
| 91 | ofs.close();
|
---|
| 92 |
|
---|
| 93 | return result;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | bool ReadFromString( const wstring &xmlBuffer )
|
---|
| 97 | {
|
---|
| 98 | bool isSuccessful = false;
|
---|
| 99 |
|
---|
| 100 | // 入力アーカイブの作成
|
---|
| 101 | std::istringstream iss( xmlBuffer );
|
---|
| 102 |
|
---|
| 103 | try{
|
---|
| 104 | boost::archive::xml_iarchive ia(iss);
|
---|
| 105 |
|
---|
| 106 | // 文字列ストリームから読込
|
---|
| 107 | ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 108 |
|
---|
| 109 | isSuccessful = true;
|
---|
| 110 | }
|
---|
| 111 | catch(...){
|
---|
| 112 | // 失敗
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if( !isSuccessful )
|
---|
| 116 | {
|
---|
| 117 | return false;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return true;
|
---|
| 121 | }
|
---|
| 122 | bool ToWString( wstring &xmlBuffer ) const
|
---|
| 123 | {
|
---|
| 124 | bool isSuccessful = false;
|
---|
| 125 |
|
---|
| 126 | // 入力アーカイブの作成
|
---|
| 127 | std::ostringstream oss;
|
---|
| 128 |
|
---|
| 129 | try{
|
---|
| 130 | boost::archive::xml_oarchive oa(oss);
|
---|
| 131 |
|
---|
| 132 | // 文字列ストリームへ書き出し
|
---|
| 133 | oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 134 |
|
---|
| 135 | xmlBuffer = oss.str();
|
---|
| 136 |
|
---|
| 137 | isSuccessful = true;
|
---|
| 138 | }
|
---|
| 139 | catch(...){
|
---|
| 140 | // 失敗
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | if( !isSuccessful )
|
---|
| 144 | {
|
---|
| 145 | return false;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return true;
|
---|
| 149 | }
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | }}
|
---|