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

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

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

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