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