Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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