test test test

Dependencies:   mbed

Committer:
mohamedmoawya
Date:
Mon May 25 19:06:11 2020 +0000
Revision:
0:e4c5e6ec922e
snake game tteest

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mohamedmoawya 0:e4c5e6ec922e 1 /* mbed Microcontroller Library
mohamedmoawya 0:e4c5e6ec922e 2 * Copyright (c) 2006-2018 ARM Limited
mohamedmoawya 0:e4c5e6ec922e 3 *
mohamedmoawya 0:e4c5e6ec922e 4 * Licensed under the Apache License, Version 2.0 (the "License");
mohamedmoawya 0:e4c5e6ec922e 5 * you may not use this file except in compliance with the License.
mohamedmoawya 0:e4c5e6ec922e 6 * You may obtain a copy of the License at
mohamedmoawya 0:e4c5e6ec922e 7 *
mohamedmoawya 0:e4c5e6ec922e 8 * http://www.apache.org/licenses/LICENSE-2.0
mohamedmoawya 0:e4c5e6ec922e 9 *
mohamedmoawya 0:e4c5e6ec922e 10 * Unless required by applicable law or agreed to in writing, software
mohamedmoawya 0:e4c5e6ec922e 11 * distributed under the License is distributed on an "AS IS" BASIS,
mohamedmoawya 0:e4c5e6ec922e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mohamedmoawya 0:e4c5e6ec922e 13 * See the License for the specific language governing permissions and
mohamedmoawya 0:e4c5e6ec922e 14 * limitations under the License.
mohamedmoawya 0:e4c5e6ec922e 15 */
mohamedmoawya 0:e4c5e6ec922e 16
mohamedmoawya 0:e4c5e6ec922e 17 #ifndef __SHAREDPTR_H__
mohamedmoawya 0:e4c5e6ec922e 18 #define __SHAREDPTR_H__
mohamedmoawya 0:e4c5e6ec922e 19
mohamedmoawya 0:e4c5e6ec922e 20 #include <stdlib.h>
mohamedmoawya 0:e4c5e6ec922e 21
mohamedmoawya 0:e4c5e6ec922e 22 #include <stdint.h>
mohamedmoawya 0:e4c5e6ec922e 23 #include <stddef.h>
mohamedmoawya 0:e4c5e6ec922e 24
mohamedmoawya 0:e4c5e6ec922e 25 #include "platform/mbed_critical.h"
mohamedmoawya 0:e4c5e6ec922e 26
mohamedmoawya 0:e4c5e6ec922e 27 /** Shared pointer class.
mohamedmoawya 0:e4c5e6ec922e 28 *
mohamedmoawya 0:e4c5e6ec922e 29 * A shared pointer is a "smart" pointer that retains ownership of an object using
mohamedmoawya 0:e4c5e6ec922e 30 * reference counting accross all smart pointers referencing that object.
mohamedmoawya 0:e4c5e6ec922e 31 *
mohamedmoawya 0:e4c5e6ec922e 32 * @code
mohamedmoawya 0:e4c5e6ec922e 33 * #include "platform/SharedPtr.h"
mohamedmoawya 0:e4c5e6ec922e 34 *
mohamedmoawya 0:e4c5e6ec922e 35 * void test() {
mohamedmoawya 0:e4c5e6ec922e 36 * struct MyStruct { int a; };
mohamedmoawya 0:e4c5e6ec922e 37 *
mohamedmoawya 0:e4c5e6ec922e 38 * // Create shared pointer
mohamedmoawya 0:e4c5e6ec922e 39 * SharedPtr<MyStruct> ptr( new MyStruct );
mohamedmoawya 0:e4c5e6ec922e 40 *
mohamedmoawya 0:e4c5e6ec922e 41 * // Increase reference count
mohamedmoawya 0:e4c5e6ec922e 42 * SharedPtr<MyStruct> ptr2( ptr );
mohamedmoawya 0:e4c5e6ec922e 43 *
mohamedmoawya 0:e4c5e6ec922e 44 * ptr = NULL; // Reference to the struct instance is still held by ptr2
mohamedmoawya 0:e4c5e6ec922e 45 *
mohamedmoawya 0:e4c5e6ec922e 46 * ptr2 = NULL; // The raw pointer is freed
mohamedmoawya 0:e4c5e6ec922e 47 * }
mohamedmoawya 0:e4c5e6ec922e 48 * @endcode
mohamedmoawya 0:e4c5e6ec922e 49 *
mohamedmoawya 0:e4c5e6ec922e 50 *
mohamedmoawya 0:e4c5e6ec922e 51 * It is similar to the std::shared_ptr class introduced in C++11;
mohamedmoawya 0:e4c5e6ec922e 52 * however, this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters and so on.)
mohamedmoawya 0:e4c5e6ec922e 53 *
mohamedmoawya 0:e4c5e6ec922e 54 * Usage: SharedPtr<Class> ptr(new Class())
mohamedmoawya 0:e4c5e6ec922e 55 *
mohamedmoawya 0:e4c5e6ec922e 56 * When ptr is passed around by value, the copy constructor and
mohamedmoawya 0:e4c5e6ec922e 57 * destructor manages the reference count of the raw pointer.
mohamedmoawya 0:e4c5e6ec922e 58 * If the counter reaches zero, delete is called on the raw pointer.
mohamedmoawya 0:e4c5e6ec922e 59 *
mohamedmoawya 0:e4c5e6ec922e 60 * To avoid loops, use "weak" references by calling the original
mohamedmoawya 0:e4c5e6ec922e 61 * pointer directly through ptr.get().
mohamedmoawya 0:e4c5e6ec922e 62 */
mohamedmoawya 0:e4c5e6ec922e 63
mohamedmoawya 0:e4c5e6ec922e 64 template <class T>
mohamedmoawya 0:e4c5e6ec922e 65 class SharedPtr {
mohamedmoawya 0:e4c5e6ec922e 66 public:
mohamedmoawya 0:e4c5e6ec922e 67 /**
mohamedmoawya 0:e4c5e6ec922e 68 * @brief Create empty SharedPtr not pointing to anything.
mohamedmoawya 0:e4c5e6ec922e 69 * @details Used for variable declaration.
mohamedmoawya 0:e4c5e6ec922e 70 */
mohamedmoawya 0:e4c5e6ec922e 71 SharedPtr(): _ptr(NULL), _counter(NULL)
mohamedmoawya 0:e4c5e6ec922e 72 {
mohamedmoawya 0:e4c5e6ec922e 73 }
mohamedmoawya 0:e4c5e6ec922e 74
mohamedmoawya 0:e4c5e6ec922e 75 /**
mohamedmoawya 0:e4c5e6ec922e 76 * @brief Create new SharedPtr
mohamedmoawya 0:e4c5e6ec922e 77 * @param ptr Pointer to take control over
mohamedmoawya 0:e4c5e6ec922e 78 */
mohamedmoawya 0:e4c5e6ec922e 79 SharedPtr(T *ptr): _ptr(ptr), _counter(NULL)
mohamedmoawya 0:e4c5e6ec922e 80 {
mohamedmoawya 0:e4c5e6ec922e 81 // Allocate counter on the heap, so it can be shared
mohamedmoawya 0:e4c5e6ec922e 82 if (_ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 83 _counter = new uint32_t;
mohamedmoawya 0:e4c5e6ec922e 84 *_counter = 1;
mohamedmoawya 0:e4c5e6ec922e 85 }
mohamedmoawya 0:e4c5e6ec922e 86 }
mohamedmoawya 0:e4c5e6ec922e 87
mohamedmoawya 0:e4c5e6ec922e 88 /**
mohamedmoawya 0:e4c5e6ec922e 89 * @brief Destructor.
mohamedmoawya 0:e4c5e6ec922e 90 * @details Decrement reference counter, and delete object if no longer pointed to.
mohamedmoawya 0:e4c5e6ec922e 91 */
mohamedmoawya 0:e4c5e6ec922e 92 ~SharedPtr()
mohamedmoawya 0:e4c5e6ec922e 93 {
mohamedmoawya 0:e4c5e6ec922e 94 decrement_counter();
mohamedmoawya 0:e4c5e6ec922e 95 }
mohamedmoawya 0:e4c5e6ec922e 96
mohamedmoawya 0:e4c5e6ec922e 97 /**
mohamedmoawya 0:e4c5e6ec922e 98 * @brief Copy constructor.
mohamedmoawya 0:e4c5e6ec922e 99 * @details Create new SharedPtr from other SharedPtr by
mohamedmoawya 0:e4c5e6ec922e 100 * copying pointer to original object and pointer to counter.
mohamedmoawya 0:e4c5e6ec922e 101 * @param source Object being copied from.
mohamedmoawya 0:e4c5e6ec922e 102 */
mohamedmoawya 0:e4c5e6ec922e 103 SharedPtr(const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
mohamedmoawya 0:e4c5e6ec922e 104 {
mohamedmoawya 0:e4c5e6ec922e 105 // Increment reference counter
mohamedmoawya 0:e4c5e6ec922e 106 if (_ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 107 core_util_atomic_incr_u32(_counter, 1);
mohamedmoawya 0:e4c5e6ec922e 108 }
mohamedmoawya 0:e4c5e6ec922e 109 }
mohamedmoawya 0:e4c5e6ec922e 110
mohamedmoawya 0:e4c5e6ec922e 111 /**
mohamedmoawya 0:e4c5e6ec922e 112 * @brief Assignment operator.
mohamedmoawya 0:e4c5e6ec922e 113 * @details Cleanup previous reference and assign new pointer and counter.
mohamedmoawya 0:e4c5e6ec922e 114 * @param source Object being assigned from.
mohamedmoawya 0:e4c5e6ec922e 115 * @return Object being assigned.
mohamedmoawya 0:e4c5e6ec922e 116 */
mohamedmoawya 0:e4c5e6ec922e 117 SharedPtr operator=(const SharedPtr &source)
mohamedmoawya 0:e4c5e6ec922e 118 {
mohamedmoawya 0:e4c5e6ec922e 119 if (this != &source) {
mohamedmoawya 0:e4c5e6ec922e 120 // Clean up by decrementing counter
mohamedmoawya 0:e4c5e6ec922e 121 decrement_counter();
mohamedmoawya 0:e4c5e6ec922e 122
mohamedmoawya 0:e4c5e6ec922e 123 // Assign new values
mohamedmoawya 0:e4c5e6ec922e 124 _ptr = source.get();
mohamedmoawya 0:e4c5e6ec922e 125 _counter = source.get_counter();
mohamedmoawya 0:e4c5e6ec922e 126
mohamedmoawya 0:e4c5e6ec922e 127 // Increment new counter
mohamedmoawya 0:e4c5e6ec922e 128 if (_ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 129 core_util_atomic_incr_u32(_counter, 1);
mohamedmoawya 0:e4c5e6ec922e 130 }
mohamedmoawya 0:e4c5e6ec922e 131 }
mohamedmoawya 0:e4c5e6ec922e 132
mohamedmoawya 0:e4c5e6ec922e 133 return *this;
mohamedmoawya 0:e4c5e6ec922e 134 }
mohamedmoawya 0:e4c5e6ec922e 135
mohamedmoawya 0:e4c5e6ec922e 136 /**
mohamedmoawya 0:e4c5e6ec922e 137 * @brief Replaces the managed pointer with a new unmanaged pointer.
mohamedmoawya 0:e4c5e6ec922e 138 * @param[in] ptr the new raw pointer to manage.
mohamedmoawya 0:e4c5e6ec922e 139 */
mohamedmoawya 0:e4c5e6ec922e 140 void reset(T *ptr)
mohamedmoawya 0:e4c5e6ec922e 141 {
mohamedmoawya 0:e4c5e6ec922e 142 // Clean up by decrementing counter
mohamedmoawya 0:e4c5e6ec922e 143 decrement_counter();
mohamedmoawya 0:e4c5e6ec922e 144
mohamedmoawya 0:e4c5e6ec922e 145 if (ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 146 // Allocate counter on the heap, so it can be shared
mohamedmoawya 0:e4c5e6ec922e 147 _counter = new uint32_t;
mohamedmoawya 0:e4c5e6ec922e 148 *_counter = 1;
mohamedmoawya 0:e4c5e6ec922e 149 }
mohamedmoawya 0:e4c5e6ec922e 150 }
mohamedmoawya 0:e4c5e6ec922e 151
mohamedmoawya 0:e4c5e6ec922e 152 /**
mohamedmoawya 0:e4c5e6ec922e 153 * @brief Replace the managed pointer with a NULL pointer.
mohamedmoawya 0:e4c5e6ec922e 154 */
mohamedmoawya 0:e4c5e6ec922e 155 void reset()
mohamedmoawya 0:e4c5e6ec922e 156 {
mohamedmoawya 0:e4c5e6ec922e 157 reset(NULL);
mohamedmoawya 0:e4c5e6ec922e 158 }
mohamedmoawya 0:e4c5e6ec922e 159
mohamedmoawya 0:e4c5e6ec922e 160 /**
mohamedmoawya 0:e4c5e6ec922e 161 * @brief Raw pointer accessor.
mohamedmoawya 0:e4c5e6ec922e 162 * @details Get raw pointer to object pointed to.
mohamedmoawya 0:e4c5e6ec922e 163 * @return Pointer.
mohamedmoawya 0:e4c5e6ec922e 164 */
mohamedmoawya 0:e4c5e6ec922e 165 T *get() const
mohamedmoawya 0:e4c5e6ec922e 166 {
mohamedmoawya 0:e4c5e6ec922e 167 return _ptr;
mohamedmoawya 0:e4c5e6ec922e 168 }
mohamedmoawya 0:e4c5e6ec922e 169
mohamedmoawya 0:e4c5e6ec922e 170 /**
mohamedmoawya 0:e4c5e6ec922e 171 * @brief Reference count accessor.
mohamedmoawya 0:e4c5e6ec922e 172 * @return Reference count.
mohamedmoawya 0:e4c5e6ec922e 173 */
mohamedmoawya 0:e4c5e6ec922e 174 uint32_t use_count() const
mohamedmoawya 0:e4c5e6ec922e 175 {
mohamedmoawya 0:e4c5e6ec922e 176 if (_ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 177 core_util_critical_section_enter();
mohamedmoawya 0:e4c5e6ec922e 178 uint32_t current_counter = *_counter;
mohamedmoawya 0:e4c5e6ec922e 179 core_util_critical_section_exit();
mohamedmoawya 0:e4c5e6ec922e 180 return current_counter;
mohamedmoawya 0:e4c5e6ec922e 181 } else {
mohamedmoawya 0:e4c5e6ec922e 182 return 0;
mohamedmoawya 0:e4c5e6ec922e 183 }
mohamedmoawya 0:e4c5e6ec922e 184 }
mohamedmoawya 0:e4c5e6ec922e 185
mohamedmoawya 0:e4c5e6ec922e 186 /**
mohamedmoawya 0:e4c5e6ec922e 187 * @brief Dereference object operator.
mohamedmoawya 0:e4c5e6ec922e 188 * @details Override to return the object pointed to.
mohamedmoawya 0:e4c5e6ec922e 189 */
mohamedmoawya 0:e4c5e6ec922e 190 T &operator*() const
mohamedmoawya 0:e4c5e6ec922e 191 {
mohamedmoawya 0:e4c5e6ec922e 192 return *_ptr;
mohamedmoawya 0:e4c5e6ec922e 193 }
mohamedmoawya 0:e4c5e6ec922e 194
mohamedmoawya 0:e4c5e6ec922e 195 /**
mohamedmoawya 0:e4c5e6ec922e 196 * @brief Dereference object member operator.
mohamedmoawya 0:e4c5e6ec922e 197 * @details Override to return return member in object pointed to.
mohamedmoawya 0:e4c5e6ec922e 198 */
mohamedmoawya 0:e4c5e6ec922e 199 T *operator->() const
mohamedmoawya 0:e4c5e6ec922e 200 {
mohamedmoawya 0:e4c5e6ec922e 201 return _ptr;
mohamedmoawya 0:e4c5e6ec922e 202 }
mohamedmoawya 0:e4c5e6ec922e 203
mohamedmoawya 0:e4c5e6ec922e 204 /**
mohamedmoawya 0:e4c5e6ec922e 205 * @brief Boolean conversion operator.
mohamedmoawya 0:e4c5e6ec922e 206 * @return Whether or not the pointer is NULL.
mohamedmoawya 0:e4c5e6ec922e 207 */
mohamedmoawya 0:e4c5e6ec922e 208 operator bool() const
mohamedmoawya 0:e4c5e6ec922e 209 {
mohamedmoawya 0:e4c5e6ec922e 210 return (_ptr != NULL);
mohamedmoawya 0:e4c5e6ec922e 211 }
mohamedmoawya 0:e4c5e6ec922e 212
mohamedmoawya 0:e4c5e6ec922e 213 private:
mohamedmoawya 0:e4c5e6ec922e 214 /**
mohamedmoawya 0:e4c5e6ec922e 215 * @brief Get pointer to reference counter.
mohamedmoawya 0:e4c5e6ec922e 216 * @return Pointer to reference counter.
mohamedmoawya 0:e4c5e6ec922e 217 */
mohamedmoawya 0:e4c5e6ec922e 218 uint32_t *get_counter() const
mohamedmoawya 0:e4c5e6ec922e 219 {
mohamedmoawya 0:e4c5e6ec922e 220 return _counter;
mohamedmoawya 0:e4c5e6ec922e 221 }
mohamedmoawya 0:e4c5e6ec922e 222
mohamedmoawya 0:e4c5e6ec922e 223 /**
mohamedmoawya 0:e4c5e6ec922e 224 * @brief Decrement reference counter.
mohamedmoawya 0:e4c5e6ec922e 225 * @details If count reaches zero, free counter and delete object pointed to.
mohamedmoawya 0:e4c5e6ec922e 226 */
mohamedmoawya 0:e4c5e6ec922e 227 void decrement_counter()
mohamedmoawya 0:e4c5e6ec922e 228 {
mohamedmoawya 0:e4c5e6ec922e 229 if (_ptr != NULL) {
mohamedmoawya 0:e4c5e6ec922e 230 uint32_t new_value = core_util_atomic_decr_u32(_counter, 1);
mohamedmoawya 0:e4c5e6ec922e 231 if (new_value == 0) {
mohamedmoawya 0:e4c5e6ec922e 232 delete _counter;
mohamedmoawya 0:e4c5e6ec922e 233 _counter = NULL;
mohamedmoawya 0:e4c5e6ec922e 234 delete _ptr;
mohamedmoawya 0:e4c5e6ec922e 235 _ptr = NULL;
mohamedmoawya 0:e4c5e6ec922e 236 }
mohamedmoawya 0:e4c5e6ec922e 237 }
mohamedmoawya 0:e4c5e6ec922e 238 }
mohamedmoawya 0:e4c5e6ec922e 239
mohamedmoawya 0:e4c5e6ec922e 240 private:
mohamedmoawya 0:e4c5e6ec922e 241 // Pointer to shared object
mohamedmoawya 0:e4c5e6ec922e 242 T *_ptr;
mohamedmoawya 0:e4c5e6ec922e 243
mohamedmoawya 0:e4c5e6ec922e 244 // Pointer to shared reference counter
mohamedmoawya 0:e4c5e6ec922e 245 uint32_t *_counter;
mohamedmoawya 0:e4c5e6ec922e 246 };
mohamedmoawya 0:e4c5e6ec922e 247
mohamedmoawya 0:e4c5e6ec922e 248 /** Non-member relational operators.
mohamedmoawya 0:e4c5e6ec922e 249 */
mohamedmoawya 0:e4c5e6ec922e 250 template <class T, class U>
mohamedmoawya 0:e4c5e6ec922e 251 bool operator== (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
mohamedmoawya 0:e4c5e6ec922e 252 {
mohamedmoawya 0:e4c5e6ec922e 253 return (lhs.get() == rhs.get());
mohamedmoawya 0:e4c5e6ec922e 254 }
mohamedmoawya 0:e4c5e6ec922e 255
mohamedmoawya 0:e4c5e6ec922e 256 template <class T, typename U>
mohamedmoawya 0:e4c5e6ec922e 257 bool operator== (const SharedPtr<T> &lhs, U rhs)
mohamedmoawya 0:e4c5e6ec922e 258 {
mohamedmoawya 0:e4c5e6ec922e 259 return (lhs.get() == (T *) rhs);
mohamedmoawya 0:e4c5e6ec922e 260 }
mohamedmoawya 0:e4c5e6ec922e 261
mohamedmoawya 0:e4c5e6ec922e 262 template <class T, typename U>
mohamedmoawya 0:e4c5e6ec922e 263 bool operator== (U lhs, const SharedPtr<T> &rhs)
mohamedmoawya 0:e4c5e6ec922e 264 {
mohamedmoawya 0:e4c5e6ec922e 265 return ((T *) lhs == rhs.get());
mohamedmoawya 0:e4c5e6ec922e 266 }
mohamedmoawya 0:e4c5e6ec922e 267
mohamedmoawya 0:e4c5e6ec922e 268 /** Non-member relational operators.
mohamedmoawya 0:e4c5e6ec922e 269 */
mohamedmoawya 0:e4c5e6ec922e 270 template <class T, class U>
mohamedmoawya 0:e4c5e6ec922e 271 bool operator!= (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
mohamedmoawya 0:e4c5e6ec922e 272 {
mohamedmoawya 0:e4c5e6ec922e 273 return (lhs.get() != rhs.get());
mohamedmoawya 0:e4c5e6ec922e 274 }
mohamedmoawya 0:e4c5e6ec922e 275
mohamedmoawya 0:e4c5e6ec922e 276 template <class T, typename U>
mohamedmoawya 0:e4c5e6ec922e 277 bool operator!= (const SharedPtr<T> &lhs, U rhs)
mohamedmoawya 0:e4c5e6ec922e 278 {
mohamedmoawya 0:e4c5e6ec922e 279 return (lhs.get() != (T *) rhs);
mohamedmoawya 0:e4c5e6ec922e 280 }
mohamedmoawya 0:e4c5e6ec922e 281
mohamedmoawya 0:e4c5e6ec922e 282 template <class T, typename U>
mohamedmoawya 0:e4c5e6ec922e 283 bool operator!= (U lhs, const SharedPtr<T> &rhs)
mohamedmoawya 0:e4c5e6ec922e 284 {
mohamedmoawya 0:e4c5e6ec922e 285 return ((T *) lhs != rhs.get());
mohamedmoawya 0:e4c5e6ec922e 286 }
mohamedmoawya 0:e4c5e6ec922e 287
mohamedmoawya 0:e4c5e6ec922e 288 #endif // __SHAREDPTR_H__