Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXAS21002 FXOS8700Q
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/include/smartpointer.h@0:977e87915078, 2019-08-28 (annotated)
- Committer:
- vithyat
- Date:
- Wed Aug 28 19:24:56 2019 +0000
- Revision:
- 0:977e87915078
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vithyat | 0:977e87915078 | 1 | /* |
vithyat | 0:977e87915078 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
vithyat | 0:977e87915078 | 3 | * SPDX-License-Identifier: Apache-2.0 |
vithyat | 0:977e87915078 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
vithyat | 0:977e87915078 | 5 | * not use this file except in compliance with the License. |
vithyat | 0:977e87915078 | 6 | * You may obtain a copy of the License at |
vithyat | 0:977e87915078 | 7 | * |
vithyat | 0:977e87915078 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vithyat | 0:977e87915078 | 9 | * |
vithyat | 0:977e87915078 | 10 | * Unless required by applicable law or agreed to in writing, software |
vithyat | 0:977e87915078 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
vithyat | 0:977e87915078 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vithyat | 0:977e87915078 | 13 | * See the License for the specific language governing permissions and |
vithyat | 0:977e87915078 | 14 | * limitations under the License. |
vithyat | 0:977e87915078 | 15 | */ |
vithyat | 0:977e87915078 | 16 | #ifndef SMART_POINTER_H |
vithyat | 0:977e87915078 | 17 | #define SMART_POINTER_H |
vithyat | 0:977e87915078 | 18 | |
vithyat | 0:977e87915078 | 19 | class ReferenceCount |
vithyat | 0:977e87915078 | 20 | { |
vithyat | 0:977e87915078 | 21 | private: |
vithyat | 0:977e87915078 | 22 | |
vithyat | 0:977e87915078 | 23 | int _count; // Reference count |
vithyat | 0:977e87915078 | 24 | |
vithyat | 0:977e87915078 | 25 | public: |
vithyat | 0:977e87915078 | 26 | |
vithyat | 0:977e87915078 | 27 | void add_ref() |
vithyat | 0:977e87915078 | 28 | { |
vithyat | 0:977e87915078 | 29 | // Increment the reference count |
vithyat | 0:977e87915078 | 30 | _count++; |
vithyat | 0:977e87915078 | 31 | } |
vithyat | 0:977e87915078 | 32 | |
vithyat | 0:977e87915078 | 33 | int release() |
vithyat | 0:977e87915078 | 34 | { |
vithyat | 0:977e87915078 | 35 | // Decrement the reference count and |
vithyat | 0:977e87915078 | 36 | // return the reference count. |
vithyat | 0:977e87915078 | 37 | return --_count; |
vithyat | 0:977e87915078 | 38 | } |
vithyat | 0:977e87915078 | 39 | }; |
vithyat | 0:977e87915078 | 40 | |
vithyat | 0:977e87915078 | 41 | template < typename T > class SmartPointer |
vithyat | 0:977e87915078 | 42 | { |
vithyat | 0:977e87915078 | 43 | private: |
vithyat | 0:977e87915078 | 44 | |
vithyat | 0:977e87915078 | 45 | T *_data; // Generic pointer to be stored |
vithyat | 0:977e87915078 | 46 | ReferenceCount *_reference; // Reference count |
vithyat | 0:977e87915078 | 47 | |
vithyat | 0:977e87915078 | 48 | |
vithyat | 0:977e87915078 | 49 | public: |
vithyat | 0:977e87915078 | 50 | |
vithyat | 0:977e87915078 | 51 | SmartPointer() |
vithyat | 0:977e87915078 | 52 | : _data(0), _reference(0) |
vithyat | 0:977e87915078 | 53 | { |
vithyat | 0:977e87915078 | 54 | // Create a new reference |
vithyat | 0:977e87915078 | 55 | _reference = new ReferenceCount(); |
vithyat | 0:977e87915078 | 56 | // Increment the reference count |
vithyat | 0:977e87915078 | 57 | _reference->add_ref(); |
vithyat | 0:977e87915078 | 58 | } |
vithyat | 0:977e87915078 | 59 | |
vithyat | 0:977e87915078 | 60 | SmartPointer(T* value) |
vithyat | 0:977e87915078 | 61 | : _data(value), _reference(0) |
vithyat | 0:977e87915078 | 62 | { |
vithyat | 0:977e87915078 | 63 | // Create a new reference |
vithyat | 0:977e87915078 | 64 | _reference = new ReferenceCount(); |
vithyat | 0:977e87915078 | 65 | // Increment the reference count |
vithyat | 0:977e87915078 | 66 | _reference->add_ref(); |
vithyat | 0:977e87915078 | 67 | } |
vithyat | 0:977e87915078 | 68 | |
vithyat | 0:977e87915078 | 69 | SmartPointer(const SmartPointer<T>& smart_pointer) |
vithyat | 0:977e87915078 | 70 | : _data(smart_pointer._data), reference(smart_pointer._reference) |
vithyat | 0:977e87915078 | 71 | { |
vithyat | 0:977e87915078 | 72 | // Copy constructor |
vithyat | 0:977e87915078 | 73 | // Copy the data and reference pointer |
vithyat | 0:977e87915078 | 74 | // and increment the reference count |
vithyat | 0:977e87915078 | 75 | _reference->add_ref(); |
vithyat | 0:977e87915078 | 76 | } |
vithyat | 0:977e87915078 | 77 | |
vithyat | 0:977e87915078 | 78 | ~SmartPointer() |
vithyat | 0:977e87915078 | 79 | { |
vithyat | 0:977e87915078 | 80 | if(_reference->release() == 0) { |
vithyat | 0:977e87915078 | 81 | delete _data; |
vithyat | 0:977e87915078 | 82 | delete _reference; |
vithyat | 0:977e87915078 | 83 | } |
vithyat | 0:977e87915078 | 84 | } |
vithyat | 0:977e87915078 | 85 | |
vithyat | 0:977e87915078 | 86 | T& operator* () |
vithyat | 0:977e87915078 | 87 | { |
vithyat | 0:977e87915078 | 88 | return *_data; |
vithyat | 0:977e87915078 | 89 | } |
vithyat | 0:977e87915078 | 90 | |
vithyat | 0:977e87915078 | 91 | T* operator-> () |
vithyat | 0:977e87915078 | 92 | { |
vithyat | 0:977e87915078 | 93 | return _data; |
vithyat | 0:977e87915078 | 94 | } |
vithyat | 0:977e87915078 | 95 | |
vithyat | 0:977e87915078 | 96 | SmartPointer<T>& operator = (const SmartPointer<T>& smart_pointer) |
vithyat | 0:977e87915078 | 97 | { |
vithyat | 0:977e87915078 | 98 | // Assignment operator |
vithyat | 0:977e87915078 | 99 | if (this != &SmartPointer) { // Avoid self assignment |
vithyat | 0:977e87915078 | 100 | // Decrement the old reference count |
vithyat | 0:977e87915078 | 101 | // if reference become zero delete the old data |
vithyat | 0:977e87915078 | 102 | if(_reference->release() == 0) { |
vithyat | 0:977e87915078 | 103 | delete _data; |
vithyat | 0:977e87915078 | 104 | delete _reference; |
vithyat | 0:977e87915078 | 105 | } |
vithyat | 0:977e87915078 | 106 | |
vithyat | 0:977e87915078 | 107 | // Copy the data and reference pointer |
vithyat | 0:977e87915078 | 108 | // and increment the reference count |
vithyat | 0:977e87915078 | 109 | _data = SmartPointer._data; |
vithyat | 0:977e87915078 | 110 | _reference = SmartPointer._reference; |
vithyat | 0:977e87915078 | 111 | _reference->add_ref(); |
vithyat | 0:977e87915078 | 112 | } |
vithyat | 0:977e87915078 | 113 | return *this; |
vithyat | 0:977e87915078 | 114 | } |
vithyat | 0:977e87915078 | 115 | |
vithyat | 0:977e87915078 | 116 | }; |
vithyat | 0:977e87915078 | 117 | |
vithyat | 0:977e87915078 | 118 | #endif // SMART_POINTER_H |