BA
/
BaBoRo_test2
Backup 1
mbed-os/platform/NonCopyable.h@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* Copyright (c) 2017 ARM Limited |
borlanic | 0:02dd72d1d465 | 2 | * |
borlanic | 0:02dd72d1d465 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 4 | * you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 5 | * You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 6 | * |
borlanic | 0:02dd72d1d465 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 8 | * |
borlanic | 0:02dd72d1d465 | 9 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 12 | * See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 13 | * limitations under the License. |
borlanic | 0:02dd72d1d465 | 14 | */ |
borlanic | 0:02dd72d1d465 | 15 | |
borlanic | 0:02dd72d1d465 | 16 | #ifndef MBED_NONCOPYABLE_H_ |
borlanic | 0:02dd72d1d465 | 17 | #define MBED_NONCOPYABLE_H_ |
borlanic | 0:02dd72d1d465 | 18 | |
borlanic | 0:02dd72d1d465 | 19 | #if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0)) |
borlanic | 0:02dd72d1d465 | 20 | #include "mbed_toolchain.h" |
borlanic | 0:02dd72d1d465 | 21 | #include "mbed_debug.h" |
borlanic | 0:02dd72d1d465 | 22 | #endif |
borlanic | 0:02dd72d1d465 | 23 | |
borlanic | 0:02dd72d1d465 | 24 | namespace mbed { |
borlanic | 0:02dd72d1d465 | 25 | |
borlanic | 0:02dd72d1d465 | 26 | /** |
borlanic | 0:02dd72d1d465 | 27 | * Inheriting from this class autogeneration of copy construction and copy |
borlanic | 0:02dd72d1d465 | 28 | * assignment operations. |
borlanic | 0:02dd72d1d465 | 29 | * |
borlanic | 0:02dd72d1d465 | 30 | * Classes which are not value type should inherit privately from this class |
borlanic | 0:02dd72d1d465 | 31 | * to avoid generation of invalid copy constructor or copy assignment operator |
borlanic | 0:02dd72d1d465 | 32 | * which can lead to unnoticeable programming errors. |
borlanic | 0:02dd72d1d465 | 33 | * |
borlanic | 0:02dd72d1d465 | 34 | * As an example consider the following signature: |
borlanic | 0:02dd72d1d465 | 35 | * |
borlanic | 0:02dd72d1d465 | 36 | * @code |
borlanic | 0:02dd72d1d465 | 37 | * class Resource; |
borlanic | 0:02dd72d1d465 | 38 | * |
borlanic | 0:02dd72d1d465 | 39 | * class Foo { |
borlanic | 0:02dd72d1d465 | 40 | * public: |
borlanic | 0:02dd72d1d465 | 41 | * Foo() : _resource(new Resource()) { } |
borlanic | 0:02dd72d1d465 | 42 | * ~Foo() { delete _resource; } |
borlanic | 0:02dd72d1d465 | 43 | * private: |
borlanic | 0:02dd72d1d465 | 44 | * Resource* _resource; |
borlanic | 0:02dd72d1d465 | 45 | * } |
borlanic | 0:02dd72d1d465 | 46 | * |
borlanic | 0:02dd72d1d465 | 47 | * Foo get_foo(); |
borlanic | 0:02dd72d1d465 | 48 | * |
borlanic | 0:02dd72d1d465 | 49 | * Foo foo = get_foo(); |
borlanic | 0:02dd72d1d465 | 50 | * @endcode |
borlanic | 0:02dd72d1d465 | 51 | * |
borlanic | 0:02dd72d1d465 | 52 | * There is a bug in this function, it returns a temporary value which will be |
borlanic | 0:02dd72d1d465 | 53 | * byte copied into foo then destroyed. Unfortunately, internally the Foo class |
borlanic | 0:02dd72d1d465 | 54 | * manage a pointer to a Resource object. This pointer will be released when the |
borlanic | 0:02dd72d1d465 | 55 | * temporary is destroyed and foo will manage a pointer to an already released |
borlanic | 0:02dd72d1d465 | 56 | * Resource. |
borlanic | 0:02dd72d1d465 | 57 | * |
borlanic | 0:02dd72d1d465 | 58 | * Two issues has to be fixed in the example above: |
borlanic | 0:02dd72d1d465 | 59 | * - Function signature has to be changed to reflect the fact that Foo |
borlanic | 0:02dd72d1d465 | 60 | * instances cannot be copied. In that case accessor should return a |
borlanic | 0:02dd72d1d465 | 61 | * reference to give access to objects already existing and managed. |
borlanic | 0:02dd72d1d465 | 62 | * Generator on the other hand should return a pointer to the created object. |
borlanic | 0:02dd72d1d465 | 63 | * |
borlanic | 0:02dd72d1d465 | 64 | * @code |
borlanic | 0:02dd72d1d465 | 65 | * // return a reference to an already managed Foo instance |
borlanic | 0:02dd72d1d465 | 66 | * Foo& get_foo(); |
borlanic | 0:02dd72d1d465 | 67 | * Foo& foo = get_foo(); |
borlanic | 0:02dd72d1d465 | 68 | * |
borlanic | 0:02dd72d1d465 | 69 | * // create a new Foo instance |
borlanic | 0:02dd72d1d465 | 70 | * Foo* make_foo(); |
borlanic | 0:02dd72d1d465 | 71 | * Foo* m = make_foo(); |
borlanic | 0:02dd72d1d465 | 72 | * @endcode |
borlanic | 0:02dd72d1d465 | 73 | * |
borlanic | 0:02dd72d1d465 | 74 | * - Copy constructor and copy assignment operator has to be made private |
borlanic | 0:02dd72d1d465 | 75 | * in the Foo class. It prevents unwanted copy of Foo objects. This can be |
borlanic | 0:02dd72d1d465 | 76 | * done by declaring copy constructor and copy assignment in the private |
borlanic | 0:02dd72d1d465 | 77 | * section of the Foo class. |
borlanic | 0:02dd72d1d465 | 78 | * |
borlanic | 0:02dd72d1d465 | 79 | * @code |
borlanic | 0:02dd72d1d465 | 80 | * class Foo { |
borlanic | 0:02dd72d1d465 | 81 | * public: |
borlanic | 0:02dd72d1d465 | 82 | * Foo() : _resource(new Resource()) { } |
borlanic | 0:02dd72d1d465 | 83 | * ~Foo() { delete _resource; } |
borlanic | 0:02dd72d1d465 | 84 | * private: |
borlanic | 0:02dd72d1d465 | 85 | * // disallow copy operations |
borlanic | 0:02dd72d1d465 | 86 | * Foo(const Foo&); |
borlanic | 0:02dd72d1d465 | 87 | * Foo& operator=(const Foo&); |
borlanic | 0:02dd72d1d465 | 88 | * // data members |
borlanic | 0:02dd72d1d465 | 89 | * Resource* _resource; |
borlanic | 0:02dd72d1d465 | 90 | * } |
borlanic | 0:02dd72d1d465 | 91 | * @endcode |
borlanic | 0:02dd72d1d465 | 92 | * |
borlanic | 0:02dd72d1d465 | 93 | * Another solution is to inherit privately from the NonCopyable class. |
borlanic | 0:02dd72d1d465 | 94 | * It reduces the boiler plate needed to avoid copy operations but more |
borlanic | 0:02dd72d1d465 | 95 | * importantly it clarifies the programmer intent and the object semantic. |
borlanic | 0:02dd72d1d465 | 96 | * |
borlanic | 0:02dd72d1d465 | 97 | * class Foo : private NonCopyable<Foo> { |
borlanic | 0:02dd72d1d465 | 98 | * public: |
borlanic | 0:02dd72d1d465 | 99 | * Foo() : _resource(new Resource()) { } |
borlanic | 0:02dd72d1d465 | 100 | * ~Foo() { delete _resource; } |
borlanic | 0:02dd72d1d465 | 101 | * private: |
borlanic | 0:02dd72d1d465 | 102 | * Resource* _resource; |
borlanic | 0:02dd72d1d465 | 103 | * } |
borlanic | 0:02dd72d1d465 | 104 | * |
borlanic | 0:02dd72d1d465 | 105 | * @tparam T The type that should be made non copyable. It prevent cases where |
borlanic | 0:02dd72d1d465 | 106 | * the empty base optimization cannot be applied and therefore ensure that the |
borlanic | 0:02dd72d1d465 | 107 | * cost of this semantic sugar is null. |
borlanic | 0:02dd72d1d465 | 108 | * |
borlanic | 0:02dd72d1d465 | 109 | * As an example, the empty base optimization is prohibited if one of the empty |
borlanic | 0:02dd72d1d465 | 110 | * base class is also a base type of the first non static data member: |
borlanic | 0:02dd72d1d465 | 111 | * |
borlanic | 0:02dd72d1d465 | 112 | * @code |
borlanic | 0:02dd72d1d465 | 113 | * struct A { }; |
borlanic | 0:02dd72d1d465 | 114 | * struct B : A { |
borlanic | 0:02dd72d1d465 | 115 | * int foo; |
borlanic | 0:02dd72d1d465 | 116 | * }; |
borlanic | 0:02dd72d1d465 | 117 | * // thanks to empty base optimization, sizeof(B) == sizeof(int) |
borlanic | 0:02dd72d1d465 | 118 | * |
borlanic | 0:02dd72d1d465 | 119 | * struct C : A { |
borlanic | 0:02dd72d1d465 | 120 | * B b; |
borlanic | 0:02dd72d1d465 | 121 | * }; |
borlanic | 0:02dd72d1d465 | 122 | * |
borlanic | 0:02dd72d1d465 | 123 | * // empty base optimization cannot be applied here because A from C and A from |
borlanic | 0:02dd72d1d465 | 124 | * // B shall have a different address. In that case, with the alignment |
borlanic | 0:02dd72d1d465 | 125 | * // sizeof(C) == 2* sizeof(int) |
borlanic | 0:02dd72d1d465 | 126 | * @endcode |
borlanic | 0:02dd72d1d465 | 127 | * |
borlanic | 0:02dd72d1d465 | 128 | * The solution to that problem is to templatize the empty class to makes it |
borlanic | 0:02dd72d1d465 | 129 | * unique to the type it is applied to: |
borlanic | 0:02dd72d1d465 | 130 | * |
borlanic | 0:02dd72d1d465 | 131 | * @code |
borlanic | 0:02dd72d1d465 | 132 | * template<typename T> |
borlanic | 0:02dd72d1d465 | 133 | * struct A<T> { }; |
borlanic | 0:02dd72d1d465 | 134 | * struct B : A<B> { |
borlanic | 0:02dd72d1d465 | 135 | * int foo; |
borlanic | 0:02dd72d1d465 | 136 | * }; |
borlanic | 0:02dd72d1d465 | 137 | * struct C : A<C> { |
borlanic | 0:02dd72d1d465 | 138 | * B b; |
borlanic | 0:02dd72d1d465 | 139 | * }; |
borlanic | 0:02dd72d1d465 | 140 | * |
borlanic | 0:02dd72d1d465 | 141 | * // empty base optimization can be applied B and C does not refer to the same |
borlanic | 0:02dd72d1d465 | 142 | * // kind of A. sizeof(C) == sizeof(B) == sizeof(int). |
borlanic | 0:02dd72d1d465 | 143 | * @endcode |
borlanic | 0:02dd72d1d465 | 144 | * |
borlanic | 0:02dd72d1d465 | 145 | * @note Compile time errors are disabled if the develop or the release profile |
borlanic | 0:02dd72d1d465 | 146 | * is used. To override this behavior and force compile time errors in all profile |
borlanic | 0:02dd72d1d465 | 147 | * set the configuration parameter "platform.force-non-copyable-error" to true. |
borlanic | 0:02dd72d1d465 | 148 | */ |
borlanic | 0:02dd72d1d465 | 149 | template<typename T> |
borlanic | 0:02dd72d1d465 | 150 | class NonCopyable { |
borlanic | 0:02dd72d1d465 | 151 | protected: |
borlanic | 0:02dd72d1d465 | 152 | /** |
borlanic | 0:02dd72d1d465 | 153 | * Disallow construction of NonCopyable objects from outside of its hierarchy. |
borlanic | 0:02dd72d1d465 | 154 | */ |
borlanic | 0:02dd72d1d465 | 155 | NonCopyable() { } |
borlanic | 0:02dd72d1d465 | 156 | /** |
borlanic | 0:02dd72d1d465 | 157 | * Disallow destruction of NonCopyable objects from outside of its hierarchy. |
borlanic | 0:02dd72d1d465 | 158 | */ |
borlanic | 0:02dd72d1d465 | 159 | ~NonCopyable() { } |
borlanic | 0:02dd72d1d465 | 160 | |
borlanic | 0:02dd72d1d465 | 161 | #if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0)) |
borlanic | 0:02dd72d1d465 | 162 | /** |
borlanic | 0:02dd72d1d465 | 163 | * NonCopyable copy constructor. |
borlanic | 0:02dd72d1d465 | 164 | * |
borlanic | 0:02dd72d1d465 | 165 | * A compile time warning is issued when this function is used and a runtime |
borlanic | 0:02dd72d1d465 | 166 | * warning is printed when the copy construction of the non copyable happens. |
borlanic | 0:02dd72d1d465 | 167 | * |
borlanic | 0:02dd72d1d465 | 168 | * If you see this warning, your code is probably doing something unspecified. |
borlanic | 0:02dd72d1d465 | 169 | * Copy of non copyable resources can lead to resource leak and random error. |
borlanic | 0:02dd72d1d465 | 170 | */ |
borlanic | 0:02dd72d1d465 | 171 | MBED_DEPRECATED("Invalid copy construction of a NonCopyable resource.") |
borlanic | 0:02dd72d1d465 | 172 | NonCopyable(const NonCopyable&) |
borlanic | 0:02dd72d1d465 | 173 | { |
borlanic | 0:02dd72d1d465 | 174 | debug("Invalid copy construction of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION); |
borlanic | 0:02dd72d1d465 | 175 | } |
borlanic | 0:02dd72d1d465 | 176 | |
borlanic | 0:02dd72d1d465 | 177 | /** |
borlanic | 0:02dd72d1d465 | 178 | * NonCopyable copy assignment operator. |
borlanic | 0:02dd72d1d465 | 179 | * |
borlanic | 0:02dd72d1d465 | 180 | * A compile time warning is issued when this function is used and a runtime |
borlanic | 0:02dd72d1d465 | 181 | * warning is printed when the copy construction of the non copyable happens. |
borlanic | 0:02dd72d1d465 | 182 | * |
borlanic | 0:02dd72d1d465 | 183 | * If you see this warning, your code is probably doing something unspecified. |
borlanic | 0:02dd72d1d465 | 184 | * Copy of non copyable resources can lead to resource leak and random error. |
borlanic | 0:02dd72d1d465 | 185 | */ |
borlanic | 0:02dd72d1d465 | 186 | MBED_DEPRECATED("Invalid copy assignment of a NonCopyable resource.") |
borlanic | 0:02dd72d1d465 | 187 | NonCopyable& operator=(const NonCopyable&) |
borlanic | 0:02dd72d1d465 | 188 | { |
borlanic | 0:02dd72d1d465 | 189 | debug("Invalid copy assignment of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION); |
borlanic | 0:02dd72d1d465 | 190 | return *this; |
borlanic | 0:02dd72d1d465 | 191 | } |
borlanic | 0:02dd72d1d465 | 192 | |
borlanic | 0:02dd72d1d465 | 193 | #else |
borlanic | 0:02dd72d1d465 | 194 | private: |
borlanic | 0:02dd72d1d465 | 195 | /** |
borlanic | 0:02dd72d1d465 | 196 | * Declare copy constructor as private, any attempt to copy construct |
borlanic | 0:02dd72d1d465 | 197 | * a NonCopyable will fail at compile time. |
borlanic | 0:02dd72d1d465 | 198 | */ |
borlanic | 0:02dd72d1d465 | 199 | NonCopyable(const NonCopyable&); |
borlanic | 0:02dd72d1d465 | 200 | |
borlanic | 0:02dd72d1d465 | 201 | /** |
borlanic | 0:02dd72d1d465 | 202 | * Declare copy assignment operator as private, any attempt to copy assign |
borlanic | 0:02dd72d1d465 | 203 | * a NonCopyable will fail at compile time. |
borlanic | 0:02dd72d1d465 | 204 | */ |
borlanic | 0:02dd72d1d465 | 205 | NonCopyable& operator=(const NonCopyable&); |
borlanic | 0:02dd72d1d465 | 206 | #endif |
borlanic | 0:02dd72d1d465 | 207 | }; |
borlanic | 0:02dd72d1d465 | 208 | |
borlanic | 0:02dd72d1d465 | 209 | } // namespace mbed |
borlanic | 0:02dd72d1d465 | 210 | |
borlanic | 0:02dd72d1d465 | 211 | #endif /* MBED_NONCOPYABLE_H_ */ |