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