From 4f7943860a7e896dac04ee93613c122cd23cf334 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 12:38:26 +0100 Subject: [PATCH 1/8] platform: Add NonCopyable class. The NonCopyable template class avoid autogeneration of copy assignement and copy construction function for classes inheriting from it. --- platform/NonCopyable.h | 168 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 platform/NonCopyable.h diff --git a/platform/NonCopyable.h b/platform/NonCopyable.h new file mode 100644 index 0000000000..c8cc092500 --- /dev/null +++ b/platform/NonCopyable.h @@ -0,0 +1,168 @@ +/* Copyright (c) 2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MBED_NONCOPYABLE_H_ +#define MBED_NONCOPYABLE_H_ + +namespace mbed { + +/** + * Inheriting from this class autogeneration of copy construction and copy + * assignement operations. + * + * Classes which are not value type should inherit privately from this class + * to avoid generation of invalid copy constructor or copy assignement operator + * which can lead to unoticeable programming errors. + * + * As an example consider the following signature: + * + * @code + * class Resource; + * + * class Foo { + * public: + * Foo() : _resource(new Resource()) { } + * ~Foo() { delete _resource; } + * private: + * Resource* _resource; + * } + * + * Foo get_foo(); + * + * Foo foo = get_foo(); + * @endcode + * + * There is a bug in this function, it returns a temporary value which will be + * byte copied into foo then destroyed. Unfortunately, internaly the Foo class + * manage a pointer to a Resource object. This pointer will be released when the + * temporary is destroyed and foo will manage a pointer to an already released + * Resource. + * + * Two issues has to be fixed in the example above: + * - Function signature has to be changed to reflect the fact that Foo + * instances cannot be copied. In that case accessor should return a + * reference to give access to objects already existing and managed. + * Generator on the other hand should return a pointer to the created object. + * + * @code + * // return a reference to an already managed Foo instance + * Foo& get_foo(); + * Foo& foo = get_foo(); + * + * // create a new Foo instance + * Foo* make_foo(); + * Foo* m = make_foo(); + * @endcode + * + * - Copy constructor and copy assignement operator has to be made private + * in the Foo class. It prevents unwanted copy of Foo objects. This can be + * done by declaring copy constructor and copy assignement in the private + * section of the Foo class. + * + * @code + * class Foo { + * public: + * Foo() : _resource(new Resource()) { } + * ~Foo() { delete _resource; } + * private: + * // disallow copy operations + * Foo(const Foo&); + * Foo& operator=(const Foo&); + * // data members + * Resource* _resource; + * } + * @endcode + * + * Another solution is to inherit privately from the NonCopyable class. + * It reduces the boiler plate needed to avoid copy operations but more + * importantly it clarifies the programer intent and the object semantic. + * + * class Foo : private NonCopyable { + * public: + * Foo() : _resource(new Resource()) { } + * ~Foo() { delete _resource; } + * private: + * Resource* _resource; + * } + * + * @tparam T The type that should be made non copyable. It prevent cases where + * the empty base optimization cannot be applied and therefore ensure that the + * cost of this semantic sugar is null. + * + * As an example, the empty base optimization is prohibited if one of the empty + * base class is also a base type of the first non static data member: + * + * @code + * struct A { }; + * struct B : A { + * int foo; + * }; + * // thanks to empty base optimization, sizeof(B) == sizeof(int) + * + * struct C : A { + * B b; + * }; + * + * // empty base optimization cannot be applied here because A from C and A from + * // B shall have a different address. In that case, with the alignement + * // sizeof(C) == 2* sizeof(int) + * @endcode + * + * The solution to that problem is to templatize the empty class to makes it + * unique to the type it is applied to: + * + * @code + * template + * struct A { }; + * struct B : A { + * int foo; + * }; + * struct C : A { + * B b; + * }; + * + * // empty base optimization can be applied B and C does not refer to the same + * // kind of A. sizeof(C) == sizeof(B) == sizeof(int). + * @endcode + */ +template +class NonCopyable { +protected: + /** + * Disalow construction of NonCopyable objects from outside of its hierarchy. + */ + NonCopyable() { } + /** + * Disalow destruction of NonCopyable objects from outside of its hierarchy. + */ + ~NonCopyable() { } + +private: + /** + * Declare copy constructor as private, any attempt to copy construct + * a NonCopyable will fail at compile time. + */ + NonCopyable(const NonCopyable&); + + /** + * Declare copy assignement operator as private, any attempt to copy assign + * a NonCopyable will fail at compile time. + */ + NonCopyable& operator=(const NonCopyable&); +}; + +} // namespace mbed + +#endif /* MBED_NONCOPYABLE_H_ */ From dcbcf648302cb29f227296979916510a15038d8b Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 12:45:52 +0100 Subject: [PATCH 2/8] platform: Tag classes DirHandle, FileHandle, FileLike, FileSystemHandle, FileSystemLike, LocalFileHandle, LocalFileSystem and PlatformMutex as non copyable. This avoid unwanted copy of these type which is a programming error. --- platform/DirHandle.h | 3 ++- platform/FileHandle.h | 3 ++- platform/FileLike.h | 3 ++- platform/FileSystemHandle.h | 3 ++- platform/FileSystemLike.h | 3 ++- platform/LocalFileSystem.h | 5 +++-- platform/PlatformMutex.h | 4 +++- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/platform/DirHandle.h b/platform/DirHandle.h index 61b1bed41b..b1dcfe2b1f 100644 --- a/platform/DirHandle.h +++ b/platform/DirHandle.h @@ -19,6 +19,7 @@ #include #include "platform/platform.h" #include "platform/FileHandle.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -41,7 +42,7 @@ namespace mbed { * @note Synchronization level: Set by subclass * @ingroup platform */ -class DirHandle { +class DirHandle : private NonCopyable { public: virtual ~DirHandle() {} diff --git a/platform/FileHandle.h b/platform/FileHandle.h index 187f7c18ee..a6b306b3fa 100644 --- a/platform/FileHandle.h +++ b/platform/FileHandle.h @@ -22,6 +22,7 @@ typedef int FILEHANDLE; #include "Callback.h" #include "platform/mbed_poll.h" #include "platform/platform.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -37,7 +38,7 @@ namespace mbed { * @note Synchronization level: Set by subclass * @ingroup platform */ -class FileHandle { +class FileHandle : private NonCopyable { public: virtual ~FileHandle() {} diff --git a/platform/FileLike.h b/platform/FileLike.h index e65e611aa9..91a3f304d0 100644 --- a/platform/FileLike.h +++ b/platform/FileLike.h @@ -19,6 +19,7 @@ #include "platform/mbed_toolchain.h" #include "platform/FileBase.h" #include "platform/FileHandle.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -31,7 +32,7 @@ namespace mbed { * @note Synchronization level: Set by subclass * @ingroup platform */ -class FileLike : public FileHandle, public FileBase { +class FileLike : public FileHandle, public FileBase, private NonCopyable { public: /** Constructor FileLike * diff --git a/platform/FileSystemHandle.h b/platform/FileSystemHandle.h index 61af823c7e..dda1f6f271 100644 --- a/platform/FileSystemHandle.h +++ b/platform/FileSystemHandle.h @@ -21,6 +21,7 @@ #include "platform/FileBase.h" #include "platform/FileHandle.h" #include "platform/DirHandle.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -35,7 +36,7 @@ namespace mbed { * * @note Synchronization level: Set by subclass */ -class FileSystemHandle { +class FileSystemHandle : private NonCopyable { public: /** FileSystemHandle lifetime */ diff --git a/platform/FileSystemLike.h b/platform/FileSystemLike.h index 65707039d9..d8923391d6 100644 --- a/platform/FileSystemLike.h +++ b/platform/FileSystemLike.h @@ -21,6 +21,7 @@ #include "platform/FileSystemHandle.h" #include "platform/FileHandle.h" #include "platform/DirHandle.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -35,7 +36,7 @@ namespace mbed { * @note Synchronization level: Set by subclass * @ingroup platform */ -class FileSystemLike : public FileSystemHandle, public FileBase { +class FileSystemLike : public FileSystemHandle, public FileBase, private NonCopyable { public: /** FileSystemLike lifetime */ diff --git a/platform/LocalFileSystem.h b/platform/LocalFileSystem.h index 6ed24ff73c..3bd64d8377 100644 --- a/platform/LocalFileSystem.h +++ b/platform/LocalFileSystem.h @@ -22,6 +22,7 @@ #include "platform/FileSystemLike.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -34,7 +35,7 @@ FILEHANDLE local_file_open(const char* name, int flags); * @class LocalFileHandle * @ingroup platform */ -class LocalFileHandle : public FileHandle { +class LocalFileHandle : public FileHandle, private NonCopyable { public: LocalFileHandle(FILEHANDLE fh); @@ -98,7 +99,7 @@ protected: * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again! * @ingroup platform */ -class LocalFileSystem : public FileSystemLike { +class LocalFileSystem : public FileSystemLike, private NonCopyable { // No modifiable state public: diff --git a/platform/PlatformMutex.h b/platform/PlatformMutex.h index 3746ee80d7..517fd3ad05 100644 --- a/platform/PlatformMutex.h +++ b/platform/PlatformMutex.h @@ -18,6 +18,8 @@ #ifndef PLATFORM_MUTEX_H #define PLATFORM_MUTEX_H +#include "platform/NonCopyable.h" + #ifdef MBED_CONF_RTOS_PRESENT #include "rtos/Mutex.h" typedef rtos::Mutex PlatformMutex; @@ -25,7 +27,7 @@ typedef rtos::Mutex PlatformMutex; /** A stub mutex for when an RTOS is not present * @ingroup platform */ -class PlatformMutex { +class PlatformMutex : private mbed::NonCopyable { public: PlatformMutex() { // Stub From 7a1e2cfc9a20f4b2be95932c051d4fc78820c890 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 12:47:27 +0100 Subject: [PATCH 3/8] platform: Replace private copy constructor and copy assignement operator by a NonCopyable tag. The class concerned by this change are: ATCmdParser, CallChain, FileBase and Stream. --- platform/ATCmdParser.h | 7 +------ platform/CallChain.h | 6 ++---- platform/FileBase.h | 5 ++--- platform/Stream.h | 8 ++------ 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/platform/ATCmdParser.h b/platform/ATCmdParser.h index 199977d9be..6922251faa 100644 --- a/platform/ATCmdParser.h +++ b/platform/ATCmdParser.h @@ -44,7 +44,7 @@ namespace mbed { -class ATCmdParser +class ATCmdParser : private NonCopyable { private: // File handle @@ -70,11 +70,6 @@ private: }; oob *_oobs; - // Prohibiting use of of copy constructor - ATCmdParser(const ATCmdParser &); - // Prohibiting copy assignment Operator - ATCmdParser &operator=(const ATCmdParser &); - public: /** diff --git a/platform/CallChain.h b/platform/CallChain.h index d702ec89c8..42e97e6d28 100644 --- a/platform/CallChain.h +++ b/platform/CallChain.h @@ -18,6 +18,7 @@ #include "platform/Callback.h" #include "platform/mbed_toolchain.h" +#include "platform/NonCopyable.h" #include namespace mbed { @@ -65,7 +66,7 @@ namespace mbed { typedef Callback *pFunctionPointer_t; class CallChainLink; -class CallChain { +class CallChain : private NonCopyable { public: /** Create an empty chain * @@ -178,10 +179,7 @@ public: return get(i); } - /* disallow copy constructor and assignment operators */ private: - CallChain(const CallChain&); - CallChain & operator = (const CallChain&); CallChainLink *_chain; }; diff --git a/platform/FileBase.h b/platform/FileBase.h index 9a451bb9bc..5df9ef8550 100644 --- a/platform/FileBase.h +++ b/platform/FileBase.h @@ -24,6 +24,7 @@ typedef int FILEHANDLE; #include "platform/platform.h" #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup platform */ @@ -39,7 +40,7 @@ typedef enum { * @class FileBase * @ingroup platform */ -class FileBase { +class FileBase : private NonCopyable { public: FileBase(const char *name, PathType t); virtual ~FileBase(); @@ -59,8 +60,6 @@ private: FileBase *_next; const char * const _name; const PathType _path_type; - FileBase(const FileBase&); - FileBase & operator = (const FileBase&); }; } // namespace mbed diff --git a/platform/Stream.h b/platform/Stream.h index c715d64bfe..fd74b0520f 100644 --- a/platform/Stream.h +++ b/platform/Stream.h @@ -19,6 +19,7 @@ #include "platform/platform.h" #include "platform/FileLike.h" #include "platform/FileHandle.h" +#include "platform/NonCopyable.h" #include #include @@ -36,7 +37,7 @@ extern char* mbed_gets(char *s, int size, std::FILE *_file); * @note Synchronization level: Set by subclass * @ingroup platform */ -class Stream : public FileLike { +class Stream : public FileLike, private NonCopyable { public: Stream(const char *name=NULL); @@ -80,11 +81,6 @@ protected: virtual void unlock() { // Stub } - - /* disallow copy constructor and assignment operators */ -private: - Stream(const Stream&); - Stream & operator = (const Stream&); }; } // namespace mbed From 4d5f805cdeca8979c1463246db0125ce9185ab9f Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 13:00:20 +0100 Subject: [PATCH 4/8] rtos: Tag non value type as NonCopyable. The types marked are: Mail, MemoryPool, Mutex, Queue, RtosTimer and Semaphore. --- rtos/Mail.h | 4 +++- rtos/MemoryPool.h | 3 ++- rtos/Mutex.h | 4 +++- rtos/Queue.h | 3 ++- rtos/RtosTimer.h | 3 ++- rtos/Semaphore.h | 3 ++- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/rtos/Mail.h b/rtos/Mail.h index 5ac0469a05..b4a0289862 100644 --- a/rtos/Mail.h +++ b/rtos/Mail.h @@ -31,6 +31,8 @@ #include "rtx_lib.h" #include "mbed_rtos1_types.h" +#include "platform/NonCopyable.h" + using namespace rtos; namespace rtos { @@ -47,7 +49,7 @@ namespace rtos { both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ template -class Mail { +class Mail : private mbed::NonCopyable > { public: /** Create and Initialise Mail queue. */ Mail() { }; diff --git a/rtos/MemoryPool.h b/rtos/MemoryPool.h index 4590d6e601..002835bf8a 100644 --- a/rtos/MemoryPool.h +++ b/rtos/MemoryPool.h @@ -28,6 +28,7 @@ #include "cmsis_os2.h" #include "mbed_rtos1_types.h" #include "mbed_rtos_storage.h" +#include "platform/NonCopyable.h" namespace rtos { /** \addtogroup rtos */ @@ -42,7 +43,7 @@ namespace rtos { both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ template -class MemoryPool { +class MemoryPool : private mbed::NonCopyable > { public: /** Create and Initialize a memory pool. */ MemoryPool() { diff --git a/rtos/Mutex.h b/rtos/Mutex.h index 2224c45980..8f5c0e2adb 100644 --- a/rtos/Mutex.h +++ b/rtos/Mutex.h @@ -27,6 +27,8 @@ #include "mbed_rtos1_types.h" #include "mbed_rtos_storage.h" +#include "platform/NonCopyable.h" + namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -38,7 +40,7 @@ namespace rtos { Memory considerations: The mutex control structures will be created on current thread's stack, both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ -class Mutex { +class Mutex : private mbed::NonCopyable { public: /** Create and Initialize a Mutex object */ Mutex(); diff --git a/rtos/Queue.h b/rtos/Queue.h index f03caeb92b..01a46bee15 100644 --- a/rtos/Queue.h +++ b/rtos/Queue.h @@ -28,6 +28,7 @@ #include "cmsis_os2.h" #include "mbed_rtos_storage.h" #include "platform/mbed_error.h" +#include "platform/NonCopyable.h" #include "mbed_rtos1_types.h" namespace rtos { @@ -45,7 +46,7 @@ namespace rtos { and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ template -class Queue { +class Queue : private mbed::NonCopyable > { public: /** Create and initialize a message Queue. */ Queue() { diff --git a/rtos/RtosTimer.h b/rtos/RtosTimer.h index 901ebcab5c..b490fd351a 100644 --- a/rtos/RtosTimer.h +++ b/rtos/RtosTimer.h @@ -26,6 +26,7 @@ #include "cmsis_os2.h" #include "rtx_lib.h" #include "platform/Callback.h" +#include "platform/NonCopyable.h" #include "platform/mbed_toolchain.h" #include "mbed_rtos1_types.h" @@ -79,7 +80,7 @@ namespace rtos { Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ -class RtosTimer { +class RtosTimer : private mbed::NonCopyable { public: /** Create timer. @param func function to be executed by this timer. diff --git a/rtos/Semaphore.h b/rtos/Semaphore.h index 8f939c3434..164df2958e 100644 --- a/rtos/Semaphore.h +++ b/rtos/Semaphore.h @@ -26,6 +26,7 @@ #include "cmsis_os2.h" #include "mbed_rtos1_types.h" #include "mbed_rtos_storage.h" +#include "platform/NonCopyable.h" namespace rtos { /** \addtogroup rtos */ @@ -37,7 +38,7 @@ namespace rtos { * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). */ -class Semaphore { +class Semaphore : private mbed::NonCopyable { public: /** Create and Initialize a Semaphore object used for managing resources. @param count number of available resources; maximum index value is (count-1). (default: 0). From 3119c4238d260cb26b8407296d7ebd67cc22e9fa Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 13:01:28 +0100 Subject: [PATCH 5/8] rtos Thread: Remove private copy constructor and assignment operators in favor of the NonCopyable traits. --- rtos/Thread.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rtos/Thread.h b/rtos/Thread.h index 3c00c36d6d..f692ff1083 100644 --- a/rtos/Thread.h +++ b/rtos/Thread.h @@ -29,6 +29,7 @@ #include "mbed_rtx_conf.h" #include "platform/Callback.h" #include "platform/mbed_toolchain.h" +#include "platform/NonCopyable.h" #include "rtos/Semaphore.h" #include "rtos/Mutex.h" @@ -69,7 +70,7 @@ namespace rtos { * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). * Additionally the stack memory for this thread will be allocated on the heap, if it wasn't supplied to the constructor. */ -class Thread { +class Thread : private mbed::NonCopyable { public: /** Allocate a new thread without starting execution @param priority initial priority of the thread function. (default: osPriorityNormal). @@ -348,10 +349,6 @@ public: virtual ~Thread(); private: - /* disallow copy constructor and assignment operators */ - Thread(const Thread&); - Thread& operator=(const Thread&); - // Required to share definitions without // delegated constructors void constructor(osPriority priority=osPriorityNormal, From f57cc80ecc15ba7ee08c74eb0b0c9fb276374421 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 14:20:38 +0100 Subject: [PATCH 6/8] event: Make the event queue non copyable. --- events/EventQueue.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/events/EventQueue.h b/events/EventQueue.h index 709479255a..9b15edb6fa 100644 --- a/events/EventQueue.h +++ b/events/EventQueue.h @@ -19,6 +19,7 @@ #include "equeue/equeue.h" #include "platform/Callback.h" +#include "platform/NonCopyable.h" #include #include @@ -47,7 +48,7 @@ class Event; * Flexible event queue for dispatching events * @ingroup events */ -class EventQueue { +class EventQueue : private mbed::NonCopyable { public: /** Create an EventQueue * From 7f5b992064eee3897a95e924cf1223374ef662c5 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 14:23:10 +0100 Subject: [PATCH 7/8] drivers: Replace private copy constructor and Copy assignement operator by NonCopyable traits. Modified classes are: BusIn, BusOut, BusInOut and InterruptManager. --- drivers/BusIn.h | 6 ++---- drivers/BusInOut.h | 8 ++------ drivers/BusOut.h | 8 ++------ drivers/InterruptManager.h | 9 ++------- 4 files changed, 8 insertions(+), 23 deletions(-) diff --git a/drivers/BusIn.h b/drivers/BusIn.h index 55a4077038..d015529435 100644 --- a/drivers/BusIn.h +++ b/drivers/BusIn.h @@ -19,6 +19,7 @@ #include "platform/platform.h" #include "drivers/DigitalIn.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -28,7 +29,7 @@ namespace mbed { * @note Synchronization level: Thread safe * @ingroup drivers */ -class BusIn { +class BusIn : private NonCopyable { public: /* Group: Configuration Methods */ @@ -115,12 +116,9 @@ protected: PlatformMutex _mutex; - /* disallow copy constructor and assignment operators */ private: virtual void lock(); virtual void unlock(); - BusIn(const BusIn&); - BusIn & operator = (const BusIn&); }; } // namespace mbed diff --git a/drivers/BusInOut.h b/drivers/BusInOut.h index 4295707f95..0be52cacc5 100644 --- a/drivers/BusInOut.h +++ b/drivers/BusInOut.h @@ -18,6 +18,7 @@ #include "drivers/DigitalInOut.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -27,7 +28,7 @@ namespace mbed { * @note Synchronization level: Thread safe * @ingroup drivers */ -class BusInOut { +class BusInOut : private NonCopyable { public: @@ -135,11 +136,6 @@ protected: int _nc_mask; PlatformMutex _mutex; - - /* disallow copy constructor and assignment operators */ -private: - BusInOut(const BusInOut&); - BusInOut & operator = (const BusInOut&); }; } // namespace mbed diff --git a/drivers/BusOut.h b/drivers/BusOut.h index 554c45f51e..d7612d4592 100644 --- a/drivers/BusOut.h +++ b/drivers/BusOut.h @@ -18,6 +18,7 @@ #include "drivers/DigitalOut.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -25,7 +26,7 @@ namespace mbed { /** A digital output bus, used for setting the state of a collection of pins * @ingroup drivers */ -class BusOut { +class BusOut : private NonCopyable { public: @@ -119,11 +120,6 @@ protected: int _nc_mask; PlatformMutex _mutex; - - /* disallow copy constructor and assignment operators */ -private: - BusOut(const BusOut&); - BusOut & operator = (const BusOut&); }; } // namespace mbed diff --git a/drivers/InterruptManager.h b/drivers/InterruptManager.h index aed49af81e..8060738115 100644 --- a/drivers/InterruptManager.h +++ b/drivers/InterruptManager.h @@ -19,6 +19,7 @@ #include "cmsis.h" #include "platform/CallChain.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" #include namespace mbed { @@ -53,7 +54,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class InterruptManager { +class InterruptManager : private NonCopyable { public: /** Get the instance of InterruptManager Class * @@ -138,12 +139,6 @@ private: void lock(); void unlock(); - // We declare the copy contructor and the assignment operator, but we don't - // implement them. This way, if someone tries to copy/assign our instance, - // he will get an error at compile time. - InterruptManager(const InterruptManager&); - InterruptManager& operator =(const InterruptManager&); - template pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { _mutex.lock(); From 3f388ca2a890774e7c28c4ec34fa65dd2663b61d Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 20 Jun 2017 14:26:29 +0100 Subject: [PATCH 8/8] drivers: Mark non identity types as non copyable with the NonCopyable traits. Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial. --- drivers/CAN.h | 3 ++- drivers/Ethernet.h | 3 ++- drivers/FlashIAP.h | 3 ++- drivers/I2C.h | 3 ++- drivers/InterruptIn.h | 3 ++- drivers/LowPowerTicker.h | 3 ++- drivers/LowPowerTimeout.h | 3 ++- drivers/LowPowerTimer.h | 3 ++- drivers/RawSerial.h | 3 ++- drivers/SPI.h | 3 ++- drivers/SPISlave.h | 3 ++- drivers/Serial.h | 3 ++- drivers/SerialBase.h | 3 ++- drivers/Ticker.h | 3 ++- drivers/Timeout.h | 3 ++- drivers/Timer.h | 3 ++- drivers/TimerEvent.h | 3 ++- drivers/UARTSerial.h | 3 ++- 18 files changed, 36 insertions(+), 18 deletions(-) diff --git a/drivers/CAN.h b/drivers/CAN.h index 36aab9de1e..65a7be8c02 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -23,6 +23,7 @@ #include "hal/can_api.h" #include "platform/Callback.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -78,7 +79,7 @@ public: /** A can bus client, used for communicating with can devices * @ingroup drivers */ -class CAN { +class CAN : private NonCopyable { public: /** Creates an CAN interface connected to specific pins. diff --git a/drivers/Ethernet.h b/drivers/Ethernet.h index d7779f318e..ae4e999589 100644 --- a/drivers/Ethernet.h +++ b/drivers/Ethernet.h @@ -17,6 +17,7 @@ #define MBED_ETHERNET_H #include "platform/platform.h" +#include "platform/NonCopyable.h" #if defined (DEVICE_ETHERNET) || defined(DOXYGEN_ONLY) @@ -54,7 +55,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class Ethernet { +class Ethernet : private NonCopyable { public: diff --git a/drivers/FlashIAP.h b/drivers/FlashIAP.h index e1943c03b3..dd7b167e84 100644 --- a/drivers/FlashIAP.h +++ b/drivers/FlashIAP.h @@ -27,6 +27,7 @@ #include "flash_api.h" #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" namespace mbed { @@ -37,7 +38,7 @@ namespace mbed { * @note Synchronization level: Thread safe * @ingroup drivers */ -class FlashIAP { +class FlashIAP : private NonCopyable { public: FlashIAP(); ~FlashIAP(); diff --git a/drivers/I2C.h b/drivers/I2C.h index 4932cd0659..b2e4eac60d 100644 --- a/drivers/I2C.h +++ b/drivers/I2C.h @@ -23,6 +23,7 @@ #include "hal/i2c_api.h" #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" +#include "platform/NonCopyable.h" #if DEVICE_I2C_ASYNCH #include "platform/CThunk.h" @@ -53,7 +54,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class I2C { +class I2C : private NonCopyable { public: enum RxStatus { diff --git a/drivers/InterruptIn.h b/drivers/InterruptIn.h index aac567a39f..0a122bdb03 100644 --- a/drivers/InterruptIn.h +++ b/drivers/InterruptIn.h @@ -25,6 +25,7 @@ #include "platform/Callback.h" #include "platform/mbed_critical.h" #include "platform/mbed_toolchain.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -56,7 +57,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class InterruptIn { +class InterruptIn : private NonCopyable { public: diff --git a/drivers/LowPowerTicker.h b/drivers/LowPowerTicker.h index 8a076141d2..2df8ef34eb 100644 --- a/drivers/LowPowerTicker.h +++ b/drivers/LowPowerTicker.h @@ -18,6 +18,7 @@ #include "platform/platform.h" #include "drivers/Ticker.h" +#include "platform/NonCopyable.h" #if defined (DEVICE_LOWPOWERTIMER) || defined(DOXYGEN_ONLY) @@ -31,7 +32,7 @@ namespace mbed { * @note Synchronization level: Interrupt safe * @ingroup drivers */ -class LowPowerTicker : public Ticker { +class LowPowerTicker : public Ticker, private NonCopyable { public: LowPowerTicker() : Ticker(get_lp_ticker_data()) { diff --git a/drivers/LowPowerTimeout.h b/drivers/LowPowerTimeout.h index c246ac1bc1..6aaefe2da8 100644 --- a/drivers/LowPowerTimeout.h +++ b/drivers/LowPowerTimeout.h @@ -22,6 +22,7 @@ #include "hal/lp_ticker_api.h" #include "drivers/LowPowerTicker.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -31,7 +32,7 @@ namespace mbed { * @note Synchronization level: Interrupt safe * @ingroup drivers */ -class LowPowerTimeout : public LowPowerTicker { +class LowPowerTimeout : public LowPowerTicker, private NonCopyable { private: virtual void handler(void) { diff --git a/drivers/LowPowerTimer.h b/drivers/LowPowerTimer.h index 4f1e15e2fb..e7e00377d9 100644 --- a/drivers/LowPowerTimer.h +++ b/drivers/LowPowerTimer.h @@ -18,6 +18,7 @@ #include "platform/platform.h" #include "drivers/Timer.h" +#include "platform/NonCopyable.h" #if defined (DEVICE_LOWPOWERTIMER) || defined(DOXYGEN_ONLY) @@ -31,7 +32,7 @@ namespace mbed { * @note Synchronization level: Interrupt safe * @ingroup drivers */ -class LowPowerTimer : public Timer { +class LowPowerTimer : public Timer, private NonCopyable { public: LowPowerTimer() : Timer(get_lp_ticker_data()) { diff --git a/drivers/RawSerial.h b/drivers/RawSerial.h index 2026e11170..b39fac73bf 100644 --- a/drivers/RawSerial.h +++ b/drivers/RawSerial.h @@ -22,6 +22,7 @@ #include "drivers/SerialBase.h" #include "hal/serial_api.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -49,7 +50,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class RawSerial: public SerialBase { +class RawSerial: public SerialBase, private NonCopyable { public: /** Create a RawSerial port, connected to the specified transmit and receive pins, with the specified baud. diff --git a/drivers/SPI.h b/drivers/SPI.h index ee177cecb2..46761f8585 100644 --- a/drivers/SPI.h +++ b/drivers/SPI.h @@ -23,6 +23,7 @@ #include "platform/PlatformMutex.h" #include "hal/spi_api.h" #include "platform/SingletonPtr.h" +#include "platform/NonCopyable.h" #if DEVICE_SPI_ASYNCH #include "platform/CThunk.h" @@ -72,7 +73,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class SPI { +class SPI : private NonCopyable { public: diff --git a/drivers/SPISlave.h b/drivers/SPISlave.h index 29a9046695..c6226b2c3d 100644 --- a/drivers/SPISlave.h +++ b/drivers/SPISlave.h @@ -17,6 +17,7 @@ #define MBED_SPISLAVE_H #include "platform/platform.h" +#include "platform/NonCopyable.h" #if defined (DEVICE_SPISLAVE) || defined(DOXYGEN_ONLY) @@ -52,7 +53,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class SPISlave { +class SPISlave : private NonCopyable { public: diff --git a/drivers/Serial.h b/drivers/Serial.h index 5294c1c2f3..549943a47c 100644 --- a/drivers/Serial.h +++ b/drivers/Serial.h @@ -24,6 +24,7 @@ #include "SerialBase.h" #include "PlatformMutex.h" #include "serial_api.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -49,7 +50,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class Serial : public SerialBase, public Stream { +class Serial : public SerialBase, public Stream, private NonCopyable { public: #if DEVICE_SERIAL_ASYNCH diff --git a/drivers/SerialBase.h b/drivers/SerialBase.h index 47a81a7aba..6723f6a2ee 100644 --- a/drivers/SerialBase.h +++ b/drivers/SerialBase.h @@ -23,6 +23,7 @@ #include "Callback.h" #include "serial_api.h" #include "mbed_toolchain.h" +#include "platform/NonCopyable.h" #if DEVICE_SERIAL_ASYNCH #include "CThunk.h" @@ -38,7 +39,7 @@ namespace mbed { * @note Synchronization level: Set by subclass * @ingroup drivers */ -class SerialBase { +class SerialBase : private NonCopyable { public: /** Set the baud rate of the serial port diff --git a/drivers/Ticker.h b/drivers/Ticker.h index 57695d16bd..917eb7c84b 100644 --- a/drivers/Ticker.h +++ b/drivers/Ticker.h @@ -19,6 +19,7 @@ #include "drivers/TimerEvent.h" #include "platform/Callback.h" #include "platform/mbed_toolchain.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -59,7 +60,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class Ticker : public TimerEvent { +class Ticker : public TimerEvent, private NonCopyable { public: Ticker() : TimerEvent() { diff --git a/drivers/Timeout.h b/drivers/Timeout.h index 54240bf783..b0508d7b88 100644 --- a/drivers/Timeout.h +++ b/drivers/Timeout.h @@ -17,6 +17,7 @@ #define MBED_TIMEOUT_H #include "drivers/Ticker.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -52,7 +53,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class Timeout : public Ticker { +class Timeout : public Ticker, private NonCopyable { protected: virtual void handler(); diff --git a/drivers/Timer.h b/drivers/Timer.h index d76ba281dc..50c47d387b 100644 --- a/drivers/Timer.h +++ b/drivers/Timer.h @@ -18,6 +18,7 @@ #include "platform/platform.h" #include "hal/ticker_api.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -46,7 +47,7 @@ namespace mbed { * @endcode * @ingroup drivers */ -class Timer { +class Timer : private NonCopyable { public: Timer(); diff --git a/drivers/TimerEvent.h b/drivers/TimerEvent.h index e330606363..45c3c14900 100644 --- a/drivers/TimerEvent.h +++ b/drivers/TimerEvent.h @@ -18,6 +18,7 @@ #include "hal/ticker_api.h" #include "hal/us_ticker_api.h" +#include "platform/NonCopyable.h" namespace mbed { /** \addtogroup drivers */ @@ -27,7 +28,7 @@ namespace mbed { * @note Synchronization level: Interrupt safe * @ingroup drivers */ -class TimerEvent { +class TimerEvent : private NonCopyable { public: TimerEvent(); TimerEvent(const ticker_data_t *data); diff --git a/drivers/UARTSerial.h b/drivers/UARTSerial.h index fda877d896..ee3a35ea1f 100644 --- a/drivers/UARTSerial.h +++ b/drivers/UARTSerial.h @@ -27,6 +27,7 @@ #include "PlatformMutex.h" #include "serial_api.h" #include "CircularBuffer.h" +#include "platform/NonCopyable.h" #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256 @@ -38,7 +39,7 @@ namespace mbed { -class UARTSerial : private SerialBase, public FileHandle { +class UARTSerial : private SerialBase, public FileHandle, private NonCopyable { public: