Simple interface for Mbed Cloud Client
Embed:
(wiki syntax)
Show/hide line numbers
smartpointer.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef SMART_POINTER_H 00017 #define SMART_POINTER_H 00018 00019 class ReferenceCount 00020 { 00021 private: 00022 00023 int _count; // Reference count 00024 00025 public: 00026 00027 void add_ref() 00028 { 00029 // Increment the reference count 00030 _count++; 00031 } 00032 00033 int release() 00034 { 00035 // Decrement the reference count and 00036 // return the reference count. 00037 return --_count; 00038 } 00039 }; 00040 00041 template < typename T > class SmartPointer 00042 { 00043 private: 00044 00045 T *_data; // Generic pointer to be stored 00046 ReferenceCount *_reference; // Reference count 00047 00048 00049 public: 00050 00051 SmartPointer() 00052 : _data(0), _reference(0) 00053 { 00054 // Create a new reference 00055 _reference = new ReferenceCount(); 00056 // Increment the reference count 00057 _reference->add_ref(); 00058 } 00059 00060 SmartPointer(T* value) 00061 : _data(value), _reference(0) 00062 { 00063 // Create a new reference 00064 _reference = new ReferenceCount(); 00065 // Increment the reference count 00066 _reference->add_ref(); 00067 } 00068 00069 SmartPointer(const SmartPointer<T>& smart_pointer) 00070 : _data(smart_pointer._data), reference(smart_pointer._reference) 00071 { 00072 // Copy constructor 00073 // Copy the data and reference pointer 00074 // and increment the reference count 00075 _reference->add_ref(); 00076 } 00077 00078 ~SmartPointer() 00079 { 00080 if(_reference->release() == 0) { 00081 delete _data; 00082 delete _reference; 00083 } 00084 } 00085 00086 T& operator* () 00087 { 00088 return *_data; 00089 } 00090 00091 T* operator-> () 00092 { 00093 return _data; 00094 } 00095 00096 SmartPointer<T>& operator = (const SmartPointer<T>& smart_pointer) 00097 { 00098 // Assignment operator 00099 if (this != &SmartPointer) { // Avoid self assignment 00100 // Decrement the old reference count 00101 // if reference become zero delete the old data 00102 if(_reference->release() == 0) { 00103 delete _data; 00104 delete _reference; 00105 } 00106 00107 // Copy the data and reference pointer 00108 // and increment the reference count 00109 _data = SmartPointer._data; 00110 _reference = SmartPointer._reference; 00111 _reference->add_ref(); 00112 } 00113 return *this; 00114 } 00115 00116 }; 00117 00118 #endif // SMART_POINTER_H
Generated on Tue Jul 12 2022 19:01:37 by 1.7.2