OpenSceneGraph 3.6.5
Referenced
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_REFERENCED
15#define OSG_REFERENCED 1
16
17#include <osg/Export>
18
19#include <OpenThreads/ScopedLock>
20#include <OpenThreads/Mutex>
21#include <OpenThreads/Atomic>
22
23#if !defined(_OPENTHREADS_ATOMIC_USE_MUTEX)
24# define _OSG_REFERENCED_USE_ATOMIC_OPERATIONS
25#endif
26
27namespace osg {
28
29// forward declare, declared after Referenced below.
30class DeleteHandler;
31class Observer;
32class ObserverSet;
33class State;
34
36template <typename T, T M()>
38{
39 depends_on() { M(); }
40};
41
44{
45
46 public:
47
48
50
52 explicit Referenced(bool threadSafeRefUnref);
53
55
56 inline Referenced& operator = (const Referenced&) { return *this; }
57
59 virtual void setThreadSafeRefUnref(bool /*threadSafe*/) {}
60
62#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
63 bool getThreadSafeRefUnref() const { return true; }
64#else
65 bool getThreadSafeRefUnref() const { return _refMutex!=0; }
66#endif
67
69#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
70 OpenThreads::Mutex* getRefMutex() const { return getGlobalReferencedMutex(); }
71#else
72 OpenThreads::Mutex* getRefMutex() const { return _refMutex; }
73#endif
74
76 static OpenThreads::Mutex* getGlobalReferencedMutex();
77
80 inline int ref() const;
81
86 inline int unref() const;
87
94 int unref_nodelete() const;
95
97 inline int referenceCount() const { return _refCount; }
98
99
102 {
103 #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
104 return static_cast<ObserverSet*>(_observerSet.get());
105 #else
106 return static_cast<ObserverSet*>(_observerSet);
107 #endif
108 }
109
112
114 void addObserver(Observer* observer) const;
115
117 void removeObserver(Observer* observer) const;
118#if 0
120 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
121
125 virtual void releaseGLObjects(osg::State* = 0) const {}
126#endif
127public:
128
129 friend class DeleteHandler;
130
133 static void setDeleteHandler(DeleteHandler* handler);
134
137
138
139 protected:
140
141 virtual ~Referenced();
142
143 void signalObserversAndDelete(bool signalDelete, bool doDelete) const;
144
146
147#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
148 mutable OpenThreads::AtomicPtr _observerSet;
149
150 mutable OpenThreads::Atomic _refCount;
151#else
152
153 mutable OpenThreads::Mutex* _refMutex;
154
155 mutable int _refCount;
156
157 mutable void* _observerSet;
158#endif
159};
160
161inline int Referenced::ref() const
162{
163#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
164 return ++_refCount;
165#else
166 if (_refMutex)
167 {
168 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
169 return ++_refCount;
170 }
171 else
172 {
173 return ++_refCount;
174 }
175#endif
176}
177
178inline int Referenced::unref() const
179{
180 int newRef;
181#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
182 newRef = --_refCount;
183 bool needDelete = (newRef == 0);
184#else
185 bool needDelete = false;
186 if (_refMutex)
187 {
188 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
189 newRef = --_refCount;
190 needDelete = newRef==0;
191 }
192 else
193 {
194 newRef = --_refCount;
195 needDelete = newRef==0;
196 }
197#endif
198
199 if (needDelete)
200 {
201 signalObserversAndDelete(true,true);
202 }
203 return newRef;
204}
205
206// intrusive_ptr_add_ref and intrusive_ptr_release allow
207// use of osg Referenced classes with boost::intrusive_ptr
208inline void intrusive_ptr_add_ref(Referenced* p) { p->ref(); }
209inline void intrusive_ptr_release(Referenced* p) { p->unref(); }
210
211}
212
213#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
void resizeGLObjectBuffers(osg::Object *object, unsigned int maxSize)
Definition Object:396
void intrusive_ptr_add_ref(Referenced *p)
Definition Referenced:208
void releaseGLObjects(osg::Object *object, osg::State *state=0)
Definition Object:400
void intrusive_ptr_release(Referenced *p)
Definition Referenced:209
Observer base class for tracking when objects are unreferenced (their reference count goes to 0) and ...
Definition Observer:25
Class used by osg::Referenced to track the observers associated with it.
Definition Observer:39
depends_on()
Definition Referenced:39
Base class for providing reference counted objects.
Definition Referenced:44
Referenced(bool threadSafeRefUnref)
Deprecated, Referenced is now always uses thread safe ref/unref, use default Referenced() constructor...
int unref_nodelete() const
Decrement the reference count by one, indicating that a pointer to this object is no longer referenci...
void addObserver(Observer *observer) const
Add a Observer that is observing this object, notify the Observer when this object gets deleted.
void signalObserversAndDelete(bool signalDelete, bool doDelete) const
OpenThreads::AtomicPtr _observerSet
Definition Referenced:148
virtual ~Referenced()
Referenced(const Referenced &)
int unref() const
Decrement the reference count by one, indicating that a pointer to this object is no longer referenci...
Definition Referenced:178
static DeleteHandler * getDeleteHandler()
Get a DeleteHandler.
void deleteUsingDeleteHandler() const
ObserverSet * getObserverSet() const
Get the ObserverSet if one is attached, otherwise return NULL.
Definition Referenced:101
bool getThreadSafeRefUnref() const
Get whether a mutex is used to ensure ref() and unref() are thread safe.
Definition Referenced:63
virtual void setThreadSafeRefUnref(bool)
Deprecated, Referenced is always theadsafe so there method now has no effect and does not need to be ...
Definition Referenced:59
int referenceCount() const
Return the number of pointers currently referencing this object.
Definition Referenced:97
OpenThreads::Atomic _refCount
Definition Referenced:150
OpenThreads::Mutex * getRefMutex() const
Get the mutex used to ensure thread safety of ref()/unref().
Definition Referenced:70
void removeObserver(Observer *observer) const
Remove Observer that is observing this object.
static OpenThreads::Mutex * getGlobalReferencedMutex()
Get the optional global Referenced mutex, this can be shared between all osg::Referenced.
int ref() const
Increment the reference count by one, indicating that this object has another pointer which is refere...
Definition Referenced:161
friend class DeleteHandler
Definition Referenced:129
static void setDeleteHandler(DeleteHandler *handler)
Set a DeleteHandler to which deletion of all referenced counted objects will be delegated.
ObserverSet * getOrCreateObserverSet() const
Get the ObserverSet if one is attached, otherwise create an ObserverSet, attach it,...
Encapsulates the current applied OpenGL modes, attributes and vertex arrays settings,...
Definition State:80
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.