]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/block.cpp
Fix memory leaks and other bad stuff
[r2c2.git] / source / libmarklin / block.cpp
index c562788811a05ec07bc4997a8df6d6b49cf14a16..a29469c4fd9b97e32751987e3c42ac6f903b078e 100644 (file)
@@ -5,7 +5,7 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <iostream>
+#include <algorithm>
 #include "control.h"
 #include "block.h"
 #include "tracktype.h"
@@ -17,11 +17,9 @@ using namespace Msp;
 
 namespace Marklin {
 
-unsigned Block::next_id = 1;
-
 Block::Block(TrafficManager &tm, Track &start):
        trfc_mgr(tm),
-       id(next_id++),
+       id(0),
        sensor_id(start.get_sensor_id()),
        turnout_id(start.get_turnout_id()),
        train(0)
@@ -50,12 +48,17 @@ Block::Block(TrafficManager &tm, Track &start):
                        }
        }
 
+       if(sensor_id)
+               id = 0x1000|sensor_id;
+       else if(turnout_id)
+               id = 0x2000|turnout_id;
+
        for(unsigned i=0; i<endpoints.size(); ++i)
        {
-               unsigned route = 1<<i;
-               endpoints[i].routes |= route;
+               unsigned path = 1<<i;
+               endpoints[i].paths |= path;
                set<Track *> visited;
-               find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
+               find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
        }
 }
 
@@ -68,7 +71,7 @@ int Block::get_endpoint_by_link(const Block &other) const
        return -1;
 }
 
-int Block::traverse(unsigned epi, float *len) const
+unsigned Block::traverse(unsigned epi, float *len) const
 {
        if(epi>=endpoints.size())
                throw InvalidParameterValue("Endpoint index out of range");
@@ -82,28 +85,25 @@ int Block::traverse(unsigned epi, float *len) const
 
        while(1)
        {
-               unsigned cur_route = 0;
+               unsigned cur_path = 0;
                unsigned tid = track->get_turnout_id();
                if(tid)
                {
                        Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
-                       cur_route = turnout.get_route();
+                       cur_path = turnout.get_path();
                }
 
                if(len)
-                       *len += track->get_type().get_route_length(cur_route);
-
-               int other_ep = track->traverse(track_ep, cur_route);
-               if(other_ep<0)
-                       return -1;
+                       *len += track->get_type().get_path_length(cur_path);
 
+               unsigned other_ep = track->traverse(track_ep, cur_path);
                for(unsigned i=0; i<endpoints.size(); ++i)
                        if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
                                return i;
 
                Track *next = track->get_link(other_ep);
-               if(tracks.count(next)==0)
-                       return -1;
+               if(!tracks.count(next))
+                       throw LogicError("Block traversal strayed out of the block");
                track_ep = next->get_endpoint_by_link(*track);
                track = next;
        }
@@ -123,6 +123,15 @@ void Block::check_link(Block &other)
                                j->link = this;
                        }
        }
+
+       if(!sensor_id && !turnout_id && endpoints.size()==2)
+       {
+               unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+               unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
+               if(id2<id1)
+                       swap(id1, id2);
+               id = (id1<<16)|id2;
+       }
 }
 
 Block *Block::get_link(unsigned epi) const
@@ -144,24 +153,7 @@ bool Block::reserve(const Train *t)
                return false;
 }
 
-void Block::print_debug()
-{
-       cout<<"Block "<<id;
-       if((*tracks.begin())->get_sensor_id())
-               cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
-       cout<<'\n';
-       for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
-       {
-               cout<<"  Endpoint, link=";
-               if(i->link)
-                       cout<<i->link->id;
-               else
-                       cout<<"none";
-               cout<<", routes="<<i->routes<<'\n';
-       }
-}
-
-void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
+void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
 {
        visited.insert(&track);
 
@@ -172,15 +164,15 @@ void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Tra
                Track *link = track.get_link(i);
                if(!link) continue;
                if(visited.count(link)) continue;
-               if(!(eps[i].routes&eps[track_ep].routes)) continue;
+               if(!(eps[i].paths&eps[track_ep].paths)) continue;
 
                if(tracks.count(link))
-                       find_routes(*link, link->get_endpoint_by_link(track), route, visited);
+                       find_paths(*link, link->get_endpoint_by_link(track), path, visited);
                else
                {
                        for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
                                if(j->track==&track && j->track_ep==i)
-                                       j->routes |= route;
+                                       j->paths |= path;
                }
        }
 }
@@ -190,7 +182,7 @@ Block::Endpoint::Endpoint(Track *t, unsigned e):
        track(t),
        track_ep(e),
        link(0),
-       routes(0)
+       paths(0)
 { }
 
 } // namespace Marklin