]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/controller.h
Don't crash if a train has no router
[r2c2.git] / source / libr2c2 / controller.h
1 #ifndef LIBR2C2_CONTROLLER_H_
2 #define LIBR2C2_CONTROLLER_H_
3
4 #include <string>
5 #include <sigc++/signal.h>
6 #include <msp/time/timedelta.h>
7
8 namespace R2C2 {
9
10 /**
11 Interface class for train controllers.  Takes input through a uniform named
12 control interface.  Provides information about train movement on output.
13 */
14 class Controller
15 {
16 public:
17         struct Control
18         {
19                 enum Type
20                 {
21                         BINARY,
22                         DISCRETE,
23                         CONTINUOUS
24                 };
25
26                 std::string name;
27                 Type type;
28                 float min_value;
29                 float max_value;
30                 float step;
31                 float value;
32
33         private:
34                 Control() { }
35
36         public:
37                 void set(float);
38
39                 static Control binary(const std::string &);
40                 static Control discrete(const std::string &, float, float, float);
41                 static Control continuous(const std::string &, float, float);
42         };
43
44         sigc::signal<void, const Control &> signal_control_changed;
45
46 protected:
47         Controller() { }
48 public:
49         virtual ~Controller() { }
50
51         virtual const char *enumerate_controls(unsigned) const = 0;
52         virtual void set_control(const std::string &, float) = 0;
53         virtual const Control &get_control(const std::string &) const = 0;
54
55         /** Returns the current speed.  Always non-negative. */
56         virtual float get_speed() const = 0;
57
58         /** Returns true if traveling in reverse. */
59         virtual bool get_reverse() const = 0;
60
61         /** Determines the distance required to come to a full stop. */
62         virtual float get_braking_distance() const = 0;
63
64         virtual void tick(const Msp::Time::TimeDelta &) = 0;
65 };
66
67 } // namespace R2C2
68
69 #endif