[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>
|
---|
[190] | 10 | #include <boost/serialization/serialization.hpp>
|
---|
| 11 | #include <boost/serialization/string.hpp>
|
---|
| 12 | #include <boost/serialization/access.hpp>
|
---|
| 13 | #include <boost/serialization/export.hpp>
|
---|
| 14 | #include <boost/serialization/level.hpp>
|
---|
| 15 | #include <boost/serialization/vector.hpp>
|
---|
| 16 | #include <boost/serialization/map.hpp>
|
---|
| 17 | #include <boost/serialization/nvp.hpp>
|
---|
| 18 | #include <boost/serialization/version.hpp>
|
---|
| 19 | #include <boost/serialization/is_abstract.hpp>
|
---|
[189] | 20 |
|
---|
[205] | 21 | #include <windows.h>
|
---|
| 22 |
|
---|
[189] | 23 | namespace Jenga{
|
---|
| 24 | namespace Common{
|
---|
| 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
| 28 | template<class T_xml_schema> class BoostXmlSupport{
|
---|
| 29 | virtual const char *RootTagName() const = 0;
|
---|
| 30 |
|
---|
[205] | 31 | void echo( const char *msg ) const
|
---|
| 32 | {
|
---|
| 33 | MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK );
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[189] | 36 | public:
|
---|
[205] | 37 | bool Read( istream& ifs, bool isShowExceptionMessage = true )
|
---|
[189] | 38 | {
|
---|
| 39 | bool isSuccessful = false;
|
---|
| 40 |
|
---|
| 41 | try{
|
---|
| 42 | boost::archive::xml_iarchive ia(ifs);
|
---|
| 43 |
|
---|
| 44 | // ファイルから読込
|
---|
| 45 | ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 46 |
|
---|
| 47 | isSuccessful = true;
|
---|
| 48 | }
|
---|
[205] | 49 | catch( boost::archive::archive_exception e )
|
---|
| 50 | {
|
---|
| 51 | if( isShowExceptionMessage )
|
---|
| 52 | {
|
---|
| 53 | echo( e.what() );
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[189] | 56 | catch(...){
|
---|
[205] | 57 | if( isShowExceptionMessage )
|
---|
| 58 | {
|
---|
| 59 | echo( "archive_exception以外の不明な例外" );
|
---|
| 60 | }
|
---|
[189] | 61 | }
|
---|
| 62 |
|
---|
| 63 | if( !isSuccessful )
|
---|
| 64 | {
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[205] | 71 | bool Write( ostream& ofs, bool isShowExceptionMessage = true ) const
|
---|
[189] | 72 | {
|
---|
| 73 | bool isSuccessful = false;
|
---|
| 74 |
|
---|
| 75 | try{
|
---|
| 76 | boost::archive::xml_oarchive oa(ofs);
|
---|
| 77 |
|
---|
| 78 | // ファイルに書き出し
|
---|
| 79 | oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 80 |
|
---|
| 81 | isSuccessful = true;
|
---|
| 82 | }
|
---|
[205] | 83 | catch( boost::archive::archive_exception e )
|
---|
| 84 | {
|
---|
| 85 | if( isShowExceptionMessage )
|
---|
| 86 | {
|
---|
| 87 | echo( e.what() );
|
---|
| 88 | }
|
---|
[189] | 89 | }
|
---|
[205] | 90 | catch(...){
|
---|
| 91 | if( isShowExceptionMessage )
|
---|
| 92 | {
|
---|
| 93 | echo( "archive_exception以外の不明な例外" );
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
[189] | 96 |
|
---|
| 97 | if( !isSuccessful )
|
---|
| 98 | {
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[205] | 105 | bool Read( const string &xmlFilePath, bool isShowExceptionMessage = true )
|
---|
[189] | 106 | {
|
---|
| 107 | bool isSuccessful = false;
|
---|
| 108 |
|
---|
| 109 | // 入力アーカイブの作成
|
---|
| 110 | std::ifstream ifs( xmlFilePath.c_str() );
|
---|
| 111 |
|
---|
[205] | 112 | bool result = Read(ifs,isShowExceptionMessage);
|
---|
[189] | 113 |
|
---|
| 114 | // 入力を閉じる
|
---|
| 115 | ifs.close();
|
---|
| 116 |
|
---|
| 117 | return result;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[205] | 120 | bool Write( const string &xmlFilePath, bool isShowExceptionMessage = true ) const
|
---|
[189] | 121 | {
|
---|
| 122 | // 出力アーカイブの作成
|
---|
| 123 | std::ofstream ofs( xmlFilePath.c_str() );
|
---|
| 124 |
|
---|
[205] | 125 | bool result = Write(ofs,isShowExceptionMessage);
|
---|
[189] | 126 |
|
---|
| 127 | // 出力を閉じる
|
---|
| 128 | ofs.close();
|
---|
| 129 |
|
---|
| 130 | return result;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[205] | 133 | bool ReadFromString( const string &xmlBuffer )
|
---|
[189] | 134 | {
|
---|
| 135 | bool isSuccessful = false;
|
---|
| 136 |
|
---|
| 137 | // 入力アーカイブの作成
|
---|
| 138 | std::istringstream iss( xmlBuffer );
|
---|
| 139 |
|
---|
| 140 | try{
|
---|
| 141 | boost::archive::xml_iarchive ia(iss);
|
---|
| 142 |
|
---|
| 143 | // 文字列ストリームから読込
|
---|
| 144 | ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 145 |
|
---|
| 146 | isSuccessful = true;
|
---|
| 147 | }
|
---|
| 148 | catch(...){
|
---|
| 149 | // 失敗
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if( !isSuccessful )
|
---|
| 153 | {
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 | bool ToWString( wstring &xmlBuffer ) const
|
---|
| 160 | {
|
---|
| 161 | bool isSuccessful = false;
|
---|
| 162 |
|
---|
| 163 | // 入力アーカイブの作成
|
---|
| 164 | std::ostringstream oss;
|
---|
| 165 |
|
---|
| 166 | try{
|
---|
| 167 | boost::archive::xml_oarchive oa(oss);
|
---|
| 168 |
|
---|
| 169 | // 文字列ストリームへ書き出し
|
---|
| 170 | oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
|
---|
| 171 |
|
---|
| 172 | xmlBuffer = oss.str();
|
---|
| 173 |
|
---|
| 174 | isSuccessful = true;
|
---|
| 175 | }
|
---|
| 176 | catch(...){
|
---|
| 177 | // 失敗
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if( !isSuccessful )
|
---|
| 181 | {
|
---|
| 182 | return false;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return true;
|
---|
| 186 | }
|
---|
| 187 | };
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | }}
|
---|