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

Last change on this file since 320 was 299, 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 <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>::ReadBinary( const string &filePath, bool isShowExceptionMessage )
193{
194 // 入力アーカイブの作成
195 std::ifstream ifs( filePath.c_str() );
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>::WriteBinary( const string &filePath, bool isShowExceptionMessage ) const
231{
232 // 出力アーカイブの作成
233 std::ofstream ofs( filePath.c_str() );
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>::ReadText( const string &filePath, bool isShowExceptionMessage )
270{
271 // 入力アーカイブの作成
272 std::ifstream ifs( filePath.c_str() );
273
274 bool isSuccessful = false;
275 try{
276 boost::archive::text_iarchive ia(ifs);
277
278 // ファイルから読込
279 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
280
281 isSuccessful = true;
282 }
283 catch( boost::archive::archive_exception e )
284 {
285 if( isShowExceptionMessage )
286 {
287 echo( e.what() );
288 }
289 }
290 catch(...){
291 if( isShowExceptionMessage )
292 {
293 echo( "archive_exception以外の不明な例外" );
294 }
295 }
296
297 // 入力を閉じる
298 ifs.close();
299
300 if( !isSuccessful )
301 {
302 return false;
303 }
304
305 return true;
306}
307template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteText( const string &filePath, bool isShowExceptionMessage ) const
308{
309 // 出力アーカイブの作成
310 std::ofstream ofs( filePath.c_str() );
311
312 bool isSuccessful = false;
313 try{
314 boost::archive::text_oarchive oa(ofs);
315
316 // ファイルに書き出し
317 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
318
319 isSuccessful = true;
320 }
321 catch( boost::archive::archive_exception e )
322 {
323 if( isShowExceptionMessage )
324 {
325 echo( e.what() );
326 }
327 }
328 catch(...){
329 if( isShowExceptionMessage )
330 {
331 echo( "archive_exception以外の不明な例外" );
332 }
333 }
334
335 // 出力を閉じる
336 ofs.close();
337
338 if( !isSuccessful )
339 {
340 return false;
341 }
342
343 return true;
344}
345
346template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadTextString( const std::string &textString )
347{
348 bool isSuccessful = false;
349
350 // 入力アーカイブの作成
351 std::istringstream iss( textString );
352
353 try{
354 boost::archive::text_iarchive ia(iss);
355
356 // 文字列ストリームから読込
357 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
358
359 isSuccessful = true;
360 }
361 catch(...){
362 // 失敗
363 }
364
365 if( !isSuccessful )
366 {
367 return false;
368 }
369
370 return true;
371}
372template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::WriteTextString( std::string &textString ) const
373{
374 // 出力アーカイブの作成
375 std::ostringstream oss;
376
377 bool isSuccessful = false;
378 try{
379 boost::archive::text_oarchive oa(oss);
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 echo( e.what() );
389 }
390 catch(...){
391 echo( "archive_exception以外の不明な例外" );
392 }
393
394 if( !isSuccessful )
395 {
396 return false;
397 }
398
399 textString = oss.str();
400
401 return true;
402}
403
404template<class T_xml_schema> bool BoostSerializationSupport<T_xml_schema>::ReadXmlFromString( const std::string &textString )
405{
406 bool isSuccessful = false;
407
408 // 入力アーカイブの作成
409 std::istringstream iss( textString );
410
411 try{
412 boost::archive::xml_iarchive ia(iss);
413
414 // 文字列ストリームから読込
415 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
416
417 isSuccessful = true;
418 }
419 catch(...){
420 // 失敗
421 }
422
423 if( !isSuccessful )
424 {
425 return false;
426 }
427
428 return true;
429}
430
431
432#include <Compiler.h>
433#include <logger.h>
434
435template class Jenga::Common::BoostSerializationSupport<ObjectModule>;
436template class Jenga::Common::BoostSerializationSupport<LoggerSetting>;
Note: See TracBrowser for help on using the repository browser.