OSGAspectStore.inl

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2003 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 OSG_BEGIN_NAMESPACE
00040
00045 /*-------------------------------------------------------------------------*/
00046 /*                             Destructor                                  */
00047
00048 inline
00049 AspectStore::~AspectStore(void)
00050 {
00051 #ifdef OSG_ENABLE_MEMORY_DEBUGGING
00052     for(UInt32 i = 0; i < _vAspects.size(); ++i)
00053     {
00054         _vAspects[i] = reinterpret_cast<FieldContainer *>(0xDEADBEEF);
00055     }
00056 #endif
00057 }
00058
00059 inline
00060 FieldContainer *AspectStore::getPtr(const UInt32 uiAspect) const
00061 {
00062     if(uiAspect < _vAspects.size())
00063     {
00064 #ifdef OSG_ENABLE_MEMORY_DEBUGGING
00065         OSG_ASSERT(_vAspects[uiAspect] != reinterpret_cast<FieldContainer *>(0xDEADBEEF));
00066 #endif
00067 
00068         return _vAspects[uiAspect];
00069     }
00070     else
00071     {
00072         return NULL;
00073    }
00074 }
00075
00076
00077 inline
00078 FieldContainer *AspectStore::getPtr(void) const
00079 {
00080     return getPtr(Thread::getCurrentAspect());
00081 }
00082
00083 inline
00084 void AspectStore::setPtrForAspect(      FieldContainer *pContainer,
00085                                   const UInt32          uiAspect  )
00086 {
00087     _vAspects.resize(osgMax<UInt32>(_vAspects.size(), uiAspect + 1), NULL);
00088
00089     _vAspects[uiAspect] = pContainer;
00090 }
00091
00092 inline
00093 void AspectStore::removePtrForAspect(const UInt32 uiAspect)
00094 {
00095     if(uiAspect >= _vAspects.size())
00096     {
00097         return;
00098     }
00099
00100     _vAspects[uiAspect] = NULL;
00101 }
00102
00103 inline
00104 UInt32 AspectStore::getNumAspects(void) const
00105 {
00106     return _vAspects.size();
00107 }
00108
00109 /*-------------------------------------------------------------------------*/
00110 /*                        Reference Counting                               */
00111
00112 inline
00113 void AspectStore::addRef(void)
00114 {
00115     osgAtomicIncrement(&_refCount);
00116 }
00117
00118 inline
00119 void AspectStore::subRef(void)
00120 {
00121     RefCountStore tmpRefCount = osgAtomicExchangeAndAdd(&_refCount, -1);
00122
00123     if(tmpRefCount <= 1)
00124         delete this;
00125 }
00126
00127 inline
00128 Int32 AspectStore::getRefCount(void)
00129 {
00130     return osgAtomicExchangeAndAdd(&_refCount, 0);
00131 }
00132
00133 /*-------------------------------------------------------------------------*/
00134 /*                            Constructors                                 */
00135
00136 inline
00137 AspectStore::AspectStore(void) :
00138     _vAspects( ),
00139     _refCount(0)
00140 {
00141 }
00142
00143 inline
00144 AspectStore::AspectStore(const AspectStore &) :
00145     _vAspects( ),
00146     _refCount(0)
00147 {
00148 }
00149
00150 inline
00151 void AspectStore::fillOffsetArray(AspectOffsetStore &       oStore,
00152                                   FieldContainer    * const pRef  )
00153 {
00154     Char8 *pRefMem = reinterpret_cast<Char8 *>(pRef);
00155
00156     oStore.resize(ThreadManager::getNumAspects());
00157
00158     std::fill(oStore.begin(), oStore.end(), -1);
00159
00160     for(UInt32 i = 0; i < _vAspects.size(); ++i)
00161     {
00162         Char8 *pCurrMem = reinterpret_cast<Char8 *>(_vAspects[i]);
00163
00164         if(pCurrMem != pRefMem && pCurrMem != NULL)
00165         {
00166             oStore[i] = pCurrMem - pRefMem;
00167         }
00168     }
00169 }
00170
00171 inline
00172 void AspectStore::dump(void)
00173 {
00174     fprintf(stderr, "RC : %d\n", _refCount);
00175
00176     for(UInt32 i = 0; i < _vAspects.size(); ++i)
00177     {
00178         fprintf(stderr, "AS %d : %p\n", i, _vAspects[i]);
00179     }
00180 }
00181
00182 inline
00183 void addRef(const OSG::AspectStoreP pObject)
00184 {
00185     if(pObject != NULL)
00186         pObject->addRef();
00187 }
00188
00189 inline
00190 void subRef(const OSG::AspectStoreP pObject)
00191 {
00192     if(pObject != NULL)
00193         pObject->subRef();
00194 }
00195
00196
00197 OSG_END_NAMESPACE