| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_DELETEHANDLER |
|---|
| 15 | #define OSG_DELETEHANDLER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Referenced> |
|---|
| 18 | |
|---|
| 19 | #include <list> |
|---|
| 20 | |
|---|
| 21 | namespace osg { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /** Class for override the default delete behavior so that users can implement their own object |
|---|
| 25 | * deletion schemes. This might be done to help implement protection of multiple threads from deleting |
|---|
| 26 | * objects unintentionally. |
|---|
| 27 | * Note, the DeleteHandler cannot itself be reference counted, otherwise it |
|---|
| 28 | * would be responsible for deleting itself! |
|---|
| 29 | * An static auto_ptr<> is used internally in Referenced.cpp to manage the |
|---|
| 30 | * DeleteHandler's memory.*/ |
|---|
| 31 | class OSG_EXPORT DeleteHandler |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | typedef std::pair<unsigned int, const osg::Referenced*> FrameNumberObjectPair; |
|---|
| 36 | typedef std::list<FrameNumberObjectPair> ObjectsToDeleteList; |
|---|
| 37 | |
|---|
| 38 | DeleteHandler(int numberOfFramesToRetainObjects=0); |
|---|
| 39 | |
|---|
| 40 | virtual ~DeleteHandler(); |
|---|
| 41 | |
|---|
| 42 | /** Set the number of frames to retain objects that are have been requested for deletion. |
|---|
| 43 | * When set to zero objects are deleted immediately, by set to 1 there are kept around for an extra frame etc. |
|---|
| 44 | * The ability to retain objects for several frames is useful to prevent premature deletion when objects |
|---|
| 45 | * are still be used the graphics threads that are using double buffering of rendering data structures with |
|---|
| 46 | * non ref_ptr<> pointers to scene graph elements.*/ |
|---|
| 47 | void setNumFramesToRetainObjects(unsigned int numberOfFramesToRetainObjects) { _numFramesToRetainObjects = numberOfFramesToRetainObjects; } |
|---|
| 48 | |
|---|
| 49 | unsigned int getNumFramesToRetainObjects() const { return _numFramesToRetainObjects; } |
|---|
| 50 | |
|---|
| 51 | /** Set the current frame number so that subsequent deletes get tagged as associated with this frame.*/ |
|---|
| 52 | void setFrameNumber(unsigned int frameNumber) { _currentFrameNumber = frameNumber; } |
|---|
| 53 | |
|---|
| 54 | /** Get the current frame number.*/ |
|---|
| 55 | unsigned int getFrameNumber() const { return _currentFrameNumber; } |
|---|
| 56 | |
|---|
| 57 | inline void doDelete(const Referenced* object) { delete object; } |
|---|
| 58 | |
|---|
| 59 | /** Flush objects that ready to be fully deleted.*/ |
|---|
| 60 | virtual void flush(); |
|---|
| 61 | |
|---|
| 62 | /** Flush all objects that the DeleteHandler holds. |
|---|
| 63 | * Note, this should only be called if there are no threads running with non ref_ptr<> pointers, such as graphics threads.*/ |
|---|
| 64 | virtual void flushAll(); |
|---|
| 65 | |
|---|
| 66 | /** Request the deletion of an object. |
|---|
| 67 | * Depending on users implementation of DeleteHandler, the delete of the object may occur |
|---|
| 68 | * straight away or be delayed until doDelete is called. |
|---|
| 69 | * The default implementation does a delete straight away.*/ |
|---|
| 70 | virtual void requestDelete(const osg::Referenced* object); |
|---|
| 71 | |
|---|
| 72 | protected: |
|---|
| 73 | |
|---|
| 74 | DeleteHandler(const DeleteHandler&): |
|---|
| 75 | _numFramesToRetainObjects(0), |
|---|
| 76 | _currentFrameNumber(0) {} |
|---|
| 77 | DeleteHandler operator = (const DeleteHandler&) { return *this; } |
|---|
| 78 | |
|---|
| 79 | unsigned int _numFramesToRetainObjects; |
|---|
| 80 | unsigned int _currentFrameNumber; |
|---|
| 81 | OpenThreads::Mutex _mutex; |
|---|
| 82 | ObjectsToDeleteList _objectsToDelete; |
|---|
| 83 | |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | #endif |
|---|