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

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

egtraブランチの内容をマージ。

File size: 11.1 KB
RevLine 
[522]1#include <map>
[288]2#include <string>
3#include <vector>
4#include <fstream>
[522]5#include <iostream>
6#include <iomanip>
7#include <ios>
8#include <streambuf>
9#include <sstream>
[215]10
[288]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>
[522]19#include <shlwapi.h>
20#include <tchar.h>
[288]21
22//boost libraries
23#include <boost/foreach.hpp>
[828]24#include <boost/cast.hpp>
[288]25
[828]26#pragma warning(push)
27#pragma warning(disable: 4244 6326)
[215]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>
[828]40#pragma warning(pop)
[215]41
[520]42#include <jenga/include/jenga.h>
[215]43
[751]44#include <abdev/ab_common/include/libs.h>
45
[215]46using namespace Jenga::Common;
47
[828]48using boost::polymorphic_downcast;
49
[215]50template<class T_xml_schema> void BoostSerializationSupport<T_xml_schema>::echo( const char *msg ) const
51{
[828]52 MessageBox( NULL, msg, "シリアライズの例外", MB_OK );
[215]53}
[523]54template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( std::istream& ifs, bool isShowExceptionMessage )
[215]55{
56 bool isSuccessful = false;
57
58 try{
59 boost::archive::xml_iarchive ia(ifs);
60
61 // ファイルから読込
[828]62 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[215]63
64 isSuccessful = true;
65 }
[828]66 catch( boost::archive::archive_exception const& e )
[215]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}
[523]87template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( std::ostream& ofs, bool isShowExceptionMessage ) const
[215]88{
89 bool isSuccessful = false;
90
91 try{
92 boost::archive::xml_oarchive oa(ofs);
93
94 // ファイルに書き出し
[828]95 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[215]96
97 isSuccessful = true;
98 }
[828]99 catch( boost::archive::archive_exception const& e )
[215]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}
[523]120template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXml( const std::string &xmlFilePath, bool isShowExceptionMessage )
[215]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}
[523]132template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteXml( const std::string &xmlFilePath, bool isShowExceptionMessage ) const
[215]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}
[279]144template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlString( const std::string &xmlString )
145{
146 bool isSuccessful = false;
[215]147
[279]148 // 入力アーカイブの作成
149 std::istringstream iss( xmlString );
150
151 try{
152 boost::archive::xml_iarchive ia(iss);
153
154 // 文字列ストリームから読込
[828]155 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[279]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 // 文字列ストリームに書き出し
[828]180 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[279]181
182 isSuccessful = true;
183 }
[828]184 catch( boost::archive::archive_exception const& e )
[279]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
[523]202template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryFile( const std::string &filePath, bool isShowExceptionMessage )
[215]203{
204 // 入力アーカイブの作成
[523]205 std::ifstream ifs( filePath.c_str(), std::ios::in | std::ios::binary );
[215]206
207 bool isSuccessful = false;
208 try{
209 boost::archive::binary_iarchive ia(ifs);
210
211 // ファイルから読込
[828]212 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[215]213
214 isSuccessful = true;
215 }
[828]216 catch( boost::archive::archive_exception const& e )
[215]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}
[523]240template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteBinaryFile( const std::string &filePath, bool isShowExceptionMessage ) const
[215]241{
242 // 出力アーカイブの作成
[523]243 std::ofstream ofs( filePath.c_str(), std::ios::out | std::ios::binary );
[215]244
245 bool isSuccessful = false;
246 try{
247 boost::archive::binary_oarchive oa(ofs);
248
249 // ファイルに書き出し
[828]250 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[215]251
252 isSuccessful = true;
253 }
[828]254 catch( boost::archive::archive_exception const& e )
[215]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
[322]279template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadBinaryString( const std::string &binaryString )
280{
281 bool isSuccessful = false;
282
283 // 入力アーカイブの作成
[523]284 std::istringstream iss( binaryString, std::ios::in | std::ios::binary );
[322]285
286 try{
287 boost::archive::binary_iarchive ia(iss);
288
289 // 文字列ストリームから読込
[828]290 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[322]291
292 isSuccessful = true;
293 }
[828]294 catch( boost::archive::archive_exception const& e )
[322]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 // 出力アーカイブの作成
[523]307 std::ostringstream oss( "", std::ios::out | std::ios::binary );
[322]308
309 bool isSuccessful = false;
310 try{
311 boost::archive::binary_oarchive oa(oss);
312
313 // 文字列ストリームに書き出し
[828]314 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[322]315
316 isSuccessful = true;
317 }
[828]318 catch( boost::archive::archive_exception const& e )
[322]319 {
320 echo( e.what() );
321 }
322 catch(...){
323 echo( "archive_exception以外の不明な例外" );
324 }
325
326 binaryString = oss.str();
327
328 return isSuccessful;
329}
330
[404]331/*
332ビルドに時間がかかるので外しておく
[523]333template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadText( const std::string &filePath, bool isShowExceptionMessage )
[215]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 // ファイルから読込
[828]343 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[215]344
345 isSuccessful = true;
346 }
[828]347 catch( boost::archive::archive_exception const& e )
[215]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}
[523]371template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteText( const std::string &filePath, bool isShowExceptionMessage ) const
[215]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 // ファイルに書き出し
[828]381 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[215]382
383 isSuccessful = true;
384 }
[828]385 catch( boost::archive::archive_exception const& e )
[215]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
[264]410template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadTextString( const std::string &textString )
[215]411{
412 bool isSuccessful = false;
413
414 // 入力アーカイブの作成
[264]415 std::istringstream iss( textString );
[215]416
417 try{
[264]418 boost::archive::text_iarchive ia(iss);
419
420 // 文字列ストリームから読込
[828]421 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[264]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 // 文字列ストリームに書き出し
[828]446 oa << boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema const*>(this) );
[264]447
448 isSuccessful = true;
449 }
[828]450 catch( boost::archive::archive_exception const& e )
[264]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{
[215]476 boost::archive::xml_iarchive ia(iss);
477
478 // 文字列ストリームから読込
[828]479 ia >> boost::serialization::make_nvp( RootTagName(), *polymorphic_downcast<T_xml_schema*>(this) );
[215]480
481 isSuccessful = true;
482 }
483 catch(...){
484 // 失敗
485 }
486
487 if( !isSuccessful )
488 {
489 return false;
490 }
491
492 return true;
493}
[404]494*/
[215]495
496
[256]497#include <logger.h>
[477]498#include <Configuration.h>
[636]499#include <abdev/ab_common/include/Lexical/Source.h>
[256]500
[215]501template class Jenga::Common::BoostSerializationSupport<LoggerSetting>;
[477]502template class Jenga::Common::BoostSerializationSupport<Configuration>;
[636]503template class Jenga::Common::BoostSerializationSupport<BasicSources>;
Note: See TracBrowser for help on using the repository browser.