]> git.tdb.fi Git - r2c2.git/commitdiff
Make use of the unified storage class for other parts of Layout
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2013 19:08:14 +0000 (22:08 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2013 19:08:14 +0000 (22:08 +0300)
13 files changed:
source/designer/routebar.cpp
source/designer/routebar.h
source/designer/zonebar.cpp
source/designer/zonebar.h
source/engineer/engineer.cpp
source/engineer/routeselect.cpp
source/libr2c2/block.cpp
source/libr2c2/layout.cpp
source/libr2c2/layout.h
source/libr2c2/route.cpp
source/libr2c2/sensor.cpp
source/libr2c2/zone.cpp
source/network/server.cpp

index 66a55d77c838dbd29a8c2847c8eb48322aa9fb1d..cebd1d76edabeb06d85dc85ce066306f278a9a02 100644 (file)
@@ -35,10 +35,10 @@ Routebar::Routebar(Designer &d):
        btn->set_tooltip("Add selected tracks to current route (A)");
        btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_route));
 
-       designer.get_layout().signal_route_added.connect(sigc::mem_fun(this, &Routebar::route_added));
-       designer.get_layout().signal_route_removed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
+       designer.get_layout().signal_track_chain_added.connect(sigc::mem_fun(this, &Routebar::track_chain_added));
+       designer.get_layout().signal_track_chain_removed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
 
-       const set<Route *> &routes = designer.get_layout().get_routes();
+       const set<Route *> &routes = designer.get_layout().get_all<Route>();
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
                (*i)->signal_name_changed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
 
@@ -50,8 +50,8 @@ void Routebar::route_selected(unsigned index, const string &)
        if(index==drp_routes->get_n_items()-1)
        {
                Layout &rlayout = designer.get_layout();
-               const set<Route *> &routes = rlayout.get_routes();
                Route *route = new Route(rlayout);
+               const set<Route *> &routes = rlayout.get_all<Route>();
                route->set_name(format("Route %d", routes.size()));
                designer.edit_route(route);
 
@@ -64,7 +64,7 @@ void Routebar::route_selected(unsigned index, const string &)
        }
        else
        {
-               const set<Route *> &routes = designer.get_layout().get_routes();
+               const set<Route *> &routes = designer.get_layout().get_all<Route>();
                set<Route *>::const_iterator i = routes.begin();
                advance(i, index);
                designer.edit_route(*i);
@@ -78,16 +78,19 @@ void Routebar::delete_route_clicked()
        delete route;
 }
 
-void Routebar::route_added(Route &r)
+void Routebar::track_chain_added(TrackChain &tc)
 {
-       r.signal_name_changed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
-       update_routes();
+       if(Route *r = dynamic_cast<Route *>(&tc))
+       {
+               r->signal_name_changed.connect(sigc::hide(sigc::mem_fun(this, &Routebar::update_routes)));
+               update_routes();
+       }
 }
 
 void Routebar::update_routes()
 {
        drp_routes->clear();
-       const set<Route *> &routes = designer.get_layout().get_routes();
+       const set<Route *> &routes = designer.get_layout().get_all<Route>();
        int selected = -1;
        unsigned n = 0;
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
index 85af6bd567e79a52c5989702bc38dae70ba1d589..e3698ebc0d08b55ba4c72b8840ab298e5c7e1f07 100644 (file)
@@ -16,7 +16,7 @@ public:
 private:
        void route_selected(unsigned, const std::string &);
        void delete_route_clicked();
-       void route_added(R2C2::Route &);
+       void track_chain_added(R2C2::TrackChain &);
        void update_routes();
 };
 
index 9f9fd0dd66690ae6ed37893df4d6dc1e893cfc78..09f8f0ceab9f19919b7dfc0b53b8165fe5c0cbfe 100644 (file)
@@ -40,20 +40,23 @@ Zonebar::Zonebar(Designer &d):
        btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_zone));
 
        Layout &rlayout = designer.get_layout();
-       rlayout.signal_zone_added.connect(sigc::mem_fun(this, &Zonebar::zone_added));
-       rlayout.signal_zone_removed.connect(sigc::hide(sigc::mem_fun(this, &Zonebar::update_groups)));
+       rlayout.signal_track_chain_added.connect(sigc::mem_fun(this, &Zonebar::track_chain_added));
+       rlayout.signal_track_chain_removed.connect(sigc::hide(sigc::mem_fun(this, &Zonebar::update_groups)));
 
