1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/impls/cpp/RefCountedPtr.h
Joel Martin 8a19f60386 Move implementations into impls/ dir
- 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.
2020-02-10 23:50:16 -06:00

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