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.
*/
namespace Msp {
+/**
+A reference counting smart pointer. When the last RefPtr for the data gets
+destroyed, the data is deleted as well.
+*/
template<typename T>
class RefPtr
{
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();
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; }
operator bool() const { return data!=0; }
template<typename U>
- static RefPtr<T> cast_dynamic(const RefPtr<U> &p) { return RefPtr<T>(dynamic_cast<T *>(p.get()), p.count); }
+ static RefPtr<T> cast_dynamic(const RefPtr<U> &p) { return RefPtr<T>(dynamic_cast<T *>(p.data), p.count); }
template<typename U> friend class RefPtr;
private:
T *data;