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

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

libファイルを跨ったテンプレート展開に対応。

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