better comment and operators -> and * for AutoPtr

master
Marc Wäckerlin 20 years ago
parent 0b13bb321d
commit 5b2783e877
  1. 52
      mrw/auto.hpp

@ -9,6 +9,9 @@
@license LGPL, see file <a href="license.html">COPYING</a> @license LGPL, see file <a href="license.html">COPYING</a>
$Log$ $Log$
Revision 1.6 2004/10/11 16:48:29 marc
better comment and operators -> and * for AutoPtr
Revision 1.5 2004/10/11 16:05:02 marc Revision 1.5 2004/10/11 16:05:02 marc
new AutoPtr new AutoPtr
@ -75,8 +78,7 @@ namespace mrw {
typedef mrw::AutoResource<T, void(*)(void*), &free, int, 0> Free; typedef mrw::AutoResource<T, void(*)(void*), &free, int, 0> Free;
}; };
@endcode @endcode
@param RESOURCE_TYPE type of the resource to manage
@param RESOURCE_TYPE type of the resorce to manage
@param FUNCTION_PTR type of the function that frees the resource @param FUNCTION_PTR type of the function that frees the resource
@param FREE_FUNCTION the function that frees the resource @param FREE_FUNCTION the function that frees the resource
@param INITIAL_VALUE_TYPE type of the initial value @param INITIAL_VALUE_TYPE type of the initial value
@ -109,7 +111,7 @@ namespace mrw {
AutoResource& operator=(RESOURCE_TYPE res) throw(std::bad_exception) { AutoResource& operator=(RESOURCE_TYPE res) throw(std::bad_exception) {
return reset(res); return reset(res);
} }
/// @brief Takeover ownership from another AutoResorce. /// @brief Takeover ownership from another AutoResource.
/// Calls @c reset() from @c this and @c release() from @c other. /// Calls @c reset() from @c this and @c release() from @c other.
AutoResource& operator=(AutoResource& other) throw(std::bad_exception) { AutoResource& operator=(AutoResource& other) throw(std::bad_exception) {
return reset(other.release()); return reset(other.release());
@ -118,6 +120,11 @@ namespace mrw {
operator const RESOURCE_TYPE&() const throw(std::bad_exception) { operator const RESOURCE_TYPE&() const throw(std::bad_exception) {
return _res; return _res;
} }
/// @brief find out, if a value is set
/// @return @c true: resource is valid
operator bool() const throw(std::bad_exception) {
return _res!=INITIAL_VALUE;
}
/// @brief get the resetted resource for resetting it. /// @brief get the resetted resource for resetting it.
/// Calls @c reset and returns the cleaned resource. /// Calls @c reset and returns the cleaned resource.
/// The intention is, that you can safely assign it a new value /// The intention is, that you can safely assign it a new value
@ -126,18 +133,13 @@ namespace mrw {
reset(); reset();
return _res; return _res;
} }
/// @brief find out, if a value is set
/// @return @c true: resource is valid
operator bool() const throw(std::bad_exception) {
return _res!=INITIAL_VALUE;
}
/// @brief Give away ownership of the resource. /// @brief Give away ownership of the resource.
/// @return old resource /// @return old resource
RESOURCE_TYPE release() throw(std::bad_exception) { RESOURCE_TYPE release() throw(std::bad_exception) {
RESOURCE_TYPE res(_res); _res=INITIAL_VALUE; RESOURCE_TYPE res(_res); _res=INITIAL_VALUE;
return res; return res;
} }
/// @brief Assign a new resorce. /// @brief Assign a new resource.
/// The old resource of @c this is freed if necessary. /// The old resource of @c this is freed if necessary.
AutoResource& reset(RESOURCE_TYPE res = INITIAL_VALUE) AutoResource& reset(RESOURCE_TYPE res = INITIAL_VALUE)
throw(std::bad_exception) { throw(std::bad_exception) {
@ -149,6 +151,18 @@ namespace mrw {
RESOURCE_TYPE _res; ///< the resource to be managed RESOURCE_TYPE _res; ///< the resource to be managed
}; };
/** @brief Automatically deletes a pointer when destructed.
@pre #include <mrw/auto.hpp>
mrw::AutoPtr is a replacement for std::auto_ptr. The problem with
standard std::auto_ptr is, that it cannot be stored in a std::map.
@warning Use this class with prudence! Should I ever find out,
how to work around the std::auto_ptr&nbsp;/ std::map problem,
then this class may become deprecated.
@param T type of the pointer to manage
*/
template <typename T> template <typename T>
class AutoPtr { class AutoPtr {
public: public:
@ -170,7 +184,7 @@ namespace mrw {
AutoPtr& operator=(T* res) throw(std::bad_exception) { AutoPtr& operator=(T* res) throw(std::bad_exception) {
return reset(res); return reset(res);
} }
/// @brief Takeover ownership from another AutoResorce. /// @brief Takeover ownership from another AutoResource.
/// Calls @c reset() from @c this and @c release() from @c other. /// Calls @c reset() from @c this and @c release() from @c other.
AutoPtr& operator=(AutoPtr& other) throw(std::bad_exception) { AutoPtr& operator=(AutoPtr& other) throw(std::bad_exception) {
return reset(other.release()); return reset(other.release());
@ -179,6 +193,15 @@ namespace mrw {
operator T* const() const throw(std::bad_exception) { operator T* const() const throw(std::bad_exception) {
return _res; return _res;
} }
/// @brief find out, if a value is set
/// @return @c true: resource is valid
operator bool() const throw(std::bad_exception) {
return _res!=0;
}
/// @brief Access the AutoPtr like a normal pointer.
T*const operator->() {return _res;}
/// @brief Dereference the AutoPtr like a normal pointer.
T& operator*() {return *_res;}
/// @brief get the resetted resource for resetting it. /// @brief get the resetted resource for resetting it.
/// Calls @c reset and returns the cleaned resource. /// Calls @c reset and returns the cleaned resource.
/// The intention is, that you can safely assign it a new value /// The intention is, that you can safely assign it a new value
@ -187,18 +210,13 @@ namespace mrw {
reset(); reset();
return _res; return _res;
} }
/// @brief find out, if a value is set
/// @return @c true: resource is valid
operator bool() const throw(std::bad_exception) {
return _res!=0;
}
/// @brief Give away ownership of the resource. /// @brief Give away ownership of the resource.
/// @return old resource /// @return old resource
T* release() throw(std::bad_exception) { T* release() throw(std::bad_exception) {
T* res(_res); _res=0; T* res(_res); _res=0;
return res; return res;
} }
/// @brief Assign a new resorce. /// @brief Assign a new resource.
/// The old resource of @c this is freed if necessary. /// The old resource of @c this is freed if necessary.
AutoPtr& reset(T* res = 0) AutoPtr& reset(T* res = 0)
throw(std::bad_exception) { throw(std::bad_exception) {
@ -220,7 +238,7 @@ namespace mrw {
*/ */
typedef mrw::AutoResource<int, int(*)(int), &close, int, -1> AutoFile; typedef mrw::AutoResource<int, int(*)(int), &close, int, -1> AutoFile;
/** @brief Resorce handle for @c mmap. /** @brief Resource handle for @c mmap.
It integrates pointer and size of a memory mapped file similar It integrates pointer and size of a memory mapped file similar
to a @c std::pair. to a @c std::pair.

Loading…
Cancel
Save