fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / FieldContainer / Fields / PointerFields / OSGChildAccessHandler.inl
blob04d958cb2e0a0904e2da4372d7fafe012ccd065c
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *                 Copyright (C) 2008 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 OSG_BEGIN_NAMESPACE
41 /*! \class ChildAccessHandler
43     Access handler for child pointer fields.
44     This class handles all reference counting and child/parent linking
45     operations for child pointer fields. The fields only call the respective
46     members when an operation on one of their values is performed.
47  */
49 /*-------------------------------------------------------------------------*/
50 /* Internal Helper                                                         */
52 template<typename RefCountPolicyT> inline 
53 typename ChildAccessHandler<RefCountPolicyT>::SFieldType * 
54     ChildAccessHandler<RefCountPolicyT>::dcastSField(
55         SFieldBaseType * const pSField)
57     return static_cast<SFieldType *>(pSField);
60 template<typename RefCountPolicyT> inline 
61 typename ChildAccessHandler<RefCountPolicyT>::MFieldType * 
62     ChildAccessHandler<RefCountPolicyT>::dcastMField(
63         MFieldBaseType * const pMField)
65     return static_cast<MFieldType *>(pMField);
68 /*-------------------------------------------------------------------------*/
69 /* Linking                                                                 */
71 template<typename RefCountPolicyT> inline 
72 void ChildAccessHandler<RefCountPolicyT>::linkParent(
73     FieldContainer * const pParent,
74     UInt16           const childFieldId,
75     FieldContainer * const pChild,
76     UInt16           const parentFieldId)
78     pChild->linkParent(pParent, childFieldId, parentFieldId);
81 template<typename RefCountPolicyT> inline 
82 void ChildAccessHandler<RefCountPolicyT>::unlinkParent(
83     FieldContainer * const pParent,
84     FieldContainer * const pChild,
85     UInt16           const parentFieldId)
87     pChild->unlinkParent(pParent, parentFieldId);
90 /*-------------------------------------------------------------------------*/
91 /* Access Handling                                                         */
93 template<typename RefCountPolicyT> inline 
94 void ChildAccessHandler<RefCountPolicyT>::onAdd(
95     SFieldBaseType * const pSField, 
96     FieldContainer * const pObj   )
98     if(pObj != NULL)
99     {
100         RefCountPolicyType::addRef(pObj);
102         linkParent(dcastSField(pSField)->getEnclosingObject(),
103                    dcastSField(pSField)->getChildFieldId   (),
104                    pObj,
105                    dcastSField(pSField)->getParentFieldId  () );
106     }
109 template<typename RefCountPolicyT> inline 
110 void ChildAccessHandler<RefCountPolicyT>::onAdd(
111     MFieldBaseType * const pMField, 
112     FieldContainer * const pObj   )
114     if(pObj != NULL)
115     {
116         RefCountPolicyType::addRef(pObj);
118         linkParent(dcastMField(pMField)->getEnclosingObject(),
119                    dcastMField(pMField)->getChildFieldId   (),
120                    pObj,
121                    dcastMField(pMField)->getParentFieldId  () );
122     }
125 template<typename RefCountPolicyT> inline 
126 void ChildAccessHandler<RefCountPolicyT>::onSub(
127     SFieldBaseType * const pSField, 
128     FieldContainer * const pObj   )
130     if(pObj != NULL)
131     {
132         unlinkParent(dcastSField(pSField)->getEnclosingObject(),
133                      pObj,
134                      dcastSField(pSField)->getParentFieldId  () );
136         RefCountPolicyType::subRef(pObj);
137     }
140 template<typename RefCountPolicyT> inline 
141 void ChildAccessHandler<RefCountPolicyT>::onSub(
142     MFieldBaseType * const pMField, 
143     FieldContainer * const pObj   )
145     if(pObj != NULL)
146     {
147         unlinkParent(dcastMField(pMField)->getEnclosingObject(),
148                      pObj,
149                      dcastMField(pMField)->getParentFieldId  () );
151         RefCountPolicyType::subRef(pObj);
152     }
155 template<typename RefCountPolicyT> inline 
156 void ChildAccessHandler<RefCountPolicyT>::onReplace(
157     SFieldBaseType * const pSField,
158     FieldContainer * const pOldObj, 
159     FieldContainer * const pNewObj)
161     RefCountPolicyType::addRef(pNewObj);
163     if(pOldObj != NULL)
164     {
165         unlinkParent(dcastSField(pSField)->getEnclosingObject(),
166                      pOldObj,
167                      dcastSField(pSField)->getParentFieldId  ());
169         RefCountPolicyType::subRef(pOldObj);
170     }
172     if(pNewObj != NULL)
173     {
174         linkParent(dcastSField(pSField)->getEnclosingObject(),
175                    dcastSField(pSField)->getChildFieldId   (),
176                    pNewObj,
177                    dcastSField(pSField)->getParentFieldId  ());
178     }
181 template<typename RefCountPolicyT> inline 
182 void ChildAccessHandler<RefCountPolicyT>::onReplace(
183     MFieldBaseType * const pMField,
184     FieldContainer * const pOldObj,
185     FieldContainer * const pNewObj)
187     RefCountPolicyType::addRef(pNewObj);
189     if(pOldObj != NULL)
190     {
191         unlinkParent(dcastMField(pMField)->getEnclosingObject(),
192                      pOldObj,
193                      dcastMField(pMField)->getParentFieldId() );
195         RefCountPolicyType::subRef(pOldObj);
196     }
198     if(pNewObj != NULL)
199     {
200         linkParent(dcastMField(pMField)->getEnclosingObject(),
201                    dcastMField(pMField)->getChildFieldId   (),
202                    pNewObj,
203                    dcastMField(pMField)->getParentFieldId ());
204     }
207 /*-------------------------------------------------------------------------*/
208 /* Sync Access Handling                                                    */
210 template<typename RefCountPolicyT> inline 
211 void ChildAccessHandler<RefCountPolicyT>::onSyncAdd(
212     SFieldBaseType * const, 
213     FieldContainer * const pObj)
215     RefCountPolicyType::addRef(pObj);
218 template<typename RefCountPolicyT> inline 
219 void ChildAccessHandler<RefCountPolicyT>::onSyncAdd(
220     MFieldBaseType * const, 
221     FieldContainer * const pObj)
223     RefCountPolicyType::addRef(pObj);
226 template<typename RefCountPolicyT> inline 
227 void ChildAccessHandler<RefCountPolicyT>::onSyncSub(
228     SFieldBaseType * const, 
229     FieldContainer * const pObj)
231     if(pObj != NULL)
232     {
233         Thread::getCurrentChangeList()->addDelayedSubRef<
234             RefCountPolicyT>(pObj);
235     }
238 template<typename RefCountPolicyT> inline 
239 void ChildAccessHandler<RefCountPolicyT>::onSyncSub(
240     MFieldBaseType * const, 
241     FieldContainer * const pObj)
243     if(pObj != NULL)
244     {
245         Thread::getCurrentChangeList()->addDelayedSubRef<
246             RefCountPolicyT>(pObj);
247     }
250 template<typename RefCountPolicyT> inline 
251 void ChildAccessHandler<RefCountPolicyT>::onSyncReplace(
252     SFieldBaseType * const pSField,
253     FieldContainer * const pOldObj,
254     FieldContainer * const pNewObj)
256     onSyncAdd(pSField, pNewObj);
257     onSyncSub(pSField, pOldObj);
260 template<typename RefCountPolicyT> inline 
261 void ChildAccessHandler<RefCountPolicyT>::onSyncReplace(
262     MFieldBaseType * const pMField,
263     FieldContainer * const pOldObj,
264     FieldContainer * const pNewObj)
266     onSyncAdd(pMField, pNewObj);
267     onSyncSub(pMField, pOldObj);
270 OSG_END_NAMESPACE