diff options
| author | Rider Linden <rider@lindenlab.com> | 2016-09-16 14:43:35 -0700 | 
|---|---|---|
| committer | Rider Linden <rider@lindenlab.com> | 2016-09-16 14:43:35 -0700 | 
| commit | 884b03e8770a64aee22281518a5f3e2a7c0420ab (patch) | |
| tree | 70c723e32dcb5d857ea7f3c4684d586047a15a30 /indra/llcommon | |
| parent | 921cfa355cfc5130a55d77690e2ff80e1f370dcb (diff) | |
| parent | 68b8d2658a741617ae9844824090efe922da1edd (diff) | |
Merge
Diffstat (limited to 'indra/llcommon')
| -rw-r--r-- | indra/llcommon/llhandle.h | 81 | 
1 files changed, 81 insertions, 0 deletions
| diff --git a/indra/llcommon/llhandle.h b/indra/llcommon/llhandle.h index 401e4d759a..feb5f41848 100644 --- a/indra/llcommon/llhandle.h +++ b/indra/llcommon/llhandle.h @@ -28,8 +28,11 @@  #define LLHANDLE_H  #include "llpointer.h" +#include "llexception.h" +#include <stdexcept>  #include <boost/type_traits/is_convertible.hpp>  #include <boost/utility/enable_if.hpp> +#include <boost/throw_exception.hpp>  /**   * Helper object for LLHandle. Don't instantiate these directly, used @@ -213,4 +216,82 @@ private:  	mutable LLRootHandle<T> mHandle;  }; + + +class LLCheckedHandleBase +{ +public: +    class Stale : public LLException +    { +    public: +        Stale() : +            LLException("Attempt to access stale handle.") +        {} +    }; + +protected: +    LLCheckedHandleBase() { } + +}; + +/** + * This is a simple wrapper for Handles, allowing direct calls to the underlying  + * pointer. The checked handle will throw a Stale if an attempt  + * is made to access the object referenced by the handle and that object has  + * been destroyed. + **/ +template <typename T>  +class LLCheckedHandle: public LLCheckedHandleBase +{ +public: + +    LLCheckedHandle(LLHandle<T> handle): +        mHandle(handle) +    { } + +    /** +     * Test the underlying handle.  If it is no longer valid, throw a Stale exception. +     */ +    void check() const +    { +        T* ptr = mHandle.get(); +        if (!ptr) +            BOOST_THROW_EXCEPTION(Stale()); +    } + +    /** +     * Cast back to an appropriate handle +     */ +    operator LLHandle<T>() const +    { +        return mHandle; +    } + +    /** +     * Converts the LLCheckedHandle to a bool. Allows for if (chkdHandle) {}  +     * Does not throw. +     */ +    /*explicit*/ operator bool() const // explicit conversion operator not available with Linux compiler +    { +        return (mHandle.get() != NULL); +    } + +    /** +     * Attempt to call a method or access a member in the structure referenced  +     * by the handle.  If the handle no longer points to a valid structure  +     * throw a Stale. +     */ +    T* operator ->() const +    { +        T* ptr = mHandle.get(); +        if (!ptr) +            BOOST_THROW_EXCEPTION(Stale()); +        return ptr; +    } + +private: + +    LLHandle<T> mHandle; +}; +  #endif | 
