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