From: Mikko Rasa Date: Fri, 7 Jun 2013 19:08:14 +0000 (+0300) Subject: Make use of the unified storage class for other parts of Layout X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=eb9c7f5200da4f3f31b41233be8cee397cb6abb9;p=r2c2.git Make use of the unified storage class for other parts of Layout --- diff --git a/source/designer/routebar.cpp b/source/designer/routebar.cpp index 66a55d7..cebd1d7 100644 --- a/source/designer/routebar.cpp +++ b/source/designer/routebar.cpp @@ -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 &routes = designer.get_layout().get_routes(); + const set &routes = designer.get_layout().get_all(); for(set::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 &routes = rlayout.get_routes(); Route *route = new Route(rlayout); + const set &routes = rlayout.get_all(); 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 &routes = designer.get_layout().get_routes(); + const set &routes = designer.get_layout().get_all(); set::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(&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 &routes = designer.get_layout().get_routes(); + const set &routes = designer.get_layout().get_all(); int selected = -1; unsigned n = 0; for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n) diff --git a/source/designer/routebar.h b/source/designer/routebar.h index 85af6bd..e3698eb 100644 --- a/source/designer/routebar.h +++ b/source/designer/routebar.h @@ -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(); }; diff --git a/source/designer/zonebar.cpp b/source/designer/zonebar.cpp index 9f9fd0d..09f8f0c 100644 --- a/source/designer/zonebar.cpp +++ b/source/designer/zonebar.cpp @@ -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 &zones = rlayout.get_all(); + for(set::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(&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 &zones = designer.get_layout().get_all(); set groups; - for(Layout::ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i) + for(set::const_iterator i=zones.begin(); i!=zones.end(); ++i) groups.insert((*i)->get_group()); Zone *cur = designer.get_current_zone(); diff --git a/source/designer/zonebar.h b/source/designer/zonebar.h index 2b48b1c..8a0b309 100644 --- a/source/designer/zonebar.h +++ b/source/designer/zonebar.h @@ -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 &); diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index b3c1f5f..e92c0ae 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -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 &blocks = layout.get_blocks(); + const set &blocks = layout.get_all(); for(set::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)))); diff --git a/source/engineer/routeselect.cpp b/source/engineer/routeselect.cpp index 770a455..ae3dd83 100644 --- a/source/engineer/routeselect.cpp +++ b/source/engineer/routeselect.cpp @@ -27,7 +27,7 @@ RouteSelect::RouteSelect(Engineer &e, Train &t): if(TrainRouter *router = train.get_ai_of_type()) current_route = router->get_route(); - const set &routes = engineer.get_layout().get_routes(); + const set &routes = engineer.get_layout().get_all(); unsigned n = 1; for(set::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 &routes = engineer.get_layout().get_routes(); + const set &routes = engineer.get_layout().get_all(); set::const_iterator i = routes.begin(); unsigned n = drp_route->get_selected_index()-1; while(i!=routes.end()) diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index f681816..2df313e 100644 --- a/source/libr2c2/block.cpp +++ b/source/libr2c2/block.cpp @@ -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; } diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index 6db66a6..efc1e0f 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -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(); + track_chains.del(); objects.del(); objects.del(); - while(!blocks.empty()) - delete *blocks.begin(); + track_chains.del(); } 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 &Layout::get_all() const { @@ -105,6 +126,42 @@ const set &Layout::get_all() const return objects.get(); } +template<> +const set &Layout::get_all() const +{ + return track_chains.get(); +} + +template<> +const set &Layout::get_all() const +{ + return track_chains.get(); +} + +template<> +const set &Layout::get_all() const +{ + return track_chains.get(); +} + +template<> +const set &Layout::get_all() const +{ + return track_chains.get(); +} + +template<> +const set &Layout::get_all() const +{ + return sensors.get(); +} + +template<> +const set &Layout::get_all() const +{ + return sensors.get(); +} + template 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 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 &blocks = track_chains.get(); for(set::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 used_tracks; - for(set::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) + const set *blocks = &track_chains.get(); + for(set::const_iterator i=blocks->begin(); i!=blocks->end(); ++i) { const set &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::iterator i=blocks.begin(); i!=blocks.end(); ++i) - for(set::iterator j=i; j!=blocks.end(); ++j) + blocks = &track_chains.get(); + for(set::iterator i=blocks->begin(); i!=blocks->end(); ++i) + for(set::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 &routes = track_chains.get(); for(set::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 &routes = track_chains.get(); for(set::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 &zones = track_chains.get(); + for(set::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 &zones = track_chains.get(); + for(set::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::iterator i=sensors.begin(); i!=sensors.end(); ++i) + for(set::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i) (*i)->tick(dt); const set &signals = objects.get(); for(set::iterator i=signals.begin(); i!=signals.end(); ++i) @@ -360,6 +386,7 @@ void Layout::save(const string &fn) const writer.write(st); } + const set &routes = track_chains.get(); for(set::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 &zones = track_chains.get(); + for(set::const_iterator i=zones.begin(); i!=zones.end(); ++i) { DataFile::Statement st("zone"); (*i)->save(st.sub); diff --git a/source/libr2c2/layout.h b/source/libr2c2/layout.h index f9a2ba1..204a60f 100644 --- a/source/libr2c2/layout.h +++ b/source/libr2c2/layout.h @@ -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 ZoneSet; typedef std::vector ZoneArray; sigc::signal signal_object_added; sigc::signal signal_object_removed; - sigc::signal signal_route_added; - sigc::signal signal_route_removed; - sigc::signal signal_zone_added; - sigc::signal signal_zone_removed; + sigc::signal signal_track_chain_added; + sigc::signal signal_track_chain_removed; sigc::signal signal_train_added; sigc::signal signal_train_removed; sigc::signal signal_block_reserved; @@ -88,10 +86,8 @@ private: Driver *driver; std::string base; Storage objects; - std::set routes; - ZoneSet zones; - std::set blocks; - std::set sensors; + Storage track_chains; + Storage sensors; std::map 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 const std::set &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 &get_blocks() const { return blocks; } void create_blocks(); void create_blocks(Track &); - void remove_block(Block &); - void add_route(Route &); - const std::set &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 &get_trains() const { return trains; } void remove_train(Train &); - void add_sensor(Sensor &); - void remove_sensor(Sensor &); - void tick(); void emergency(const std::string &); diff --git a/source/libr2c2/route.cpp b/source/libr2c2/route.cpp index a01b80b..ffb420a 100644 --- a/source/libr2c2/route.cpp +++ b/source/libr2c2/route.cpp @@ -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) diff --git a/source/libr2c2/sensor.cpp b/source/libr2c2/sensor.cpp index 1b7e8cc..8a65d3d 100644 --- a/source/libr2c2/sensor.cpp +++ b/source/libr2c2/sensor.cpp @@ -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) diff --git a/source/libr2c2/zone.cpp b/source/libr2c2/zone.cpp index 00edb57..23b0ac9 100644 --- a/source/libr2c2/zone.cpp +++ b/source/libr2c2/zone.cpp @@ -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) diff --git a/source/network/server.cpp b/source/network/server.cpp index 5b3d419..951fb9d 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -160,7 +160,7 @@ void Server::Connection::handshake_done() comm.send(pkt); } - const set &routes = server.layout.get_routes(); + const set &routes = server.layout.get_all(); for(set::const_iterator i=routes.begin(); i!=routes.end(); ++i) if(!(*i)->is_temporary()) {