The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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