]> git.tdb.fi Git - r2c2.git/commitdiff
Add helper class for processing driver options
authorMikko Rasa <tdb@tdb.fi>
Mon, 31 Mar 2014 14:27:14 +0000 (17:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 31 Mar 2014 14:27:14 +0000 (17:27 +0300)
source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h
source/libr2c2/centralstation.cpp
source/libr2c2/centralstation.h
source/libr2c2/driver.cpp
source/libr2c2/driver.h
source/libr2c2/dummy.cpp
source/libr2c2/dummy.h
source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h

index eb97d4914e1172a427542516832a20f7b4904a5e..ecac7b81e69fb14b6faa435d9c3753f538d7053b 100644 (file)
@@ -18,8 +18,8 @@ ArduControl::ProtocolInfo ArduControl::protocol_info[2] =
        { 0x3FFF, 126, 15 }  // MFX
 };
 
-ArduControl::ArduControl(const string &dev):
-       serial(dev),
+ArduControl::ArduControl(const Options &opts):
+       serial(opts.get<string>(string(), "ttyUSB0")),
        debug(1),
        state_file("arducontrol.state"),
        power(false),
index a8bdfa7a7ba1b018d3798c9ff3d50f25f8d640ff..c1e7256ee3f00674b113c4b4e429a77f0700ec79 100644 (file)
@@ -369,7 +369,7 @@ private:
        static ProtocolInfo protocol_info[2];
 
 public:
-       ArduControl(const std::string &);
+       ArduControl(const Options &);
        ~ArduControl();
 
        virtual void set_power(bool);
index 124c1beed8aa14efba0fc96bc59a8ab17ca7a709..fc7855b0726fc9fbcf8183d45e3b05ddd57d35ad 100644 (file)
@@ -14,7 +14,7 @@ using namespace Msp;
 
 namespace R2C2 {
 
-CentralStation::CentralStation(const string &host):
+CentralStation::CentralStation(const Options &opts):
        socket(Net::INET),
        pending_commands(0),
        power(false),
@@ -23,7 +23,7 @@ CentralStation::CentralStation(const string &host):
        accessories_synced(false),
        sensors_synced(false)
 {
-       RefPtr<Net::SockAddr> addr = Net::resolve(host+":15471");
+       RefPtr<Net::SockAddr> addr = Net::resolve(opts.get<string>(string())+":15471");
        socket.connect(*addr);
 
        IO::print("Connected to central station at %s\n", addr->str());
index 91bef1a3e1ab830966a3ff137a8965b325bed701..f23fa66806cb809e7be4de1edc934811a227fabe 100644 (file)
@@ -126,7 +126,7 @@ private:
        bool sensors_synced;
 
 public:
-       CentralStation(const std::string &);
+       CentralStation(const Options &);
        ~CentralStation();
 
        virtual void set_power(bool);
index 04df5f4e82465f9c8d57ab28b0e24ec222e122bb..6fbef93e1f13be64a90afcf3fb5a0037d11fe04a 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/strings/utils.h>
 #include "arducontrol.h"
 #include "centralstation.h"
 #include "driver.h"
@@ -5,6 +6,7 @@
 #include "intellibox.h"
 
 using namespace std;
+using namespace Msp;
 
 namespace R2C2 {
 
@@ -29,4 +31,26 @@ Driver *Driver::create(const string &str)
        throw invalid_argument("Driver::create");
 }
 
+
+Driver::Options::Options(const string &optstr)
+{
+       vector<string> parts = split(optstr, ':');
+       for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+       {
+               string::size_type equals = i->find('=');
+               if(equals==0)
+                       throw invalid_argument("Driver::Options::Options");
+
+               if(equals==string::npos)
+                       set(string(), *i);
+               else
+                       set(i->substr(0, equals), i->substr(equals+1));
+       }
+}
+
+void Driver::Options::set(const string &key, const string &value)
+{
+       opts[key] = value;
+}
+
 } // namespace R2C2
index 7c762d0066b4b9b668030edb0e53dec09d0ebf50..1222540c0b81e19bff985870c730124d2ea16b2f 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef LIBR2C2_DRIVER_H_
 #define LIBR2C2_DRIVER_H_
 
+#include <map>
 #include <string>
 #include <sigc++/signal.h>
+#include <msp/strings/lexicalcast.h>
 
 namespace R2C2 {
 
@@ -13,6 +15,29 @@ class VehicleType;
 class Driver
 {
 public:
+       class Options
+       {
+       private:
+               typedef std::map<std::string, std::string> OptionMap;
+
+               OptionMap opts;
+
+       public:
+               Options() { }
+               Options(const std::string &);
+
+               void set(const std::string &, const std::string &);
+
+               template<typename T>
+               T get(const std::string &k, const T &d = T()) const
+               {
+                       OptionMap::const_iterator i = opts.find(k);
+                       if(i==opts.end())
+                               return d;
+                       return Msp::lexical_cast<T>(i->second);
+               }
+       };
+
        struct DetectedLocomotive
        {
                std::string protocol;
index 9ccb1bcceb94f6fe9630b8ad04e328a5feffcec8..cfee07417b3105b75c2eba5a1029f3056ead320a 100644 (file)
@@ -7,22 +7,10 @@ using namespace Msp;
 
 namespace R2C2 {
 
-Dummy::Dummy(const string &params):
-       power(true)
-{
-       vector<string> opts = split(params, ':');
-       for(vector<string>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
-       {
-               string::size_type equals = i->find('=');
-               if(equals!=string::npos)
-               {
-                       string name = i->substr(0, equals);
-                       string value = i->substr(equals+1);
-                       if(name=="turnout_delay")
-                               turnout_delay = lexical_cast<unsigned>(value)*Time::msec;
-               }
-       }
-}
+Dummy::Dummy(const Options &opts):
+       power(true),
+       turnout_delay(opts.get("turnout_delay", 0U)*Time::sec)
+{ }
 
 void Dummy::set_power(bool p)
 {
index 77a07a4bc4e7895bbfd1b996141a5db8124c349a..6943413a8142d5500136ce259afd89bc2ba8ced9 100644 (file)
@@ -31,7 +31,7 @@ private:
        Msp::Time::TimeDelta turnout_delay;
 
 public:
-       Dummy(const std::string &);
+       Dummy(const Options &);
 
        virtual void set_power(bool);
        virtual bool get_power() const { return power; }
index 975c143fe46baf904d00db9b19a3777c68d627a1..4c8f2ac783b1e6fbf73bdff9aa73d2f2b1a73e2c 100644 (file)
@@ -13,8 +13,8 @@ using namespace Msp;
 
 namespace R2C2 {
 
-Intellibox::Intellibox(const string &dev):
-       serial(dev),
+Intellibox::Intellibox(const Options &opts):
+       serial(opts.get<string>(string(), "ttyUSB0")),
        power(false),
        halted(false),
        update_sensors(false),
index 3e298592ead1c0ee8c61b2e755f86c25324e3851..df379a380518069ba0351ebdc06604c686f50556 100644 (file)
@@ -127,7 +127,7 @@ private:
        Msp::Time::TimeStamp next_event_query;
 
 public:
-       Intellibox(const std::string &);
+       Intellibox(const Options &);
 
        virtual void set_power(bool);
        virtual bool get_power() const { return power; }