fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Base / OSGStateChunk.cpp
blob8c821c4ff1360b943c28b8dcdaf84dfd34da28ff
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright(C) 2000-2002 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 "OSGConfig.h"
48 #include "OSGStateChunk.h"
49 #include "OSGLog.h"
52 OSG_BEGIN_NAMESPACE
54 /*! \ingroup GrpSystemStateBase
55 \nohierarchy
57 struct StateChunkClassCompare
59 bool operator()(const StateChunkClass *lhs,
60 const StateChunkClass *rhs)
62 return lhs->getPriority() < rhs->getPriority();
66 OSG_END_NAMESPACE
68 OSG_USING_NAMESPACE
70 // Documentation for this class is emited in the
71 // OSGStateChunkBase.cpp file.
72 // To modify it, please change the .fcd file (OSGStateChunk.fcd) and
73 // regenerate the base file.
75 /***************************************************************************\
76 * Types *
77 \***************************************************************************/
79 /*! \class OSG::StateChunkClass
81 See \ref StateChunkClass for the
82 conceptual background.
86 /*! \var OSG::StateChunkClass::_classId
87 The numerical ID associated with each StateChunkClass. It is used to
88 uniquely identify and quickly compare the class.
90 \dev The classId is consecutive in the number of slots allocated to the
91 chunk class, i.e. if a class has id 4 and 4 chunks, the next registered
92 class will get id 8. The classId is used as an index into the OSG::State's
93 chunk vector, which necessitates this behaviour. \enddev
94 */
96 std::vector<StateChunkClass *> *StateChunkClass::_classes = NULL;
97 std::vector<StateChunkClass *> *StateChunkClass::_initClasses = NULL;
99 /*! The global vector of known StateChunkClasses' number of concurrently
100 active slots. Use StateChunkClass::getNumSlots with the Class's ID to access
103 \dev See the OSG::StateChunkClass::_classId for details. \enddev
106 std::vector<UInt32 > *StateChunkClass::_numslots = NULL;
109 bool StateChunkClass::terminate(void)
111 delete _classes;
112 delete _initClasses;
113 delete _numslots;
115 return true;
118 bool StateChunkClass::initialize(void)
120 std::stable_sort(_initClasses->begin (),
121 _initClasses->end (),
122 StateChunkClassCompare());
124 _classes = new std::vector<StateChunkClass *>;
125 _numslots = new std::vector< UInt32 >;
127 for(UInt32 i = 0; i < _initClasses->size(); ++i)
129 (*_initClasses)[i]->_classId = UInt32(_classes->size());
131 for(UInt32 j = 0; j < (*_initClasses)[i]->_numSlots; ++j)
133 _classes ->push_back((*_initClasses)[i]);
134 _numslots->push_back((*_initClasses)[i]->_numSlots);
138 for(UInt32 i = 0; i < _classes->size(); ++i)
140 FDEBUG_GV(("[%d/%d][%d] %s %d %d\n",
142 StateChunkClass::getUsedSlots(),
143 (*_classes)[i]->_classId,
144 (*_classes)[i]->_className.c_str(),
145 (*_classes)[i]->_priority,
146 (*_classes)[i]->_numSlots));
149 return true;
152 OSG_BEGIN_NAMESPACE
154 StateChunkClass::StateChunkClassInit StateChunkClass::InitHelper;
156 OSG_END_NAMESPACE
158 /*! Constructor. The name is mandatory, the number of concurrently active slots
159 is optional, default is 1.
162 StateChunkClass::StateChunkClass(const Char8 *name,
163 UInt32 numslots,
164 UInt32 priority) :
165 _classId ( 0),
166 _numSlots (numslots),
167 _priority (priority),
168 _className( name)
170 if(!_initClasses)
172 _initClasses = new std::vector<StateChunkClass *>;
174 addPostFactoryExitFunction(&StateChunkClass::terminate);
177 _initClasses->push_back(this);
180 const Char8 *StateChunkClass::getName(void) const
182 return _className.c_str();
185 Int32 StateChunkClass::getNumSlots(void) const
187 return(*_numslots)[_classId];
190 /*! Access the name for the class whose id is index.
193 const Char8 *StateChunkClass::getName(UInt32 index)
195 if(index >=(*_classes).size())
196 return "<Unknown StatChunkClass!>";
198 return(*_classes)[index]->_className.c_str();
202 /*! \var OSG::StateChunkClass::iterator
204 Iterator type to access the chunk class list.
207 /*! Iterator to allow access to all known StateChunkClasses.
210 StateChunkClass::iterator StateChunkClass::begin(void)
212 return _classes->begin();
215 /*! Iterator to allow access to all known StateChunkClasses.
218 StateChunkClass::iterator StateChunkClass::end(void)
220 return _classes->end();
224 /***************************************************************************\
225 * Class variables *
226 \***************************************************************************/
228 /***************************************************************************\
229 * Class methods *
230 \***************************************************************************/
232 void StateChunk::initMethod(InitPhase ePhase)
234 Inherited::initMethod(ePhase);
237 /***************************************************************************\
238 * Instance methods *
239 \***************************************************************************/
241 /*-------------------------------------------------------------------------*\
242 - public -
243 \*-------------------------------------------------------------------------*/
246 /*------------- constructors & destructors --------------------------------*/
248 StateChunk::StateChunk(void) :
249 Inherited()
253 StateChunk::StateChunk(const StateChunk &source) :
254 Inherited(source)
258 StateChunk::~StateChunk(void)
262 UInt16 StateChunk::getChunkId(void)
264 return 0;
267 /*------------------------------- Sync -----------------------------------*/
269 void StateChunk::changed(ConstFieldMaskArg whichField,
270 UInt32 origin,
271 BitVector details)
273 Inherited::changed(whichField, origin, details);
276 /*------------------------------ Output ----------------------------------*/
278 void StateChunk::dump( UInt32 OSG_CHECK_ARG(uiIndent),
279 const BitVector OSG_CHECK_ARG(bvFlags )) const
281 SLOG << "Dump StateChunk NI" << std::endl;
285 /*------------------------------ State ------------------------------------*/
287 #ifndef OSG_DISABLE_DEPRECATED
288 void StateChunk::update(DrawEnv *)
291 #endif
293 /*! Check if the chunk is transparent, i.e. needs to be rendered after the
294 opaque objects.
297 bool StateChunk::isTransparent(void) const
299 return false;
302 void StateChunk::updateObjectDependencies(DrawEnv *pEnv,
303 UInt32 index)
307 /*---------------------- Chunk Class Access -------------------------------*/
309 const StateChunkClass *StateChunk::getClass(void) const
311 return NULL;
314 /*-------------------------- Comparison -----------------------------------*/
316 /*! Calculate how expensive it is to switch from one instance of the chunk
317 class to another. In most cases not implemented yet, will return 0.
320 Real32 StateChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
322 return 0;
325 bool StateChunk::operator <(const StateChunk &other) const
327 return this < &other;
330 /*! Compare two chunks. In most cases not implemented yet, will return false.
333 bool StateChunk::operator ==(const StateChunk &OSG_CHECK_ARG(other)) const
335 return false;
338 bool StateChunk::operator !=(const StateChunk &other) const
340 return !(*this == other);