]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/driver.h
Don't diverge from critical blocks
[r2c2.git] / source / libr2c2 / driver.h
1 #ifndef LIBR2C2_DRIVER_H_
2 #define LIBR2C2_DRIVER_H_
3
4 #include <map>
5 #include <string>
6 #include <sigc++/signal.h>
7 #include <msp/strings/lexicalcast.h>
8
9 namespace R2C2 {
10
11 class SignalType;
12 class TrackType;
13 class VehicleType;
14
15 class Driver
16 {
17 public:
18         class Options
19         {
20         private:
21                 typedef std::map<std::string, std::string> OptionMap;
22
23                 OptionMap opts;
24
25         public:
26                 Options() { }
27                 Options(const std::string &);
28
29                 void set(const std::string &, const std::string &);
30
31                 template<typename T>
32                 T get(const std::string &k, const T &d = T()) const
33                 {
34                         OptionMap::const_iterator i = opts.find(k);
35                         if(i==opts.end())
36                                 return d;
37                         return Msp::lexical_cast<T>(i->second);
38                 }
39         };
40
41         struct DetectedLocomotive
42         {
43                 std::string protocol;
44                 unsigned address;
45                 std::string name;
46         };
47
48         sigc::signal<void, bool> signal_power;
49         sigc::signal<void, bool> signal_halt;
50         sigc::signal<void, const DetectedLocomotive &> signal_locomotive_detected;
51         sigc::signal<void, const DetectedLocomotive &> signal_locomotive_gone;
52         sigc::signal<void, unsigned, unsigned, bool> signal_loco_speed;
53         sigc::signal<void, unsigned, unsigned, bool> signal_loco_function;
54         sigc::signal<void, unsigned, unsigned> signal_turnout;
55         sigc::signal<void, unsigned> signal_turnout_failed;
56         sigc::signal<void, unsigned, unsigned> signal_signal;
57         sigc::signal<void, unsigned, bool> signal_sensor;
58
59 protected:
60         Driver() { }
61 public:
62         virtual ~Driver() { }
63
64         virtual void set_power(bool) = 0;
65         virtual bool get_power() const = 0;
66         virtual void halt(bool) = 0;
67         virtual bool is_halted() const = 0;
68
69         virtual const char *enumerate_protocols(unsigned) const = 0;
70         virtual unsigned get_protocol_speed_steps(const std::string &) const = 0;
71
72         virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const = 0;
73         virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &) = 0;
74         virtual void remove_loco(unsigned) = 0;
75         virtual void set_loco_speed(unsigned, unsigned) = 0;
76         virtual void set_loco_reverse(unsigned, bool) = 0;
77         virtual void set_loco_function(unsigned, unsigned, bool) = 0;
78
79         virtual unsigned add_turnout(unsigned, const TrackType &) = 0;
80         virtual void remove_turnout(unsigned) = 0;
81         virtual void set_turnout(unsigned, unsigned) = 0;
82         virtual unsigned get_turnout(unsigned) const = 0;
83
84         virtual unsigned add_signal(unsigned, const SignalType &) = 0;
85         virtual void remove_signal(unsigned) = 0;
86         virtual void set_signal(unsigned, unsigned) = 0;
87         virtual unsigned get_signal(unsigned) const = 0;
88
89         virtual unsigned add_sensor(unsigned) = 0;
90         virtual void remove_sensor(unsigned) = 0;
91         virtual void set_sensor(unsigned, bool) = 0;
92         virtual bool get_sensor(unsigned) const = 0;
93
94         virtual void tick() = 0;
95         virtual void flush() = 0;
96
97         static Driver *create(const std::string &);
98 };
99
100 } // namespace R2C2
101
102 #endif