mirror of
https://github.com/kanaka/mal.git
synced 2024-09-19 09:38:28 +03:00
8a19f60386
- Reorder README to have implementation list after "learning tool" bullet. - This also moves tests/ and libs/ into impls. It would be preferrable to have these directories at the top level. However, this causes difficulties with the wasm implementations which need pre-open directories and have trouble with paths starting with "../../". So in lieu of that, symlink those directories to the top-level. - Move the run_argv_test.sh script into the tests directory for general hygiene.
78 lines
1.6 KiB
C++
78 lines
1.6 KiB
C++
#ifndef INCLUDE_REFCOUNTEDPTR_H
|
|
#define INCLUDE_REFCOUNTEDPTR_H
|
|
|
|
#include "Debug.h"
|
|
|
|
#include <cstddef>
|
|
|
|
class RefCounted {
|
|
public:
|
|
RefCounted() : m_refCount(0) { }
|
|
virtual ~RefCounted() { }
|
|
|
|
const RefCounted* acquire() const { m_refCount++; return this; }
|
|
int release() const { return --m_refCount; }
|
|
int refCount() const { return m_refCount; }
|
|
|
|
private:
|
|
RefCounted(const RefCounted&); // no copy ctor
|
|
RefCounted& operator = (const RefCounted&); // no assignments
|
|
|
|
mutable int m_refCount;
|
|
};
|
|
|
|
template<class T>
|
|
class RefCountedPtr {
|
|
public:
|
|
RefCountedPtr() : m_object(0) { }
|
|
|
|
RefCountedPtr(T* object) : m_object(0)
|
|
{ acquire(object); }
|
|
|
|
RefCountedPtr(const RefCountedPtr& rhs) : m_object(0)
|
|
{ acquire(rhs.m_object); }
|
|
|
|
const RefCountedPtr& operator = (const RefCountedPtr& rhs) {
|
|
acquire(rhs.m_object);
|
|
return *this;
|
|
}
|
|
|
|
bool operator == (const RefCountedPtr& rhs) const {
|
|
return m_object == rhs.m_object;
|
|
}
|
|
|
|
bool operator != (const RefCountedPtr& rhs) const {
|
|
return m_object != rhs.m_object;
|
|
}
|
|
|
|
operator bool () const {
|
|
return m_object != NULL;
|
|
}
|
|
|
|
~RefCountedPtr() {
|
|
release();
|
|
}
|
|
|
|
T* operator -> () const { return m_object; }
|
|
T* ptr() const { return m_object; }
|
|
|
|
private:
|
|
void acquire(T* object) {
|
|
if (object != NULL) {
|
|
object->acquire();
|
|
}
|
|
release();
|
|
m_object = object;
|
|
}
|
|
|
|
void release() {
|
|
if ((m_object != NULL) && (m_object->release() == 0)) {
|
|
delete m_object;
|
|
}
|
|
}
|
|
|
|
T* m_object;
|
|
};
|
|
|
|
#endif // INCLUDE_REFCOUNTEDPTR_H
|