]> git.tdb.fi Git - r2c2.git/commitdiff
Simplify some interfaces by using track and block iterators
authorMikko Rasa <tdb@tdb.fi>
Sat, 1 Jun 2013 08:02:39 +0000 (11:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 1 Jun 2013 08:02:39 +0000 (11:02 +0300)
13 files changed:
source/engineer/trainpanel.cpp
source/libr2c2/block.cpp
source/libr2c2/block.h
source/libr2c2/blockallocator.cpp
source/libr2c2/blockallocator.h
source/libr2c2/blockiter.cpp
source/libr2c2/route.cpp
source/libr2c2/route.h
source/libr2c2/train.cpp
source/libr2c2/train.h
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/vehicle.cpp
source/libr2c2/vehicle.h

index 1076fedaedbbb17152084bb337d6f0e69d89c97f..e6a91a8f6dc1c2b272fa38adb88c4a3adc62c165 100644 (file)
@@ -253,25 +253,9 @@ void TrainPanel::place(Track *track, unsigned ep)
 {
        pick_conn.disconnect();
 
-       Block &block = track->get_block();
-       TrackIter iter(track, ep);
-
-       while(block.has_track(*iter))
-       {
-               const vector<Block::Endpoint> &eps = block.get_endpoints();
-               bool ok = false;
-               for(unsigned i=0; (!ok && i<eps.size()); ++i)
-                       if(eps[i].track==iter.track() && eps[i].track_ep==iter.entry())
-                       {
-                               train.place(block, i);
-                               ok = true;
-                       }
-
-               if(ok)
-                       break;
-
-               iter = iter.flip().reverse();
-       }
+       BlockIter block = TrackIter(track, ep).block_iter();
+       if(block)
+               train.place(block);
 }
 
 void TrainPanel::go_to(Track *track, unsigned)
index 8bab92c1f87f63159ebda4f221cc0e1535de2d39..386f2378a3b20011733cb54a17880cdd64a2d50b 100644 (file)
@@ -52,7 +52,7 @@ Block::Block(Layout &l, Track &start):
        {
                unsigned path = 1<<i;
                endpoints[i].paths |= path;
-               find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
+               find_paths(endpoints[i].track_iter(), path);
        }
 
        if(sensor_id && layout.has_driver())
@@ -105,7 +105,7 @@ float Block::get_path_length(unsigned entry, const Route *route) const
        if(entry>=endpoints.size())
                throw out_of_range("Block::get_path_length");
 
-       TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
+       TrackIter t_iter = endpoints[entry].track_iter();
 
        float result = 0;
        while(t_iter && has_track(*t_iter))
@@ -185,7 +185,7 @@ void Block::tick(const Time::TimeDelta &dt)
        }
 }
 
-void Block::find_paths(TrackIter track, unsigned path)
+void Block::find_paths(const TrackIter &track, unsigned path)
 {
        unsigned mask = track.endpoint().paths;
        for(unsigned i=0; mask>>i; ++i)
@@ -254,4 +254,9 @@ Block::Endpoint::Endpoint(Track *t, unsigned e):
        paths(0)
 { }
 
+TrackIter Block::Endpoint::track_iter() const
+{
+       return TrackIter(track, track_ep);
+}
+
 } // namespace R2C2
index f8ba1934e2049911e900f9deae7ab74c6785dd83..23fb3c789c6c77a0c0768b7d52dd9c140b678e82 100644 (file)
@@ -31,6 +31,8 @@ public:
                unsigned paths;
 
                Endpoint(Track *, unsigned);
+
+               TrackIter track_iter() const;
        };
 
        sigc::signal<void, Train *> signal_reserved;
@@ -68,7 +70,7 @@ public:
        Train *get_train() const { return train; }
        void tick(const Msp::Time::TimeDelta &);
 private:
-       void find_paths(TrackIter, unsigned);
+       void find_paths(const TrackIter &, unsigned);
        void determine_id();
        void sensor_event(unsigned, bool);
 };
