mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 1 /* Copyright (c) 2017 ARM Limited
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 2 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 3 * Licensed under the Apache License, Version 2.0 (the "License");
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 4 * you may not use this file except in compliance with the License.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 5 * You may obtain a copy of the License at
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 6 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 7 * http://www.apache.org/licenses/LICENSE-2.0
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 8 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 9 * Unless required by applicable law or agreed to in writing, software
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 10 * distributed under the License is distributed on an "AS IS" BASIS,
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 12 * See the License for the specific language governing permissions and
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 13 * limitations under the License.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 14 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 15
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 16 #ifndef MBED_NONCOPYABLE_H_
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 17 #define MBED_NONCOPYABLE_H_
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 18
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 19 namespace mbed {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 20
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 21 /**
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 22 * Inheriting from this class autogeneration of copy construction and copy
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 23 * assignement operations.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 24 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 25 * Classes which are not value type should inherit privately from this class
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 26 * to avoid generation of invalid copy constructor or copy assignement operator
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 27 * which can lead to unoticeable programming errors.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 28 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 29 * As an example consider the following signature:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 30 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 31 * @code
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 32 * class Resource;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 33 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 34 * class Foo {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 35 * public:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 36 * Foo() : _resource(new Resource()) { }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 37 * ~Foo() { delete _resource; }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 38 * private:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 39 * Resource* _resource;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 40 * }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 41 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 42 * Foo get_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 43 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 44 * Foo foo = get_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 45 * @endcode
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 46 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 47 * There is a bug in this function, it returns a temporary value which will be
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 48 * byte copied into foo then destroyed. Unfortunately, internaly the Foo class
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 49 * manage a pointer to a Resource object. This pointer will be released when the
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 50 * temporary is destroyed and foo will manage a pointer to an already released
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 51 * Resource.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 52 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 53 * Two issues has to be fixed in the example above:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 54 * - Function signature has to be changed to reflect the fact that Foo
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 55 * instances cannot be copied. In that case accessor should return a
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 56 * reference to give access to objects already existing and managed.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 57 * Generator on the other hand should return a pointer to the created object.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 58 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 59 * @code
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 60 * // return a reference to an already managed Foo instance
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 61 * Foo& get_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 62 * Foo& foo = get_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 63 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 64 * // create a new Foo instance
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 65 * Foo* make_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 66 * Foo* m = make_foo();
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 67 * @endcode
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 68 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 69 * - Copy constructor and copy assignement operator has to be made private
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 70 * in the Foo class. It prevents unwanted copy of Foo objects. This can be
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 71 * done by declaring copy constructor and copy assignement in the private
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 72 * section of the Foo class.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 73 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 74 * @code
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 75 * class Foo {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 76 * public:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 77 * Foo() : _resource(new Resource()) { }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 78 * ~Foo() { delete _resource; }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 79 * private:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 80 * // disallow copy operations
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 81 * Foo(const Foo&);
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 82 * Foo& operator=(const Foo&);
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 83 * // data members
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 84 * Resource* _resource;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 85 * }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 86 * @endcode
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 87 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 88 * Another solution is to inherit privately from the NonCopyable class.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 89 * It reduces the boiler plate needed to avoid copy operations but more
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 90 * importantly it clarifies the programer intent and the object semantic.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 91 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 92 * class Foo : private NonCopyable<Foo> {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 93 * public:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 94 * Foo() : _resource(new Resource()) { }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 95 * ~Foo() { delete _resource; }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 96 * private:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 97 * Resource* _resource;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 98 * }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 99 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 100 * @tparam T The type that should be made non copyable. It prevent cases where
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 101 * the empty base optimization cannot be applied and therefore ensure that the
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 102 * cost of this semantic sugar is null.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 103 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 104 * As an example, the empty base optimization is prohibited if one of the empty
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 105 * base class is also a base type of the first non static data member:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 106 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 107 * @code
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 108 * struct A { };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 109 * struct B : A {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 110 * int foo;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 111 * };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 112 * // thanks to empty base optimization, sizeof(B) == sizeof(int)
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 113 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 114 * struct C : A {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 115 * B b;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 116 * };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 117 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 118 * // empty base optimization cannot be applied here because A from C and A from
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 119 * // B shall have a different address. In that case, with the alignement
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 120 * // sizeof(C) == 2* sizeof(int)
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 121 * @endcode
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 122 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 123 * The solution to that problem is to templatize the empty class to makes it
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 124 * unique to the type it is applied to:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 125 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 126 * @code
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 127 * template<typename T>
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 128 * struct A<T> { };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 129 * struct B : A<B> {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 130 * int foo;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 131 * };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 132 * struct C : A<C> {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 133 * B b;
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 134 * };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 135 *
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 136 * // empty base optimization can be applied B and C does not refer to the same
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 137 * // kind of A. sizeof(C) == sizeof(B) == sizeof(int).
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 138 * @endcode
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 139 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 140 template<typename T>
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 141 class NonCopyable {
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 142 protected:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 143 /**
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 144 * Disalow construction of NonCopyable objects from outside of its hierarchy.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 145 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 146 NonCopyable() { }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 147 /**
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 148 * Disalow destruction of NonCopyable objects from outside of its hierarchy.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 149 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 150 ~NonCopyable() { }
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 151
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 152 private:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 153 /**
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 154 * Declare copy constructor as private, any attempt to copy construct
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 155 * a NonCopyable will fail at compile time.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 156 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 157 NonCopyable(const NonCopyable&);
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 158
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 159 /**
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 160 * Declare copy assignement operator as private, any attempt to copy assign
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 161 * a NonCopyable will fail at compile time.
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 162 */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 163 NonCopyable& operator=(const NonCopyable&);
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 164 };
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 165
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 166 } // namespace mbed
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 167
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 168 #endif /* MBED_NONCOPYABLE_H_ */
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 0:bdf663c61a82 169