/usr/include/ossim/base/ossimReferenced.h is in libossim-dev 1.8.16-4ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | /* -*-c++-*- libossim - Copyright (C) since 2004 Garrett Potts
* LICENSE: LGPL
* Author: Garrett Potts
*/
#ifndef ossimReferenced_HEADER
#define ossimReferenced_HEADER
#include <ossim/base/ossimConstants.h>
#include <OpenThreads/ScopedLock>
#include <OpenThreads/Mutex>
class OSSIMDLLEXPORT ossimReferenced
{
public:
ossimReferenced()
: theRefMutex(new OpenThreads::Mutex),
theRefCount(0)
{}
ossimReferenced(const ossimReferenced&)
: theRefMutex(new OpenThreads::Mutex),
theRefCount(0)
{}
inline ossimReferenced& operator = (const ossimReferenced&) { return *this; }
/*! increment the reference count by one, indicating that
this object has another pointer which is referencing it.*/
inline void ref() const;
/*! decrement the reference count by one, indicating that
a pointer to this object is referencing it. If the
reference count goes to zero, it is assumed that this object
is no longer referenced and is automatically deleted.*/
inline void unref() const;
/*! decrement the reference count by one, indicating that
a pointer to this object is referencing it. However, do
not delete it, even if ref count goes to 0. Warning, unref_nodelete()
should only be called if the user knows exactly who will
be resonsible for, one should prefer unref() over unref_nodelete()
as the later can lead to memory leaks.*/
inline void unref_nodelete() const
{
if (theRefMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*theRefMutex);
--theRefCount;
}
else
{
--theRefCount;
}
}
/*! return the number pointers currently referencing this object. */
inline int referenceCount() const { return theRefCount; }
protected:
virtual ~ossimReferenced();
mutable OpenThreads::Mutex* theRefMutex;
mutable int theRefCount;
};
inline void ossimReferenced::ref() const
{
if (theRefMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*theRefMutex);
++theRefCount;
}
else
{
++theRefCount;
}
}
inline void ossimReferenced::unref() const
{
bool needDelete = false;
if (theRefMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*theRefMutex);
--theRefCount;
needDelete = theRefCount<=0;
}
else
{
--theRefCount;
needDelete = theRefCount<=0;
}
if (needDelete)
{
delete this;
}
#if 0
--theRefCount;
if (theRefCount==0)
{
delete this;
}
#endif
}
#endif
|