source: dev/trunk/abdev/BasicCompiler_Common/src/BoostSerializationSupport.cpp@ 264

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

デバッグデータとしてオブジェクトモジュールのシリアライズを可能にした(その先の処理はまだ動かない)

File size: 7.6 KB
Line 
1#include "stdafx.h"
2
3#include <boost/archive/xml_oarchive.hpp>
4#include <boost/archive/xml_iarchive.hpp>
5#include <boost/archive/text_oarchive.hpp>
6#include <boost/archive/text_iarchive.hpp>
7#include <boost/archive/binary_oarchive.hpp>
8#include <boost/archive/binary_iarchive.hpp>
9#include <boost/serialization/string.hpp>
10#include <boost/serialization/access.hpp>
11#include <boost/serialization/level.hpp>
12#include <boost/serialization/vector.hpp>
13#include <boost/serialization/map.hpp>
14#include <boost/serialization/version.hpp>
15#include <boost/serialization/is_abstract.hpp>
16
17#include <BoostSerializationSupport.h>
18
19#include <windows.h>
20
21using namespace Jenga::Common;
22
23template<class T_xml_schema> void BoostSerializationSupport<T_xml_schema>::echo( const char *msg ) const
24{
25 MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK );
26}
27template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( istream& ifs, bool isShowExceptionMessage )
28{
29 bool isSuccessful = false;
30
31 try{
32 boost::archive::xml_iarchive ia(ifs);
33
34 // ファイルから読込
35 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
36
37 isSuccessful = true;
38 }
39 catch( boost::archive::archive_exception e )
40 {
41 if( isShowExceptionMessage )
42 {
43 echo( e.what() );
44 }
45 }
46 catch(...){
47 if( isShowExceptionMessage )
48 {
49 echo( "archive_exception以外の不明な例外" );
50 }
51 }
52
53 if( !isSuccessful )
54 {
55 return false;
56 }
57
58 return true;
59}
60template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( ostream& ofs, bool isShowExceptionMessage ) const
61{
62 bool isSuccessful = false;
63
64 try{
65 boost::archive::xml_oarchive oa(ofs);
66
67 // ファイルに書き出し
68 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
69
70 isSuccessful = true;
71 }
72 catch( boost::archive::archive_exception e )
73 {
74 if( isShowExceptionMessage )
75 {
76 echo( e.what() );
77 }
78 }
79 catch(...){
80 if( isShowExceptionMessage )
81 {
82 echo( "archive_exception以外の不明な例外" );
83 }
84 }
85
86 if( !isSuccessful )
87 {
88 return false;
89 }
90
91 return true;
92}
93template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( const string &xmlFilePath, bool isShowExceptionMessage )
94{
95 // 入力アーカイブの作成
96 std::ifstream ifs( xmlFilePath.c_str() );
97
98 bool result = ReadXml(ifs,isShowExceptionMessage);
99
100 // 入力を閉じる
101 ifs.close();
102
103 return result;
104}
105template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( const string &xmlFilePath, bool isShowExceptionMessage ) const
106{
107 // 出力アーカイブの作成
108 std::ofstream ofs( xmlFilePath.c_str() );
109
110 bool result = WriteXml(ofs,isShowExceptionMessage);
111
112 // 出力を閉じる
113 ofs.close();
114
115 return result;
116}
117
118template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinary( const string &filePath, bool isShowExceptionMessage )
119{
120 // 入力アーカイブの作成
121 std::ifstream ifs( filePath.c_str() );
122
123 bool isSuccessful = false;
124 try{
125 boost::archive::binary_iarchive ia(ifs);
126
127 // ファイルから読込
128 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
129
130 isSuccessful = true;
131 }
132 catch( boost::archive::archive_exception e )
133 {
134 if( isShowExceptionMessage )
135 {
136 echo( e.what() );
137 }
138 }
139 catch(...){
140 if( isShowExceptionMessage )
141 {
142 echo( "archive_exception以外の不明な例外" );
143 }
144 }
145
146 // 入力を閉じる
147 ifs.close();
148
149 if( !isSuccessful )
150 {
151 return false;
152 }
153
154 return true;
155}
156template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinary( const string &filePath, bool isShowExceptionMessage ) const
157{
158 // 出力アーカイブの作成
159 std::ofstream ofs( filePath.c_str() );
160
161 bool isSuccessful = false;
162 try{
163 boost::archive::binary_oarchive oa(ofs);
164
165 // ファイルに書き出し
166 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
167
168 isSuccessful = true;
169 }
170 catch( boost::archive::archive_exception e )
171 {
172 if( isShowExceptionMessage )
173 {
174 echo( e.what() );
175 }
176 }
177 catch(...){
178 if( isShowExceptionMessage )
179 {
180 echo( "archive_exception以外の不明な例外" );
181 }
182 }
183
184 // 出力を閉じる
185 ofs.close();
186
187 if( !isSuccessful )
188 {
189 return false;
190 }
191
192 return true;
193}
194
195template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadText( const string &filePath, bool isShowExceptionMessage )
196{
197 // 入力アーカイブの作成
198 std::ifstream ifs( filePath.c_str() );
199
200 bool isSuccessful = false;
201 try{
202 boost::archive::text_iarchive ia(ifs);
203
204 // ファイルから読込
205 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
206
207 isSuccessful = true;
208 }
209 catch( boost::archive::archive_exception e )
210 {
211 if( isShowExceptionMessage )
212 {
213 echo( e.what() );
214 }
215 }
216 catch(...){
217 if( isShowExceptionMessage )
218 {
219 echo( "archive_exception以外の不明な例外" );
220 }
221 }
222
223 // 入力を閉じる
224 ifs.close();
225
226 if( !isSuccessful )
227 {
228 return false;
229 }
230
231 return true;
232}
233template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteText( const string &filePath, bool isShowExceptionMessage ) const
234{
235 // 出力アーカイブの作成
236 std::ofstream ofs( filePath.c_str() );
237
238 bool isSuccessful = false;
239 try{
240 boost::archive::text_oarchive oa(ofs);
241
242 // ファイルに書き出し
243 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
244
245 isSuccessful = true;
246 }
247 catch( boost::archive::archive_exception e )
248 {
249 if( isShowExceptionMessage )
250 {
251 echo( e.what() );
252 }
253 }
254 catch(...){
255 if( isShowExceptionMessage )
256 {
257 echo( "archive_exception以外の不明な例外" );
258 }
259 }
260
261 // 出力を閉じる
262 ofs.close();
263
264 if( !isSuccessful )
265 {
266 return false;
267 }
268
269 return true;
270}
271
272template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadTextString( const std::string &textString )
273{
274 bool isSuccessful = false;
275
276 // 入力アーカイブの作成
277 std::istringstream iss( textString );
278
279 try{
280 boost::archive::text_iarchive ia(iss);
281
282 // 文字列ストリームから読込
283 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
284
285 isSuccessful = true;
286 }
287 catch(...){
288 // 失敗
289 }
290
291 if( !isSuccessful )
292 {
293 return false;
294 }
295
296 return true;
297}
298template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteTextString( std::string &textString ) const
299{
300 // 出力アーカイブの作成
301 std::ostringstream oss;
302
303 bool isSuccessful = false;
304 try{
305 boost::archive::text_oarchive oa(oss);
306
307 // 文字列ストリームに書き出し
308 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
309
310 isSuccessful = true;
311 }
312 catch( boost::archive::archive_exception e )
313 {
314 echo( e.what() );
315 }
316 catch(...){
317 echo( "archive_exception以外の不明な例外" );
318 }
319
320 if( !isSuccessful )
321 {
322 return false;
323 }
324
325 textString = oss.str();
326
327 return true;
328}
329
330template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlFromString( const std::string &textString )
331{
332 bool isSuccessful = false;
333
334 // 入力アーカイブの作成
335 std::istringstream iss( textString );
336
337 try{
338 boost::archive::xml_iarchive ia(iss);
339
340 // 文字列ストリームから読込
341 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
342
343 isSuccessful = true;
344 }
345 catch(...){
346 // 失敗
347 }
348
349 if( !isSuccessful )
350 {
351 return false;
352 }
353
354 return true;
355}
356
357
358#include <Compiler.h>
359#include <logger.h>
360
361template class Jenga::Common::BoostSerializationSupport<ObjectModule>;
362template class Jenga::Common::BoostSerializationSupport<LoggerSetting>;
Note: See TracBrowser for help on using the repository browser.