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

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