Skip to content

Commit 5839988

Browse files
committed
made simplecpp::TokenList::Stream an abstract class and moved implementation-specific parts into StdIStream
1 parent 1a8970d commit 5839988

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

simplecpp.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,12 @@ void simplecpp::Token::printOut() const
229229

230230
class simplecpp::TokenList::Stream {
231231
public:
232-
Stream(std::istream &istr)
233-
: istr(istr)
234-
, bom(getAndSkipBOM())
235-
, isUtf16(bom == 0xfeff || bom == 0xfffe)
236-
{
237-
}
232+
virtual ~Stream() {}
238233

239-
int get() {
240-
return istr.get();
241-
}
242-
int peek() {
243-
return istr.peek();
244-
}
245-
void unget() {
246-
istr.unget();
247-
}
248-
bool good() {
249-
return istr.good();
250-
}
234+
virtual int get() = 0;
235+
virtual int peek() = 0;
236+
virtual void unget() = 0;
237+
virtual bool good() = 0;
251238

252239
unsigned char readChar()
253240
{
@@ -308,7 +295,12 @@ class simplecpp::TokenList::Stream {
308295
unget();
309296
}
310297

311-
private:
298+
protected:
299+
void init() {
300+
bom = getAndSkipBOM();
301+
isUtf16 = (bom == 0xfeff || bom == 0xfffe);
302+
}
303+
312304
unsigned short getAndSkipBOM()
313305
{
314306
const int ch1 = peek();
@@ -336,17 +328,41 @@ class simplecpp::TokenList::Stream {
336328
return 0;
337329
}
338330

331+
unsigned short bom;
332+
bool isUtf16;
333+
};
334+
335+
class StdIStream : public simplecpp::TokenList::Stream {
336+
public:
337+
StdIStream(std::istream &istr)
338+
: istr(istr)
339+
{
340+
init();
341+
}
342+
343+
virtual int get() {
344+
return istr.get();
345+
}
346+
virtual int peek() {
347+
return istr.peek();
348+
}
349+
virtual void unget() {
350+
istr.unget();
351+
}
352+
virtual bool good() {
353+
return istr.good();
354+
}
355+
356+
private:
339357
std::istream &istr;
340-
const unsigned short bom;
341-
const bool isUtf16;
342358
};
343359

344360
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}
345361

346362
simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
347363
: frontToken(nullptr), backToken(nullptr), files(filenames)
348364
{
349-
simplecpp::TokenList::Stream stream(istr);
365+
StdIStream stream(istr);
350366
readfile(stream,filename,outputList);
351367
}
352368

@@ -1324,7 +1340,7 @@ namespace simplecpp {
13241340
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
13251341
const std::string def(name + ' ' + value);
13261342
std::istringstream istr(def);
1327-
simplecpp::TokenList::Stream stream(istr);
1343+
StdIStream stream(istr);
13281344
tokenListDefine.readfile(stream);
13291345
if (!parseDefine(tokenListDefine.cfront()))
13301346
throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value);

0 commit comments

Comments
 (0)