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

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

性能がよいというdlmallocを導入。

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