GME  13
AutoRouterBox.cpp
Go to the documentation of this file.
00001 // AutoRouterBox.cpp : Implementation of CAutoRouterBox
00002 
00003 #include "stdafx.h"
00004 #include "AutoRouterBox.h"
00005 #include "AutoRouterPort.h"
00006 #include "AutoRouterGraph.h"
00007 
00008 
00009 CAutoRouterBox::CAutoRouterBox():
00010         owner(NULL),
00011         rect(0, 0, 0, 0),
00012         atomic(false)
00013 {
00014         CalculateSelfPoints();
00015 }
00016 
00017 void CAutoRouterBox::CalculateSelfPoints()
00018 {
00019         selfpoints[0].x = rect.left;
00020         selfpoints[0].y = rect.top;
00021 
00022         selfpoints[1].x = rect.right - 1;
00023         selfpoints[1].y = rect.top;
00024 
00025         selfpoints[2].x = rect.right - 1;
00026         selfpoints[2].y = rect.bottom - 1;
00027 
00028         selfpoints[3].x = rect.left;
00029         selfpoints[3].y = rect.bottom - 1;
00030 }
00031 
00032 void CAutoRouterBox::DeleteAllPorts()
00033 {
00034         for (CAutoRouterPortList::size_type i = 0; i < ports.size(); i++)
00035         {
00036                 ports[i]->SetOwner(NULL);
00037                 delete ports[i];
00038         }
00039 
00040         ports.clear();
00041 
00042         atomic = false;
00043 }
00044 
00045 CAutoRouterGraph* CAutoRouterBox::GetOwner(void) const
00046 {
00047         return owner;
00048 }
00049 
00050 bool CAutoRouterBox::HasOwner(void) const
00051 {
00052         return owner != NULL;
00053 }
00054 
00055 void CAutoRouterBox::SetOwner(CAutoRouterGraph* graph)
00056 {
00057         owner = graph;
00058 }
00059 
00060 CAutoRouterPort* CAutoRouterBox::CreatePort(void) const
00061 {
00062         CAutoRouterPort* port = new CAutoRouterPort();
00063         ASSERT( port != NULL );
00064 
00065         return port;
00066 }
00067 
00068 bool CAutoRouterBox::HasNoPort(void) const
00069 {
00070         return ports.size() == 0;
00071 }
00072 
00073 long CAutoRouterBox::GetPortCount(void) const
00074 {
00075         return ports.size();
00076 }
00077 
00078 bool CAutoRouterBox::IsAtomic(void) const
00079 {
00080         return atomic;
00081 }
00082 
00083 void CAutoRouterBox::AddPort(CAutoRouterPort* port)
00084 {
00085         ASSERT(port != NULL);
00086         if (port == NULL)
00087                 return;
00088 
00089         port->SetOwner(this);
00090 
00091         ports.push_back(port);
00092 
00093 #ifdef _DEBUG
00094         AssertValidPort(port);
00095 #endif
00096 }
00097 
00098 void CAutoRouterBox::DeletePort(CAutoRouterPort* port)
00099 {
00100         ASSERT(port != NULL);
00101         if (port == NULL)
00102                 return;
00103 
00104 #ifdef _DEBUG
00105         CAutoRouterBox* ownerBox = port->GetOwner();
00106         ASSERT( ownerBox == this );
00107 
00108         AssertValidPort(port);
00109 
00110         if( atomic )
00111                 ASSERT( ports.size() == 1 );
00112 #endif
00113 
00114         std::vector<CAutoRouterPort*>::iterator iter = std::find(ports.begin(), ports.end(), port);
00115 
00116         if (iter == ports.end())
00117         {
00118                 //error
00119                 return;
00120         }
00121 
00122         (*iter)->SetOwner(NULL);
00123 
00124         ports.erase(iter);
00125 
00126         delete *iter;
00127 
00128         atomic = false;
00129 }
00130 
00131 const CAutoRouterPortList& CAutoRouterBox::GetPortList(void) const
00132 {
00133         return ports;
00134 }
00135 
00136 CRect CAutoRouterBox::GetRect(void) const
00137 {
00138         return rect;
00139 }
00140 
00141 bool CAutoRouterBox::IsRectEmpty(void) const
00142 {
00143         return rect.IsRectEmpty() == TRUE;
00144 }
00145 
00146 void CAutoRouterBox::SetRect(const CRect& r)
00147 {
00148         ASSERT( r.Width() >= 3 && r.Height() >= 3 );
00149         ASSERT( r.TopLeft().x >= ED_MINCOORD && r.TopLeft().y >= ED_MINCOORD );
00150         ASSERT( r.BottomRight().x <= ED_MAXCOORD && r.BottomRight().y <= ED_MAXCOORD );
00151         ASSERT( ports.size() == 0 || atomic );
00152 
00153         rect = r;
00154         CalculateSelfPoints();
00155 
00156         if( atomic )
00157         {
00158                 ASSERT( ports.size() == 1 );
00159                 ports[0]->SetRect(r);
00160         }
00161 }
00162 
00163 void CAutoRouterBox::SetRectByPoint(const CPoint& point)
00164 {
00165         ShiftBy(point);
00166 }
00167 
00168 void CAutoRouterBox::ShiftBy(const CPoint& offset)
00169 {
00170         rect += offset;
00171 
00172         for (CAutoRouterPortList::size_type i = 0; i < ports.size(); i++)
00173         {
00174                 ports[i]->ShiftBy(offset);
00175         }
00176 
00177         CalculateSelfPoints();
00178 }
00179 
00180 CPoint* CAutoRouterBox::GetSelfPoints(void) const
00181 {
00182         return (CPoint*)selfpoints;
00183 }
00184 
00185 bool CAutoRouterBox::IsBoxAt(const CPoint& point, long nearness) const
00186 {
00187         return IsPointIn(point, rect, nearness);
00188 }
00189 
00190 bool CAutoRouterBox::IsBoxClip(const CRect& r) const
00191 {
00192         return IsRectClip(rect, r);
00193 }
00194 
00195 bool CAutoRouterBox::IsBoxIn(const CRect& r) const
00196 {
00197         return IsRectIn(rect, r);
00198 }
00199 
00200 void CAutoRouterBox::Destroy(void)
00201 {
00202         //ideally it could be placed in the finaldestruct(), 
00203         //but the deleted ports hold references to this box, 
00204         //so the finaldestruct() is not called until the ports are deleted
00205         
00206         this->SetOwner(NULL);
00207         DeleteAllPorts();
00208 }
00209 
00210 // --- Debug
00211 
00212 #ifdef _DEBUG
00213 
00214 void CAutoRouterBox::AssertValid() const
00215 {
00216         for (CAutoRouterPortList::size_type i = 0; i < ports.size(); i++)
00217         {
00218                 AssertValidPort(ports[i]);
00219         }
00220 }
00221 
00222 void CAutoRouterBox::AssertValidPort(CAutoRouterPort* port) const
00223 {
00224         port->AssertValid();
00225 
00226         if( owner != NULL ) {
00227                 CAutoRouterBox* ownerBox = port->GetOwner();
00228                 ASSERT( ownerBox == this );
00229         }
00230 
00231         CRect r = port->GetRect();
00232         ASSERT( IsRectIn(r, rect) );
00233 }
00234 
00235 #endif
00236 
00237 // CAutoRouterBox
00238