From 9def58c4bbec8788430740a1e9afce64a496231f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 4 Nov 2010 20:36:00 +0000 Subject: [PATCH] Add some shortcut functions for getting endpoints --- source/engineer/engineer.cpp | 4 ++-- source/libmarklin/block.cpp | 10 +++++++++- source/libmarklin/block.h | 1 + source/libmarklin/blockiter.cpp | 10 +++++++++- source/libmarklin/blockiter.h | 1 + source/libmarklin/route.cpp | 4 ++-- source/libmarklin/trackiter.cpp | 8 ++++++++ source/libmarklin/trackiter.h | 2 ++ source/libmarklin/tracktype.cpp | 8 ++++++++ source/libmarklin/tracktype.h | 1 + source/libmarklin/train.cpp | 14 +++++++------- 11 files changed, 50 insertions(+), 13 deletions(-) diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 68b85aa..e7b047d 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -219,7 +219,7 @@ void Engineer::tick() delete picking_path; picking_path = new Path3D(*track); if(picking_entry>=0) - picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths); + picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths); else picking_path->set_mask(picking_track->get_type().get_paths()); picking_path->set_color(GL::Color(0)); @@ -271,7 +271,7 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned) else if(btn==3 && picking_entry>=0) { picking_entry = (picking_entry+1)%picking_track->get_type().get_endpoints().size(); - picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths); + picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths); } } else diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index 1519401..e8e367f 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -84,6 +84,14 @@ bool Block::has_track(Track &t) const return tracks.count(&t); } +const Block::Endpoint &Block::get_endpoint(unsigned i) const +{ + if(i>=endpoints.size()) + throw InvalidParameterValue("Endpoint index out of range"); + + return endpoints[i]; +} + int Block::get_endpoint_by_link(Block &other) const { for(unsigned i=0; iget_type().get_endpoints()[track.entry()].paths; + unsigned mask = track.endpoint().paths; for(unsigned i=0; mask>>i; ++i) if(mask&(1< &get_tracks() const { return tracks; } bool has_track(Track &) const; const std::vector &get_endpoints() const { return endpoints; } + const Endpoint &get_endpoint(unsigned) const; int get_endpoint_by_link(Block &) const; float get_path_length(unsigned, const Route * = 0) const; void check_link(Block &); diff --git a/source/libmarklin/blockiter.cpp b/source/libmarklin/blockiter.cpp index 9615957..245f963 100644 --- a/source/libmarklin/blockiter.cpp +++ b/source/libmarklin/blockiter.cpp @@ -34,10 +34,18 @@ TrackIter BlockIter::track_iter() const if(!_block) return TrackIter(); - const Block::Endpoint &ep = _block->get_endpoints()[_entry]; + const Block::Endpoint &ep = _block->get_endpoint(_entry); return TrackIter(ep.track, ep.track_ep); } +const Block::Endpoint &BlockIter::endpoint() const +{ + if(!_block) + throw InvalidState("BlockIter is null"); + + return _block->get_endpoint(_entry); +} + int BlockIter::get_exit(const Route *route) const { const vector &eps = _block->get_endpoints(); diff --git a/source/libmarklin/blockiter.h b/source/libmarklin/blockiter.h index 2b8256f..c86a4e5 100644 --- a/source/libmarklin/blockiter.h +++ b/source/libmarklin/blockiter.h @@ -30,6 +30,7 @@ public: Block *block() const { return _block; } unsigned entry() const { return _entry; } TrackIter track_iter() const; + const Block::Endpoint &endpoint() const; private: int get_exit(const Route *) const; diff --git a/source/libmarklin/route.cpp b/source/libmarklin/route.cpp index 663bd88..befaa6d 100644 --- a/source/libmarklin/route.cpp +++ b/source/libmarklin/route.cpp @@ -87,7 +87,7 @@ list dijkstra(const TrackIter &from, const Pred &goal) break; } - unsigned paths = lowest.track->get_type().get_endpoints()[lowest.track.entry()].paths; + unsigned paths = lowest.track.endpoint().paths; for(unsigned i=0; paths>>i; ++i) if(paths&(1<get_turnout_id()) { - const TrackType::Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)]; + const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_endpoint_by_link(**i)); int p = get_turnout(tid2); if(p>=0 && !(ep.paths&(1<get_type().get_endpoint(_entry); +} + int TrackIter::get_exit(unsigned path) const { const vector &eps = _track->get_type().get_endpoints(); diff --git a/source/libmarklin/trackiter.h b/source/libmarklin/trackiter.h index e756061..e2f1d85 100644 --- a/source/libmarklin/trackiter.h +++ b/source/libmarklin/trackiter.h @@ -10,6 +10,7 @@ Distributed under the GPL #include #include +#include "tracktype.h" namespace Marklin { @@ -30,6 +31,7 @@ public: Track *track() const { return _track; } unsigned entry() const { return _entry; } + const TrackType::Endpoint &endpoint() const; private: int get_exit(unsigned) const; diff --git a/source/libmarklin/tracktype.cpp b/source/libmarklin/tracktype.cpp index 432cea8..9f2979b 100644 --- a/source/libmarklin/tracktype.cpp +++ b/source/libmarklin/tracktype.cpp @@ -59,6 +59,14 @@ bool TrackType::is_dead_end() const return endpoints.size()<2; } +const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const +{ + if(i>=endpoints.size()) + throw InvalidParameterValue("Endpoint index out of range"); + + return endpoints[i]; +} + TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const { if(epi>=endpoints.size()) diff --git a/source/libmarklin/tracktype.h b/source/libmarklin/tracktype.h index b1aff1e..d13ea28 100644 --- a/source/libmarklin/tracktype.h +++ b/source/libmarklin/tracktype.h @@ -60,6 +60,7 @@ public: unsigned get_autofit_preference() const { return autofit_preference; } const std::vector &get_parts() const { return parts; } const std::vector &get_endpoints() const { return endpoints; } + const Endpoint &get_endpoint(unsigned) const; TrackPoint get_point(unsigned, unsigned, float) const; private: diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index e4afef7..d9bade7 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -291,7 +291,7 @@ bool Train::divert(Track &from) int route_path = route->route->get_turnout(from.get_turnout_id()); // Check that more than one path is available - unsigned ep_paths = track->get_type().get_endpoints()[track.entry()].paths; + unsigned ep_paths = track.endpoint().paths; if(!(ep_paths&(ep_paths-1))) return false; @@ -391,7 +391,7 @@ void Train::place(Block &block, unsigned entry) } else { - const Block::Endpoint &bep = block.get_endpoints()[entry]; + const Block::Endpoint &bep = block.get_endpoint(entry); vehicles.back()->place(*bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER); } } @@ -730,9 +730,9 @@ void Train::sensor_event(unsigned addr, bool state) // Check if we've reached the next route if(routes.size()>1) { - const set &rtracks = (++routes.begin())->route->get_tracks(); + const Route &route = *(++routes.begin())->route; for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) - if(rtracks.count((*j)->get_endpoints()[j->entry()].track)) + if(route.has_track(*j->track_iter())) { routes.pop_front(); // XXX Exceptions? @@ -952,7 +952,7 @@ void Train::reserve_more() if(block->get_turnout_id()) { - const TrackType::Endpoint &track_ep = track->get_type().get_endpoints()[track.entry()]; + const TrackType::Endpoint &track_ep = track.endpoint(); bool multiple_paths = (track_ep.paths&(track_ep.paths-1)); if(multiple_paths && cur_route!=routes.end() && cur_route->diversion!=block->get_turnout_id()) @@ -1006,14 +1006,14 @@ void Train::check_turnout_paths(bool set) if((*i)->get_turnout_id()) { TrackIter track = i->track_iter(); - const TrackType::Endpoint &track_ep = track->get_type().get_endpoints()[track.entry()]; + const TrackType::Endpoint &track_ep = track.endpoint(); unsigned path = 0; list::iterator j = i; if(++j!=blocks.end()) { TrackIter rev = j->track_iter().flip(); - unsigned mask = rev->get_type().get_endpoints()[rev.entry()].paths&track_ep.paths; + unsigned mask = rev.endpoint().paths&track_ep.paths; for(path=0; mask>1; mask>>=1, ++path) ; } else -- 2.43.0