-       const Layout::ZoneSet &zones = rlayout.get_zones();
-       for(Layout::ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
+       const set<Zone *> &zones = rlayout.get_all<Zone>();
+       for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
                (*i)->signal_name_changed.connect(sigc::mem_fun(this, &Zonebar::zone_renamed));
 
        update_groups();
 }
 
-void Zonebar::zone_added(Zone &zone)
+void Zonebar::track_chain_added(TrackChain &chain)
 {
-       zone.signal_name_changed.connect(sigc::mem_fun(this, &Zonebar::zone_renamed));
-       update_groups();
+       if(Zone *zone = dynamic_cast<Zone *>(&chain))
+       {
+               zone->signal_name_changed.connect(sigc::mem_fun(this, &Zonebar::zone_renamed));
+               update_groups();
+       }
 }
 
 void Zonebar::zone_renamed(const string &, const string &, unsigned)
@@ -114,9 +117,9 @@ void Zonebar::delete_zone_clicked()
 
 void Zonebar::update_groups()
 {
-       const Layout::ZoneSet &zones = designer.get_layout().get_zones();
+       const set<Zone *> &zones = designer.get_layout().get_all<Zone>();
        set<string> groups;
-       for(Layout::ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
+       for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
                groups.insert((*i)->get_group());
 
        Zone *cur = designer.get_current_zone();
index 2b48b1cf004d5a029d55da0259e2f455ed52ab5f..8a0b3098ab451c41087f124807aa28007cfc3086 100644 (file)
@@ -17,7 +17,7 @@ public:
        Zonebar(Designer &);
 
 private:
-       void zone_added(R2C2::Zone &);
+       void track_chain_added(R2C2::TrackChain &);
        void zone_renamed(const std::string &, const std::string &, unsigned);
        void group_selected(unsigned, const std::string &);
        void number_selected(unsigned, const std::string &);
index b3c1f5f69960d970b2533dad00adc41f2c10b6de..e92c0ae161674c1b70d967f7fad5746245b5431a 100644 (file)
@@ -73,7 +73,7 @@ Engineer::Engineer(int argc, char **argv):
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
        layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
        layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
-       const set<Block *> &blocks = layout.get_blocks();
+       const set<Block *> &blocks = layout.get_all<Block>();
        for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
                (*i)->get_sensor().signal_state_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &Engineer::reset_block_color), sigc::ref(**i))));
 
index 770a455a4130b7406e22a6f348c38b680b757626..ae3dd8340949304b22c4a08bf3ba313307a153b9 100644 (file)
@@ -27,7 +27,7 @@ RouteSelect::RouteSelect(Engineer &e, Train &t):
        if(TrainRouter *router = train.get_ai_of_type<TrainRouter>())
                current_route = router->get_route();
 
-       const set<Route *> &routes = engineer.get_layout().get_routes();
+       const set<Route *> &routes = engineer.get_layout().get_all<Route>();
        unsigned n = 1;
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
                if(!(*i)->is_temporary())
@@ -43,7 +43,7 @@ void RouteSelect::on_ok_clicked()
 {
        if(drp_route->get_selected_index()>0)
        {
-               const set<Route *> &routes = engineer.get_layout().get_routes();
+               const set<Route *> &routes = engineer.get_layout().get_all<Route>();
                set<Route *>::const_iterator i = routes.begin();
                unsigned n = drp_route->get_selected_index()-1;
                while(i!=routes.end())
index f6818166d22c8996c58199abb45e464c2b54fcbc..2df313e27c1f8c53104c4314908be2391eec7b52 100644 (file)
@@ -45,7 +45,7 @@ Block::Block(Layout &l, Track &start):
 
        sensor = new TrackCircuit(layout, *this);
 
-       layout.add_block(*this);
+       layout.add(*this);
 }
 
 Block::~Block()
@@ -62,7 +62,7 @@ Block::~Block()
                        blk->break_link(*this);
                }
 
-       layout.remove_block(*this);
+       layout.remove(*this);
 
        delete sensor;
 }
