Stxxl  1.4.0
include/stxxl/bits/io/request_ptr.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/io/request_ptr.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2009 Johannes Singler <singler@ira.uka.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #ifndef STXXL_IO__REQUEST_PTR_H_
00015 #define STXXL_IO__REQUEST_PTR_H_
00016 
00017 #include <cassert>
00018 
00019 #include <stxxl/bits/namespace.h>
00020 #include <stxxl/bits/io/request.h>
00021 #include <stxxl/bits/verbose.h>
00022 
00023 
00024 __STXXL_BEGIN_NAMESPACE
00025 
00026 //! \addtogroup iolayer
00027 //! \{
00028 
00029 //! \brief A smart wrapper for \c request pointer.
00030 
00031 #define STXXL_VERBOSE_request_ptr(msg) STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request_ptr::" << msg << " ptr=" << static_cast<void *>(ptr))
00032 
00033 //! Implemented as reference counting smart pointer.
00034 class request_ptr
00035 {
00036     request * ptr;
00037     void add_ref()
00038     {
00039         if (ptr)
00040         {
00041             ptr->add_ref();
00042         }
00043     }
00044     void sub_ref()
00045     {
00046         if (ptr)
00047         {
00048             if (ptr->sub_ref())
00049             {
00050                 STXXL_VERBOSE_request_ptr("sub_ref(): the last ref, deleting");
00051                 delete ptr;
00052                 ptr = NULL;
00053             }
00054             else
00055             {
00056                 STXXL_VERBOSE_request_ptr("sub_ref(): more refs left");
00057             }
00058         }
00059     }
00060 
00061 public:
00062     //! \brief Constructs an \c request_ptr from \c request pointer
00063     request_ptr(request * ptr_ = NULL) : ptr(ptr_)
00064     {
00065         STXXL_VERBOSE_request_ptr("(request*)");
00066         add_ref();
00067     }
00068     //! \brief Constructs an \c request_ptr from a \c request_ptr object
00069     request_ptr(const request_ptr & p) : ptr(p.ptr)
00070     {
00071         STXXL_VERBOSE_request_ptr("(request_ptr&)");
00072         add_ref();
00073     }
00074     //! \brief Destructor
00075     ~request_ptr()
00076     {
00077         STXXL_VERBOSE_request_ptr("~()");
00078         sub_ref();
00079     }
00080     //! \brief Assignment operator from \c request_ptr object
00081     //! \return reference to itself
00082     request_ptr & operator = (const request_ptr & p)
00083     {
00084         // assert(p.ptr);
00085         return (*this = p.ptr); //call the operator below;
00086     }
00087     //! \brief Assignment operator from \c request pointer
00088     //! \return reference to itself
00089     request_ptr & operator = (request * p)
00090     {
00091         STXXL_VERBOSE_request_ptr("operator=(request=" << static_cast<void *>(p) << ") {BEGIN}");
00092         if (p != ptr)
00093         {
00094             sub_ref();
00095             ptr = p;
00096             add_ref();
00097         }
00098         STXXL_VERBOSE_request_ptr("operator=(request=" << static_cast<void *>(p) << ") {END}");
00099         return *this;
00100     }
00101     //! \brief "Star" operator
00102     //! \return reference to owned \c request object
00103     request & operator * () const
00104     {
00105         assert(ptr);
00106         return *ptr;
00107     }
00108     //! \brief "Arrow" operator
00109     //! \return pointer to owned \c request object
00110     request * operator -> () const
00111     {
00112         assert(ptr);
00113         return ptr;
00114     }
00115 
00116     bool operator == (const request_ptr & rp2) const
00117     {
00118         return ptr == rp2.ptr;
00119     }
00120 
00121     //! \brief Access to owned \c request object (synonym for \c operator->() )
00122     //! \return reference to owned \c request object
00123     //! \warning Creation another \c request_ptr from the returned \c request or deletion
00124     //!  causes unpredictable behaviour. Do not do that!
00125     request * get() const { return ptr; }
00126 
00127     //! \brief Returns true if object is initialized
00128     bool valid() const { return ptr != NULL; }
00129 
00130     //! \brief Returns true if object is not initialized
00131     bool empty() const { return ptr == NULL; }
00132 };
00133 
00134 //! \}
00135 
00136 __STXXL_END_NAMESPACE
00137 
00138 #endif // !STXXL_IO__REQUEST_PTR_H_
00139 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines