STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
external_shared_ptr.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/external_shared_ptr.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2011 Daniel Godas-Lopez <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_EXTERNAL_SHARED_PTR_HEADER
14 #define STXXL_COMMON_EXTERNAL_SHARED_PTR_HEADER
15 
16 #include <stxxl/bits/namespace.h>
17 #include <ostream>
18 
20 
21 //! \addtogroup support
22 //! \{
23 
24 /*!
25  * This class takes a shared pointer, increments its reference count and wraps
26  * it in a way that the resulting object can be copied, dumped to disk, and
27  * destroyed without affecting the refcount. When the object is retrieved from
28  * disk and recreated on internal memory, it will still hold a reference to the
29  * same memory block and can be used right away by calling the "get" method or
30  * unwrapped with the "unwrap" method to decrement the refcount.
31  *
32  * In the context of this template, a shared pointer is an object of a class P
33  * that fulfills the following requirements:
34  *
35  * - Can be copy-constructed
36  * - Has an assignment operator (so that the get method can be used)
37  * - Contains a pointer to a reference count stored outside the class
38  * - Increments the reference count on copy-construction
39  * - Decrements the reference count on destruction
40  *
41  * Both the Boost and c++0x implementations of shared_ptr fulfill these
42  * requirements. At the moment of writing the author is not aware of any
43  * implementations of shared pointers that can't be used with this wrapper.
44  */
45 template <class P>
47 {
48 private:
49  /*!
50  * We store the pointer like this so that the refcount does not get
51  * incremented when the wrapper is copy-constructed, or decremented when
52  * the wrapper is destroyed.
53  *
54  * The whole external_shared_ptr object will be aligned by the compiler to
55  * a multiple of its size. The size of the object is sizeof(P) as the
56  * buffer is its only member. The buffer is placed in the class at offset 0
57  * so the alignment of the stored P should be alright without any
58  * additional hints.
59  */
60  char data[sizeof(P)];
61 
62 public:
63  /*!
64  * This constructor needs to be defined so that the [] operator in maps and
65  * hash tables works. If unwrap() or get() are called for an object
66  * constructed this way the behavior is undefined.
67  */
69  { }
70 
71  /*!
72  * Copy the pointer to internal storage and increment the refcount (the
73  * destructor never gets called).
74  */
76  {
77  new (data)P(ptr);
78  }
79 
80  /*!
81  * Call the destructor to decrement the refcount. If this is called more
82  * than once the results are undefined.
83  */
84  void unwrap()
85  {
86  P* p = reinterpret_cast<P*>((void*)data);
87  p->~P();
88  }
89 
90  /*!
91  * If this is called after unwrap() the behaviour is undefined.
92  */
93  P get() const
94  {
95  P* p = reinterpret_cast<P*>((void*)data);
96  return *p;
97  }
98 
99  bool operator == (const external_shared_ptr& x) const
100  {
101  P* p1 = reinterpret_cast<P*>((void*)data);
102  P* p2 = reinterpret_cast<P*>((void*)x.data);
103 
104  return *p1 == *p2;
105  }
106 
107  //! Output contained data items
108  friend std::ostream&
109  operator << (std::ostream& os, const external_shared_ptr& p)
110  {
111  return os << p.get();
112  }
113 };
114 
115 //! \}
116 
118 
119 #endif // !STXXL_COMMON_EXTERNAL_SHARED_PTR_HEADER
P get() const
If this is called after unwrap() the behaviour is undefined.
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:228
external_shared_ptr()
This constructor needs to be defined so that the [] operator in maps and hash tables works...
external_shared_ptr(P ptr)
Copy the pointer to internal storage and increment the refcount (the destructor never gets called)...
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
This class takes a shared pointer, increments its reference count and wraps it in a way that the resu...
char data[sizeof(P)]
We store the pointer like this so that the refcount does not get incremented when the wrapper is copy...
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:192
void unwrap()
Call the destructor to decrement the refcount.
#define STXXL_END_NAMESPACE
Definition: namespace.h:17