• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

request_ptr.h

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 <[email protected]>
00007  *  Copyright (C) 2009 Johannes Singler <[email protected]>
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 
00028 
00030 
00031 #define STXXL_VERBOSE_request_ptr(msg) STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request_ptr::" << msg << " ptr=" << static_cast<void *>(ptr))
00032 
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:
00063     request_ptr(request * ptr_ = NULL) : ptr(ptr_)
00064     {
00065         STXXL_VERBOSE_request_ptr("(request*)");
00066         add_ref();
00067     }
00069     request_ptr(const request_ptr & p) : ptr(p.ptr)
00070     {
00071         STXXL_VERBOSE_request_ptr("(request_ptr&)");
00072         add_ref();
00073     }
00075     ~request_ptr()
00076     {
00077         STXXL_VERBOSE_request_ptr("~()");
00078         sub_ref();
00079     }
00082     request_ptr & operator = (const request_ptr & p)
00083     {
00084         // assert(p.ptr);
00085         return (*this = p.ptr); //call the operator below;
00086     }
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     }
00103     request & operator * () const
00104     {
00105         assert(ptr);
00106         return *ptr;
00107     }
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 
00125     request * get() const { return ptr; }
00126 
00128     bool valid() const { return ptr != NULL; }
00129 
00131     bool empty() const { return ptr == NULL; }
00132 };
00133 
00135 
00136 __STXXL_END_NAMESPACE
00137 
00138 #endif // !STXXL_IO__REQUEST_PTR_H_
00139 // vim: et:ts=4:sw=4

Generated by  doxygen 1.7.1