]> git.tdb.fi Git - r2c2.git/commitdiff
Make Route hold non-const Tracks to match Block
authorMikko Rasa <tdb@tdb.fi>
Fri, 22 Oct 2010 21:08:17 +0000 (21:08 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 22 Oct 2010 21:08:17 +0000 (21:08 +0000)
Make several functions take non-const parameters to avoid escaping constness:
- Track::get_endpoint_by_link
- Block::get_endpoint_by_link
- Layout3D::get_track

13 files changed:
source/3d/layout.cpp
source/3d/layout.h
source/designer/designer.cpp
source/libmarklin/block.cpp
source/libmarklin/block.h
source/libmarklin/layout.cpp
source/libmarklin/layout.h
source/libmarklin/route.cpp
source/libmarklin/route.h
source/libmarklin/track.cpp
source/libmarklin/track.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index 70c55096cfa91eed259b6be9ebd99ef701dfd88c..e16b6e6d788a135efddad08753dd66348c2655d9 100644 (file)
@@ -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<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if(&(*i)->get_track()==&t)
index a0c77868ae58bc68a3c804843622996d119d2263..f09603de5b00dad0d9ad06178a7e1eef71d98aa0 100644 (file)
@@ -38,7 +38,7 @@ public:
        void add_track(Track3D &);
        void remove_track(Track3D &);
        const std::list<Track3D *> &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 &);
index 7fbab1fd4cdd705abfdfe877f6de8603ffcf544a..e5608331023d038d9146ccb53d375555dbf2dc4f 100644 (file)
@@ -227,9 +227,7 @@ void Designer::add_selection_to_route()
 
        try
        {
-               const set<Track *> &stracks = selection.get_tracks();
-               set<const Track *> tracks(stracks.begin(), stracks.end());
-               cur_route->add_tracks(tracks);
+               cur_route->add_tracks(selection.get_tracks());
        }
        catch(const Exception &e)
        {
index db1942bb5b613fde7ff1b71272383455740bc875..e5dbb6dbef68bfd9594e833981a8d0792de7db5e 100644 (file)
@@ -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<endpoints.size(); ++i)
                if(endpoints[i].link==&other)
index 3da5eec2dcc4e433aadf0182991aa648f5613640..deaf4c48170f3286d296f88b1fff8e5b5428fa3a 100644 (file)
@@ -49,7 +49,7 @@ public:
        unsigned get_turnout_id() const { return turnout_id; }
        const std::set<Track *> &get_tracks() const { return tracks; }
        const std::vector<Endpoint> &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 &);
