fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / FieldContainer / Connector / OSGConnectorAttachment.cpp
blobc18b17e743fe176423379ded12c9b0ed9b065b9e
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include <boost/bind.hpp>
48 #include "OSGConfig.h"
50 #include "OSGConnectorAttachment.h"
51 #include "OSGAttachmentContainer.h"
53 OSG_BEGIN_NAMESPACE
55 // Documentation for this class is emitted in the
56 // OSGConnectorAttachmentBase.cpp file.
57 // To modify it, please change the .fcd file (OSGConnectorAttachment.fcd) and
58 // regenerate the base file.
60 /***************************************************************************\
61 * Class variables *
62 \***************************************************************************/
64 /***************************************************************************\
65 * Class methods *
66 \***************************************************************************/
68 void ConnectorAttachment::initMethod(InitPhase ePhase)
70 Inherited::initMethod(ePhase);
72 if(ePhase == TypeObject::SystemPost)
78 /***************************************************************************\
79 * Instance methods *
80 \***************************************************************************/
82 /*-------------------------------------------------------------------------*\
83 - private -
84 \*-------------------------------------------------------------------------*/
86 /*----------------------- constructors & destructors ----------------------*/
88 ConnectorAttachment::ConnectorAttachment(void) :
89 Inherited (),
90 _vConnections()
94 ConnectorAttachment::ConnectorAttachment(const ConnectorAttachment &source) :
95 Inherited (source),
96 _vConnections( )
100 ConnectorAttachment::~ConnectorAttachment(void)
104 /*----------------------------- class specific ----------------------------*/
106 void ConnectorAttachment::changed(ConstFieldMaskArg whichField,
107 UInt32 origin,
108 BitVector details)
110 Inherited::changed(whichField, origin, details);
113 void ConnectorAttachment::dump( UInt32 ,
114 const BitVector ) const
116 SLOG << "Dump ConnectorAttachment NI" << std::endl;
119 void ConnectorAttachment::addConnection (BasicFieldConnector *pConnector)
121 if(pConnector != NULL)
123 _vConnections.push_back(pConnector);
127 void ConnectorAttachment::processChanged(FieldContainer *pObj,
128 BitVector whichField,
129 UInt32 origin )
131 ConnectionStore::const_iterator cIt = _vConnections.begin();
132 ConnectionStore::const_iterator cEnd = _vConnections.end ();
134 while(cIt != cEnd)
136 if((*cIt)->match(whichField) == true)
138 (*cIt)->process();
141 ++cIt;
145 void ConnectorAttachment::targetDestroyed(FieldContainer *pObj,
146 BitVector whichField,
147 UInt32 origin )
149 if(whichField == 0x0000)
151 this->removeConnectionTo(pObj);
155 bool ConnectorAttachment::hasConnectionTo(const FieldContainer *pDst) const
157 bool returnValue = false;
159 ConnectionStore::const_iterator cIt = _vConnections.begin();
160 ConnectionStore::const_iterator cEnd = _vConnections.end ();
162 while(cIt != cEnd)
164 if((*cIt)->getDst() == pDst)
166 returnValue = true;
167 break;
170 ++cIt;
173 return returnValue;
176 void ConnectorAttachment::removeConnectionTo(const FieldContainer *pDst)
178 ConnectionStore::iterator cIt = _vConnections.begin();
179 ConnectionStore::const_iterator cEnd = _vConnections.end ();
181 while(cIt != cEnd)
183 if((*cIt)->getDst() == pDst)
185 delete (*cIt);
187 cIt = _vConnections.erase(cIt);
189 cEnd = _vConnections.end();
191 else
193 ++cIt;
198 void ConnectorAttachment::removeConnections( BitVector bSrcMask,
199 const FieldContainer *pDst,
200 BitVector bDstMask)
202 ConnectionStore::iterator cIt = _vConnections.begin();
203 ConnectionStore::const_iterator cEnd = _vConnections.end ();
205 ConnectionCount mConnCount;
207 this->countConnections(mConnCount);
209 while(cIt != cEnd)
211 if((*cIt)->match(bSrcMask, pDst, bDstMask) == true)
213 --(mConnCount[(*cIt)->getDst()]);
215 delete (*cIt);
217 cIt = _vConnections.erase(cIt);
219 cEnd = _vConnections.end();
221 else
223 ++cIt;
227 ConnectionCount::const_iterator ccIt = mConnCount.begin();
228 ConnectionCount::const_iterator ccEnd = mConnCount.end ();
230 while(ccIt != ccEnd)
232 if(ccIt->second == 0)
234 ccIt->first->subChangedFunctor(
235 boost::bind(&ConnectorAttachment::targetDestroyed,
236 this,
237 _1,
239 _3));
242 ++ccIt;
246 void ConnectorAttachment::countConnections(ConnectionCount &mCount)
248 ConnectionStore::const_iterator cIt = _vConnections.begin();
249 ConnectionStore::const_iterator cEnd = _vConnections.end ();
251 ConnectionCount::iterator ccIt;
253 while(cIt != cEnd)
255 ccIt = mCount.find((*cIt)->getDst());
257 if(ccIt == mCount.end())
259 mCount[(*cIt)->getDst()] = 1;
261 else
263 ++(ccIt->second);
266 ++cIt;
271 bool ConnectorAttachment::unlinkParent(FieldContainer * const pParent,
272 UInt16 const parentFieldId)
274 pParent->subChangedFunctor(
275 boost::bind(&ConnectorAttachment::processChanged,
276 this,
277 _1,
279 _3));
281 return Inherited::unlinkParent(pParent, parentFieldId);
284 void ConnectorAttachment::resolveLinks(void)
286 for(UInt32 i = 0; i < _vConnections.size(); ++i)
288 FieldContainer *pDst = _vConnections[i]->getDst();
290 pDst->subChangedFunctor(
291 boost::bind(&ConnectorAttachment::targetDestroyed,
292 this,
293 _1,
295 _3));
297 delete _vConnections[i];
300 _vConnections.clear();
303 /*---------------------------------------------------------------------*/
304 /*! \name Connector handling */
305 /*! \{ */
307 /*! \ingroup GrpBaseFieldContainerConnector
308 \relatesalso AttachmentContainer
311 void addConnector(OSG::AttachmentContainer *pContainer,
312 OSG::BasicFieldConnector *pConn )
314 if(pContainer == NULL)
316 FFATAL(("addConnector: no container?!?"));
317 return;
320 if(pConn == NULL)
322 FFATAL(("addConnector: no connector?!?"));
323 return;
326 ConnectorAttachmentUnrecPtr pCA = NULL;
328 Attachment *att = pContainer->findAttachment(
329 ConnectorAttachment::getClassType().getGroupId());
331 if(att == NULL)
333 pCA = ConnectorAttachment::createDependent(
334 pContainer->getFieldFlags()->_bNamespaceMask);
336 pContainer->addChangedFunctor(
337 boost::bind(&ConnectorAttachment::processChanged,
338 pCA.get(),
339 _1,
341 _3),
342 "");
344 pContainer->addAttachment(pCA);
346 else
348 pCA = dynamic_cast<ConnectorAttachment *>(att);
350 if(pCA == NULL)
352 FFATAL(("setName: Attachment is not castable to Conn?!?"));
353 return;
357 if(pCA->hasConnectionTo(pConn->getDst()) == false)
359 FieldContainer *pDst = pConn->getDst();
361 pDst->addChangedFunctor(
362 boost::bind(&ConnectorAttachment::targetDestroyed,
363 pCA.get(),
364 _1,
366 _3),
367 "");
370 pCA->addConnection(pConn);
373 /*! \ingroup GrpBaseFieldContainerConnector
374 \relatesalso AttachmentContainer
377 void subConnector(OSG::AttachmentContainer *pSrcContainer,
378 OSG::BitVector bSrcMask,
379 OSG::FieldContainer *pDstContainer,
380 OSG::BitVector bDstMask)
382 if(pSrcContainer == NULL)
384 FFATAL(("subConnector: no container?!?"));
385 return;
388 ConnectorAttachmentUnrecPtr pCA = NULL;
390 Attachment *att = pSrcContainer->findAttachment(
391 ConnectorAttachment::getClassType().getGroupId());
393 pCA = dynamic_cast<ConnectorAttachment *>(att);
395 if(pCA != NULL)
397 pCA->removeConnections(bSrcMask, pDstContainer, bDstMask);
401 /*! \} */
402 /*---------------------------------------------------------------------*/
404 OSG_END_NAMESPACE