NonCopyable.h 6.6 KB

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