Skip to content

Commit 80c9f8b

Browse files
committed
Platform: Turn NonCopyable compile time error into warnings.
Turn the compile time error issued when a NonCopyable resource is copied into a compile time and runtime warning. If the application is compiled with the debug profile the compile time error remains. The compile time error can be enforced by setting the library option force-non-copyable-error to true.
1 parent 5310451 commit 80c9f8b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

platform/NonCopyable.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#ifndef MBED_NONCOPYABLE_H_
1717
#define MBED_NONCOPYABLE_H_
1818

19+
#if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0))
20+
#include "mbed_toolchain.h"
21+
#include "mbed_debug.h"
22+
#endif
23+
1924
namespace mbed {
2025

2126
/**
@@ -136,6 +141,10 @@ namespace mbed {
136141
* // empty base optimization can be applied B and C does not refer to the same
137142
* // kind of A. sizeof(C) == sizeof(B) == sizeof(int).
138143
* @endcode
144+
*
145+
* @note Compile time errors are disabled if the develop or the release profile
146+
* is used. To override this behavior and force compile time errors in all profile
147+
* set the configuration parameter "platform.force-non-copyable-error" to true.
139148
*/
140149
template<typename T>
141150
class NonCopyable {
@@ -149,6 +158,39 @@ class NonCopyable {
149158
*/
150159
~NonCopyable() { }
151160

161+
#if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0))
162+
/**
163+
* NonCopyable copy constructor.
164+
*
165+
* A compile time warning is issued when this function is used and a runtime
166+
* warning is printed when the copy construction of the non copyable happens.
167+
*
168+
* If you see this warning, your code is probably doing something unspecified.
169+
* Copy of non copyable resources can lead to resource leak and random error.
170+
*/
171+
MBED_DEPRECATED("Invalid copy construction of a NonCopyable resource.")
172+
NonCopyable(const NonCopyable&)
173+
{
174+
debug("Invalid copy construction of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION);
175+
}
176+
177+
/**
178+
* NonCopyable copy assignment operator.
179+
*
180+
* A compile time warning is issued when this function is used and a runtime
181+
* warning is printed when the copy construction of the non copyable happens.
182+
*
183+
* If you see this warning, your code is probably doing something unspecified.
184+
* Copy of non copyable resources can lead to resource leak and random error.
185+
*/
186+
MBED_DEPRECATED("Invalid copy assignment of a NonCopyable resource.")
187+
NonCopyable& operator=(const NonCopyable&)
188+
{
189+
debug("Invalid copy assignment of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION);
190+
return *this;
191+
}
192+
193+
#else
152194
private:
153195
/**
154196
* Declare copy constructor as private, any attempt to copy construct
@@ -161,6 +203,7 @@ class NonCopyable {
161203
* a NonCopyable will fail at compile time.
162204
*/
163205
NonCopyable& operator=(const NonCopyable&);
206+
#endif
164207
};
165208

166209
} // namespace mbed

platform/mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"default-serial-baud-rate": {
2020
"help": "Default baud rate for a Serial or RawSerial instance (if not specified in the constructor)",
2121
"value": 9600
22+
},
23+
24+
"force-non-copyable-error": {
25+
"help": "Force compile time error when a NonCopyable object is copied",
26+
"value": false
2227
}
2328
},
2429
"target_overrides": {

0 commit comments

Comments
 (0)