blob: cc24243d26e75017d7bb482895692bfae0169806 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#pragma once
#include <cassert>
#include <iostream>
#include <string>
template <typename T>
class SharedPointer
{
public:
// The type pointed to.
using element_type = T;
/**
** \brief Construct a counted reference to a newly allocated object.
*/
SharedPointer(element_type* p = nullptr);
/**
** \brief Destroy the SharedPointer.
** The object pointed to is destroyed only if it is not referenced anymore.
** Take care to free everything ...
*/
~SharedPointer();
/**
** \brief Copy constructor.
** \return A new SharedPointer, pointing
** to the underlying data of \a other.
**
** Although the implementation is subsumed by the previous, more
** generic one, the C++ standard still mandates this specific
** signature. Otherwise, the compiler will provide a default
** implementation, which is of course wrong. Note that the
** same applies for the assignment operator.
*/
SharedPointer(const SharedPointer<element_type>& other);
/// \name Assignments.
/// \{
/**
** \brief Replace the underlying data with \a p
** A call to \c reset is strictly equivalent to:
** \code
** r = SharedPointer<element_type>(p);
** \endcode
** This line implicitly calls the destructor of \a r before assigning
** it a new value.
** \warning You can't reset your \c SharedPointer with the
** same pointer as the current one. In this case, nothing should
** be done.
** \warning Be mindful of other \c SharedPointers
** which could also be pointing to the same data.
*/
void reset(element_type* p);
/**
** \brief Reference assignment
** Its behavior is similar to \c reset() but you
** have to use the existing SharedPointer \a other
** instead of creating a new instance.
*/
SharedPointer<element_type>&
operator=(const SharedPointer<element_type>& other);
/// \}
/// \name Access to data.
/// \{
// Access via a reference.
element_type& operator*() const;
// Access via a pointer.
element_type* operator->() const;
// Returns the underlying data pointer.
element_type* get() const;
// Returns the number of SharedPointer objects referring to the same managed
// object
long use_count() const;
/// \}
/// \name Equality operators.
/// \{
/**
** \brief Reference comparison.
** Returns true if the two references point to the same object.
*/
template <typename U>
bool operator==(const SharedPointer<U>& rhs) const;
/**
** \brief Reference comparison.
** Returns false if the two references point to the same object.
*/
template <typename U>
bool operator!=(const SharedPointer<U>& rhs) const;
/**
** \brief Reference comparison.
** Returns true if this points to \a p.
*/
bool operator==(const element_type* p) const;
/**
** \brief Reference comparison.
** Returns false if this points to \a p.
*/
bool operator!=(const element_type* p) const;
/**
** \brief Move assignment operator for SharedPointer.
**
** This operator transfers ownership of the managed object from the
** \a other to this SharePointer.
** After the assignment, the \a other will be empty.
** \warning Don't forget to update the reference counter.
**
** Returns a reference to this SharedPointer.
*/
SharedPointer<element_type>& operator=(SharedPointer&& other) noexcept;
/**
** \brief Move constructor for SharedPointer.
**
** This constructor transfers ownership of the managed object from \a other
** SharedPointer to the newly created SharedPointer.
**
*/
SharedPointer(SharedPointer&& other);
/// \}
/**
** \brief Test for validity
** Returns true if the reference is valid, i.e. it points to an object.
** This conversion operator allows users to write:
**
** \code
** SharedPointer<int> p1;
** if (p1) // Here false
** ...
** \endcode
*/
operator bool() const;
/**
** \brief Test fellowship.
** Returns true if the SharedPointer points to an object which
** is of the specified type.
** Look at the given example file shared_pointer_is_a.cc
** to better understand its purpose.
*/
template <typename U>
bool is_a() const;
private:
// Pointer to the underlying data.
element_type* data_;
// A counter shared between all ref pointers to \a data_.
long* count_;
};
#include "shared_pointer.hxx"
|