Stxxl  1.3.2
request_ptr.h
1 /***************************************************************************
2  * include/stxxl/bits/io/request_ptr.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2009 Johannes Singler <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_IO__REQUEST_PTR_H_
15 #define STXXL_IO__REQUEST_PTR_H_
16 
17 #include <cassert>
18 
19 #include <stxxl/bits/namespace.h>
20 #include <stxxl/bits/io/request.h>
21 #include <stxxl/bits/verbose.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
28 
30 
31 #define STXXL_VERBOSE_request_ptr(msg) STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request_ptr::" << msg << " ptr=" << static_cast<void *>(ptr))
32 
35 {
36  request * ptr;
37  void add_ref()
38  {
39  if (ptr)
40  {
41  ptr->add_ref();
42  }
43  }
44  void sub_ref()
45  {
46  if (ptr)
47  {
48  if (ptr->sub_ref())
49  {
50  STXXL_VERBOSE_request_ptr("sub_ref(): the last ref, deleting");
51  delete ptr;
52  ptr = NULL;
53  }
54  else
55  {
56  STXXL_VERBOSE_request_ptr("sub_ref(): more refs left");
57  }
58  }
59  }
60 
61 public:
63  request_ptr(request * ptr_ = NULL) : ptr(ptr_)
64  {
65  STXXL_VERBOSE_request_ptr("(request*)");
66  add_ref();
67  }
69  request_ptr(const request_ptr & p) : ptr(p.ptr)
70  {
71  STXXL_VERBOSE_request_ptr("(request_ptr&)");
72  add_ref();
73  }
76  {
78  sub_ref();
79  }
83  {
84  // assert(p.ptr);
85  return (*this = p.ptr); //call the operator below;
86  }
90  {
91  STXXL_VERBOSE_request_ptr("operator=(request=" << static_cast<void *>(p) << ") {BEGIN}");
92  if (p != ptr)
93  {
94  sub_ref();
95  ptr = p;
96  add_ref();
97  }
98  STXXL_VERBOSE_request_ptr("operator=(request=" << static_cast<void *>(p) << ") {END}");
99  return *this;
100  }
104  {
105  assert(ptr);
106  return *ptr;
107  }
111  {
112  assert(ptr);
113  return ptr;
114  }
115 
116  bool operator == (const request_ptr & rp2) const
117  {
118  return ptr == rp2.ptr;
119  }
120 
125  request * get() const { return ptr; }
126 
128  bool valid() const { return ptr != NULL; }
129 
131  bool empty() const { return ptr == NULL; }
132 };
133 
135 
136 __STXXL_END_NAMESPACE
137 
138 #endif // !STXXL_IO__REQUEST_PTR_H_
139 // vim: et:ts=4:sw=4
bool empty() const
Returns true if object is not initialized.
Definition: request_ptr.h:131
Request with basic properties like file and offset.
Definition: request.h:39
request_ptr(const request_ptr &p)
Constructs an request_ptr from a request_ptr object.
Definition: request_ptr.h:69
request_ptr(request *ptr_=NULL)
Constructs an request_ptr from request pointer.
Definition: request_ptr.h:63
request & operator*() const
&quot;Star&quot; operator
Definition: request_ptr.h:103
request_ptr & operator=(const request_ptr &p)
Assignment operator from request_ptr object.
Definition: request_ptr.h:82
request * operator->() const
&quot;Arrow&quot; operator
Definition: request_ptr.h:110
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
~request_ptr()
Destructor.
Definition: request_ptr.h:75
#define STXXL_VERBOSE_request_ptr(msg)
A smart wrapper for request pointer.
Definition: request_ptr.h:31
bool valid() const
Returns true if object is initialized.
Definition: request_ptr.h:128