index ac840d3debfdfabb3b78c02eab31ed2120f0280f..c9cc5b44440fe6987dff33fb0939cfbf4ca79363 100644 (file)
@@ -30,12 +30,15 @@ BlockAllocator::BlockAllocator(Train &t):
                        (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
 }
 
-void BlockAllocator::start_from(Block &block, unsigned entry)
+void BlockAllocator::start_from(const BlockIter &block)
 {
+       if(!block)
+               throw invalid_argument("BlockAllocator::start_from");
+
        release_blocks(blocks.begin(), blocks.end());
 
-       blocks.push_back(BlockIter(&block, entry));
-       if(!block.reserve(&train))
+       blocks.push_back(block);
+       if(!block->reserve(&train))
        {
                blocks.pop_back();
                return;
index 3ef3fa047796e2cceaaef10fb92dfd76558ab53b..7ff5d77f2cf538b4effc9fa4c40826b9a8cb956d 100644 (file)
@@ -40,7 +40,7 @@ private:
 public:
        BlockAllocator(Train &);
 
-       void start_from(Block &, unsigned);
+       void start_from(const BlockIter &);
        void clear();
        bool empty() const { return blocks.empty(); }
        void stop_at(const Block *);
index e662a92ad2020337088d1055d8850c5c0cc93620..038600bbffa933a25c6e5022790b04437b4000dc 100644 (file)
@@ -26,8 +26,7 @@ TrackIter BlockIter::track_iter() const
        if(!_block)
                return TrackIter();
 
-       const Block::Endpoint &ep = _block->get_endpoint(_entry);
-       return TrackIter(ep.track, ep.track_ep);
+       return _block->get_endpoint(_entry).track_iter();
 }
 
 const Block::Endpoint &BlockIter::endpoint() const
index 38ea9f681d77da64f10600b369124c27724aca59..80c61088eda68223be46299bac6621b289291d35 100644 (file)
@@ -297,9 +297,12 @@ void Route::add_tracks(const set<Track *> &trks)
        update_turnouts();
 }
 
