source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/BoostSerializationSupport.cpp@ 479

Last change on this file since 479 was 477, 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
37using namespace Jenga::Common;
38
39template<class T_xml_schema> void BoostSerializationSupport<T_xml_schema>::echo( const char *msg ) const
40{
41 MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK );
42}
43template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( istream& ifs, bool isShowExceptionMessage )
44{
45 bool isSuccessful = false;
46
47 try{
48 boost::archive::xml_iarchive ia(ifs);
49
50 // ファイルから読込
51 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
52
53 isSuccessful = true;
54 }
55 catch( boost::archive::archive_exception e )
56 {
57 if( isShowExceptionMessage )
58 {
59 echo( e.what() );
60 }
61 }
62 catch(...){
63 if( isShowExceptionMessage )
64 {
65 echo( "archive_exception以外の不明な例外" );
66 }
67 }
68
69 if( !isSuccessful )
70 {
71 return false;
72 }
73
74 return true;
75}
76template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( ostream& ofs, bool isShowExceptionMessage ) const
77{
78 bool isSuccessful = false;
79
80 try{
81 boost::archive::xml_oarchive oa(ofs);
82
83 // ファイルに書き出し
84 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
85
86 isSuccessful = true;
87 }
88 catch( boost::archive::archive_exception e )
89 {
90 if( isShowExceptionMessage )
91 {
92 echo( e.what() );
93 }
94 }
95 catch(...){
96 if( isShowExceptionMessage )
97 {
98 echo( "archive_exception以外の不明な例外" );
99 }
100 }
101
102 if( !isSuccessful )
103 {
104 return false;
105 }
106
107 return true;
108}
109template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( const string &xmlFilePath, bool isShowExceptionMessage )
110{
111 // 入力アーカイブの作成
112 std::ifstream ifs( xmlFilePath.c_str() );
113
114 bool result = ReadXml(ifs,isShowExceptionMessage);
115
116 // 入力を閉じる
117 ifs.close();
118
119 return result;
120}
121template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( const string &xmlFilePath, bool isShowExceptionMessage ) const
122{
123 // 出力アーカイブの作成
124 std::ofstream ofs( xmlFilePath.c_str() );
125
126 bool result = WriteXml(ofs,isShowExceptionMessage);
127
128 // 出力を閉じる
129 ofs.close();
130
131 return result;
132}
133template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlString( const std::string &xmlString )
134{
135 bool isSuccessful = false;
136
137 // 入力アーカイブの作成
138 std::istringstream iss( xmlString );
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}
159template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXmlString( std::string &xmlString ) const
160{
161 // 出力アーカイブの作成
162 std::ostringstream oss;
163
164 bool isSuccessful = false;
165 try{
166 boost::archive::xml_oarchive oa(oss);
167
168 // 文字列ストリームに書き出し
169 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
170
171 isSuccessful = true;
172 }
173 catch( boost::archive::archive_exception e )
174 {
175 echo( e.what() );
176 }
177 catch(...){
178 echo( "archive_exception以外の不明な例外" );
179 }
180
181 if( !isSuccessful )
182 {
183 return false;
184 }
185
186 xmlString = oss.str();
187
188 return true;
189}
190
191template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryFile( const string &filePath, bool isShowExceptionMessage )
192{
193 // 入力アーカイブの作成
194 std::ifstream ifs( filePath.c_str(), ios::in | ios::binary );
195
196 bool isSuccessful = false;
197 try{
198 boost::archive::binary_iarchive ia(ifs);
199
200 // ファイルから読込
201 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
202
203 isSuccessful = true;
204 }
205 catch( boost::archive::archive_exception e )
206 {
207 if( isShowExceptionMessage )
208 {
209 echo( e.what() );
210 }
211 }
212 catch(...){
213 if( isShowExceptionMessage )
214 {
215 echo( "archive_exception以外の不明な例外" );
216 }
217 }
218
219 // 入力を閉じる
220 ifs.close();
221
222 if( !isSuccessful )
223 {
224 return false;
225 }
226
227 return true;
228}
229template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinaryFile( const string &filePath, bool isShowExceptionMessage ) const
230{
231 // 出力アーカイブの作成
232 std::ofstream ofs( filePath.c_str(), ios::out | ios::binary );
233
234 bool isSuccessful = false;
235 try{
236 boost::archive::binary_oarchive oa(ofs);
237
238 // ファイルに書き出し
239 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
240
241 isSuccessful = true;
242 }
243 catch( boost::archive::archive_exception e )
244 {
245 if( isShowExceptionMessage )
246 {
247 echo( e.what() );
248 }
249 }
250 catch(...){
251 if( isShowExceptionMessage )
252 {
253 echo( "archive_exception以外の不明な例外" );
254 }
255 }
256
257 // 出力を閉じる
258 ofs.close();
259
260 if( !isSuccessful )
261 {
262 return false;
263 }
264
265 return true;
266}
267
268template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryString( const std::string &binaryString )
269{
270 bool isSuccessful = false;
271
272 // 入力アーカイブの作成
273 std::istringstream iss( binaryString, ios::in | ios::binary );
274
275 try{
276 boost::archive::binary_iarchive ia(iss);
277
278 // 文字列ストリームから読込
279 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
280
281 isSuccessful = true;
282 }
283 catch( boost::archive::archive_exception e )
284 {
285 echo( e.what() );
286 }
287 catch(...){
288 echo( "archive_exception以外の不明な例外" );
289 }
290
291 return isSuccessful;
292}
293template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinaryString( std::string &binaryString ) const
294{
295 // 出力アーカイブの作成
296 std::ostringstream oss( "", ios::out | ios::binary );
297
298 bool isSuccessful = false;
299 try{
300 boost::archive::binary_oarchive oa(oss);
301
302 // 文字列ストリームに書き出し
303 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
304
305 isSuccessful = true;
306 }
307 catch( boost::archive::archive_exception e )
308 {
309 echo( e.what() );
310 }
311 catch(...){
312 echo( "archive_exception以外の不明な例外" );
313 }
314
315 binaryString = oss.str();
316
317 return isSuccessful;
318}
319
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
486#include <logger.h>
487#include <Configuration.h>
488
489template class Jenga::Common::BoostSerializationSupport<LoggerSetting>;
490template class Jenga::Common::BoostSerializationSupport<Configuration>;
Note: See TracBrowser for help on using the repository browser.