source: dev/trunk/abdev/BasicCompiler_Common/src/BoostSerializationSupport.cpp@ 322

Last change on this file since 322 was 322, checked in by dai_9181, 17 years ago

コンパイラ組み込みテンプレートエンジンを実装。
静的リンクライブラリ、デバッグ情報の内部形式をテキストからバイナリに変更した。

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