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<string>(0)).get_tracks().begin());
break;
case TRAVEL:
- pending_block = &parse_location(row.strparam);
+ pending_block = &parse_location(row.get_param<string>(0));
executing = false;
break;
case WAIT:
- wait_timeout = t+row.intparam*Time::sec;
+ wait_timeout = t+row.get_param<unsigned>(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<unsigned>(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<string>(0)));
break;
}
void Timetable::save(list<DataFile::Statement> &st) const
{
for(vector<Row>::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)
}
-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<typename T>
+Timetable::Row::Row(RowType t, const T &p):
+ type(t)
+{
+ params.push_back(p);
+}
+
+template<typename T>
+const T &Timetable::Row::get_param(unsigned i) const
+{
+ if(i>=params.size())
+ throw InvalidParameterValue("Parameter index out of range");
+ return params[i].value<T>();
+}
string Timetable::Row::str() const
{
switch(type)
{
case GOTO:
- return "go to "+strparam;
+ return "set route to "+get_param<string>(0);
case TRAVEL:
- return "travel to "+strparam;
+ return "travel to "+get_param<string>(0);
case WAIT:
- return format("wait for %d seconds", intparam);
+ return format("wait for %d seconds", get_param<unsigned>(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<unsigned>(0));
+ case REVERSE:
+ return "reverse";
case ROUTE:
- return "set route "+strparam;
+ return "set route "+get_param<string>(0);
default:
return "invalid row";
}
}
+DataFile::Statement Timetable::Row::save() const
+{
+ switch(type)
+ {
+ case GOTO:
+ return DataFile::Statement("goto"), get_param<string>(0);
+ case TRAVEL:
+ return DataFile::Statement("travel"), get_param<string>(0);
+ case WAIT:
+ return DataFile::Statement("wait"), get_param<unsigned>(0);
+ case ARRIVE:
+ return DataFile::Statement("arrive");
+ case SPEED:
+ return DataFile::Statement("speed"), get_param<unsigned>(0);
+ case REVERSE:
+ return DataFile::Statement("reverse");
+ case ROUTE:
+ return DataFile::Statement("route"), get_param<string>(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;
++nondigit;
return Row(WAIT, lexical_cast<unsigned>(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.size() && (isdigit(s[nondigit]) || s[nondigit]=='-'))
++nondigit;
- return Row(SPEED, lexical_cast<int>(s.substr(10, nondigit-10)));
+ return Row(SPEED, lexical_cast<unsigned>(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");
}
Timetable::Loader::Loader(Timetable &tt):
DataFile::ObjectLoader<Timetable>(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)
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));
}