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