From 42398f0178d499bb013ccc08f0c29f69f6f1ad2d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 2 Oct 2010 11:47:33 +0000 Subject: [PATCH] Add reverse and arrival row types to Timetable Change "go to" into "set route to" More flexible row parameters Add Row::save method --- source/libmarklin/timetable.cpp | 143 +++++++++++++++++++------------- source/libmarklin/timetable.h | 18 ++-- 2 files changed, 98 insertions(+), 63 deletions(-) diff --git a/source/libmarklin/timetable.cpp b/source/libmarklin/timetable.cpp index bd9f5f0..f3d49c4 100644 --- a/source/libmarklin/timetable.cpp +++ b/source/libmarklin/timetable.cpp @@ -83,30 +83,33 @@ void Timetable::tick(const Time::TimeStamp &t) executing = true; } - while(executing) + if(executing) { Row &row = rows[current_row]; switch(row.type) { case GOTO: - train.go_to(**parse_location(row.strparam).get_tracks().begin()); + train.go_to(**parse_location(row.get_param(0)).get_tracks().begin()); break; case TRAVEL: - pending_block = &parse_location(row.strparam); + pending_block = &parse_location(row.get_param(0)); executing = false; break; case WAIT: - wait_timeout = t+row.intparam*Time::sec; + wait_timeout = t+row.get_param(0)*Time::sec; executing = false; break; case ARRIVE: executing = false; break; case SPEED: - train.set_control("speed", row.intparam/3.6*train.get_layout().get_catalogue().get_scale()); + train.set_control("speed", row.get_param(0)/3.6*train.get_layout().get_catalogue().get_scale()); + break; + case REVERSE: + train.set_control("reverse", !train.get_control("reverse")); break; case ROUTE: - train.set_route(&train.get_layout().get_route(row.strparam)); + train.set_route(&train.get_layout().get_route(row.get_param(0))); break; } @@ -118,29 +121,7 @@ void Timetable::tick(const Time::TimeStamp &t) void Timetable::save(list &st) const { for(vector::const_iterator i=rows.begin(); i!=rows.end(); ++i) - { - switch(i->type) - { - case GOTO: - st.push_back((DataFile::Statement("goto"), i->strparam)); - break; - case TRAVEL: - st.push_back((DataFile::Statement("travel"), i->strparam)); - break; - case WAIT: - st.push_back((DataFile::Statement("wait"), i->intparam)); - break; - case ARRIVE: - st.push_back(DataFile::Statement("arrive")); - break; - case SPEED: - st.push_back((DataFile::Statement("speed"), i->intparam)); - break; - case ROUTE: - st.push_back((DataFile::Statement("route"), i->strparam)); - break; - } - } + st.push_back(i->save()); } Block &Timetable::parse_location(const string &loc) @@ -171,44 +152,80 @@ void Timetable::train_arrived() } -Timetable::Row::Row(RowType t, int p): - type(t), - intparam(p) +Timetable::Row::Row(RowType t): + type(t) { } -Timetable::Row::Row(RowType t, const string &p): - type(t), - intparam(0), - strparam(p) -{ } +template +Timetable::Row::Row(RowType t, const T &p): + type(t) +{ + params.push_back(p); +} + +template +const T &Timetable::Row::get_param(unsigned i) const +{ + if(i>=params.size()) + throw InvalidParameterValue("Parameter index out of range"); + return params[i].value(); +} string Timetable::Row::str() const { switch(type) { case GOTO: - return "go to "+strparam; + return "set route to "+get_param(0); case TRAVEL: - return "travel to "+strparam; + return "travel to "+get_param(0); case WAIT: - return format("wait for %d seconds", intparam); + return format("wait for %d seconds", get_param(0)); case ARRIVE: - return "wait for arrival"; + return "travel until arrival"; case SPEED: - return format("set speed %d km/h", intparam); + return format("set speed %d km/h", get_param(0)); + case REVERSE: + return "reverse"; case ROUTE: - return "set route "+strparam; + return "set route "+get_param(0); default: return "invalid row"; } } +DataFile::Statement Timetable::Row::save() const +{ + switch(type) + { + case GOTO: + return DataFile::Statement("goto"), get_param(0); + case TRAVEL: + return DataFile::Statement("travel"), get_param(0); + case WAIT: + return DataFile::Statement("wait"), get_param(0); + case ARRIVE: + return DataFile::Statement("arrive"); + case SPEED: + return DataFile::Statement("speed"), get_param(0); + case REVERSE: + return DataFile::Statement("reverse"); + case ROUTE: + return DataFile::Statement("route"), get_param(0); + default: + return DataFile::Statement(); + } +} + Timetable::Row Timetable::Row::parse(const string &s) { - if(!s.compare(0, 6, "go to ")) - return Row(GOTO, s.substr(6)); - else if(!s.compare(0, 10, "travel to ")) - return Row(TRAVEL, s.substr(10)); + if(!s.compare(0, 7, "travel ")) + { + if(!s.compare(7, 3, "to ")) + return Row(TRAVEL, s.substr(10)); + else if(!s.compare(7, string::npos, "until arrival")) + return Row(ARRIVE); + } else if(!s.compare(0, 9, "wait for ") && isdigit(s[9])) { unsigned nondigit = 10; @@ -216,17 +233,21 @@ Timetable::Row Timetable::Row::parse(const string &s) ++nondigit; return Row(WAIT, lexical_cast(s.substr(9, nondigit-9))); } - else if(s=="wait for arrival") - return Row(ARRIVE, 0); else if(!s.compare(0, 10, "set speed ")) { unsigned nondigit = 11; while(nondigit(s.substr(10, nondigit-10))); + return Row(SPEED, lexical_cast(s.substr(10, nondigit-10))); } + else if(s=="reverse") + return Row(REVERSE); else if(!s.compare(0, 10, "set route ")) + { + if(!s.compare(10, 3, "to ")) + return Row(GOTO, s.substr(13)); return Row(ROUTE, s.substr(10)); + } throw InvalidParameterValue("Invalid row"); } @@ -235,17 +256,18 @@ Timetable::Row Timetable::Row::parse(const string &s) Timetable::Loader::Loader(Timetable &tt): DataFile::ObjectLoader(tt) { - add("arrive", &Loader::arrive); - add("goto", &Loader::go_to); - add("route", &Loader::route); - add("speed", &Loader::speed); - add("travel", &Loader::travel); - add("wait", &Loader::wait); + add("arrive", &Loader::arrive); + add("goto", &Loader::go_to); + add("route", &Loader::route); + add("speed", &Loader::speed); + add("reverse", &Loader::reverse); + add("travel", &Loader::travel); + add("wait", &Loader::wait); } void Timetable::Loader::arrive() { - obj.rows.push_back(Row(ARRIVE, 0)); + obj.rows.push_back(Row(ARRIVE)); } void Timetable::Loader::go_to(const string &t) @@ -258,7 +280,12 @@ void Timetable::Loader::route(const string &r) obj.rows.push_back(Row(ROUTE, r)); } -void Timetable::Loader::speed(int s) +void Timetable::Loader::reverse() +{ + obj.rows.push_back(Row(REVERSE)); +} + +void Timetable::Loader::speed(unsigned s) { obj.rows.push_back(Row(SPEED, s)); } diff --git a/source/libmarklin/timetable.h b/source/libmarklin/timetable.h index 3abbf83..72e12d3 100644 --- a/source/libmarklin/timetable.h +++ b/source/libmarklin/timetable.h @@ -30,7 +30,8 @@ public: void arrive(); void go_to(const std::string &); void route(const std::string &); - void speed(int); + void reverse(); + void speed(unsigned); void travel(const std::string &); void wait(unsigned); }; @@ -42,20 +43,27 @@ public: WAIT, ARRIVE, SPEED, + REVERSE, ROUTE }; struct Row { RowType type; - int intparam; - std::string strparam; + std::vector params; - Row(RowType, int); - Row(RowType, const std::string &); + Row(RowType); + + template + Row(RowType, const T &); + + template + const T &get_param(unsigned) const; std::string str() const; + Msp::DataFile::Statement save() const; + static Row parse(const std::string &); }; -- 2.45.2