]> git.tdb.fi Git - r2c2.git/commitdiff
Add reverse and arrival row types to Timetable
authorMikko Rasa <tdb@tdb.fi>
Sat, 2 Oct 2010 11:47:33 +0000 (11:47 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 2 Oct 2010 11:47:33 +0000 (11:47 +0000)
Change "go to" into "set route to"
More flexible row parameters
Add Row::save method

source/libmarklin/timetable.cpp
source/libmarklin/timetable.h

index bd9f5f0f046d5c24cd5c99eccf05ddd6b8233247..f3d49c48fa1d44f0b38f8efe94d903c9dc664720 100644 (file)
@@ -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<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;
                }
 
@@ -118,29 +121,7 @@ void Timetable::tick(const Time::TimeStamp &t)
 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)
@@ -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<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;
@@ -216,17 +233,21 @@ Timetable::Row Timetable::Row::parse(const string &s)
                        ++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");
 }
@@ -235,17 +256,18 @@ Timetable::Row Timetable::Row::parse(const string &s)
 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)
@@ -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));
 }
index 3abbf83149e87e256bb9852a1c146a49c833c629..72e12d3d9980f5ca1a66732678c88abfd17cf20e 100644 (file)
@@ -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<Msp::Variant> params;
 
-               Row(RowType, int);
-               Row(RowType, const std::string &);
+               Row(RowType);
+
+               template<typename T>
+               Row(RowType, const T &);
+
+               template<typename T>
+               const T &get_param(unsigned) const;
 
                std::string str() const;
 
+               Msp::DataFile::Statement save() const;
+
                static Row parse(const std::string &);
        };