A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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