From: Mikko Rasa Date: Sat, 1 Jun 2013 08:02:39 +0000 (+0300) Subject: Simplify some interfaces by using track and block iterators X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=54392d65e2053d1eacb4cfcc435f1013993f2973;p=r2c2.git Simplify some interfaces by using track and block iterators --- diff --git a/source/engineer/trainpanel.cpp b/source/engineer/trainpanel.cpp index 1076fed..e6a91a8 100644 --- a/source/engineer/trainpanel.cpp +++ b/source/engineer/trainpanel.cpp @@ -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 &eps = block.get_endpoints(); - bool ok = false; - for(unsigned i=0; (!ok && i=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 diff --git a/source/libr2c2/block.h b/source/libr2c2/block.h index f8ba193..23fb3c7 100644 --- a/source/libr2c2/block.h +++ b/source/libr2c2/block.h @@ -31,6 +31,8 @@ public: unsigned paths; Endpoint(Track *, unsigned); + + TrackIter track_iter() const; }; sigc::signal 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); }; diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index ac840d3..c9cc5b4 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -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; diff --git a/source/libr2c2/blockallocator.h b/source/libr2c2/blockallocator.h index 3ef3fa0..7ff5d77 100644 --- a/source/libr2c2/blockallocator.h +++ b/source/libr2c2/blockallocator.h @@ -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 *); diff --git a/source/libr2c2/blockiter.cpp b/source/libr2c2/blockiter.cpp index e662a92..038600b 100644 --- a/source/libr2c2/blockiter.cpp +++ b/source/libr2c2/blockiter.cpp @@ -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 diff --git a/source/libr2c2/route.cpp b/source/libr2c2/route.cpp index 38ea9f6..80c6108 100644 --- a/source/libr2c2/route.cpp +++ b/source/libr2c2/route.cpp @@ -297,9 +297,12 @@ void Route::add_tracks(const set &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; } diff --git a/source/libr2c2/route.h b/source/libr2c2/route.h index 3ee7e5e..e5164ce 100644 --- a/source/libr2c2/route.h +++ b/source/libr2c2/route.h @@ -75,7 +75,7 @@ public: const std::map &get_turnouts() const { return turnouts; } void add_track(Track &); void add_tracks(const std::set &); - void add_track_chain(Track &, unsigned, const TurnoutMap &); + void add_track_chain(const TrackIter &, const TurnoutMap &); const std::set &get_tracks() const { return tracks; } bool has_track(Track &) const; void save(std::list &) const; diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index aa6cc48..72714d7 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -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); } } diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index 94348e1..6b384f8 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -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 *); diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index ce38c4e..0de707b 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -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(); diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index ca2aa42..1c4a5b0 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -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(); } diff --git a/source/libr2c2/vehicle.h b/source/libr2c2/vehicle.h index bd815ab..d259201 100644 --- a/source/libr2c2/vehicle.h +++ b/source/libr2c2/vehicle.h @@ -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;