BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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