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

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

インターフェイス実装周りの仕様整備

File size: 10.3 KB
Line 
1#include <string>
2#include <vector>
3#include <fstream>
4
5#include <windows.h>
6#include <stdio.h>
7#include <string.h>
8#include <math.h>
9#include <commctrl.h>
10#include <time.h>
11#include <limits.h>
12#include <shlobj.h>
13
14//boost libraries
15#include <boost/foreach.hpp>
16
17#include <jenga/include/common/String.h>
18
19#include "../common.h"
20
21#include <boost/archive/xml_oarchive.hpp>
22#include <boost/archive/xml_iarchive.hpp>
23#include <boost/archive/text_oarchive.hpp>
24#include <boost/archive/text_iarchive.hpp>
25#include <boost/archive/binary_oarchive.hpp>
26#include <boost/archive/binary_iarchive.hpp>
27#include <boost/serialization/string.hpp>
28#include <boost/serialization/access.hpp>
29#include <boost/serialization/level.hpp>
30#include <boost/serialization/vector.hpp>
31#include <boost/serialization/map.hpp>
32#include <boost/serialization/version.hpp>
33#include <boost/serialization/is_abstract.hpp>
34
35#include <BoostSerializationSupport.h>
36#include <Hashmap.h>
37#include <Compiler.h>
38
39using namespace Jenga::Common;
40
41template<class T_xml_schema> void BoostSerializationSupport<T_xml_schema>::echo( const char *msg ) const
42{
43 MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK );
44}
45template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( istream& ifs, bool isShowExceptionMessage )
46{
47 bool isSuccessful = false;
48
49 try{
50 boost::archive::xml_iarchive ia(ifs);
51
52 // ファイルから読込
53 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
54
55 isSuccessful = true;
56 }
57 catch( boost::archive::archive_exception e )
58 {
59 if( isShowExceptionMessage )
60 {
61 echo( e.what() );
62 }
63 }
64 catch(...){
65 if( isShowExceptionMessage )
66 {
67 echo( "archive_exception以外の不明な例外" );
68 }
69 }
70
71 if( !isSuccessful )
72 {
73 return false;
74 }
75
76 return true;
77}
78template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( ostream& ofs, bool isShowExceptionMessage ) const
79{
80 bool isSuccessful = false;
81
82 try{
83 boost::archive::xml_oarchive oa(ofs);
84
85 // ファイルに書き出し
86 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
87
88 isSuccessful = true;
89 }
90 catch( boost::archive::archive_exception e )
91 {
92 if( isShowExceptionMessage )
93 {
94 echo( e.what() );
95 }
96 }
97 catch(...){
98 if( isShowExceptionMessage )
99 {
100 echo( "archive_exception以外の不明な例外" );
101 }
102 }
103
104 if( !isSuccessful )
105 {
106 return false;
107 }
108
109 return true;
110}
111template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( const string &xmlFilePath, bool isShowExceptionMessage )
112{
113 // 入力アーカイブの作成
114 std::ifstream ifs( xmlFilePath.c_str() );
115
116 bool result = ReadXml(ifs,isShowExceptionMessage);
117
118 // 入力を閉じる
119 ifs.close();
120
121 return result;
122}
123template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( const string &xmlFilePath, bool isShowExceptionMessage ) const
124{
125 // 出力アーカイブの作成
126 std::ofstream ofs( xmlFilePath.c_str() );
127
128 bool result = WriteXml(ofs,isShowExceptionMessage);
129
130 // 出力を閉じる
131 ofs.close();
132
133 return result;
134}
135template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlString( const std::string &xmlString )
136{
137 bool isSuccessful = false;
138
139 // 入力アーカイブの作成
140 std::istringstream iss( xmlString );
141
142 try{
143 boost::archive::xml_iarchive ia(iss);
144
145 // 文字列ストリームから読込
146 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
147
148 isSuccessful = true;
149 }
150 catch(...){
151 // 失敗
152 }
153
154 if( !isSuccessful )
155 {
156 return false;
157 }
158
159 return true;
160}
161template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXmlString( std::string &xmlString ) const
162{
163 // 出力アーカイブの作成
164 std::ostringstream oss;
165
166 bool isSuccessful = false;
167 try{
168 boost::archive::xml_oarchive oa(oss);
169
170 // 文字列ストリームに書き出し
171 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
172
173 isSuccessful = true;
174 }
175 catch( boost::archive::archive_exception e )
176 {
177 echo( e.what() );
178 }
179 catch(...){
180 echo( "archive_exception以外の不明な例外" );
181 }
182
183 if( !isSuccessful )
184 {
185 return false;
186 }
187
188 xmlString = oss.str();
189
190 return true;
191}
192
193template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryFile( const string &filePath, bool isShowExceptionMessage )
194{
195 // 入力アーカイブの作成
196 std::ifstream ifs( filePath.c_str(), ios::in | ios::binary );
197
198 bool isSuccessful = false;
199 try{
200 boost::archive::binary_iarchive ia(ifs);
201
202 // ファイルから読込
203 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
204
205 isSuccessful = true;
206 }
207 catch( boost::archive::archive_exception e )
208 {
209 if( isShowExceptionMessage )
210 {
211 echo( e.what() );
212 }
213 }
214 catch(...){
215 if( isShowExceptionMessage )
216 {
217 echo( "archive_exception以外の不明な例外" );
218 }
219 }
220
221 // 入力を閉じる
222 ifs.close();
223
224 if( !isSuccessful )
225 {
226 return false;
227 }
228
229 return true;
230}
231template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinaryFile( const string &filePath, bool isShowExceptionMessage ) const
232{
233 // 出力アーカイブの作成
234 std::ofstream ofs( filePath.c_str(), ios::out | ios::binary );
235
236 bool isSuccessful = false;
237 try{
238 boost::archive::binary_oarchive oa(ofs);
239
240 // ファイルに書き出し
241 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
242
243 isSuccessful = true;
244 }
245 catch( boost::archive::archive_exception e )
246 {
247 if( isShowExceptionMessage )
248 {
249 echo( e.what() );
250 }
251 }
252 catch(...){
253 if( isShowExceptionMessage )
254 {
255 echo( "archive_exception以外の不明な例外" );
256 }
257 }
258
259 // 出力を閉じる
260 ofs.close();
261
262 if( !isSuccessful )
263 {
264 return false;
265 }
266
267 return true;
268}
269
270template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryString( const std::string &binaryString )
271{
272 bool isSuccessful = false;
273
274 // 入力アーカイブの作成
275 std::istringstream iss( binaryString, ios::in | ios::binary );
276
277 try{
278 boost::archive::binary_iarchive ia(iss);
279
280 // 文字列ストリームから読込
281 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
282
283 isSuccessful = true;
284 }
285 catch( boost::archive::archive_exception e )
286 {
287 echo( e.what() );
288 }
289 catch(...){
290 echo( "archive_exception以外の不明な例外" );
291 }
292
293 return isSuccessful;
294}
295template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinaryString( std::string &binaryString ) const
296{
297 // 出力アーカイブの作成
298 std::ostringstream oss( "", ios::out | ios::binary );
299
300 bool isSuccessful = false;
301 try{
302 boost::archive::binary_oarchive oa(oss);
303
304 // 文字列ストリームに書き出し
305 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
306
307 isSuccessful = true;
308 }
309 catch( boost::archive::archive_exception e )
310 {
311 echo( e.what() );
312 }
313 catch(...){
314 echo( "archive_exception以外の不明な例外" );
315 }
316
317 binaryString = oss.str();
318
319 return isSuccessful;
320}
321
322template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadText( const string &filePath, bool isShowExceptionMessage )
323{
324 // 入力アーカイブの作成
325 std::ifstream ifs( filePath.c_str() );
326
327 bool isSuccessful = false;
328 try{
329 boost::archive::text_iarchive ia(ifs);
330
331 // ファイルから読込
332 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
333
334 isSuccessful = true;
335 }
336 catch( boost::archive::archive_exception e )
337 {
338 if( isShowExceptionMessage )
339 {
340 echo( e.what() );
341 }
342 }
343 catch(...){
344 if( isShowExceptionMessage )
345 {
346 echo( "archive_exception以外の不明な例外" );
347 }
348 }
349
350 // 入力を閉じる
351 ifs.close();
352
353 if( !isSuccessful )
354 {
355 return false;
356 }
357
358 return true;
359}
360template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteText( const string &filePath, bool isShowExceptionMessage ) const
361{
362 // 出力アーカイブの作成
363 std::ofstream ofs( filePath.c_str() );
364
365 bool isSuccessful = false;
366 try{
367 boost::archive::text_oarchive oa(ofs);
368
369 // ファイルに書き出し
370 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
371
372 isSuccessful = true;
373 }
374 catch( boost::archive::archive_exception e )
375 {
376 if( isShowExceptionMessage )
377 {
378 echo( e.what() );
379 }
380 }
381 catch(...){
382 if( isShowExceptionMessage )
383 {
384 echo( "archive_exception以外の不明な例外" );
385 }
386 }
387
388 // 出力を閉じる
389 ofs.close();
390
391 if( !isSuccessful )
392 {
393 return false;
394 }
395
396 return true;
397}
398
399template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadTextString( const std::string &textString )
400{
401 bool isSuccessful = false;
402
403 // 入力アーカイブの作成
404 std::istringstream iss( textString );
405
406 try{
407 boost::archive::text_iarchive ia(iss);
408
409 // 文字列ストリームから読込
410 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
411
412 isSuccessful = true;
413 }
414 catch(...){
415 // 失敗
416 }
417
418 if( !isSuccessful )
419 {
420 return false;
421 }
422
423 return true;
424}
425template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteTextString( std::string &textString ) const
426{
427 // 出力アーカイブの作成
428 std::ostringstream oss;
429
430 bool isSuccessful = false;
431 try{
432 boost::archive::text_oarchive oa(oss);
433
434 // 文字列ストリームに書き出し
435 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
436
437 isSuccessful = true;
438 }
439 catch( boost::archive::archive_exception e )
440 {
441 echo( e.what() );
442 }
443 catch(...){
444 echo( "archive_exception以外の不明な例外" );
445 }
446
447 if( !isSuccessful )
448 {
449 return false;
450 }
451
452 textString = oss.str();
453
454 return true;
455}
456
457template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlFromString( const std::string &textString )
458{
459 bool isSuccessful = false;
460
461 // 入力アーカイブの作成
462 std::istringstream iss( textString );
463
464 try{
465 boost::archive::xml_iarchive ia(iss);
466
467 // 文字列ストリームから読込
468 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
469
470 isSuccessful = true;
471 }
472 catch(...){
473 // 失敗
474 }
475
476 if( !isSuccessful )
477 {
478 return false;
479 }
480
481 return true;
482}
483
484
485#include <Compiler.h>
486#include <logger.h>
487
488template class Jenga::Common::BoostSerializationSupport<ObjectModule>;
489template class Jenga::Common::BoostSerializationSupport<LoggerSetting>;
Note: See TracBrowser for help on using the repository browser.