2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2007-02-02 20:41:13 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2007-02-02 20:41:13 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2007-02-02 20:41:13 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2007-02-02 20:41:13 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_DELETEHANDLER
|
|
|
|
#define OSG_DELETEHANDLER 1
|
|
|
|
|
|
|
|
#include <osg/Referenced>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
|
2012-03-29 23:08:15 +08:00
|
|
|
/** Class for overriding the default delete behaviour so that users can implement their own object
|
|
|
|
* deletion schemes.
|
|
|
|
* This might be used to implement a protection scheme that avoids
|
|
|
|
* multiple threads deleting objects unintentionally.
|
2007-02-02 20:41:13 +08:00
|
|
|
* Note, the DeleteHandler cannot itself be reference counted, otherwise it
|
|
|
|
* would be responsible for deleting itself!
|
2012-03-29 23:08:15 +08:00
|
|
|
* A static auto_ptr<> is used internally in Referenced.cpp to manage the
|
2007-02-02 20:41:13 +08:00
|
|
|
* DeleteHandler's memory.*/
|
2007-02-05 18:48:20 +08:00
|
|
|
class OSG_EXPORT DeleteHandler
|
2007-02-02 20:41:13 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-12-23 04:11:05 +08:00
|
|
|
typedef std::pair<unsigned int, const osg::Referenced*> FrameNumberObjectPair;
|
2007-02-02 20:41:13 +08:00
|
|
|
typedef std::list<FrameNumberObjectPair> ObjectsToDeleteList;
|
|
|
|
|
|
|
|
DeleteHandler(int numberOfFramesToRetainObjects=0);
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
virtual ~DeleteHandler();
|
2007-02-02 20:41:13 +08:00
|
|
|
|
2012-03-29 23:08:15 +08:00
|
|
|
/** Set the number of frames to retain objects that have been requested for deletion.
|
|
|
|
* When set to zero objects are deleted immediately, by setting to 1 they are kept around for an extra frame etc.
|
2007-12-11 01:30:18 +08:00
|
|
|
* The ability to retain objects for several frames is useful to prevent premature deletion when objects
|
2012-03-29 23:08:15 +08:00
|
|
|
* are still being used by graphics threads that use double buffering of rendering data structures with
|
2012-03-22 01:36:20 +08:00
|
|
|
* non ref_ptr<> pointers to scene graph elements.*/
|
2010-12-23 04:11:05 +08:00
|
|
|
void setNumFramesToRetainObjects(unsigned int numberOfFramesToRetainObjects) { _numFramesToRetainObjects = numberOfFramesToRetainObjects; }
|
2007-02-02 20:41:13 +08:00
|
|
|
|
2010-12-23 04:11:05 +08:00
|
|
|
unsigned int getNumFramesToRetainObjects() const { return _numFramesToRetainObjects; }
|
2007-02-02 20:41:13 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
/** Set the current frame number so that subsequent deletes get tagged as associated with this frame.*/
|
2010-12-23 04:11:05 +08:00
|
|
|
void setFrameNumber(unsigned int frameNumber) { _currentFrameNumber = frameNumber; }
|
2007-02-02 20:41:13 +08:00
|
|
|
|
|
|
|
/** Get the current frame number.*/
|
2010-12-23 04:11:05 +08:00
|
|
|
unsigned int getFrameNumber() const { return _currentFrameNumber; }
|
2007-02-02 20:41:13 +08:00
|
|
|
|
|
|
|
inline void doDelete(const Referenced* object) { delete object; }
|
|
|
|
|
2012-03-29 23:08:15 +08:00
|
|
|
/** Flush objects that are ready to be fully deleted.*/
|
2007-02-02 20:41:13 +08:00
|
|
|
virtual void flush();
|
|
|
|
|
|
|
|
/** Flush all objects that the DeleteHandler holds.
|
|
|
|
* Note, this should only be called if there are no threads running with non ref_ptr<> pointers, such as graphics threads.*/
|
|
|
|
virtual void flushAll();
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Request the deletion of an object.
|
|
|
|
* Depending on users implementation of DeleteHandler, the delete of the object may occur
|
2007-02-02 20:41:13 +08:00
|
|
|
* straight away or be delayed until doDelete is called.
|
|
|
|
* The default implementation does a delete straight away.*/
|
|
|
|
virtual void requestDelete(const osg::Referenced* object);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2007-07-09 23:02:03 +08:00
|
|
|
DeleteHandler(const DeleteHandler&):
|
|
|
|
_numFramesToRetainObjects(0),
|
|
|
|
_currentFrameNumber(0) {}
|
2007-02-05 21:44:16 +08:00
|
|
|
DeleteHandler operator = (const DeleteHandler&) { return *this; }
|
|
|
|
|
2010-12-23 04:11:05 +08:00
|
|
|
unsigned int _numFramesToRetainObjects;
|
|
|
|
unsigned int _currentFrameNumber;
|
|
|
|
OpenThreads::Mutex _mutex;
|
|
|
|
ObjectsToDeleteList _objectsToDelete;
|
2007-02-02 20:41:13 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|