source: dev/trunk/ab5.0/abdev/ab_common/src/Lexical/ObjectModule.cpp@ 750

Last change on this file since 750 was 750, checked in by イグトランス (egtra), 16 years ago

BOOST_FOREACHを可能なものはVC++ 2005 for eachへ置換(やや速くなる)。

File size: 6.3 KB
Line 
1#pragma warning(disable : 4996)
2
3#include <map>
4#include <string>
5#include <vector>
6#include <fstream>
7#include <iostream>
8#include <iomanip>
9#include <ios>
10#include <streambuf>
11#include <sstream>
12
13#define WIN32_LEAN_AND_MEAN
14#include <windows.h>
15#include <stdio.h>
16#include <string.h>
17#include <math.h>
18#include <time.h>
19#include <limits.h>
20#include <shlobj.h>
21#include <tchar.h>
22
23//boost libraries
24#include <boost/foreach.hpp>
25
26
27// ObjectModuleの内容をXmlで生成する場合はこの行を有効にする
28//#define OBJECT_MODULE_IS_NOT_BINARY
29
30
31#ifdef OBJECT_MODULE_IS_NOT_BINARY
32#include <boost/archive/xml_oarchive.hpp>
33#include <boost/archive/xml_iarchive.hpp>
34#else
35#include <boost/archive/binary_oarchive.hpp>
36#include <boost/archive/binary_iarchive.hpp>
37#endif
38#include <boost/serialization/string.hpp>
39#include <boost/serialization/access.hpp>
40#include <boost/serialization/level.hpp>
41#include <boost/serialization/vector.hpp>
42#include <boost/serialization/map.hpp>
43#include <boost/serialization/version.hpp>
44#include <boost/serialization/is_abstract.hpp>
45#include <boost/serialization/serialization.hpp>
46#include <boost/serialization/nvp.hpp>
47#include <boost/serialization/export.hpp>
48
49#define foreach(v, c) for each (v in c)
50
51#include <jenga/include/jenga.h>
52#include <abdev/ab_common/include/ab_common.h>
53
54
55void ObjectModule::StaticLink( ObjectModule &objectModule, bool isSll )
56{
57 const std::vector<int> relationTable = this->GetRelationTable( objectModule.relationalObjectModuleNames );
58
59 long dataSectionBaseOffset = dataTable.GetSize();
60
61 // メタ情報を結合
62 meta.StaticLink( objectModule.meta, dataSectionBaseOffset, relationTable );
63
64 if( !isSll )
65 {
66 // グローバル ネイティブコードを結合
67 objectModule.globalNativeCode.ResetDataSectionBaseOffset( dataSectionBaseOffset );
68 objectModule.globalNativeCode.ResetRelationalObjectModuleIndex( relationTable );
69 globalNativeCode.PutEx( objectModule.globalNativeCode );
70
71 // データテーブルを結合
72 objectModule.dataTable.ResetDataSectionBaseOffset( dataSectionBaseOffset );
73 dataTable.Add( objectModule.dataTable );
74 }
75
76 // TODO: basbufがいらなくなったら消す
77 extern char *basbuf;
78 basbuf = this->source.GetBuffer();
79}
80
81void ObjectModule::Resolve( ResolveErrors &resolveErrors )
82{
83 this->meta.Resolve( *this, resolveErrors );
84
85 // グローバルネイティブコードを解決(スケジュールを解決する)
86 this->globalNativeCode.Resolve( *this, resolveErrors );
87
88 // データテーブルを解決(スケジュールを解決する)
89 this->dataTable.Resolve( *this, resolveErrors );
90}
91
92const std::vector<int> ObjectModule::GetRelationTable( const Jenga::Common::Strings &oldRelationalObjectModuleNames )
93{
94 // 要素 = 古いインデックス、値 = 新しいインデックス
95 std::vector<int> relationTable;
96
97 // リレーションテーブルを構築
98 foreach( const std::string &oldRelationalObjectModuleName, oldRelationalObjectModuleNames )
99 {
100 bool isMatch = false;
101 for( int i=0; i<static_cast<int>(this->relationalObjectModuleNames.size()); i++ )
102 {
103 if( oldRelationalObjectModuleName == this->relationalObjectModuleNames[i] )
104 {
105 isMatch = true;
106 relationTable.push_back( i );
107 break;
108 }
109 }
110
111 if( !isMatch )
112 {
113 // エラー
114 _ASSERT( false );
115 }
116 }
117
118 return relationTable;
119}
120
121bool ObjectModule::Read( const std::string &filePath )
122{
123 // XMLとして読み込む
124 bool isSuccessful = false;
125
126 try{
127#ifdef OBJECT_MODULE_IS_NOT_BINARY
128 std::ifstream ifs( filePath.c_str() );
129 boost::archive::xml_iarchive ia(ifs);
130#else
131 std::ifstream ifs( filePath.c_str(), std::ios::in | std::ios::binary );
132 boost::archive::binary_iarchive ia(ifs);
133#endif
134
135 // ファイルから読込
136 ia >> boost::serialization::make_nvp( RootTagName(), *this );
137
138 isSuccessful = true;
139 }
140 catch( boost::archive::archive_exception& e )
141 {
142 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
143 }
144 catch(...){
145 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
146 }
147
148 if( !isSuccessful )
149 {
150 return false;
151 }
152
153 return true;
154}
155bool ObjectModule::Write( const std::string &filePath ) const
156{
157 bool isSuccessful = false;
158
159 try{
160#ifdef OBJECT_MODULE_IS_NOT_BINARY
161 std::ofstream ofs( filePath.c_str() );
162 boost::archive::xml_oarchive oa(ofs);
163#else
164 std::ofstream ofs( filePath.c_str(), std::ios::out | std::ios::binary );
165 boost::archive::binary_oarchive oa(ofs);
166#endif
167
168 // ファイルに書き出し
169 oa << boost::serialization::make_nvp( RootTagName(), *this );
170
171 isSuccessful = true;
172 }
173 catch( boost::archive::archive_exception& e )
174 {
175 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
176 }
177 catch(...){
178 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
179 }
180
181 if( !isSuccessful )
182 {
183 return false;
184 }
185
186 return true;
187}
188bool ObjectModule::ReadString( const std::string &str )
189{
190 bool isSuccessful = false;
191
192 // 入力アーカイブの作成
193
194 try{
195#ifdef OBJECT_MODULE_IS_NOT_BINARY
196 std::istringstream iss( str );
197 boost::archive::xml_iarchive ia(iss);
198#else
199 std::istringstream iss( str, std::ios::in | std::ios::binary );
200 boost::archive::binary_iarchive ia(iss);
201#endif
202
203 // 文字列ストリームから読込
204 ia >> boost::serialization::make_nvp( RootTagName(), *this );
205
206 isSuccessful = true;
207 }
208 catch( boost::archive::archive_exception& e )
209 {
210 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
211 }
212 catch(...){
213 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
214 }
215
216 if( !isSuccessful )
217 {
218 return false;
219 }
220
221 return true;
222}
223bool ObjectModule::WriteString( std::string &str ) const
224{
225 // 出力アーカイブの作成
226
227 bool isSuccessful = false;
228 try{
229#ifdef OBJECT_MODULE_IS_NOT_BINARY
230 std::ostringstream oss;
231 boost::archive::xml_oarchive oa(oss);
232#else
233 std::ostringstream oss( "", std::ios::out | std::ios::binary );
234 boost::archive::binary_oarchive oa(oss);
235#endif
236
237 // 文字列ストリームに書き出し
238 oa << boost::serialization::make_nvp( RootTagName(), *this );
239
240 str = oss.str();
241
242 isSuccessful = true;
243 }
244 catch( boost::archive::archive_exception &e )
245 {
246 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
247 }
248 catch(...){
249 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
250 }
251
252 return isSuccessful;
253}
Note: See TracBrowser for help on using the repository browser.