fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Dynamics / Base / OSGFrameHandler.cpp
blobee2150eff8ff02f1682f9d1943b3b08f076fae7f
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 "OSGConfig.h"
48 #include "OSGFrameHandler.h"
50 OSG_BEGIN_NAMESPACE
52 // Documentation for this class is emitted in the
53 // OSGFrameHandlerBase.cpp file.
54 // To modify it, please change the .fcd file (OSGFrameHandler.fcd) and
55 // regenerate the base file.
57 /***************************************************************************\
58 * Class variables *
59 \***************************************************************************/
61 bool FrameHandler::_bRegistersInstanceRelease = false;
62 FrameHandlerUnrecPtr FrameHandler::_pGlobalInstance = NULL;
64 /***************************************************************************\
65 * Class methods *
66 \***************************************************************************/
68 void FrameHandler::initMethod(InitPhase ePhase)
70 Inherited::initMethod(ePhase);
72 if(ePhase == TypeObject::SystemPost)
77 FrameHandler *FrameHandler::the(void)
79 if(GlobalSystemState != Running)
80 return NULL;
82 if(_pGlobalInstance == NULL)
84 FrameHandlerUnrecPtr pHandler = FrameHandler::create();
86 setGlobalInstance(pHandler);
89 return _pGlobalInstance;
92 void FrameHandler::setGlobalInstance(FrameHandler *pHandler)
94 if(_bRegistersInstanceRelease == false)
96 _bRegistersInstanceRelease = true;
98 addPreFactoryExitFunction(&FrameHandler::releaseGlobalInstance);
101 _pGlobalInstance = pHandler;
104 bool FrameHandler::releaseGlobalInstance(void)
106 _pGlobalInstance = NULL;
108 return true;
111 /***************************************************************************\
112 * Instance methods *
113 \***************************************************************************/
115 /*-------------------------------------------------------------------------*\
116 - private -
117 \*-------------------------------------------------------------------------*/
119 /*----------------------- constructors & destructors ----------------------*/
121 FrameHandler::FrameHandler(void) :
122 Inherited (),
123 _mfFrameTasks (),
124 _mfUninitializedFrameTasks(),
125 _mfInitializedFrameTasks ()
129 FrameHandler::FrameHandler(const FrameHandler &source) :
130 Inherited (source),
131 _mfFrameTasks ( ),
132 _mfUninitializedFrameTasks( ),
133 _mfInitializedFrameTasks ( )
137 FrameHandler::~FrameHandler(void)
141 /*----------------------------- class specific ----------------------------*/
143 void FrameHandler::changed(ConstFieldMaskArg whichField,
144 UInt32 origin,
145 BitVector details)
147 Inherited::changed(whichField, origin, details);
150 void FrameHandler::dump( UInt32 ,
151 const BitVector ) const
153 SLOG << "Dump FrameHandler NI" << std::endl;
156 void FrameHandler::addTask(FrameTaskInterface *pTask,
157 bool bNoFrameFunction)
159 if(pTask == NULL)
160 return;
162 if(bNoFrameFunction == false)
163 this->pushToFrameTasks (pTask);
165 this->pushToUninitializedFrameTasks(pTask);
168 void FrameHandler::removeTask(FrameTaskInterface *pTask)
170 if(pTask != NULL)
172 bool rc = this->removeObjFromFrameTasks (pTask);
173 rc |= this->removeObjFromInitializedFrameTasks (pTask);
175 bool rcU = this->removeObjFromUninitializedFrameTasks(pTask);
177 if(rc == true && rcU == false)
179 pTask->shutdown();
184 bool FrameHandler::init(void)
186 bool returnValue = true;
188 InterfaceStoreConstIt tIt = this->getMFUninitializedFrameTasks()->begin();
189 InterfaceStoreConstIt tEnd = this->getMFUninitializedFrameTasks()->end ();
191 while(tIt != tEnd)
193 returnValue &= (*tIt)->init();
195 this->pushToInitializedFrameTasks(*tIt);
197 ++tIt;
200 this->clearUninitializedFrameTasks();
202 return returnValue;
205 void FrameHandler::frame(Time frameTime)
207 if(_mfUninitializedFrameTasks.size() != 0)
209 this->init();
212 setCurrTime(frameTime);
214 if(_sfStartTime.getValue() < 0.0)
216 setStartTime(_sfCurrTime.getValue());
217 setLastTime(0.f);
220 _sfCurrTime.getValue() -= _sfStartTime.getValue();
222 if(_sfPaused.getValue() == false)
224 SFTime *pSFTimeStamp = editSFTimeStamp();
226 if(_sfConstantTime.getValue() == true)
228 pSFTimeStamp->getValue() += _sfConstantTimeStep.getValue();
230 if(pSFTimeStamp->getValue() < 0.)
231 pSFTimeStamp->setValue(0.0);
233 else
235 pSFTimeStamp->getValue() +=
236 (_sfCurrTime.getValue() - _sfLastTime.getValue()) *
237 _sfTimeScale.getValue();
239 if(pSFTimeStamp->getValue() < 0.)
240 pSFTimeStamp->setValue(0.0);
244 setLastTime(_sfCurrTime.getValue());
246 ++(editSFFrameCount()->getValue());
248 callTasks();
251 void FrameHandler::frame(void)
253 frame(getSystemTime());
256 void FrameHandler::shutdown(void)
258 InterfaceStoreConstIt tIt = getMFInitializedFrameTasks()->begin();
259 InterfaceStoreConstIt tEnd = getMFInitializedFrameTasks()->end ();
261 while(tIt != tEnd)
263 (*tIt)->shutdown();
265 ++tIt;
269 void FrameHandler::callTasks(void)
271 Int32 currPrio = TypeTraits<Int32>::getMin();
272 InterfaceStoreConstIt tIt = getMFFrameTasks()->begin();
273 InterfaceStoreConstIt tEnd = getMFFrameTasks()->end ();
275 while(tIt != tEnd)
277 if((*tIt)->getPriority() > currPrio)
279 currPrio = (*tIt)->getPriority();
280 commitChanges();
283 (*tIt)->frame(_sfTimeStamp.getValue(), _sfFrameCount.getValue());
285 ++tIt;
289 //! Get the FrameHandler::_mfFrameTasks field.
290 const FrameHandler::InterfaceStore *FrameHandler::getMFFrameTasks(void) const
292 return &_mfFrameTasks;
295 //! Get the FrameHandler::_mfUninitializedFrameTasks field.
296 const FrameHandler::InterfaceStore *
297 FrameHandler::getMFUninitializedFrameTasks(void) const
299 return &_mfUninitializedFrameTasks;
302 //! Get the FrameHandler::_mfUninitializedFrameTasks field.
303 const FrameHandler::InterfaceStore *
304 FrameHandler::getMFInitializedFrameTasks(void) const
306 return &_mfInitializedFrameTasks;
309 void FrameHandler::pushToFrameTasks(FrameTaskInterface * const value)
311 InterfaceStoreIt tIt = std::lower_bound(_mfFrameTasks.begin(),
312 _mfFrameTasks.end (), value,
313 FrameTaskLess() );
315 _mfFrameTasks.insert(tIt, value);
318 void FrameHandler::pushToUninitializedFrameTasks(
319 FrameTaskInterface * const value)
321 _mfUninitializedFrameTasks.push_back(value);
324 void FrameHandler::pushToInitializedFrameTasks(
325 FrameTaskInterface * const value)
327 _mfInitializedFrameTasks.push_back(value);
331 bool FrameHandler::removeObjFromFrameTasks(FrameTaskInterface * const value)
333 bool returnValue = false;
335 InterfaceStoreIt it = std::find(_mfFrameTasks.begin(),
336 _mfFrameTasks.end (), value);
338 if(it != _mfFrameTasks.end())
340 _mfFrameTasks.erase(it);
342 returnValue = true;
345 return returnValue;
348 bool FrameHandler::removeObjFromUninitializedFrameTasks(
349 FrameTaskInterface * const value)
351 bool returnValue = false;
353 InterfaceStoreIt it = std::find(_mfUninitializedFrameTasks.begin(),
354 _mfUninitializedFrameTasks.end (), value);
356 if(it != _mfUninitializedFrameTasks.end())
358 _mfUninitializedFrameTasks.erase(it);
360 returnValue = true;
363 return returnValue;
366 bool FrameHandler::removeObjFromInitializedFrameTasks(
367 FrameTaskInterface * const value)
369 bool returnValue = false;
371 InterfaceStoreIt it = std::find(_mfInitializedFrameTasks.begin(),
372 _mfInitializedFrameTasks.end (), value);
374 if(it != _mfInitializedFrameTasks.end())
376 _mfInitializedFrameTasks.erase(it);
378 returnValue = true;
381 return returnValue;
384 void FrameHandler::clearFrameTasks(void)
386 _mfFrameTasks.clear();
389 void FrameHandler::clearUninitializedFrameTasks(void)
391 _mfUninitializedFrameTasks.clear();
394 void FrameHandler::clearInitializedFrameTasks(void)
396 _mfInitializedFrameTasks.clear();
399 void FrameHandler::resolveLinks(void)
401 Inherited::resolveLinks();
403 static_cast<FrameHandler *>(this)->clearFrameTasks();
404 static_cast<FrameHandler *>(this)->clearUninitializedFrameTasks();
405 static_cast<FrameHandler *>(this)->clearInitializedFrameTasks ();
408 OSG_END_NAMESPACE