From: Mikko Rasa Date: Thu, 4 Feb 2010 11:06:40 +0000 (+0000) Subject: Some more code reformatting X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=215e719d0ef85f748898660d15d01e77ac551de9;p=libs%2Fdatafile.git Some more code reformatting Remove an old and incomplete file that was not used for anything --- diff --git a/source/binaryparser.h b/source/binaryparser.h index 26fa380..12340e5 100644 --- a/source/binaryparser.h +++ b/source/binaryparser.h @@ -30,7 +30,8 @@ private: public: BinaryParser(Input &i, const std::string &s); - Statement parse(); + + virtual Statement parse(); private: Statement parse_statement(); long long parse_int(); diff --git a/source/binarywriter.h b/source/binarywriter.h index 15a88a3..db89a5e 100644 --- a/source/binarywriter.h +++ b/source/binarywriter.h @@ -31,7 +31,8 @@ private: public: BinaryWriter(IO::Base &o); - void write(const Statement &st); + + virtual void write(const Statement &st); private: void write_(const Statement &st); void collect_keywords(const Statement &st); diff --git a/source/collection.h b/source/collection.h index 0bbc9fc..42dc2c9 100644 --- a/source/collection.h +++ b/source/collection.h @@ -114,7 +114,7 @@ private: template struct ItemCreatorBridge: public ItemCreatorBase { - virtual S *create(Collection &, const std::string &) const =0; + virtual S *create(Collection &, const std::string &) const = 0; }; template diff --git a/source/constant.h b/source/constant.h deleted file mode 100644 index 6f68bf0..0000000 --- a/source/constant.h +++ /dev/null @@ -1,16 +0,0 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef MSP_DATAFILE_CONSTANT_H_ -#define MSP_DATAFILE_CONSTANT_H_ - -class Constant -{ -public: - -}; - -#endif diff --git a/source/input.h b/source/input.h index ad921ec..e574866 100644 --- a/source/input.h +++ b/source/input.h @@ -14,16 +14,18 @@ namespace DataFile { class Input { +private: + IO::Base ∈ + unsigned line; + int next; + public: Input(IO::Base &); + int get(); int peek(); unsigned get_line_number() const { return line; } operator bool() const; -private: - IO::Base ∈ - unsigned line; - int next; }; } // namespace DataFile diff --git a/source/loader.cpp b/source/loader.cpp index 6b0557f..655514e 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -12,11 +12,10 @@ using namespace std; namespace Msp { namespace DataFile { -void Loader::load(const Statement &st) +Loader::~Loader() { - for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) - load_statement(*i); - finish(); + for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i) + delete i->second; } void Loader::load(Parser &p) @@ -30,30 +29,11 @@ void Loader::load(Parser &p) finish(); } -Loader::~Loader() -{ - for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i) - delete i->second; -} - -void Loader::load_sub_with(Loader &ldr) -{ - if(!cur_st) - throw InvalidState("load_sub called without current statement"); - - ldr.load(*cur_st); -} - -void Loader::add(const string &k, LoaderAction *a) +void Loader::load(const Statement &st) { - ActionMap::iterator i = actions.find(k); - if(i!=actions.end()) - { - delete i->second; - i->second = a; - } - else - actions[k] = a; + for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) + load_statement(*i); + finish(); } void Loader::load_statement(const Statement &st) @@ -78,5 +58,25 @@ void Loader::load_statement(const Statement &st) cur_st = 0; } +void Loader::load_sub_with(Loader &ldr) +{ + if(!cur_st) + throw InvalidState("load_sub called without current statement"); + + ldr.load(*cur_st); +} + +void Loader::add(const string &k, LoaderAction *a) +{ + ActionMap::iterator i = actions.find(k); + if(i!=actions.end()) + { + delete i->second; + i->second = a; + } + else + actions[k] = a; +} + } // namespace DataFile } // namespace Msp diff --git a/source/loader.h b/source/loader.h index 0c1dda2..57481cf 100644 --- a/source/loader.h +++ b/source/loader.h @@ -44,24 +44,50 @@ See also classes BasicLoader and BasicLoader2. class Loader { private: - /** - Loads data from a statement. - */ - void load(const Statement &st); + typedef std::map ActionMap; + + ActionMap actions; + const Statement *cur_st; +protected: + Loader(): cur_st(0) { } public: - /** - Loads statements from a parser. - */ + virtual ~Loader(); + + /** Loads statements from a parser. */ void load(Parser &p); - virtual ~Loader(); +private: + /** Loads data from a statement. */ + void load(const Statement &st); + + /** Processes a single statement */ + void load_statement(const Statement &st); + protected: - Loader(): cur_st(0) { } + /** Loads a sub-object from the statement being processed. The Loader class + of the sub-object is automatically used. */ + template + void load_sub(S &s) + { + typename S::Loader ldr(s); + load_sub_with(ldr); + } + + /** Loads a sub-object from the statement being processed with an extra + parameter for the Loader. The Loader class of the sub-object is + automatically used. */ + template + void load_sub(S &s, T &p) + { + typename S::Loader ldr(s, p); + load_sub_with(ldr); + } - /** - Adds a keyword that is loaded with a zero-argument function. - */ + /** Processes the current statement's substatements with another Loader. */ + void load_sub_with(Loader &); + + /** Adds a keyword that is loaded by calling a function. */ template void add(const std::string &k, void (L::*func)()) { add(k, new LoaderFunc0(func)); } @@ -86,9 +112,7 @@ protected: void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4)) { add(k, new LoaderFunc5(func)); } - /** - Adds a keyword that is loaded into a variable of the loaded object. - */ + /** Adds a keyword that is loaded into a member of the loaded object. */ template void add(const std::string &k, T0 L::*p0) { add(k, new LoadValue1(p0)); } @@ -97,44 +121,17 @@ protected: void add(const std::string &k, T0 L::*p0, T1 L::*p1) { add(k, new LoadValue2(p0, p1)); } - /** - Adds a keyword that is recognized but ignored. - */ + /** Adds a keyword that is recognized but ignored. */ void add(const std::string &k) { add(k, 0); } - /** - Loads a sub-object from the statement being processed. The Loader class of - the sub-object is automatically used. - */ - template - void load_sub(S &s) - { - typename S::Loader ldr(s); - load_sub_with(ldr); - } - - /** - Loads a sub-object from the statement being processed with an extra parameter - for the Loader. The Loader class of the sub-object is automatically used. - */ - template - void load_sub(S &s, T &p) - { - typename S::Loader ldr(s, p); - load_sub_with(ldr); - } - - /** - Processes the current statement's substatements with another Loader. - */ - void load_sub_with(Loader &); +private: + void add(const std::string &, LoaderAction *); - /** - Returns the source of the statement being processed. This can be used to - implement relative paths in include-like statements. Note that the source - may not necessarily be a file. - */ +protected: + /** Returns the source of the statement being processed. This can be used + to implement relative paths in include-like statements. Note that the + source may not necessarily be a file. */ const std::string &get_source() const { if(!cur_st) @@ -143,14 +140,6 @@ protected: } virtual void finish() { } -private: - typedef std::map ActionMap; - - ActionMap actions; - const Statement *cur_st; - - void add(const std::string &, LoaderAction *); - void load_statement(const Statement &st); }; diff --git a/source/loaderaction.h b/source/loaderaction.h index 972757b..6eebecc 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -21,14 +21,13 @@ Base class for loader actions. */ class LoaderAction { -public: - /** - Called when a statement is to be loaded. - */ - virtual void execute(Loader &, const Statement &) const=0; - virtual ~LoaderAction() { } protected: LoaderAction() { } +public: + virtual ~LoaderAction() { } + + /** Called to process a statement. */ + virtual void execute(Loader &, const Statement &) const = 0; }; @@ -38,17 +37,19 @@ Loads a statement by calling a function that takes no arguments. template class LoaderFunc0: public LoaderAction { -public: +private: typedef void (L::*FuncType)(); + FuncType func; + +public: LoaderFunc0(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=0) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(); }; -private: - FuncType func; }; @@ -58,17 +59,19 @@ Loads a statement by calling a function that takes one argument. template class LoaderFunc1: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get()); } -private: - FuncType func; }; @@ -78,11 +81,15 @@ Loads a statement by calling a function that takes an array of values. template class LoaderFunc1 &>: public LoaderAction { -public: +private: typedef void (L::*FuncType)(const std::vector &); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { std::vector values; values.reserve(st.args.size()); @@ -90,8 +97,6 @@ public: values.push_back(i->get()); (dynamic_cast(l).*func)(values); } -private: - FuncType func; }; @@ -101,139 +106,155 @@ Loads a statement by calling a function with the statement itself as argument. template class LoaderFunc1: public LoaderAction { -public: +private: typedef void (L::*FuncType)(const Statement &); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { (dynamic_cast(l).*func)(st); } -private: - FuncType func; }; template class LoaderFunc2: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1); + FuncType func; + +public: LoaderFunc2(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); } -private: - FuncType func; }; template class LoaderFunc3: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2); + FuncType func; + +public: LoaderFunc3(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=3) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); } -private: - FuncType func; }; template class LoaderFunc4: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2, A3); + FuncType func; + +public: LoaderFunc4(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=4) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get()); } -private: - FuncType func; }; template class LoaderFunc5: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2, A3, A4); + FuncType func; + +public: LoaderFunc5(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=5) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get(), st.args[4].get()); } -private: - FuncType func; }; template class LoadValue1: public LoaderAction { -public: +private: typedef T0 L::*Pointer0Type; + Pointer0Type ptr0; + +public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); dynamic_cast(l).get_object().*ptr0=st.args[0].get(); } -private: - Pointer0Type ptr0; }; template class LoadValue1: public LoaderAction { -public: +private: typedef T0 *L::*Pointer0Type; + Pointer0Type ptr0; + +public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); typename L::Loader &ldr=dynamic_cast(l); ldr.get_object().*ptr0=ldr.get_collection().template get(st.args[0].get()); } -private: - Pointer0Type ptr0; }; template class LoadValue2: public LoaderAction { -public: +private: typedef T0 L::*Pointer0Type; typedef T1 L::*Pointer1Type; + Pointer0Type ptr0; + Pointer1Type ptr1; + +public: LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); dynamic_cast(l).get_object().*ptr0=st.args[0].get(); dynamic_cast(l).get_object().*ptr1=st.args[1].get(); } -private: - Pointer0Type ptr0; - Pointer1Type ptr1; }; } // namespace DataFile diff --git a/source/parsermode.h b/source/parsermode.h index 78257ce..339bdb7 100644 --- a/source/parsermode.h +++ b/source/parsermode.h @@ -28,7 +28,7 @@ protected: public: virtual ~ParserMode() { } - virtual Statement parse() =0; + virtual Statement parse() = 0; }; } // namespace DataFile diff --git a/source/textparser.h b/source/textparser.h index bfae4af..7d92c58 100644 --- a/source/textparser.h +++ b/source/textparser.h @@ -19,6 +19,7 @@ class TextParser: public ParserMode { public: TextParser(Input &, const std::string &); + virtual Statement parse(); protected: Statement parse_statement(const Token *); diff --git a/source/textwriter.h b/source/textwriter.h index de6de45..4caa6b8 100644 --- a/source/textwriter.h +++ b/source/textwriter.h @@ -17,7 +17,8 @@ class TextWriter: public WriterMode { public: TextWriter(IO::Base &o); - void write(const Statement &st); + + virtual void write(const Statement &st); private: void write_(const Statement &st, unsigned); }; diff --git a/source/writermode.h b/source/writermode.h index 5014f25..793a67c 100644 --- a/source/writermode.h +++ b/source/writermode.h @@ -24,7 +24,7 @@ protected: public: virtual ~WriterMode() { } - virtual void write(const Statement &st) =0; + virtual void write(const Statement &st) = 0; }; } // namespace DataFile