DeleteHandler 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef OSG_DELETEHANDLER
  14. #define OSG_DELETEHANDLER 1
  15. #include <osg/Referenced>
  16. #include <list>
  17. namespace osg {
  18. /** Class for overriding the default delete behaviour so that users can implement their own object
  19. * deletion schemes.
  20. * This might be used to implement a protection scheme that avoids
  21. * multiple threads deleting objects unintentionally.
  22. * Note, the DeleteHandler cannot itself be reference counted, otherwise it
  23. * would be responsible for deleting itself!
  24. * A static auto_ptr<> is used internally in Referenced.cpp to manage the
  25. * DeleteHandler's memory.*/
  26. class OSG_EXPORT DeleteHandler
  27. {
  28. public:
  29. typedef std::pair<unsigned int, const osg::Referenced*> FrameNumberObjectPair;
  30. typedef std::list<FrameNumberObjectPair> ObjectsToDeleteList;
  31. DeleteHandler(int numberOfFramesToRetainObjects=0);
  32. virtual ~DeleteHandler();
  33. /** Set the number of frames to retain objects that have been requested for deletion.
  34. * When set to zero objects are deleted immediately, by setting to 1 they are kept around for an extra frame etc.
  35. * The ability to retain objects for several frames is useful to prevent premature deletion when objects
  36. * are still being used by graphics threads that use double buffering of rendering data structures with
  37. * non ref_ptr<> pointers to scene graph elements.*/
  38. void setNumFramesToRetainObjects(unsigned int numberOfFramesToRetainObjects) { _numFramesToRetainObjects = numberOfFramesToRetainObjects; }
  39. unsigned int getNumFramesToRetainObjects() const { return _numFramesToRetainObjects; }
  40. /** Set the current frame number so that subsequent deletes get tagged as associated with this frame.*/
  41. void setFrameNumber(unsigned int frameNumber) { _currentFrameNumber = frameNumber; }
  42. /** Get the current frame number.*/
  43. unsigned int getFrameNumber() const { return _currentFrameNumber; }
  44. inline void doDelete(const Referenced* object) { delete object; }
  45. /** Flush objects that are ready to be fully deleted.*/
  46. virtual void flush();
  47. /** Flush all objects that the DeleteHandler holds.
  48. * Note, this should only be called if there are no threads running with non ref_ptr<> pointers, such as graphics threads.*/
  49. virtual void flushAll();
  50. /** Request the deletion of an object.
  51. * Depending on users implementation of DeleteHandler, the delete of the object may occur
  52. * straight away or be delayed until doDelete is called.
  53. * The default implementation does a delete straight away.*/
  54. virtual void requestDelete(const osg::Referenced* object);
  55. protected:
  56. DeleteHandler(const DeleteHandler&):
  57. _numFramesToRetainObjects(0),
  58. _currentFrameNumber(0) {}
  59. DeleteHandler operator = (const DeleteHandler&) { return *this; }
  60. unsigned int _numFramesToRetainObjects;
  61. unsigned int _currentFrameNumber;
  62. OpenThreads::Mutex _mutex;
  63. ObjectsToDeleteList _objectsToDelete;
  64. };
  65. }
  66. #endif