Struct ComPtr

Synopsis

#include <Source/Falcor/Core/API/Shader.h>

template<typename T>
struct dlldecl ComPtr

Description

Minimal smart pointer for working with COM objects.

Methods

ComPtrInitialize to a null pointer.
~ComPtrRelease reference to the pointed-to object.
attachTransfer ownership of reference from the caller.
detachTransfer ownership of reference to the caller.
getGet the underlying object pointer.
operator T*Cast to object pointer type.
operator&
operator*Dereference underlying pointer.
operator->Access members of underlying object.
operator=Add a new reference to an existing object.
readRefGet a readable reference suitable for use as a function input argument.
setNullClear out object pointer.
swapSwap pointers with another reference.
writeRefGet a writable reference suitable for use as a function output argument.

Source

Lines 38-130 in Source/Falcor/Core/API/Shader.h.

template<typename T>
struct dlldecl ComPtr
{
public:
    /// Type of the smart pointer itself
    typedef ComPtr ThisType;
    /// Initialize to a null pointer.
    ComPtr() : mpObject(nullptr) {}
    ComPtr(nullptr_t) : mpObject(nullptr) {}
    /// Release reference to the pointed-to object.
    ~ComPtr() { if (mpObject) (mpObject)->Release(); }
    /// Add a new reference to an existing object.
    explicit ComPtr(T* pObject) : mpObject(pObject) { if (pObject) (pObject)->AddRef(); }
    /// Add a new reference to an existing object.
    ComPtr(const ThisType& rhs) : mpObject(rhs.mpObject) { if (mpObject) (mpObject)->AddRef(); }
    /// Add a new reference to an existing object
    T* operator=(T* in)
    {
        if(in) in->AddRef();
        if(mpObject) mpObject->Release();
        mpObject = in;
        return in;
    }
    /// Add a new reference to an existing object
    const ThisType& operator=(const ThisType& rhs)
    {
        if(rhs.mpObject) rhs.mpObject->AddRef();
        if(mpObject) mpObject->Release();
        mpObject = rhs.mpObject;
        return *this;
    }
    /// Transfer ownership of a reference.
    ComPtr(ThisType&& rhs) : mpObject(rhs.mpObject) { rhs.mpObject = nullptr; }
    /// Transfer ownership of a reference.
    ComPtr& operator=(ThisType&& rhs) { T* swap = mpObject; mpObject = rhs.mpObject; rhs.mpObject = swap; return *this; }
    /// Clear out object pointer.
    void setNull()
    {
        if( mpObject )
        {
            mpObject->Release();
            mpObject = nullptr;
        }
    }
    /// Swap pointers with another reference.
    void swap(ThisType& rhs)
    {
        T* tmp = mpObject;
        mpObject = rhs.mpObject;
        rhs.mpObject = tmp;
    }
    /// Get the underlying object pointer.
    T* get() const { return mpObject; }
    /// Cast to object pointer type.
    operator T*() const { return mpObject; }
    /// Access members of underlying object.
    T* operator->() const { return mpObject; }
    /// Dereference underlying pointer.
    T& operator*() { return *mpObject; }
    /// Transfer ownership of reference to the caller.
    T* detach() { T* ptr = mpObject; mpObject = nullptr; return ptr; }
    /// Transfer ownership of reference from the caller.
    void attach(T* in) { T* old = mpObject; mpObject = in; if(old) old->Release(); }
    /// Get a writable reference suitable for use as a function output argument.
    T** writeRef() { setNull(); return &mpObject; }
    /// Get a readable reference suitable for use as a function input argument.
    T*const* readRef() const { return &mpObject; }
protected:
    // Disabled: do not take the address of a smart pointer.
    T** operator&();
    /// The underlying raw object pointer
    T* mpObject;
};





Add Discussion as Guest

Log in to DocsForge