1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-28 19:23:53 +00:00
Aquaria/ExternalLibs/ttvfs/VFSSelfRefCounter.h

44 lines
1.1 KiB
C
Raw Permalink Normal View History

#ifndef SELFREFCOUNTER_H
#define SELFREFCOUNTER_H
#include "VFSDefines.h"
VFS_NAMESPACE_START
// self must point to the object that holds the counter.
template <class T, bool DELSELF = true> class SelfRefCounter
{
private:
T *self;
2014-04-06 17:19:33 +00:00
int c;
SelfRefCounter(SelfRefCounter& r); // forbid copy constructor
inline unsigned int _deref(void)
{
2014-04-06 17:19:33 +00:00
unsigned int cc = (unsigned int)(c - 1); // copy c, in case we get deleted
if(DELSELF && !cc)
{
delete self;
}
return cc;
}
public:
SelfRefCounter(T *p): self(p), c(1) {}
~SelfRefCounter() { /* DEBUG(ASSERT(c <= 1)); */ } // its ok if the last reference calls delete instead of _deref()
inline unsigned int count(void) { return c; }
// post-increment (dummy int)
2014-04-06 17:19:33 +00:00
inline unsigned int operator++(int) { unsigned int cc = c; ++c; return cc; }
inline unsigned int operator--(int) { unsigned int cc = c; _deref(); return cc; }
// pre-increment
2014-04-06 17:19:33 +00:00
inline unsigned int operator++(void) { return (unsigned int)++c; }
inline unsigned int operator--(void) { return _deref(); }
};
VFS_NAMESPACE_END
#endif