unique_interface_ptr

A unique_interface_ptr is a smart pointer which represents exclusive ownership of an object that implements interface IObject and will call the Destroy method when it destructs.

Consider the following $interface definitions and a class that implements them:


$interface+ Ix : ceda::IObject
{
    int get() const;
};

$interface+ Iy : Ix
{
    void set(int x);
};

$class X isa Iy
{
public:
    X(int x) : x(x) {}
    int get() const { return x; }
    void set(int v) { x = v; }
private:    
    int x;
};

X implements interface IObject so therefore a Destroy method is generated by the compiler which calls delete this.


void X::Destroy() const
{
    delete this;
}

In the following example the variable p which is a unique_interface_ptr takes ownership of the dynamically allocated instance of class X ensuring it is deleted:


void main()
{
    ceda::unique_interface_ptr<Iy> p(new X(10));
    assert( p->get() == 10 );
}

If D is a sub-interface of B, then unique_interface_ptr<D> is implicitly convertible to unique_interface_ptr<B>.

In many ways a unique_interface_ptr is analogous to a std:::unique_ptr :