#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Jenga{ namespace Common{ using namespace std; template class BoostXmlSupport{ virtual const char *RootTagName() const = 0; void echo( const char *msg ) const { MessageBox( NULL, msg, "XMLシリアライズの例外", MB_OK ); } public: bool Read( istream& ifs, bool isShowExceptionMessage = true ) { bool isSuccessful = false; try{ boost::archive::xml_iarchive ia(ifs); // ファイルから読込 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this ); isSuccessful = true; } catch( boost::archive::archive_exception e ) { if( isShowExceptionMessage ) { echo( e.what() ); } } catch(...){ if( isShowExceptionMessage ) { echo( "archive_exception以外の不明な例外" ); } } if( !isSuccessful ) { return false; } return true; } bool Write( ostream& ofs, bool isShowExceptionMessage = true ) const { bool isSuccessful = false; try{ boost::archive::xml_oarchive oa(ofs); // ファイルに書き出し oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this ); isSuccessful = true; } catch( boost::archive::archive_exception e ) { if( isShowExceptionMessage ) { echo( e.what() ); } } catch(...){ if( isShowExceptionMessage ) { echo( "archive_exception以外の不明な例外" ); } } if( !isSuccessful ) { return false; } return true; } bool Read( const string &xmlFilePath, bool isShowExceptionMessage = true ) { bool isSuccessful = false; // 入力アーカイブの作成 std::ifstream ifs( xmlFilePath.c_str() ); bool result = Read(ifs,isShowExceptionMessage); // 入力を閉じる ifs.close(); return result; } bool Write( const string &xmlFilePath, bool isShowExceptionMessage = true ) const { // 出力アーカイブの作成 std::ofstream ofs( xmlFilePath.c_str() ); bool result = Write(ofs,isShowExceptionMessage); // 出力を閉じる ofs.close(); return result; } bool ReadFromString( const string &xmlBuffer ) { bool isSuccessful = false; // 入力アーカイブの作成 std::istringstream iss( xmlBuffer ); try{ boost::archive::xml_iarchive ia(iss); // 文字列ストリームから読込 ia >> boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this ); isSuccessful = true; } catch(...){ // 失敗 } if( !isSuccessful ) { return false; } return true; } bool ToWString( wstring &xmlBuffer ) const { bool isSuccessful = false; // 入力アーカイブの作成 std::ostringstream oss; try{ boost::archive::xml_oarchive oa(oss); // 文字列ストリームへ書き出し oa << boost::serialization::make_nvp( RootTagName(), *(T_xml_schema *)this ); xmlBuffer = oss.str(); isSuccessful = true; } catch(...){ // 失敗 } if( !isSuccessful ) { return false; } return true; } }; }}