-void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts)
+void Route::add_track_chain(const TrackIter &start, const TurnoutMap &trnts)
 {
-       TrackIter iter(&start, ep);
+       if(!start)
+               throw invalid_argument("Route::add_track_chain");
+
+       TrackIter iter = start;
        while(iter)
        {
                if(iter->get_type().is_dead_end())
@@ -451,9 +454,9 @@ void Route::Loader::finish()
                        {
                                Track *link = (*i)->get_link(k);
                                if(!obj.tracks.count(link))
-                                       obj.add_track_chain(*link, link->get_link_slot(**i), turnouts);
+                                       obj.add_track_chain(TrackIter(link, link->get_link_slot(**i)), turnouts);
                                if(!obj.tracks.count(*i))
-                                       obj.add_track_chain(**i, k, turnouts);
+                                       obj.add_track_chain(TrackIter(*i, k), turnouts);
                                break;
                        }
 
index 3ee7e5ead2306313d2afb293bae4b7931124f439..e5164ce06a283f854d5ce2f7e1881bbac159610f 100644 (file)
@@ -75,7 +75,7 @@ public:
        const std::map<unsigned, int> &get_turnouts() const { return turnouts; }
        void add_track(Track &);
        void add_tracks(const std::set<Track *> &);
-       void add_track_chain(Track &, unsigned, const TurnoutMap &);
+       void add_track_chain(const TrackIter &, const TurnoutMap &);
        const std::set<Track *> &get_tracks() const { return tracks; }
        bool has_track(Track &) const;
        void save(std::list<Msp::DataFile::Statement> &) const;
index aa6cc481855ff3655ad2e29ff7ad65bdc5db66d1..72714d730032094f5ae5419c24ffdef38a13ff1e 100644 (file)
@@ -192,26 +192,22 @@ void Train::ai_message(const TrainAI::Message &msg)
                (*i)->message(msg);
 }
 
-void Train::place(Block &block, unsigned entry)
+void Train::place(const BlockIter &block)
 {
+       if(!block)
+               throw invalid_argument("Train::place");
        if(controller->get_speed())
                throw logic_error("moving");
 
        set_active(false);
        accurate_position = false;
 
-       allocator.start_from(block, entry);
+       allocator.start_from(block);
 
        if(reverse)
-       {
-               TrackIter track = BlockIter(&block, entry).reverse().track_iter();
-               vehicles.front()->place(*track, track.entry(), 0, Vehicle::FRONT_BUFFER);
-       }
+               vehicles.front()->place(block.reverse().track_iter(), 0, Vehicle::FRONT_BUFFER);
        else
-       {
-               const Block::Endpoint &bep = block.get_endpoint(entry);
-               vehicles.back()->place(*bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
-       }
+               vehicles.back()->place(block.track_iter(), 0, Vehicle::BACK_BUFFER);
 }
 
 void Train::unplace()
@@ -259,7 +255,7 @@ void Train::free_noncritical_blocks()
 
        Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
 
-       TrackIter track(veh.get_track(), veh.get_entry());
+       TrackIter track = veh.get_track_iter();
        BlockIter block = allocator.first();
        const BlockIter &last_cur = allocator.last_current();
        const BlockIter &last = allocator.last();
@@ -519,10 +515,10 @@ void Train::block_state_changed(Block &block, Block::State state)
                                        if(reverse)
                                        {
                                                track = track.flip();
-                                               vehicles.back()->place(*track, track.entry(), 0, Vehicle::BACK_AXLE);
+                                               vehicles.back()->place(track, 0, Vehicle::BACK_AXLE);
                                        }
                                        else
-                                               vehicles.front()->place(*track, track.entry(), 0, Vehicle::FRONT_AXLE);
+                                               vehicles.front()->place(track, 0, Vehicle::FRONT_AXLE);
                                }
 
                                if(i==last_cur)
@@ -550,7 +546,7 @@ float Train::get_reserved_distance_until(const Block *until_block, bool back) co
        Vehicle &veh = *(reverse!=back ? vehicles.back() : vehicles.front());
        const VehicleType &vtype = veh.get_type();
 
-       TrackIter track(veh.get_track(), veh.get_entry());
+       TrackIter track = veh.get_track_iter();
        if(!track)  // XXX Probably unnecessary
                return 0;
 
@@ -626,7 +622,7 @@ void Train::Loader::finish()
        {
                TrackIter track = obj.allocator.first().track_iter();
                float offset = 2*obj.layout.get_catalogue().get_scale();
-               obj.vehicles.back()->place(*track, track.entry(), offset, Vehicle::BACK_BUFFER);
+               obj.vehicles.back()->place(track, offset, Vehicle::BACK_BUFFER);
        }
 }
 
index 94348e105c551a76edcd0730af57aac7ed3dd18b..6b384f8220b9eece5e5873ead9ab07283d601755 100644 (file)
@@ -113,7 +113,7 @@ public:
                return 0;
        }
 
-       void place(Block &, unsigned);
+       void place(const BlockIter &);
        void unplace();
        bool is_placed() const { return !allocator.empty(); }
        void stop_at(Block *);
index ce38c4e194e193de84a53595621a493dbef2c89b..0de707b16d21c167a88b9f153ac4945c719c647a 100644 (file)
@@ -226,7 +226,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        state(MOVING)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
-       track = TrackIter(veh->get_track(), veh->get_entry());
+       track = veh->get_track_iter();
        // TODO margins
        offset = veh->get_offset()+veh->get_type().get_length()/2;
        path = track->get_active_path();
@@ -244,7 +244,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
                veh = next;
        back_offset = veh->get_offset()-veh->get_type().get_length()/2;
 
-       TrackIter iter(veh->get_track(), veh->get_entry());
+       TrackIter iter = veh->get_track_iter();
        while(back_offset<0)
        {
                TrackIter prev = iter.flip().reverse();
index ca2aa42316a16d8da5b5eea506e5cd87b8c79e48..1c4a5b0f441ec9d5e6b40f711cc54afd176cbf08 100644 (file)
@@ -87,9 +87,12 @@ void Vehicle::detach_front()
        prev = 0;
 }
 
