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
RevLine 
[266]1#pragma once
2
[735]3int ChangeReturnCodeImpl(char *buffer);
4
[279]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 }
[810]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 }
[279]33 ~IncludedFilesRelation()
34 {
35 }
[810]36 IncludedFilesRelation& operator =(IncludedFilesRelation&& y)
37 {
38 filePaths = std::move(y.filePaths);
39 lineFileNumbers = std::move(y.lineFileNumbers);
40 return *this;
41 }
[279]42
[810]43 IncludedFilesRelation& operator =(IncludedFilesRelation const& y)
44 {
45 return *this = std::move(IncludedFilesRelation(y));
46 }
47
[279]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 {
[308]62 return (int)filePaths.size();
[279]63 }
64
65 int AddFile( const std::string &filePath )
66 {
67 filePaths.push_back( filePath );
[308]68 return (int)filePaths.size()-1;
[279]69 }
70 void AddLine( int fileNumber )
71 {
72 lineFileNumbers.push_back( fileNumber );
73 }
74
75 int GetLineCounts() const
76 {
[308]77 return (int)lineFileNumbers.size();
[279]78 }
79};
80
[266]81class Text{
82protected:
83 char *buffer;
84 int length;
85
86public:
87
88 Text(){
89 buffer = (char *)calloc( 1, 1 );
90 length = 0;
91 }
[280]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 }
[810]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 }
[266]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 );
[700]116 strcpy( buffer + length, str.c_str() );
[266]117 length += (int)str.size();
118 }
[268]119 void Add( const std::vector<char> &str )
120 {
121 buffer = (char *)realloc( buffer, length + str.size() + 1 );
[700]122 strcpy( buffer + length, &str[0] );
[268]123 length += (int)str.size();
124 }
[266]125
[523]126 bool ReadFile( const std::string &filePath );
[266]127
[735]128 static void SlideString(char *buffer, int slide){
[700]129 memmove(buffer+slide, buffer, strlen(buffer)+1);
[266]130 }
[810]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 }
[266]138};
139
140class BasicSource : public Text
141{
[523]142 static const std::string generateDirectiveName;
[266]143
[279]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:
[281]190 void Realloc( int newLength )
191 {
192 bool isEqualBasbuf = false;
193 extern char *basbuf;
[282]194 if( basbuf == buffer + 2 )
[281]195 {
196 isEqualBasbuf = true;
197 }
198
[266]199 buffer = (char *)realloc( buffer, newLength + 255 );
200
201 length = newLength;
202
[281]203 if( isEqualBasbuf )
204 {
205 basbuf = buffer + 2;
206 }
[266]207 }
208
209 void IncludeFiles();
210
211 void ChangeReturnLineChar();
212
213 void RemoveComments();
214
[523]215 bool ReadFile_InIncludeDirective( const std::string &filePath );
[602]216 void DirectiveIncludeOrRequire( const std::string &mainSourceFilePath, const std::string &includeDirPath );
[266]217
218 void RemoveReturnLineUnderbar();
219
[322]220 void Initialize( const std::string &source );
221
[266]222public:
223 BasicSource(){}
[280]224 BasicSource( const BasicSource &basicSource )
225 : Text( basicSource )
226 , includedFilesRelation( basicSource.includedFilesRelation )
227 {
228 }
[810]229 BasicSource(BasicSource&& basicSource)
230 : Text(std::move(basicSource))
231 , includedFilesRelation(std::move(basicSource.includedFilesRelation))
232 {
233 }
[322]234 BasicSource( const std::string &source )
235 {
236 Initialize( source );
237 }
[266]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 }
[304]251 void _ResetLength()
252 {
[739]253 length = static_cast<int>(strlen( buffer ));
[304]254 }
[266]255
[739]256 // 指定したインデックスが何行目かを取得
257 bool GetLineFromIndex( int index, int &result ) const;
258
[279]259 const IncludedFilesRelation &GetIncludedFilesRelation() const
260 {
261 return includedFilesRelation;
262 }
263
[266]264 void SetBuffer( const char *buffer );
265
[602]266 bool ReadFile( const std::string &filePath, bool isDebug, bool isDll, bool isUnicode, int majorVer, const std::string &mainSourceFilePath, const std::string &includeDirPath );
[266]267
268 void Addition( const char *buffer );
269
[280]270 bool GetLineInfo( int sourceCodePos, int &line, std::string &fileName ) const;
[279]271
[810]272 BasicSource& operator =(const BasicSource &source)
273 {
[266]274 Realloc( source.length );
[700]275 strcpy( buffer, source.buffer );
[810]276 return *this;
[266]277 }
[810]278 BasicSource& operator =(BasicSource&& source)
279 {
280 Text::SwapImpl(*this, source);
281 return *this;
282 }
[266]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 }
[555]292
293 std::string cannotIncludePath;
294 int cannotIncludeSourcePos;
[266]295};
[636]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};
[632]312
313class SourceCodePosition
314{
[637]315 int relationalObjectModuleIndex;
[632]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 {
[637]323 ar & BOOST_SERIALIZATION_NVP( relationalObjectModuleIndex );
[632]324 ar & BOOST_SERIALIZATION_NVP( pos );
325 }
326
327public:
[637]328 SourceCodePosition( int relationalObjectModuleIndex, int pos )
329 : relationalObjectModuleIndex( relationalObjectModuleIndex )
[632]330 , pos( pos )
331 {
332 }
333 SourceCodePosition()
[637]334 : relationalObjectModuleIndex( -1 )
335 , pos( -1 )
[632]336 {
337 }
338
[637]339 int GetRelationalObjectModuleIndex() const;
340 void SetRelationalObjectModuleIndex( int relationalObjectModuleIndex )
[632]341 {
[637]342 this->relationalObjectModuleIndex = relationalObjectModuleIndex;
[632]343 }
344 int GetPos() const
345 {
346 return pos;
347 }
[637]348 void SetPos( int pos )
349 {
350 this->pos = pos;
351 }
352
353 bool IsNothing() const;
[632]354};
Note: See TracBrowser for help on using the repository browser.