source: dev/trunk/jenga/include/common/BoostXmlSupport.h@ 203

Last change on this file since 203 was 190, checked in by dai_9181, 17 years ago
File size: 2.9 KB
Line 
1#pragma once
2
3#include <vector>
4#include <string>
5#include <fstream>
6#include <sstream>
7
8#include <boost/archive/xml_oarchive.hpp>
9#include <boost/archive/xml_iarchive.hpp>
10#include <boost/serialization/serialization.hpp>
11#include <boost/serialization/string.hpp>
12#include <boost/serialization/access.hpp>
13#include <boost/serialization/export.hpp>
14#include <boost/serialization/level.hpp>
15#include <boost/serialization/vector.hpp>
16#include <boost/serialization/map.hpp>
17#include <boost/serialization/nvp.hpp>
18#include <boost/serialization/version.hpp>
19#include <boost/serialization/is_abstract.hpp>
20
21namespace Jenga{
22namespace Common{
23
24using namespace std;
25
26template<class T_xml_schema> class BoostXmlSupport{
27 virtual const char *RootTagName() const = 0;
28
29public:
30 bool Read( istream& ifs )
31 {
32 bool isSuccessful = false;
33
34 try{
35 boost::archive::xml_iarchive ia(ifs);
36
37 // ファイルから読込
38 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
39
40 isSuccessful = true;
41 }
42 catch(...){
43 // 失敗
44 }
45
46 if( !isSuccessful )
47 {
48 return false;
49 }
50
51 return true;
52 }
53
54 bool Write( ostream& ofs ) const
55 {
56 bool isSuccessful = false;
57
58 try{
59 boost::archive::xml_oarchive oa(ofs);
60
61 // ファイルに書き出し
62 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
63
64 isSuccessful = true;
65 }
66 catch( ... ){
67 // 失敗
68 }
69
70 if( !isSuccessful )
71 {
72 return false;
73 }
74
75 return true;
76 }
77
78 bool Read( const string &xmlFilePath )
79 {
80 bool isSuccessful = false;
81
82 // 入力アーカイブの作成
83 std::ifstream ifs( xmlFilePath.c_str() );
84
85 bool result = Read(ifs);
86
87 // 入力を閉じる
88 ifs.close();
89
90 return result;
91 }
92
93 bool Write( const string &xmlFilePath ) const
94 {
95 // 出力アーカイブの作成
96 std::ofstream ofs( xmlFilePath.c_str() );
97
98 bool result = Write(ofs);
99
100 // 出力を閉じる
101 ofs.close();
102
103 return result;
104 }
105
106 bool ReadFromString( const wstring &xmlBuffer )
107 {
108 bool isSuccessful = false;
109
110 // 入力アーカイブの作成
111 std::istringstream iss( xmlBuffer );
112
113 try{
114 boost::archive::xml_iarchive ia(iss);
115
116 // 文字列ストリームから読込
117 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
118
119 isSuccessful = true;
120 }
121 catch(...){
122 // 失敗
123 }
124
125 if( !isSuccessful )
126 {
127 return false;
128 }
129
130 return true;
131 }
132 bool ToWString( wstring &xmlBuffer ) const
133 {
134 bool isSuccessful = false;
135
136 // 入力アーカイブの作成
137 std::ostringstream oss;
138
139 try{
140 boost::archive::xml_oarchive oa(oss);
141
142 // 文字列ストリームへ書き出し
143 oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this );
144
145 xmlBuffer = oss.str();
146
147 isSuccessful = true;
148 }
149 catch(...){
150 // 失敗
151 }
152
153 if( !isSuccessful )
154 {
155 return false;
156 }
157
158 return true;
159 }
160};
161
162
163}}
Note: See TracBrowser for help on using the repository browser.