source: dev/trunk/jenga/include/common/BoostSerializationSupport.h@ 213

Last change on this file since 213 was 213, checked in by dai_9181, 17 years ago

BoostXmlSupportクラスの名前をBoostSerializationSupportに変更

File size: 5.1 KB
Line 
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#include <boost/archive/binary_oarchive.hpp>
11#include <boost/archive/binary_iarchive.hpp>
12#include <boost/serialization/serialization.hpp>
13#include <boost/serialization/string.hpp>
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/export.hpp>
16#include <boost/serialization/level.hpp>
17#include <boost/serialization/vector.hpp>
18#include <boost/serialization/map.hpp>
19#include <boost/serialization/nvp.hpp>
20#include <boost/serialization/version.hpp>
21#include <boost/serialization/is_abstract.hpp>
22
23#include <windows.h>
24
25namespace Jenga{
26namespace Common{
27
28using namespace std;
29
30template<class T_xml_schema> class BoostSerializationSupport{
31 virtual const char *RootTagName() const = 0;
32
33 void echo( const char *msg ) const
34 {
35 MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK );
36 }
37
38public:
39 bool ReadXml( istream& ifs, bool isShowExceptionMessage = true )
40 {
41 bool isSuccessful = false;
42
43 try{
44 boost::archive::xml_iarchive ia(ifs);
45
46 // ファイルから読込
47 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
48
49 isSuccessful = true;
50 }
51 catch( boost::archive::archive_exception e )
52 {
53 if( isShowExceptionMessage )
54 {
55 echo( e.what() );
56 }
57 }
58 catch(...){
59 if( isShowExceptionMessage )
60 {
61 echo( "archive_exception以外の不明な例外" );
62 }
63 }
64
65 if( !isSuccessful )
66 {
67 return false;
68 }
69
70 return true;
71 }
72
73 bool WriteXml( ostream& ofs, bool isShowExceptionMessage = true ) const
74 {
75 bool isSuccessful = false;
76
77 try{
78 boost::archive::xml_oarchive oa(ofs);
79
80 // ファイルに書き出し
81 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
82
83 isSuccessful = true;
84 }
85 catch( boost::archive::archive_exception e )
86 {
87 if( isShowExceptionMessage )
88 {
89 echo( e.what() );
90 }
91 }
92 catch(...){
93 if( isShowExceptionMessage )
94 {
95 echo( "archive_exception以外の不明な例外" );
96 }
97 }
98
99 if( !isSuccessful )
100 {
101 return false;
102 }
103
104 return true;
105 }
106
107 bool ReadXml( const string &xmlFilePath, bool isShowExceptionMessage = true )
108 {
109 // 入力アーカイブの作成
110 std::ifstream ifs( xmlFilePath.c_str() );
111
112 bool result = ReadXml(ifs,isShowExceptionMessage);
113
114 // 入力を閉じる
115 ifs.close();
116
117 return result;
118 }
119 bool WriteXml( const string &xmlFilePath, bool isShowExceptionMessage = true ) const
120 {
121 // 出力アーカイブの作成
122 std::ofstream ofs( xmlFilePath.c_str() );
123
124 bool result = WriteXml(ofs,isShowExceptionMessage);
125
126 // 出力を閉じる
127 ofs.close();
128
129 return result;
130 }
131
132 bool ReadBinaly( const string &xmlFilePath, bool isShowExceptionMessage = true )
133 {
134 // 入力アーカイブの作成
135 std::ifstream ifs( xmlFilePath.c_str() );
136
137 bool isSuccessful = false;
138 try{
139 boost::archive::binary_iarchive ia(ifs);
140
141 // ファイルから読込
142 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
143
144 isSuccessful = true;
145 }
146 catch( boost::archive::archive_exception e )
147 {
148 if( isShowExceptionMessage )
149 {
150 echo( e.what() );
151 }
152 }
153 catch(...){
154 if( isShowExceptionMessage )
155 {
156 echo( "archive_exception以外の不明な例外" );
157 }
158 }
159
160 // 入力を閉じる
161 ifs.close();
162
163 if( !isSuccessful )
164 {
165 return false;
166 }
167
168 return true;
169 }
170 bool WriteBinaly( const string &xmlFilePath, bool isShowExceptionMessage = true ) const
171 {
172 // 出力アーカイブの作成
173 std::ofstream ofs( xmlFilePath.c_str() );
174
175 bool isSuccessful = false;
176 try{
177 boost::archive::binary_oarchive oa(ofs);
178
179 // ファイルに書き出し
180 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
181
182 isSuccessful = true;
183 }
184 catch( boost::archive::archive_exception e )
185 {
186 if( isShowExceptionMessage )
187 {
188 echo( e.what() );
189 }
190 }
191 catch(...){
192 if( isShowExceptionMessage )
193 {
194 echo( "archive_exception以外の不明な例外" );
195 }
196 }
197
198 // 出力を閉じる
199 ofs.close();
200
201 if( !isSuccessful )
202 {
203 return false;
204 }
205
206 return true;
207 }
208
209 bool ReadXmlFromString( const string &xmlBuffer )
210 {
211 bool isSuccessful = false;
212
213 // 入力アーカイブの作成
214 std::istringstream iss( xmlBuffer );
215
216 try{
217 boost::archive::xml_iarchive ia(iss);
218
219 // 文字列ストリームから読込
220 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
221
222 isSuccessful = true;
223 }
224 catch(...){
225 // 失敗
226 }
227
228 if( !isSuccessful )
229 {
230 return false;
231 }
232
233 return true;
234 }
235 bool ToXmlWString( wstring &xmlBuffer ) const
236 {
237 bool isSuccessful = false;
238
239 // 入力アーカイブの作成
240 std::ostringstream oss;
241
242 try{
243 boost::archive::xml_oarchive oa(oss);
244
245 // 文字列ストリームへ書き出し
246 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
247
248 xmlBuffer = oss.str();
249
250 isSuccessful = true;
251 }
252 catch(...){
253 // 失敗
254 }
255
256 if( !isSuccessful )
257 {
258 return false;
259 }
260
261 return true;
262 }
263};
264
265
266}}
Note: See TracBrowser for help on using the repository browser.