From 1e0ba312edbe0feea88737380ef1a71782779437 Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 17 Oct 2011 21:04:34 -0700 Subject: Redefined how the IO class works. The ref counting should now work properly. --- includes/Handle.h | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'includes/Handle.h') diff --git a/includes/Handle.h b/includes/Handle.h index 0e19e50..2f2a029 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -10,6 +10,9 @@ class ENoEnt : public GeneralException { ENoEnt(const char * name) : GeneralException(String("No such file or directory: `") + name + "'") { } }; +class IOBase; + +template class IO; class Handle { @@ -34,25 +37,41 @@ class Handle { Handle() : m_refCount(0) { } private: void addRef() { m_refCount++; } - void delRef() { if (--m_refCount == 0) { if (!isClosed()) close(); delete this; } } - int refCount() { return m_refCount; } + void delRef() { + if (--m_refCount == 0) { + if (!isClosed()) + close(); + delete this; + } + } + friend class IOBase; + template friend class IO; int m_refCount; }; -class IO { +class IOBase { public: - IO() : m_h(NULL) { } - IO(Handle * h) { setHandle(h); } - ~IO() { if (m_h) m_h->delRef(); } - IO(const IO & io) { setHandle(io.m_h); } - IO & operator=(const IO & io) { if (m_h) m_h->delRef(); setHandle(io.m_h); return *this; } - Handle * operator->() { Assert(m_h); return m_h; } + IOBase() : m_h(NULL) { } + ~IOBase() { if (m_h) m_h->delRef(); } protected: void setHandle(Handle * h) { m_h = h; m_h->addRef(); } - private: Handle * m_h; + template + friend class IO; +}; + +template +class IO : public IOBase { + public: + IO() { } + IO(T * h) { setHandle(h); } + IO(const IO & io) { setHandle(io.m_h); } + template + IO(const IO & io) { setHandle(io.m_h); } + IO & operator=(const IO & io) { if (m_h) m_h->delRef(); setHandle(io.m_h); return *this; } + T * operator->() { Assert(m_h); return dynamic_cast(m_h); } }; class SeekableHandle : public Handle { -- cgit v1.2.3