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

Last change on this file since 600 was 600, checked in by dai_9181, 16 years ago

依存関係を整理中

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