From: Mikko Rasa Date: Fri, 22 Oct 2010 21:22:41 +0000 (+0000) Subject: Add Block::has_track and Route::has_track methods X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2029c5e4220e0809a39744a28ca9e2ff22e8ad28;p=r2c2.git Add Block::has_track and Route::has_track methods --- diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index e560833..5ea589b 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -609,7 +609,7 @@ void Designer::show_route(const Route *route) for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) { Track3D &t3d = layout_3d->get_track(**i); - if(route && route->get_tracks().count(*i)) + if(route && route->has_track(**i)) { t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0)); if((*i)->get_type().is_turnout()) diff --git a/source/engineer/trainpanel.cpp b/source/engineer/trainpanel.cpp index 922e374..c1fc1d9 100644 --- a/source/engineer/trainpanel.cpp +++ b/source/engineer/trainpanel.cpp @@ -282,7 +282,7 @@ void TrainPanel::place(Track *track, unsigned ep) Track *next = track->get_links()[ep]; ep = next->traverse(next->get_endpoint_by_link(*track), 0); track = next; - if(!block.get_tracks().count(track)) + if(!block.has_track(*track)) break; } } diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index e5dbb6d..780ee1b 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -72,6 +72,11 @@ Block::~Block() layout.remove_block(*this); } +bool Block::has_track(Track &t) const +{ + return tracks.count(&t); +} + int Block::get_endpoint_by_link(Block &other) const { for(unsigned i=0; i &get_tracks() const { return tracks; } + bool has_track(Track &) const; const std::vector &get_endpoints() const { return endpoints; } int get_endpoint_by_link(Block &) const; unsigned traverse(unsigned, float * =0) const; diff --git a/source/libmarklin/layout.cpp b/source/libmarklin/layout.cpp index f89b783..f8ad397 100644 --- a/source/libmarklin/layout.cpp +++ b/source/libmarklin/layout.cpp @@ -98,7 +98,7 @@ Block &Layout::get_block(unsigned id) const Block &Layout::get_block_by_track(Track &t) const { for(set::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) - if((*i)->get_tracks().count(&t)) + if((*i)->has_track(t)) return **i; throw InvalidParameterValue("No block found for track"); @@ -131,9 +131,9 @@ void Layout::create_blocks(Track &track) const vector &links = track.get_links(); for(set::iterator i=blocks.begin(); i!=blocks.end();) { - bool del = (*i)->get_tracks().count(&track); + bool del = (*i)->has_track(track); for(vector::const_iterator j=links.begin(); (!del && j!=links.end()); ++j) - del = (*i)->get_tracks().count(*j); + del = (*i)->has_track(**j); if(del) delete *i++; diff --git a/source/libmarklin/route.cpp b/source/libmarklin/route.cpp index 926dd28..d4fcf80 100644 --- a/source/libmarklin/route.cpp +++ b/source/libmarklin/route.cpp @@ -316,6 +316,11 @@ void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts) } } +bool Route::has_track(Track &t) const +{ + return tracks.count(&t); +} + void Route::save(list &st) const { st.push_back((DataFile::Statement("name"), name)); diff --git a/source/libmarklin/route.h b/source/libmarklin/route.h index 901383a..75241d8 100644 --- a/source/libmarklin/route.h +++ b/source/libmarklin/route.h @@ -61,6 +61,7 @@ public: void add_tracks(const std::set &); void add_track_chain(Track &, unsigned, const TurnoutMap &); const std::set &get_tracks() const { return tracks; } + bool has_track(Track &) const; void save(std::list &) const; private: unsigned check_validity(Track &) const; diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 6b6b89e..5bc50d8 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -219,13 +219,13 @@ void Train::set_route(const Route *r) BlockRef next = last.next(); const Block::Endpoint &first_ep = first.block->get_endpoints()[first.entry]; const Block::Endpoint &next_ep = next.block->get_endpoints()[next.entry]; - if(!r->get_tracks().count(next_ep.track)) + if(!r->has_track(*next_ep.track)) { Route *lead = Route::find(*next_ep.track, next_ep.track_ep, *r); create_lead_route(lead, lead); routes.push_front(lead); } - else if(!r->get_tracks().count(first_ep.track)) + else if(!r->has_track(*first_ep.track)) routes.push_front(create_lead_route(0, r)); } @@ -237,7 +237,7 @@ void Train::set_route(const Route *r) void Train::go_to(Track &to) { for(list::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) - if(i->block->get_tracks().count(&to)) + if(i->block->has_track(to)) { signal_arrived.emit(); set_route(0); @@ -348,12 +348,12 @@ bool Train::divert(Track &from) Track *next = track->get_link(track->traverse(ep, path)); for(list::iterator i=route; (end==routes.end() && i!=routes.end()); ++i) - if(i->route->get_tracks().count(next)) + if(i->route->has_track(*next)) end = i; if(end!=routes.end()) break; - else if(!diversion->get_tracks().count(next)) + else if(!diversion->has_track(*next)) throw Exception("Pathfinder returned a bad route"); ep = next->get_endpoint_by_link(*track); @@ -472,7 +472,7 @@ void Train::free_noncritical_blocks() Track *track = veh.get_track(); list::iterator block = cur_blocks.begin(); bool in_rsv = false; - while(block!=rsv_blocks.end() && !block->block->get_tracks().count(track)) + while(block!=rsv_blocks.end() && !block->block->has_track(*track)) { ++block; if(block==cur_blocks.end()) @@ -497,7 +497,7 @@ void Train::free_noncritical_blocks() entry = next->get_endpoint_by_link(*track); track = next; - if(!block->block->get_tracks().count(track)) + if(!block->block->has_track(*track)) { ++block; if(block==cur_blocks.end()) @@ -588,7 +588,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) bool ok = false; for(list::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i) - ok = i->block->get_tracks().count(track); + ok = i->block->has_track(*track); float d = get_real_speed(current_speed)*(dt/Time::sec); if(ok) @@ -793,7 +793,7 @@ void Train::sensor_event(unsigned addr, bool state) list::iterator end = cur_blocks.begin(); for(list::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) { - if(i->block->get_tracks().count(veh.get_track())) + if(i->block->has_track(*veh.get_track())) break; if(i->block->get_sensor_id()) { @@ -925,7 +925,7 @@ unsigned Train::reserve_more() break; } } - else if(!routes.empty() && routes.front().route->get_tracks().count(entry_ep.track)) + else if(!routes.empty() && routes.front().route->has_track(*entry_ep.track)) cur_route = routes.begin(); if(link->get_endpoints().size()<2) @@ -1117,7 +1117,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co return 0; list::const_iterator block = cur_blocks.begin(); - while(block!=rsv_blocks.end() && !block->block->get_tracks().count(track)) + while(block!=rsv_blocks.end() && !block->block->has_track(*track)) { ++block; if(block==cur_blocks.end()) @@ -1146,7 +1146,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co Track *next = track->get_link(track->traverse(entry)); - if(!block->block->get_tracks().count(next)) + if(!block->block->has_track(*next)) { if(back) { @@ -1276,14 +1276,14 @@ void Train::reverse_blocks(list &blocks) const bool Train::advance_route(list::iterator &iter, Track &track) { - while(iter!=routes.end() && !iter->route->get_tracks().count(&track)) + while(iter!=routes.end() && !iter->route->has_track(track)) ++iter; if(iter==routes.end()) return false; list::iterator next = iter; ++next; - if(next!=routes.end() && next->diversion && next->route->get_tracks().count(&track)) + if(next!=routes.end() && next->diversion && next->route->has_track(track)) iter = next; return true; @@ -1303,7 +1303,7 @@ Route *Train::create_lead_route(Route *lead, const Route *target) { const set &btracks = i->block->get_tracks(); for(set::const_iterator j=btracks.begin(); j!=btracks.end(); ++j) - if(!target || !target->get_tracks().count(*j)) + if(!target || !target->has_track(**j)) tracks.insert(*j); if(++i==cur_blocks.end()) @@ -1320,7 +1320,7 @@ bool Train::is_valid_diversion(const Route &diversion, Track &from, unsigned fro float diversion_len = 0; Track *track = &from; unsigned ep = from_ep; - while(diversion.get_tracks().count(track)) + while(diversion.has_track(*track)) { unsigned path = 0; if(track->get_turnout_id()) @@ -1350,7 +1350,7 @@ bool Train::is_valid_diversion(const Route &diversion, Track &from, unsigned fro path = route->route->get_turnout(track->get_turnout_id()); route_len += track->get_type().get_path_length(path); - if(track!=&from && diversion.get_tracks().count(track)) + if(track!=&from && diversion.has_track(*track)) break; if(visited.count(track))