unique_objunique_objgetreleasereset
The class template unique_obj is a Smart Reference with the following characteristics:
unique_obj type at any given time.
unique_obj may be passed to and returned from functions, transfering ownership from the caller to the function in the first case, and from the function to the caller in the second.
unique_obj may neither be assigned to nor be used to initialize a second instance of a specialization of the template.
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.
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);
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);
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.
<boost/interfaces/unique_obj.hpp>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
unique_obj| Interface | - |
An interface defined using the Interface Definition Language (IDL). |
unique_obj::element_typetypedef 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_objunique_obj();
Constructs an empty instance of unique_obj. Never throws.
template<typename T> explicit unique_obj(T* t);
| T | - |
A class implementing |
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.
gettemplate<typename Interface> element_type* get(const unique_obj<Interface>& obj);
| 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.
releasetemplate<typename Interface> manual_ptr<Interface>* release(unique_obj<Interface>& obj);
| 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.
resettemplate<typename Interface> void reset(unique_obj<Interface>& obj);
| 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);
| Interface | - |
An interface defined using the Interface Definition Language (IDL). |
| T | - |
A class implementing |
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
Revised 16 Jan, 2005
© Copyright Jonathan Turkanis, 2005
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah