Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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