index 6db66a62fc99644e2bedf7e85942b67d49e12b5b..efc1e0f74cbc97627de661b40caa4cc09dd6ac8e 100644 (file)
@@ -48,14 +48,11 @@ Layout::~Layout()
 
        while(!trains.empty())
                delete trains.begin()->second;
-       while(!routes.empty())
-               delete *routes.begin();
-       while(!zones.empty())
-               delete *zones.begin();
+       track_chains.del<Route>();
+       track_chains.del<Zone>();
        objects.del<Signal>();
        objects.del<Track>();
-       while(!blocks.empty())
-               delete *blocks.begin();
+       track_chains.del<Block>();
 }
 
 Driver &Layout::get_driver() const
@@ -81,6 +78,30 @@ void Layout::add(Track &t)
        }
 }
 
+void Layout::add(TrackChain &g)
+{
+       if(track_chains.insert(g))
+               signal_track_chain_added.emit(g);
+}
+
+void Layout::add(Block &b)
+{
+       if(track_chains.insert(b))
+       {
+               b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
+               signal_track_chain_added.emit(b);
+       }
+}
+
+void Layout::add(Sensor &s)
+{
+       if(sensors.insert(s))
+       {
+               s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
+               s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
+       }
+}
+
 template<>
 const set<Object *> &Layout::get_all<Object>() const
 {
@@ -105,6 +126,42 @@ const set<Vehicle *> &Layout::get_all<Vehicle>() const
        return objects.get<Vehicle>();
 }
 
+template<>
+const set<TrackChain *> &Layout::get_all<TrackChain>() const
+{
+       return track_chains.get();
+}
+
+template<>
+const set<Block *> &Layout::get_all<Block>() const
+{
+       return track_chains.get<Block>();
+}
+
+template<>
+const set<Route *> &Layout::get_all<Route>() const
+{
+       return track_chains.get<Route>();
+}
+
+template<>
+const set<Zone *> &Layout::get_all<Zone>() const
+{
+       return track_chains.get<Zone>();
+}
+
+template<>
+const set<Sensor *> &Layout::get_all<Sensor>() const
+{
+       return sensors.get<Sensor>();
+}
+
+template<>
+const set<TrackCircuit *> &Layout::get_all<TrackCircuit>() const
+{
+       return sensors.get<TrackCircuit>();
+}
+
 template<typename T>
 T *Layout::pick(const Ray &ray)
 {
@@ -138,6 +195,17 @@ void Layout::remove(Track &t)
        }
 }
 
+void Layout::remove(TrackChain &g)
+{
+       if(track_chains.erase(g))
+               signal_track_chain_removed.emit(g);
+}
+
+void Layout::remove(Sensor &s)
+{
+       sensors.erase(s);
+}
+
 unsigned Layout::allocate_turnout_id()
 {
        set<unsigned> used_ids;
@@ -154,14 +222,9 @@ unsigned Layout::allocate_turnout_id()
        return result;
 }
 
-void Layout::add_block(Block &b)
-{
-       blocks.insert(&b);
-       b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
-}
-
 Block &Layout::get_block(unsigned id) const
 {
+       const set<Block *> &blocks = track_chains.get<Block>();
        for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
                if((*i)->get_id()==id)
                        return **i;
@@ -172,7 +235,8 @@ Block &Layout::get_block(unsigned id) const
 void Layout::create_blocks()
 {
        set<Track *> used_tracks;
-       for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+       const set<Block *> *blocks = &track_chains.get<Block>();
+       for(set<Block *>::const_iterator i=blocks->begin(); i!=blocks->end(); ++i)
        {
                const set<Track *> &btracks = (*i)->get_tracks();
                used_tracks.insert(btracks.begin(), btracks.end());
@@ -186,8 +250,9 @@ void Layout::create_blocks()
                        used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
                }
 
-       for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
+       blocks = &track_chains.get<Block>();
+       for(set<Block *>::iterator i=blocks->begin(); i!=blocks->end(); ++i)
+               for(set<Block *>::iterator j=i; j!=blocks->end(); ++j)
                        if(j!=i)
                                (*i)->check_link(**j);
 }
@@ -211,19 +276,9 @@ void Layout::create_blocks(Track &track)
        create_blocks();
 }
 
