From: Mikko Rasa Date: Mon, 11 Jun 2007 06:37:33 +0000 (+0000) Subject: Win32 doesn't have execinfo.h, so don't include it X-Git-Tag: 1.0~25 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=d61b4678e4e4a2a3661a2472dc7349cc9fd5eaad;p=libs%2Fcore.git Win32 doesn't have execinfo.h, so don't include it Add KeyError Add release() to RefPtr --- diff --git a/source/core/error.h b/source/core/error.h index eecda49..d030c41 100644 --- a/source/core/error.h +++ b/source/core/error.h @@ -39,6 +39,15 @@ public: InvalidParameterValue(const std::string &w_): Exception(w_) { } }; +/** +Thrown when a lookup from a map fails. +*/ +class KeyError: public Exception +{ +public: + KeyError(const std::string &w_): Exception(w_) { } +}; + /** Thrown when the current object state doesn't allow the requested action. */ diff --git a/source/core/refptr.h b/source/core/refptr.h index ee020e0..a44c13c 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -9,6 +9,10 @@ Distributed under the LGPL namespace Msp { +/** +A reference counting smart pointer. When the last RefPtr for the data gets +destroyed, the data is deleted as well. +*/ template class RefPtr { @@ -16,8 +20,6 @@ public: RefPtr(): data(0), count(0) { } RefPtr(T *d): data(d), count(0) { if(data) count=new unsigned(1); } RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } - ~RefPtr() { decref(); } - RefPtr &operator=(const RefPtr &p) { decref(); @@ -26,6 +28,22 @@ public: incref(); return *this; } + + ~RefPtr() { decref(); } + + /** + Makes the RefPtr release its reference of the data. Note that if there are + other RefPtrs left with the same data, it might still get deleted + automatically. + */ + T *release() + { + T *d=data; + data=0; + decref(); + count=0; + return d; + } T *get() const { return data; } T &operator*() const { return *data; } @@ -33,7 +51,7 @@ public: operator bool() const { return data!=0; } template - static RefPtr cast_dynamic(const RefPtr &p) { return RefPtr(dynamic_cast(p.get()), p.count); } + static RefPtr cast_dynamic(const RefPtr &p) { return RefPtr(dynamic_cast(p.data), p.count); } template friend class RefPtr; private: T *data; diff --git a/source/debug/backtrace.cpp b/source/debug/backtrace.cpp index 2078996..c0bfd9d 100644 --- a/source/debug/backtrace.cpp +++ b/source/debug/backtrace.cpp @@ -4,7 +4,9 @@ This file is part of libmspcore Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#ifndef WIN32 #include +#endif #include "backtrace.h" using namespace std;