-void Vehicle::place(Track &t, unsigned e, float o, PlaceMode m)
+void Vehicle::place(const TrackIter &t, float o, PlaceMode m)
 {
-       track_pos = TrackPosition(&t, e, o);
+       if(!t)
+               throw invalid_argument("Vehicle::place");
+
+       track_pos = TrackPosition(t, o);
 
        if(m==FRONT_AXLE)
                track_pos.advance(-type.get_front_axle_offset());
@@ -431,14 +434,11 @@ Vehicle::Rod::Rod(const VehicleType::Rod &t):
 
 
 Vehicle::TrackPosition::TrackPosition():
-       track(0),
-       ep(0),
        offs(0)
 { }
 
-Vehicle::TrackPosition::TrackPosition(Track *t, unsigned e, float o):
+Vehicle::TrackPosition::TrackPosition(const TrackIter &t, float o):
        track(t),
-       ep(e),
        offs(o)
 { }
 
@@ -448,33 +448,30 @@ void Vehicle::TrackPosition::advance(float d)
                return;
 
        offs += d;
-       TrackIter iter(track, ep);
-       while(iter)
+       while(track)
        {
-               float path_len = iter->get_type().get_path_length(iter->get_active_path());
+               float path_len = track->get_type().get_path_length(track->get_active_path());
 
                if(offs>path_len)
                {
                        offs -= path_len;
-                       iter = iter.next();
+                       track = track.next();
                }
                else
                        break;
        }
 
-       while(iter && offs<0)
+       while(track && offs<0)
        {
-               iter = iter.flip().reverse();
+               track = track.flip().reverse();
 
-               if(iter)
+               if(track)
                {
-                       float path_len = iter->get_type().get_path_length(iter->get_active_path());
+                       float path_len = track->get_type().get_path_length(track->get_active_path());
                        offs += path_len;
                }
        }
 
-       track = iter.track();
-       ep = iter.entry();
        if(!track)
                offs = 0;
 }
@@ -482,7 +479,7 @@ void Vehicle::TrackPosition::advance(float d)
 TrackPoint Vehicle::TrackPosition::get_point() const
 {
        if(track)
-               return track->get_point(ep, offs);
+               return track->get_point(track.entry(), offs);
        else
                return TrackPoint();
 }
index bd815ab19f497bec7d07470d09700186765548ce..d259201f0ec4df982fc353e005c1730f80d29e72 100644 (file)
@@ -3,12 +3,12 @@
 
 #include "geometry.h"
 #include "object.h"
+#include "trackiter.h"
 #include "vehicletype.h"
 
 namespace R2C2 {
 
 class Layout;
-class Track;
 
 class attachment_error: public std::logic_error
 {
@@ -59,12 +59,11 @@ public:
 private:
        struct TrackPosition
        {
-               Track *track;
-               unsigned ep;
+               TrackIter track;
                float offs;
 
                TrackPosition();
-               TrackPosition(Track *, unsigned, float);
+               TrackPosition(const TrackIter &, float);
                void advance(float);
                TrackPoint get_point() const;
        };
@@ -96,11 +95,12 @@ public:
        // TODO implement these - should call place() with suitable parameters
        virtual void set_position(const Vector &) { }
        virtual void set_rotation(const Angle &) { }
-       void place(Track &, unsigned, float, PlaceMode = CENTER);
+       void place(const TrackIter &, float, PlaceMode = CENTER);
        void unplace();
        void advance(float);
-       Track *get_track() const { return track_pos.track; }
-       unsigned get_entry() const { return track_pos.ep; }
+       const TrackIter &get_track_iter() const { return track_pos.track; }
+       Track *get_track() const { return track_pos.track.track(); }
+       unsigned get_entry() const { return track_pos.track.entry(); }
        float get_offset() const { return track_pos.offs; }
        const Axle &get_fixed_axle(unsigned) const;
        const Bogie &get_bogie(unsigned) const;