Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

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