From: Mikko Rasa Date: Mon, 31 Mar 2014 14:27:14 +0000 (+0300) Subject: Add helper class for processing driver options X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2c08ce7d004b20a1cd9bc68fccbf734a6ba2d242;p=r2c2.git Add helper class for processing driver options --- diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index eb97d49..ecac7b8 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -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(), "ttyUSB0")), debug(1), state_file("arducontrol.state"), power(false), diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index a8bdfa7..c1e7256 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -369,7 +369,7 @@ private: static ProtocolInfo protocol_info[2]; public: - ArduControl(const std::string &); + ArduControl(const Options &); ~ArduControl(); virtual void set_power(bool); diff --git a/source/libr2c2/centralstation.cpp b/source/libr2c2/centralstation.cpp index 124c1be..fc7855b 100644 --- a/source/libr2c2/centralstation.cpp +++ b/source/libr2c2/centralstation.cpp @@ -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 addr = Net::resolve(host+":15471"); + RefPtr addr = Net::resolve(opts.get(string())+":15471"); socket.connect(*addr); IO::print("Connected to central station at %s\n", addr->str()); diff --git a/source/libr2c2/centralstation.h b/source/libr2c2/centralstation.h index 91bef1a..f23fa66 100644 --- a/source/libr2c2/centralstation.h +++ b/source/libr2c2/centralstation.h @@ -126,7 +126,7 @@ private: bool sensors_synced; public: - CentralStation(const std::string &); + CentralStation(const Options &); ~CentralStation(); virtual void set_power(bool); diff --git a/source/libr2c2/driver.cpp b/source/libr2c2/driver.cpp index 04df5f4..6fbef93 100644 --- a/source/libr2c2/driver.cpp +++ b/source/libr2c2/driver.cpp @@ -1,3 +1,4 @@ +#include #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 parts = split(optstr, ':'); + for(vector::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 diff --git a/source/libr2c2/driver.h b/source/libr2c2/driver.h index 7c762d0..1222540 100644 --- a/source/libr2c2/driver.h +++ b/source/libr2c2/driver.h @@ -1,8 +1,10 @@ #ifndef LIBR2C2_DRIVER_H_ #define LIBR2C2_DRIVER_H_ +#include #include #include +#include namespace R2C2 { @@ -13,6 +15,29 @@ class VehicleType; class Driver { public: + class Options + { + private: + typedef std::map OptionMap; + + OptionMap opts; + + public: + Options() { } + Options(const std::string &); + + void set(const std::string &, const std::string &); + + template + 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(i->second); + } + }; + struct DetectedLocomotive { std::string protocol; diff --git a/source/libr2c2/dummy.cpp b/source/libr2c2/dummy.cpp index 9ccb1bc..cfee074 100644 --- a/source/libr2c2/dummy.cpp +++ b/source/libr2c2/dummy.cpp @@ -7,22 +7,10 @@ using namespace Msp; namespace R2C2 { -Dummy::Dummy(const string ¶ms): - power(true) -{ - vector opts = split(params, ':'); - for(vector::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(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) { diff --git a/source/libr2c2/dummy.h b/source/libr2c2/dummy.h index 77a07a4..6943413 100644 --- a/source/libr2c2/dummy.h +++ b/source/libr2c2/dummy.h @@ -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; } diff --git a/source/libr2c2/intellibox.cpp b/source/libr2c2/intellibox.cpp index 975c143..4c8f2ac 100644 --- a/source/libr2c2/intellibox.cpp +++ b/source/libr2c2/intellibox.cpp @@ -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(), "ttyUSB0")), power(false), halted(false), update_sensors(false), diff --git a/source/libr2c2/intellibox.h b/source/libr2c2/intellibox.h index 3e29859..df379a3 100644 --- a/source/libr2c2/intellibox.h +++ b/source/libr2c2/intellibox.h @@ -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; }