-void Layout::remove_block(Block &b)
-{
-       blocks.erase(&b);
-}
-
-void Layout::add_route(Route &r)
-{
-       if(routes.insert(&r).second)
-               signal_route_added.emit(r);
-}
-
 Route &Layout::get_route(const string &name) const
 {
+       const set<Route *> &routes = track_chains.get<Route>();
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
                if((*i)->get_name()==name)
                        return **i;
@@ -232,26 +287,16 @@ Route &Layout::get_route(const string &name) const
 
 void Layout::update_routes()
 {
+       const set<Route *> &routes = track_chains.get<Route>();
        for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
                (*i)->update_turnouts();
 }
 
-void Layout::remove_route(Route &r)
-{
-       if(routes.erase(&r))
-               signal_route_removed.emit(r);
-}
-
-void Layout::add_zone(Zone &z)
-{
-       if(zones.insert(&z).second)
-               signal_zone_added.emit(z);
-}
-
 Layout::ZoneArray Layout::get_zones(const string &group) const
 {
        ZoneArray result;
-       for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
+       const set<Zone *> &zones = track_chains.get<Zone>();
+       for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
                if((*i)->get_group()==group)
                        result.push_back(*i);
 
@@ -262,19 +307,14 @@ Layout::ZoneArray Layout::get_zones(const string &group) const
 
 Zone &Layout::get_zone(const string &group, unsigned num) const
 {
-       for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
+       const set<Zone *> &zones = track_chains.get<Zone>();
+       for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
                if((*i)->get_group()==group && (*i)->get_number()==num)
                        return **i;
 
        throw key_error(format("%s %d", group, num));
 }
 
-void Layout::remove_zone(Zone &z)
-{
-       if(zones.erase(&z))
-               signal_zone_removed.emit(z);
-}
-
 void Layout::add_train(Train &t)
 {
        insert_unique(trains, t.get_address(), &t);
@@ -292,20 +332,6 @@ void Layout::remove_train(Train &t)
                signal_train_removed.emit(t);
 }
 
-void Layout::add_sensor(Sensor &s)
-{
-       if(sensors.insert(&s).second)
-       {
-               s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
-               s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
-       }
-}
-
-void Layout::remove_sensor(Sensor &s)
-{
-       sensors.erase(&s);
-}
-
 void Layout::tick()
 {
        if(driver)
@@ -317,7 +343,7 @@ void Layout::tick()
                dt = t-last_tick;
        last_tick = t;
 
-       for(set<Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
+       for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
                (*i)->tick(dt);
        const set<Signal *> &signals = objects.get<Signal>();
        for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
@@ -360,6 +386,7 @@ void Layout::save(const string &fn) const
                writer.write(st);
        }
 
+       const set<Route *> &routes = track_chains.get<Route>();
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
        {
                if((*i)->is_temporary())
@@ -370,7 +397,8 @@ void Layout::save(const string &fn) const
                writer.write(st);
        }
 
-       for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
+       const set<Zone *> &zones = track_chains.get<Zone>();
+       for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
        {
                DataFile::Statement st("zone");
                (*i)->save(st.sub);
index f9a2ba1bd5f7cf73f06dbd39956c0afa4166cffb..204a60f6d90b9e64718386cbeb384c3f511fa570 100644 (file)
@@ -18,6 +18,7 @@ class Object;
 class Route;
 class Signal;
 class Track;
+class TrackChain;
 class Train;
 class Vehicle;
 class Zone;
@@ -68,15 +69,12 @@ private:
        };
 
 public:
-       typedef std::set<Zone *> ZoneSet;
        typedef std::vector<Zone *> ZoneArray;
 
        sigc::signal<void, Object &> signal_object_added;
        sigc::signal<void, Object &> signal_object_removed;
-       sigc::signal<void, Route &> signal_route_added;
-       sigc::signal<void, Route &> signal_route_removed;
-       sigc::signal<void, Zone &> signal_zone_added;
-       sigc::signal<void, Zone &> signal_zone_removed;
+       sigc::signal<void, TrackChain &> signal_track_chain_added;
+       sigc::signal<void, TrackChain &> signal_track_chain_removed;
        sigc::signal<void, Train &> signal_train_added;
        sigc::signal<void, Train &> signal_train_removed;
        sigc::signal<void, Block &, Train *> signal_block_reserved;
@@ -88,10 +86,8 @@ private:
        Driver *driver;
        std::string base;
        Storage<Object> objects;
-       std::set<Route *> routes;
-       ZoneSet zones;
-       std::set<Block *> blocks;
-       std::set<Sensor *> sensors;
+       Storage<TrackChain> track_chains;
+       Storage<Sensor> sensors;
        std::map<unsigned, Train *> trains;
        Msp::Time::TimeStamp last_tick;
        unsigned next_turnout_id;
@@ -107,6 +103,9 @@ public:
 
        void add(Object &);
        void add(Track &);
+       void add(TrackChain &);
+       void add(Block &);
+       void add(Sensor &);
 
        template<typename T>
        const std::set<T *> &get_all() const;
@@ -116,36 +115,26 @@ public:
 
        void remove(Object &);
        void remove(Track &);
+       void remove(TrackChain &);
+       void remove(Sensor &);
 
        unsigned allocate_turnout_id();
 
-       void add_block(Block &);
        Block &get_block(unsigned) const;
-       const std::set<Block *> &get_blocks() const { return blocks; }
        void create_blocks();
        void create_blocks(Track &);
-       void remove_block(Block &);
 
-       void add_route(Route &);
-       const std::set<Route *> &get_routes() const { return routes; }
        Route &get_route(const std::string &) const;
        void update_routes();
-       void remove_route(Route &);
 
-       void add_zone(Zone &);
-       const ZoneSet &get_zones() const { return zones; }
        ZoneArray get_zones(const std::string &) const;
        Zone &get_zone(const std::string &, unsigned) const;
-       void remove_zone(Zone &);
 
        void add_train(Train &);
        Train &get_train(unsigned) const;
        const std::map<unsigned, Train *> &get_trains() const { return trains; }
        void remove_train(Train &);
 
-       void add_sensor(Sensor &);
-       void remove_sensor(Sensor &);
-
        void tick();
        void emergency(const std::string &);
 
index a01b80b3b183be4a8591568e18ad9deaf4a538d1..ffb420adabfa9cbd7911dad2e525e7a0a0b22afb 100644 (file)
@@ -132,12 +132,12 @@ Route::Route(Layout &l):
        TrackChain(l),
        temporary(false)
 {
-       layout.add_route(*this);
+       layout.add(*this);
 }
 
 Route::~Route()
 {
-       layout.remove_route(*this);
+       layout.remove(*this);
 }
 
 void Route::set_name(const string &n)
index 1b7e8cc10c08881a49a22743684046b64683a9fe..8a65d3dedf20f52419d130bfd1612f5bb3cc2a30 100644 (file)
@@ -14,12 +14,12 @@ Sensor::Sensor(Layout &l):
        if(layout.has_driver())
                layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Sensor::event));
 
-       layout.add_sensor(*this);
+       layout.add(*this);
 }
 
 Sensor::~Sensor()
 {
-       layout.remove_sensor(*this);
+       layout.remove(*this);
 }
 
 void Sensor::tick(const Time::TimeDelta &dt)
index 00edb5799f6d4d9b49f481f7d7ef3f76113666ce..23b0ac9f093f3962507473f6258c082d3bb3245a 100644 (file)
@@ -13,12 +13,12 @@ Zone::Zone(Layout &l):
        TrackChain(l),
        number(0)
 {
-       layout.add_zone(*this);
+       layout.add(*this);
 }
 
 Zone::~Zone()
 {
-       layout.remove_zone(*this);
+       layout.remove(*this);
 }
 
 void Zone::set_name(const string &g, const string &q, unsigned n)
index 5b3d419f2c010cebe4cf10333873759d3c0146d0..951fb9d4be658c4ee480d8a3088749f7fdeca65f 100644 (file)
@@ -160,7 +160,7 @@ void Server::Connection::handshake_done()
                comm.send(pkt);
        }
 
-       const set<Route *> &routes = server.layout.get_routes();
+       const set<Route *> &routes = server.layout.get_all<Route>();
        for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
                if(!(*i)->is_temporary())
                {