Class Template unique_obj

Description
Examples
Headers
Reference
Synopsis
Class Template unique_obj
Function Template get
Function Template release
Function Template reset

Description

The class template unique_obj is a Smart Reference with the following characteristics:

Ownership may be transfered from an lvalue of type unique_obj using the function template move.

unique_obj is patterned on the template move_ptr introduced in [Hinnant1] as a safe replacement for std::auto_ptr. A free implementation of move_ptr is available here: Boost.MovePtr.

Examples

I. Receiving Ownership

By declaring a function to return a specialization of unique_obj, the programmer indicates that the function transfers ownership of the managed object to the caller and ensures that the object is freed if the caller ignores the return value.

    unique_obj<IScrollbar> NewScrollbar(const Style& style);

II. Relinquishing Ownership

By declaring a function with a parameter whose type is specialization of unique_obj, the programmer indicates that the function takes ownership of the managed object and ensures that the object will be freed unless it is explicitly released.

    void ListView::AddItem(unique_obj<ListItem> item);

III. RAII

unique_obj can be used like std::auto_ptr to ensure that resources are cleaned up at the end of a block in an exception-safe manner.

    void f()
    {
        unique_obj<IAnimal> animal(new Hound);
        std::cout << "name = " << animal.name() << "\n";
        animal.eat();
        animal.speak();
    }

In the above example, the dynamically allocate Hound is deleted before the function f finishes execution, even if an exception is thrown.

Headers

<boost/interfaces/unique_obj.hpp>

Reference

Synopsis

namespace boost { namespace interfaces {

template<typename Interface>
class unique_obj {
public:
    typedef typename fixed_view<Interface> element_type;

        // Constructors

    unique_obj();

    template<typename T>
    explicit unique_obj(T* t);

    unique_obj(const unique_obj& ptr);

    template<typename Subinterface>
    unique_obj(const unique_obj<Subinterface>& ptr);

    template<typename Subinterface>
    unique_obj(move_source< unique_obj<Subinterface> > src);

        // Destructor

    ~unique_obj();

        // Assignment

    unique_obj& operator=(unique_obj ptr);

    template<typename Subinterface>    
    unique_obj& operator=(unique_obj<Subinterface> ptr);

    operator unspecified-bool-type() const;
};

template<typename Interface>
fixed_view<Interface>* get(const unique_object<Interface>& obj);

template<typename Interface>
manual_ptr<Interface> release(unique_object<Interface>& obj);

template<typename Interface>
void reset(unique_object<Interface>& obj);

template<typename Interface, typename T>
void reset(unique_object<Interface>& obj, T* t);

} } // End namespace boost::interfaces

Class Template unique_obj

Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

unique_obj::element_type

    typedef typename fixed_view<T> element_type;

Used by the dereferencing operators, in place of the template parameter Interface, to help prevent resource leaks and double deletes. See Example.

unique_obj::unique_obj

    unique_obj();

Constructs an empty instance of unique_obj. Never throws.

    template<typename T>
    explicit unique_obj(T* t);
Template Parameters
T-

A class implementing Interface. The type T must be complete and have an accessible destructor, but need not be complete at the point t is freed.

Constructs an instance of unique_obj which takes ownership of the given object. Unless, after zero or more transfers of ownership, the object is released using unique_obj::release, it will eventually be freed correctly using delete.

    unique_obj(const unique_obj& ptr);

    template<typename Subinterface>
    unique_obj(const unique_obj<Subinterface>& ptr);

Constructs a copy of the given instance of unique_obj, taking ownership of the managed object. Never throws.

    template<typename Subinterface>
    unique_obj(move_source< unique_obj<Subinterface> > src);

Constructs an instance of unique_obj from the return value of the function move, taking ownership of the object managed by the instance of unique_obj on which move was invoked. Allows explicit transfer of ownership from an lvalue of type unique_obj when implicit tranfer would be forbidden.

Never throws.

unique_obj::~unique_obj

    ~unique_obj();

If there is currently a managed object, frees it using delete.

Throws only if the destructor of the bound object throws.

unique_obj::operator=

    unique_obj& operator=(unique_obj ptr);

    template<typename Subinterface>    
    unique_obj& operator=(unique_obj<Subinterface> ptr);

Takes ownership of the pointer managed by the given instance of unique_obj. Never throws.

unique_obj::operator unspecified-bool-type()

    operator unspecified-bool-type() const;

Returns an unspecified value which, when used in boolean contexts, is equivalent to get(*this) != 0. Never throws.

Function Template get

    template<typename Interface>
    element_type* get(const unique_obj<Interface>& obj);
Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

Returns a pointer through which member functions of the object bound to the given instance of unique_obj can be accessed. Never throws.

Function Template release

    template<typename Interface>
    manual_ptr<Interface>* release(unique_obj<Interface>& obj);
Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

Causes the given instance of unique_obj to relinguish ownership of the bound object, if any. Returns an instance of manual_ptr which can be used to access the member functions of the bound object and to free it. Never throws.

Function Template reset

    template<typename Interface>
    void reset(unique_obj<Interface>& obj);
Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

Causes the given instance of unique_obj to relinquish ownership of the bound object, if any, and free it using delete.

    template<typename Interface, typename T>
    void reset(unique_obj<Interface>& obj, T* t);
Template Parameters
Interface-

An interface defined using the Interface Definition Language (IDL).

T-

A class implementing Interface. The type T must be complete and have an accessible destructor, but need not be complete at the point t is freed.

Causes the given instance of unique_obj to relinquish ownership of the bound object, if any, and free it using delete. Next, takes ownership of the given object. Never throws


Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah