From: Mikko Rasa Date: Fri, 22 Oct 2010 21:08:17 +0000 (+0000) Subject: Make Route hold non-const Tracks to match Block X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f79b3fd58999a73a4a28663b46fdd96c58167a18;p=r2c2.git Make Route hold non-const Tracks to match Block Make several functions take non-const parameters to avoid escaping constness: - Track::get_endpoint_by_link - Block::get_endpoint_by_link - Layout3D::get_track --- diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 70c5509..e16b6e6 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -55,7 +55,7 @@ void Layout3D::remove_track(Track3D &t) tracks.erase(i); } -Track3D &Layout3D::get_track(const Track &t) const +Track3D &Layout3D::get_track(Track &t) const { for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) if(&(*i)->get_track()==&t) diff --git a/source/3d/layout.h b/source/3d/layout.h index a0c7786..f09603d 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -38,7 +38,7 @@ public: void add_track(Track3D &); void remove_track(Track3D &); const std::list &get_tracks() const { return tracks; } - Track3D &get_track(const Track &) const; + Track3D &get_track(Track &) const; Track3D *pick_track(float, float, float) const; void add_vehicle(Vehicle3D &); diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 7fbab1f..e560833 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -227,9 +227,7 @@ void Designer::add_selection_to_route() try { - const set &stracks = selection.get_tracks(); - set tracks(stracks.begin(), stracks.end()); - cur_route->add_tracks(tracks); + cur_route->add_tracks(selection.get_tracks()); } catch(const Exception &e) { diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index db1942b..e5dbb6d 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -72,7 +72,7 @@ Block::~Block() layout.remove_block(*this); } -int Block::get_endpoint_by_link(const Block &other) const +int Block::get_endpoint_by_link(Block &other) const { for(unsigned i=0; i &get_tracks() const { return tracks; } const std::vector &get_endpoints() const { return endpoints; } - int get_endpoint_by_link(const Block &) const; + int get_endpoint_by_link(Block &) const; unsigned traverse(unsigned, float * =0) const; unsigned traverse(unsigned, const Route *, float * =0) const; void check_link(Block &); diff --git a/source/libmarklin/layout.cpp b/source/libmarklin/layout.cpp index fc3d86e..f89b783 100644 --- a/source/libmarklin/layout.cpp +++ b/source/libmarklin/layout.cpp @@ -95,10 +95,10 @@ Block &Layout::get_block(unsigned id) const throw KeyError("Unknown block", lexical_cast(id)); } -Block &Layout::get_block_by_track(const Track &t) 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(const_cast(&t))) + if((*i)->get_tracks().count(&t)) return **i; throw InvalidParameterValue("No block found for track"); @@ -126,12 +126,12 @@ void Layout::create_blocks() (*i)->check_link(**j); } -void Layout::create_blocks(const Track &track) +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(const_cast(&track)); + bool del = (*i)->get_tracks().count(&track); for(vector::const_iterator j=links.begin(); (!del && j!=links.end()); ++j) del = (*i)->get_tracks().count(*j); diff --git a/source/libmarklin/layout.h b/source/libmarklin/layout.h index 0035e7a..0f88fab 100644 --- a/source/libmarklin/layout.h +++ b/source/libmarklin/layout.h @@ -81,10 +81,10 @@ public: void add_block(Block &); Block &get_block(unsigned) const; - Block &get_block_by_track(const Track &) const; + Block &get_block_by_track(Track &) const; const std::set &get_blocks() const { return blocks; } void create_blocks(); - void create_blocks(const Track &); + void create_blocks(Track &); void remove_block(Block &); void add_route(Route &); diff --git a/source/libmarklin/route.cpp b/source/libmarklin/route.cpp index d2ed3b7..926dd28 100644 --- a/source/libmarklin/route.cpp +++ b/source/libmarklin/route.cpp @@ -19,11 +19,11 @@ namespace { using namespace Marklin; -typedef std::pair Key; +typedef std::pair Key; struct Node { - const Track *track; + Track *track; unsigned ep; Node *prev; float dist; @@ -32,11 +32,11 @@ struct Node track(0), ep(0), prev(0), dist(0) { } - Node(const Track &t, unsigned e): + Node(Track &t, unsigned e): track(&t), ep(e), prev(0), dist(0) { } - Node(const Track &t, unsigned e, Node &r, float d): + Node(Track &t, unsigned e, Node &r, float d): track(&t), ep(e), prev(&r), dist(prev->dist+d) { } @@ -46,24 +46,24 @@ struct Node struct TrackMatch { - const Track &track; + Track &track; - TrackMatch(const Track &t): track(t) { } + TrackMatch(Track &t): track(t) { } - bool operator()(const Track &t) const { return &t==&track; } + bool operator()(Track &t) const { return &t==&track; } }; struct TrackInSet { - const set &tracks; + const set &tracks; - TrackInSet(const set &t): tracks(t) { } + TrackInSet(const set &t): tracks(t) { } - bool operator()(const Track &t) const { return tracks.count(&t); } + bool operator()(Track &t) const { return tracks.count(&t); } }; template -list dijkstra(const Track &from, unsigned ep, const Pred &goal) +list dijkstra(Track &from, unsigned ep, const Pred &goal) { map track_nodes; priority_queue nodes; @@ -112,7 +112,7 @@ list dijkstra(const Track &from, unsigned ep, const Pred &goal) if(!final) throw InvalidParameterValue("Could not find a route"); - list result; + list result; for(Node *node=final; node; node=node->prev) result.push_front(node->track); @@ -120,12 +120,12 @@ list dijkstra(const Track &from, unsigned ep, const Pred &goal) } template -Route *create_route(const Track &from, unsigned ep, const Pred &goal) +Route *create_route(Track &from, unsigned ep, const Pred &goal) { - list tracks = dijkstra(from, ep, goal); + list tracks = dijkstra(from, ep, goal); Route *route = new Route(from.get_layout()); - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) route->add_track(**i); route->set_name("Pathfinder"); @@ -178,7 +178,7 @@ void Route::set_turnout(unsigned addr, unsigned path) void Route::update_turnouts() { set found; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) if(unsigned tid = (*i)->get_turnout_id()) { found.insert(tid); @@ -237,7 +237,7 @@ int Route::get_turnout(unsigned id) const return -1; } -void Route::add_track(const Track &trk) +void Route::add_track(Track &trk) { if(tracks.count(&trk)) return; @@ -257,17 +257,17 @@ void Route::add_track(const Track &trk) update_turnouts(); } -void Route::add_tracks(const set &trks) +void Route::add_tracks(const set &trks) { - set pending; - for(set::const_iterator i=trks.begin(); i!=trks.end(); ++i) + set pending; + for(set::const_iterator i=trks.begin(); i!=trks.end(); ++i) if(!tracks.count(*i)) pending.insert(*i); while(!pending.empty()) { bool found = false; - for(set::const_iterator i=pending.begin(); i!=pending.end(); ++i) + for(set::const_iterator i=pending.begin(); i!=pending.end(); ++i) if(tracks.empty() || check_validity(**i)==7) { tracks.insert(*i); @@ -283,9 +283,9 @@ void Route::add_tracks(const set &trks) update_turnouts(); } -void Route::add_track_chain(const Track &start, unsigned ep, const TurnoutMap &trnts) +void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts) { - const Track *track = &start; + Track *track = &start; while(1) { if(track->get_type().is_dead_end()) @@ -323,10 +323,10 @@ void Route::save(list &st) const st.push_back((DataFile::Statement("turnout"), i->first, i->second)); } -unsigned Route::check_validity(const Track &trk) const +unsigned Route::check_validity(Track &trk) const { unsigned result = 4; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) { int epi=(*i)->get_endpoint_by_link(trk); if(epi>=0) @@ -380,17 +380,17 @@ void Route::track_removed(Track &t) tracks.erase(&t); } -Route *Route::find(const Track &from, unsigned ep, const Track &to) +Route *Route::find(Track &from, unsigned ep, Track &to) { return create_route(from, ep, TrackMatch(to)); } -Route *Route::find(const Track &from, unsigned ep, const Route &to) +Route *Route::find(Track &from, unsigned ep, const Route &to) { return create_route(from, ep, TrackInSet(to.get_tracks())); } -Route *Route::find(const Track &from, unsigned ep, const set &to) +Route *Route::find(Track &from, unsigned ep, const set &to) { return create_route(from, ep, TrackInSet(to)); } diff --git a/source/libmarklin/route.h b/source/libmarklin/route.h index 3825c7e..901383a 100644 --- a/source/libmarklin/route.h +++ b/source/libmarklin/route.h @@ -42,7 +42,7 @@ private: Layout &layout; std::string name; bool temporary; - std::set tracks; + std::set tracks; TurnoutMap turnouts; public: @@ -57,19 +57,19 @@ public: void update_turnouts(); int get_turnout(unsigned) const; const std::map &get_turnouts() const { return turnouts; } - void add_track(const Track &); - void add_tracks(const std::set &); - void add_track_chain(const Track &, unsigned, const TurnoutMap &); - const std::set &get_tracks() const { return tracks; } + void add_track(Track &); + void add_tracks(const std::set &); + void add_track_chain(Track &, unsigned, const TurnoutMap &); + const std::set &get_tracks() const { return tracks; } void save(std::list &) const; private: - unsigned check_validity(const Track &) const; + unsigned check_validity(Track &) const; void track_removed(Track &); public: - static Route *find(const Track &, unsigned, const Track &); - static Route *find(const Track &, unsigned, const Route &); - static Route *find(const Track &, unsigned, const std::set &); + static Route *find(Track &, unsigned, Track &); + static Route *find(Track &, unsigned, const Route &); + static Route *find(Track &, unsigned, const std::set &); }; } // namespace Marklin diff --git a/source/libmarklin/track.cpp b/source/libmarklin/track.cpp index c940e1c..6938820 100644 --- a/source/libmarklin/track.cpp +++ b/source/libmarklin/track.cpp @@ -137,7 +137,7 @@ void Track::set_active_path(unsigned p) active_path = (active_path&1) | (p&2); } -int Track::get_endpoint_by_link(const Track &other) const +int Track::get_endpoint_by_link(Track &other) const { for(unsigned i=0; i::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i) - if(i->block->get_tracks().count(const_cast(&to))) + if(i->block->get_tracks().count(&to)) { signal_arrived.emit(); set_route(0); @@ -315,7 +315,7 @@ bool Train::divert(Track &from) unsigned ep = track->get_endpoint_by_link(from); - set tracks; + set tracks; for(list::iterator i=routes.begin(); i!=routes.end(); ++i) tracks.insert(i->route->get_tracks().begin(), i->route->get_tracks().end()); Route *diversion = 0; @@ -764,7 +764,7 @@ 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 set &rtracks = (++routes.begin())->route->get_tracks(); for(list::iterator j=rsv_blocks.begin(); j!=i; ++j) if(rtracks.count(j->block->get_endpoints()[j->entry].track)) { @@ -1274,7 +1274,7 @@ void Train::reverse_blocks(list &blocks) const i->entry = i->block->traverse(i->entry); } -bool Train::advance_route(list::iterator &iter, const Track &track) +bool Train::advance_route(list::iterator &iter, Track &track) { while(iter!=routes.end() && !iter->route->get_tracks().count(&track)) ++iter; @@ -1298,7 +1298,7 @@ Route *Train::create_lead_route(Route *lead, const Route *target) lead->set_temporary(true); } - set tracks; + set tracks; for(list::iterator i=cur_blocks.begin(); i!=rsv_blocks.end(); ) { const set &btracks = i->block->get_tracks(); @@ -1315,10 +1315,10 @@ Route *Train::create_lead_route(Route *lead, const Route *target) return lead; } -bool Train::is_valid_diversion(const Route &diversion, const Track &from, unsigned from_ep) +bool Train::is_valid_diversion(const Route &diversion, Track &from, unsigned from_ep) { float diversion_len = 0; - const Track *track = &from; + Track *track = &from; unsigned ep = from_ep; while(diversion.get_tracks().count(track)) { @@ -1327,7 +1327,7 @@ bool Train::is_valid_diversion(const Route &diversion, const Track &from, unsign path = diversion.get_turnout(track->get_turnout_id()); diversion_len += track->get_type().get_path_length(path); - const Track *next = track->get_link(track->traverse(ep, path)); + Track *next = track->get_link(track->traverse(ep, path)); ep = next->get_endpoint_by_link(*track); track = next; @@ -1339,7 +1339,7 @@ bool Train::is_valid_diversion(const Route &diversion, const Track &from, unsign if(!advance_route(route, from)) return false; - set visited; + set visited; float route_len = 0; track = &from; ep = from_ep; @@ -1357,7 +1357,7 @@ bool Train::is_valid_diversion(const Route &diversion, const Track &from, unsign return false; visited.insert(track); - const Track *next = track->get_link(track->traverse(ep, path)); + Track *next = track->get_link(track->traverse(ep, path)); ep = next->get_endpoint_by_link(*track); track = next; diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index d9026c7..7039c9e 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -141,7 +141,7 @@ public: Timetable *get_timetable() { return timetable; } void set_route(const Route *); - void go_to(const Track &); + void go_to(Track &); bool divert(Track &); const Route *get_route() const; void place(Block &, unsigned); @@ -174,9 +174,9 @@ private: void release_blocks(std::list &); void release_blocks(std::list &, std::list::iterator, std::list::iterator); void reverse_blocks(std::list &) const; - bool advance_route(std::list::iterator &, const Track &); + bool advance_route(std::list::iterator &, Track &); Route *create_lead_route(Route *, const Route *); - bool is_valid_diversion(const Route &, const Track &, unsigned); + bool is_valid_diversion(const Route &, Track &, unsigned); }; } // namespace Marklin