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

Last change on this file was 829, checked in by イグトランス (egtra), 12 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

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