Skip to content

Commit 9ebfbc0

Browse files
author
Donatien Garnier
committed
Re-introduce a shared pointer class in mbed OS
1 parent f12afde commit 9ebfbc0

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

platform/SharedPtr.h

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef __SHAREDPTR_H__
18+
#define __SHAREDPTR_H__
19+
20+
#include <stdlib.h>
21+
22+
#include <stdint.h>
23+
#include <stddef.h>
24+
25+
/** Shared pointer class.
26+
*
27+
* Similar to std::shared_ptr in C++11.
28+
*
29+
* Usage: SharedPtr<class> POINTER(new class())
30+
*
31+
* When POINTER is passed around by value the copy constructor and
32+
* destructor counts the number of references to the original object.
33+
* If the counter reaches zero, delete is called on the object pointed to.
34+
*
35+
* To avoid loops, "weak" references should be used by calling the original
36+
* pointer directly through POINTER.get().
37+
*/
38+
39+
template <class T>
40+
class SharedPtr {
41+
public:
42+
/**
43+
* @brief Create empty SharedPtr not pointing to anything.
44+
* @details Used for variable declaration.
45+
*/
46+
SharedPtr(): pointer(NULL), counter(NULL) {
47+
}
48+
49+
/**
50+
* @brief Create new SharedPtr
51+
* @param _pointer Pointer to take control over
52+
*/
53+
SharedPtr(T* _pointer): pointer(_pointer) {
54+
// allocate counter on the heap so it can be shared
55+
counter = (uint32_t*) malloc(sizeof(uint32_t));
56+
*counter = 1;
57+
}
58+
59+
/**
60+
* @brief Destructor.
61+
* @details Decrement reference counter and delete object if no longer pointed to.
62+
*/
63+
~SharedPtr() {
64+
decrementCounter();
65+
}
66+
67+
/**
68+
* @brief Copy constructor.
69+
* @details Create new SharePointer from other SharedPtr by
70+
* copying pointer to original object and pointer to counter.
71+
* @param source Object being copied from.
72+
*/
73+
SharedPtr(const SharedPtr& source): pointer(source.pointer), counter(source.counter) {
74+
// increment reference counter
75+
if (counter) {
76+
(*counter)++;
77+
}
78+
}
79+
80+
/**
81+
* @brief Assignment operator.
82+
* @details Cleanup previous reference and assign new pointer and counter.
83+
* @param source Object being assigned from.
84+
* @return Object being assigned.
85+
*/
86+
SharedPtr operator=(const SharedPtr& source) {
87+
if (this != &source) {
88+
// clean up by decrementing counter
89+
decrementCounter();
90+
91+
// assign new values
92+
pointer = source.get();
93+
counter = source.getCounter();
94+
95+
// increment new counter
96+
if (counter) {
97+
(*counter)++;
98+
}
99+
}
100+
101+
return *this;
102+
}
103+
104+
/**
105+
* @brief Raw pointer accessor.
106+
* @details Get raw pointer to object pointed to.
107+
* @return Pointer.
108+
*/
109+
T* get() const {
110+
return pointer;
111+
}
112+
113+
/**
114+
* @brief Reference count accessor.
115+
* @return Reference count.
116+
*/
117+
uint32_t use_count() const {
118+
if (counter) {
119+
return *counter;
120+
} else {
121+
return 0;
122+
}
123+
}
124+
125+
/**
126+
* @brief Dereference object operator.
127+
* @details Override to return the object pointed to.
128+
*/
129+
T& operator*() const {
130+
return *pointer;
131+
}
132+
133+
/**
134+
* @brief Dereference object member operator.
135+
* @details Override to return return member in object pointed to.
136+
*/
137+
T* operator->() const {
138+
return pointer;
139+
}
140+
141+
/**
142+
* @brief Boolean conversion operator.
143+
* @return Whether or not the pointer is NULL.
144+
*/
145+
operator bool() const {
146+
return (pointer != 0);
147+
}
148+
149+
private:
150+
/**
151+
* @brief Get pointer to reference counter.
152+
* @return Pointer to reference counter.
153+
*/
154+
uint32_t* getCounter() const {
155+
return counter;
156+
}
157+
158+
/**
159+
* @brief Decrement reference counter.
160+
* @details If count reaches zero, free counter and delete object pointed to.
161+
*/
162+
void decrementCounter() {
163+
if (counter) {
164+
if (*counter == 1) {
165+
free(counter);
166+
delete pointer;
167+
} else {
168+
(*counter)--;
169+
}
170+
}
171+
}
172+
173+
private:
174+
// pointer to shared object
175+
T* pointer;
176+
177+
// pointer to shared reference counter
178+
uint32_t* counter;
179+
};
180+
181+
/** Non-member relational operators.
182+
*/
183+
template <class T, class U>
184+
bool operator== (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
185+
return (lhs.get() == rhs.get());
186+
}
187+
188+
template <class T, typename U>
189+
bool operator== (const SharedPtr<T>& lhs, U rhs) {
190+
return (lhs.get() == (T*) rhs);
191+
}
192+
193+
template <class T, typename U>
194+
bool operator== (U lhs, const SharedPtr<T>& rhs) {
195+
return ((T*) lhs == rhs.get());
196+
}
197+
198+
/** Non-member relational operators.
199+
*/
200+
template <class T, class U>
201+
bool operator!= (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
202+
return (lhs.get() != rhs.get());
203+
}
204+
205+
template <class T, typename U>
206+
bool operator!= (const SharedPtr<T>& lhs, U rhs) {
207+
return (lhs.get() != (T*) rhs);
208+
}
209+
210+
template <class T, typename U>
211+
bool operator!= (U lhs, const SharedPtr<T>& rhs) {
212+
return ((T*) lhs != rhs.get());
213+
}
214+
215+
#endif // __SHAREDPTR_H__

0 commit comments

Comments
 (0)