source: dev/branches/egtra/ab5.0/abdev/ab_common/include/Lexical/Source.h@ 810

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

ムーブコンストラクタ・ムーブ代入演算子の導入

File size: 8.1 KB
Line 
1#pragma once
2
3int ChangeReturnCodeImpl(char *buffer);
4
5class IncludedFilesRelation
6{
7 std::vector<std::string> filePaths;
8 std::vector<int> lineFileNumbers;
9
10 // XMLシリアライズ用
11private:
12 friend class boost::serialization::access;
13 template<class Archive> void serialize(Archive& ar, const unsigned int version)
14 {
15 ar & BOOST_SERIALIZATION_NVP( filePaths );
16 ar & BOOST_SERIALIZATION_NVP( lineFileNumbers );
17 }
18
19public:
20 IncludedFilesRelation()
21 {
22 }
23 IncludedFilesRelation(IncludedFilesRelation const& y)
24 : filePaths(y.filePaths)
25 , lineFileNumbers(y.lineFileNumbers)
26 {
27 }
28 IncludedFilesRelation(IncludedFilesRelation&& y)
29 : filePaths(std::move(y.filePaths))
30 , lineFileNumbers(std::move(y.lineFileNumbers))
31 {
32 }
33 ~IncludedFilesRelation()
34 {
35 }
36 IncludedFilesRelation& operator =(IncludedFilesRelation&& y)
37 {
38 filePaths = std::move(y.filePaths);
39 lineFileNumbers = std::move(y.lineFileNumbers);
40 return *this;
41 }
42
43 IncludedFilesRelation& operator =(IncludedFilesRelation const& y)
44 {
45 return *this = std::move(IncludedFilesRelation(y));
46 }
47
48 const int GetFileNumber( int lineNumber ) const
49 {
50 return lineFileNumbers[lineNumber];
51 }
52 const std::string &GetFilePath( int lineNumber ) const
53 {
54 return filePaths[GetFileNumber( lineNumber )];
55 }
56 const std::string &GetFilePathFromFileNumber( int fileNumber ) const
57 {
58 return filePaths[fileNumber];
59 }
60 int GetFileCounts() const
61 {
62 return (int)filePaths.size();
63 }
64
65 int AddFile( const std::string &filePath )
66 {
67 filePaths.push_back( filePath );
68 return (int)filePaths.size()-1;
69 }
70 void AddLine( int fileNumber )
71 {
72 lineFileNumbers.push_back( fileNumber );
73 }
74
75 int GetLineCounts() const
76 {
77 return (int)lineFileNumbers.size();
78 }
79};
80
81class Text{
82protected:
83 char *buffer;
84 int length;
85
86public:
87
88 Text(){
89 buffer = (char *)calloc( 1, 1 );
90 length = 0;
91 }
92 Text( const Text &text )
93 : length( text.length )
94 {
95 buffer = (char *)malloc( length + 1 );
96 memcpy( buffer, text.buffer, length );
97 buffer[length] = 0;
98 }
99 Text(Text&& text)
100 : length( text.length )
101 {
102 buffer = text.buffer;
103 text.buffer = static_cast<char*>(calloc(1, 1));
104 text.length = 0;
105 }
106 ~Text(){
107 free( buffer );
108 }
109 void Clear()
110 {
111 length = 0;
112 }
113 void Add( const std::string &str )
114 {
115 buffer = (char *)realloc( buffer, length + str.size() + 1 );
116 strcpy( buffer + length, str.c_str() );
117 length += (int)str.size();
118 }
119 void Add( const std::vector<char> &str )
120 {
121 buffer = (char *)realloc( buffer, length + str.size() + 1 );
122 strcpy( buffer + length, &str[0] );
123 length += (int)str.size();
124 }
125
126 bool ReadFile( const std::string &filePath );
127
128 static void SlideString(char *buffer, int slide){
129 memmove(buffer+slide, buffer, strlen(buffer)+1);
130 }
131
132protected:
133 static void SwapImpl(Text& lhs, Text& rhs)
134 {
135 std::swap(lhs.buffer, rhs.buffer);
136 std::swap(lhs.length, rhs.length);
137 }
138};
139
140class BasicSource : public Text
141{
142 static const std::string generateDirectiveName;
143
144 IncludedFilesRelation includedFilesRelation;
145
146 // XMLシリアライズ用
147private:
148 friend class boost::serialization::access;
149 BOOST_SERIALIZATION_SPLIT_MEMBER();
150 template<class Archive> void load(Archive& ar, const unsigned int version)
151 {
152 std::string _buffer;
153 ar & BOOST_SERIALIZATION_NVP( _buffer );
154 ar & BOOST_SERIALIZATION_NVP( length );
155 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
156
157 // 読み込み後の処理
158 Realloc( length );
159 for( int i=0; i<length; i++ )
160 {
161 ULONG_PTR l1 = ( ( _buffer[i*3] >= 'a' ) ? ( _buffer[i*3] - 'a' + 0x0a ) : ( _buffer[i*3] - '0' ) ) * 0x10;
162 ULONG_PTR l2 = ( _buffer[i*3+1] >= 'a' ) ? ( _buffer[i*3+1] - 'a' + 0x0a ) : ( _buffer[i*3+1] - '0' );
163 ULONG_PTR l = l1 + l2;
164 buffer[i] = static_cast<char>(l);
165 }
166 buffer[length] = 0;
167 }
168 template<class Archive> void save(Archive& ar, const unsigned int version) const
169 {
170 // 保存準備
171 char *tempCode = (char *)calloc( (length+1) * 3, 1 );
172 for( int i=0; i<length; i++ )
173 {
174 char temp[32];
175 sprintf( temp, "%02x,", (unsigned char)buffer[i] );
176 tempCode[i*3] = temp[0];
177 tempCode[i*3+1] = temp[1];
178 tempCode[i*3+2] = temp[2];
179 }
180
181 std::string _buffer = tempCode;
182 free( tempCode );
183
184 ar & BOOST_SERIALIZATION_NVP( _buffer );
185 ar & BOOST_SERIALIZATION_NVP( length );
186 ar & BOOST_SERIALIZATION_NVP( includedFilesRelation );
187 }
188
189private:
190 void Realloc( int newLength )
191 {
192 bool isEqualBasbuf = false;
193 extern char *basbuf;
194 if( basbuf == buffer + 2 )
195 {
196 isEqualBasbuf = true;
197 }
198
199 buffer = (char *)realloc( buffer, newLength + 255 );
200
201 length = newLength;
202
203 if( isEqualBasbuf )
204 {
205 basbuf = buffer + 2;
206 }
207 }
208
209 void IncludeFiles();
210
211 void ChangeReturnLineChar();
212
213 void RemoveComments();
214
215 bool ReadFile_InIncludeDirective( const std::string &filePath );
216 void DirectiveIncludeOrRequire( const std::string &mainSourceFilePath, const std::string &includeDirPath );
217
218 void RemoveReturnLineUnderbar();
219
220 void Initialize( const std::string &source );
221
222public:
223 BasicSource(){}
224 BasicSource( const BasicSource &basicSource )
225 : Text( basicSource )
226 , includedFilesRelation( basicSource.includedFilesRelation )
227 {
228 }
229 BasicSource(BasicSource&& basicSource)
230 : Text(std::move(basicSource))
231 , includedFilesRelation(std::move(basicSource.includedFilesRelation))
232 {
233 }
234 BasicSource( const std::string &source )
235 {
236 Initialize( source );
237 }
238 ~BasicSource(){}
239
240 char *GetBuffer(){
241 return buffer+2;
242 }
243 const char *GetBuffer() const
244 {
245 return buffer+2;
246 }
247 int GetLength() const
248 {
249 return length-2;
250 }
251 void _ResetLength()
252 {
253 length = static_cast<int>(strlen( buffer ));
254 }
255
256 // 指定したインデックスが何行目かを取得
257 bool GetLineFromIndex( int index, int &result ) const;
258
259 const IncludedFilesRelation &GetIncludedFilesRelation() const
260 {
261 return includedFilesRelation;
262 }
263
264 void SetBuffer( const char *buffer );
265
266 bool ReadFile( const std::string &filePath, bool isDebug, bool isDll, bool isUnicode, int majorVer, const std::string &mainSourceFilePath, const std::string &includeDirPath );
267
268 void Addition( const char *buffer );
269
270 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
271
272 BasicSource& operator =(const BasicSource &source)
273 {
274 Realloc( source.length );
275 strcpy( buffer, source.buffer );
276 return *this;
277 }
278 BasicSource& operator =(BasicSource&& source)
279 {
280 Text::SwapImpl(*this, source);
281 return *this;
282 }
283
284 char operator[]( int index ) const
285 {
286 if( index>GetLength() )
287 {
288 Jenga::Throw( "BasicSource bad access" );
289 }
290 return buffer[2+index];
291 }
292
293 std::string cannotIncludePath;
294 int cannotIncludeSourcePos;
295};
296class BasicSources
297 : public std::vector<BasicSource>
298 , public Jenga::Common::BoostSerializationSupport<BasicSources>
299{
300 // XMLシリアライズ用
301private:
302 virtual const char *RootTagName() const
303 {
304 return "basicSources";
305 }
306 friend class boost::serialization::access;
307 template<class Archive> void serialize(Archive& ar, const unsigned int version)
308 {
309 ar & boost::serialization::make_nvp("vector_BasicSource", boost::serialization::base_object<std::vector<BasicSource>>(*this));
310 }
311};
312
313class SourceCodePosition
314{
315 int relationalObjectModuleIndex;
316 int pos;
317
318 // XMLシリアライズ用
319private:
320 friend class boost::serialization::access;
321 template<class Archive> void serialize(Archive& ar, const unsigned int version)
322 {
323 ar & BOOST_SERIALIZATION_NVP( relationalObjectModuleIndex );
324 ar & BOOST_SERIALIZATION_NVP( pos );
325 }
326
327public:
328 SourceCodePosition( int relationalObjectModuleIndex, int pos )
329 : relationalObjectModuleIndex( relationalObjectModuleIndex )
330 , pos( pos )
331 {
332 }
333 SourceCodePosition()
334 : relationalObjectModuleIndex( -1 )
335 , pos( -1 )
336 {
337 }
338
339 int GetRelationalObjectModuleIndex() const;
340 void SetRelationalObjectModuleIndex( int relationalObjectModuleIndex )
341 {
342 this->relationalObjectModuleIndex = relationalObjectModuleIndex;
343 }
344 int GetPos() const
345 {
346 return pos;
347 }
348 void SetPos( int pos )
349 {
350 this->pos = pos;
351 }
352
353 bool IsNothing() const;
354};
Note: See TracBrowser for help on using the repository browser.