]> git.tdb.fi Git - r2c2.git/commitdiff
Add some shortcut functions for getting endpoints
authorMikko Rasa <tdb@tdb.fi>
Thu, 4 Nov 2010 20:36:00 +0000 (20:36 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 4 Nov 2010 20:36:00 +0000 (20:36 +0000)
source/engineer/engineer.cpp
source/libmarklin/block.cpp
source/libmarklin/block.h
source/libmarklin/blockiter.cpp
source/libmarklin/blockiter.h
source/libmarklin/route.cpp
source/libmarklin/trackiter.cpp
source/libmarklin/trackiter.h
source/libmarklin/tracktype.cpp
source/libmarklin/tracktype.h
source/libmarklin/train.cpp

index 68b85aa04cf3c3acdc8c89bd438b3ffcc75a5400..e7b047d311b42db72493adfc2d0e290ae95c7343 100644 (file)
@@ -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
index 1519401220edf59481427c68621985c0cf528f69..e8e367f7255715dc3c7ac2343763ae7da11021bf 100644 (file)
@@ -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; i<endpoints.size(); ++i)
@@ -163,7 +171,7 @@ bool Block::reserve(Train *t)
 
 void Block::find_paths(TrackIter track, unsigned path)
 {
-       unsigned mask = track->get_type().get_endpoints()[track.entry()].paths;
+       unsigned mask = track.endpoint().paths;
        for(unsigned i=0; mask>>i; ++i)
                if(mask&(1<<i))
                {
index 2aed9a40df680f3a769befd02cbe0357b30dd5fd..41f904096c0820a33c95bb96cce1df0cd1a60f77 100644 (file)
@@ -51,6 +51,7 @@ public:
        const std::set<Track *> &get_tracks() const { return tracks; }
        bool has_track(Track &) const;
        const std::vector<Endpoint> &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 &);
index 96159577cb6c488aa062ba33298f0499075cddfd..245f9634a5d0b18fab9c9c24985fe3cd34aac67c 100644 (file)
@@ -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<Block::Endpoint> &eps = _block->get_endpoints();
index 2b8256f73f5be4a5688e357edefd0539d74f8ee6..c86a4e5cf65a45df544551a465dfc88aae377478 100644 (file)
@@ -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;
index 663bd8847c08f2467c1d9376b8900271bcba7428..befaa6de9245a9dbe5d36f02a3a333f265c14fc1 100644 (file)
@@ -87,7 +87,7 @@ list<Track *> 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<<i))
                        {
@@ -188,7 +188,7 @@ void Route::update_turnouts()
 
                                if(unsigned tid2 = links[j]->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<<p)))
                                        {
index 5f633997cc26e3dcdf0ac0354946325928f77377..b98022862dd903c25779c507f5f2dfc540199e5b 100644 (file)
@@ -29,6 +29,14 @@ TrackIter::TrackIter(Track *t, unsigned e):
                throw InvalidParameterValue("Endpoint index not valid for track");
 }
 
+const TrackType::Endpoint &TrackIter::endpoint() const
+{
+       if(!_track)
+               throw InvalidState("TrackIter is null");
+
+       return _track->get_type().get_endpoint(_entry);
+}
+
 int TrackIter::get_exit(unsigned path) const
 {
        const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
index e7560611277ea06c7a45a9fa68fa93327ea3e8f6..e2f1d85dfde9e682cc0d3a7b2f555bdd0d0313d9 100644 (file)
@@ -10,6 +10,7 @@ Distributed under the GPL
 
 #include <set>
 #include <msp/core/refptr.h>
+#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;
index 432cea847f52b0e3b02415b8f503abc733d1b048..9f2979bebff80ca4112eb2c3013d6a4209da1ddf 100644 (file)
@@ -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())
index b1aff1e0920e3229113c517d5fb228c62a511501..d13ea28c98354785075e08177ae514b985b92bf5 100644 (file)
@@ -60,6 +60,7 @@ public:
        unsigned get_autofit_preference() const { return autofit_preference; }
        const std::vector<TrackPart> &get_parts() const { return parts; }
        const std::vector<Endpoint> &get_endpoints() const { return endpoints; }
+       const Endpoint &get_endpoint(unsigned) const;
        TrackPoint get_point(unsigned, unsigned, float) const;
 
 private:
index e4afef7e37c516b17e74960e73e88d39f7bb8fd7..d9bade7dca338c5f20a53ca2624f53f2d4420298 100644 (file)
@@ -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<Track *> &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<BlockIter>::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