index fc3d86ed9e39cc51b6b4fef1733c1546184cf5a0..f89b78335d30f0f720778ccbcd4b3822fc8defb8 100644 (file)
@@ -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<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               if((*i)->get_tracks().count(const_cast<Track *>(&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<Track *> &links = track.get_links();
        for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end();)
        {
-               bool del = (*i)->get_tracks().count(const_cast<Track *>(&track));
+               bool del = (*i)->get_tracks().count(&track);
                for(vector<Track *>::const_iterator j=links.begin(); (!del && j!=links.end()); ++j)
                        del = (*i)->get_tracks().count(*j);
 
index 0035e7a5481fdadc37ea29d51dd1903ed7863d84..0f88fab5c36ca690c04b07ff91940877fea96756 100644 (file)
@@ -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<Block *> &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 &);
index d2ed3b7c6fba23d4d0d0b200d619bb61e09dda81..926dd28dd3a903ac468ea9f25f953c56b38f5b13 100644 (file)
@@ -19,11 +19,11 @@ namespace {
 
 using namespace Marklin;
 
-typedef std::pair<const Track *, unsigned> Key;
+typedef std::pair<Track *, unsigned> 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<const Track *> &tracks;
+       const set<Track *> &tracks;
 
-       TrackInSet(const set<const Track *> &t): tracks(t) { }
+       TrackInSet(const set<Track *> &t): tracks(t) { }
 
-       bool operator()(const Track &t) const { return tracks.count(&t); }
+       bool operator()(Track &t) const { return tracks.count(&t); }
 };
 
 template<typename Pred>
-list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
+list<Track *> dijkstra(Track &from, unsigned ep, const Pred &goal)
 {
        map<Key, Node> track_nodes;
        priority_queue<Node> nodes;
@@ -112,7 +112,7 @@ list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
        if(!final)
                throw InvalidParameterValue("Could not find a route");
 
-       list<const Track *> result;
+       list<Track *> result;
        for(Node *node=final; node; node=node->prev)
                result.push_front(node->track);
 
@@ -120,12 +120,12 @@ list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
 }
 
 template<typename Pred>
-Route *create_route(const Track &from, unsigned ep, const Pred &goal)
+Route *create_route(Track &from, unsigned ep, const Pred &goal)
 {
-       list<const Track *> tracks = dijkstra(from, ep, goal);
+       list<Track *> tracks = dijkstra(from, ep, goal);
 
        Route *route = new Route(from.get_layout());
-       for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       for(list<Track *>::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<unsigned> found;
-       for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       for(set<Track *>::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<const Track *> &trks)
+void Route::add_tracks(const set<Track *> &trks)
 {
-       set<const Track *> pending;
-       for(set<const Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
+       set<Track *> pending;
+       for(set<Track *>::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 Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
+               for(set<Track *>::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<const Track *> &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<DataFile::Statement> &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 Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       for(set<Track *>::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<const Track *> &to)
+Route *Route::find(Track &from, unsigned ep, const set<Track *> &to)
 {
        return create_route(from, ep, TrackInSet(to));
 }
index 3825c7eb286b233ef9bb29f291db2b3633460484..901383a50d8e7727807fac14b0ed2727eee8fb08 100644 (file)
@@ -42,7 +42,7 @@ private:
        Layout &layout;
        std::string name;
        bool temporary;
-       std::set<const Track *> tracks;
+       std::set<Track *> tracks;
        TurnoutMap turnouts;
 
 public:
@@ -57,19 +57,19 @@ public:
        void update_turnouts();
        int get_turnout(unsigned) const;
        const std::map<unsigned, int> &get_turnouts() const { return turnouts; }
-       void add_track(const Track &);
-       void add_tracks(const std::set<const Track *> &);
-       void add_track_chain(const Track &, unsigned, const TurnoutMap &);
-       const std::set<const Track *> &get_tracks() const { return tracks; }
+       void add_track(Track &);
+       void add_tracks(const std::set<Track *> &);
+       void add_track_chain(Track &, unsigned, const TurnoutMap &);
+       const std::set<Track *> &get_tracks() const { return tracks; }
        void save(std::list<Msp::DataFile::Statement> &) 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<const Track *> &);
+       static Route *find(Track &, unsigned, Track &);
+       static Route *find(Track &, unsigned, const Route &);
+       static Route *find(Track &, unsigned, const std::set<Track *> &);
 };
 
 } // namespace Marklin
index c940e1cd93d15e517f1501506e228172362aba6c..6938820fafc26fe1b43c86405fc1703762f10364 100644 (file)
@@ -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<links.size(); ++i)
                if(links[i]==&other)
index 34d484c9f67bc487b3fc15b4afcf9778b6dd0be7..aff73966b36199b86751d75b972e0afce129aa1e 100644 (file)
@@ -70,7 +70,7 @@ public:
        void set_active_path(unsigned);
        unsigned get_active_path() const { return active_path; }
 
-       int get_endpoint_by_link(const Track &) const;
+       int get_endpoint_by_link(Track &) const;
        Point get_endpoint_position(unsigned) const;
        float get_endpoint_direction(unsigned) const;
        bool snap_to(Track &, bool);
index ccb7cc25bbf8714706e549c3bb4cee6ff07e2b32..6b6b89ed8d2d4af8485a2cc1f704762b007518ca 100644 (file)
@@ -234,10 +234,10 @@ void Train::set_route(const Route *r)
        signal_route_changed.emit(get_route());
 }
 
-void Train::go_to(const Track &to)
+void Train::go_to(Track &to)
 {
        for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
-               if(i->block->get_tracks().count(const_cast<Track *>(&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<const Track *> tracks;
+       set<Track *> tracks;
        for(list<RouteRef>::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<const Track *> &rtracks = (++routes.begin())->route->get_tracks();
+                               const set<Track *> &rtracks = (++routes.begin())->route->get_tracks();
                                for(list<BlockRef>::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<BlockRef> &blocks) const
                i->entry = i->block->traverse(i->entry);
 }
 
-bool Train::advance_route(list<RouteRef>::iterator &iter, const Track &track)
+bool Train::advance_route(list<RouteRef>::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<const Track *> tracks;
+       set<Track *> tracks;
        for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=rsv_blocks.end(); )
        {
                const set<Track *> &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<const Track *> visited;
+       set<Track *> 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;
 
index d9026c7be84ba78299874d63305b92c204176f82..7039c9ea87d9ec8484ee6b9de6c7d981b0044203 100644 (file)
@@ -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<BlockRef> &);
        void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
        void reverse_blocks(std::list<BlockRef> &) const;
-       bool advance_route(std::list<RouteRef>::iterator &, const Track &);
+       bool advance_route(std::list<RouteRef>::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