2017-06-20 11:38:26 +00:00
|
|
|
/* 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_
|
|
|
|
|
2017-11-14 10:09:07 +00:00
|
|
|
#if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0))
|
|
|
|
#include "mbed_toolchain.h"
|
|
|
|
#include "mbed_debug.h"
|
|
|
|
#endif
|
|
|
|
|
2017-11-14 10:06:38 +00:00
|
|
|
namespace mbed {
|
2017-06-20 11:38:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-23 13:34:43 +00:00
|
|
|
* Prevents generation of copy constructor and copy assignment operator in
|
|
|
|
* derived classes.
|
|
|
|
*
|
|
|
|
* @par Usage
|
|
|
|
*
|
|
|
|
* To prevent generation of copy constructor and copy assignment operator simply
|
|
|
|
* inherit privately from the NonCopyable class.
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* class Resource : NonCopyable<Resource> { };
|
|
|
|
*
|
|
|
|
* Resource r;
|
|
|
|
* // generates compile time error:
|
|
|
|
* Resource r2 = r;
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @par Background information
|
|
|
|
*
|
|
|
|
* Instances of polymorphic classes are not meant to be copied. Unfortunately,
|
|
|
|
* the C++ standards generates a default copy constructor and copy assignment
|
|
|
|
* function if these functions have not been defined in the class.
|
|
|
|
*
|
|
|
|
* Consider the following example:
|
|
|
|
*
|
2017-06-20 11:38:26 +00:00
|
|
|
* @code
|
2018-10-23 13:34:43 +00:00
|
|
|
* // base class representing a connection
|
|
|
|
* struct Connection {
|
|
|
|
* Connection();
|
|
|
|
* virtual ~Connection();
|
|
|
|
* virtual void open() = 0;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class SerialConnection : public Connection {
|
2017-11-14 10:06:38 +00:00
|
|
|
* public:
|
2018-10-23 13:34:43 +00:00
|
|
|
* SerialConnection(Serial*);
|
|
|
|
*
|
2017-06-20 11:38:26 +00:00
|
|
|
* private:
|
2018-10-23 13:34:43 +00:00
|
|
|
* Serial* _serial;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* Connection& get_connection() {
|
|
|
|
* static SerialConnection serial_connection;
|
|
|
|
* return serial_connection;
|
2017-06-20 11:38:26 +00:00
|
|
|
* }
|
2017-11-14 10:06:38 +00:00
|
|
|
*
|
2018-10-23 13:34:43 +00:00
|
|
|
* Connection connection = get_connection();
|
2017-06-20 11:38:26 +00:00
|
|
|
* @endcode
|
2018-10-23 13:34:43 +00:00
|
|
|
*
|
|
|
|
* There is a subtile bug in this code, the function get_connection returns a
|
|
|
|
* reference to a Connection which is captured by value instead of reference.
|
|
|
|
*
|
|
|
|
* When the reference returned by get_connection is copied into connection, the
|
|
|
|
* vtable and others members defined in Connection are copied but members defined
|
|
|
|
* in SerialConnection are left apart. This can cause severe crashes or bugs if
|
|
|
|
* the virtual functions captured uses members not present in the base
|
|
|
|
* declaration.
|
|
|
|
*
|
|
|
|
* To solve that problem, the copy constructor and assignment operator have to
|
|
|
|
* be declared (but doesn't need to be defined) in the private section of the
|
|
|
|
* Connection class:
|
|
|
|
*
|
2017-11-14 10:06:38 +00:00
|
|
|
* @code
|
2018-10-23 13:34:43 +00:00
|
|
|
* struct Connection {
|
2017-06-20 11:38:26 +00:00
|
|
|
* private:
|
2018-10-23 13:34:43 +00:00
|
|
|
* Connection(const Connection&);
|
|
|
|
* Connection& operator=(const Connection&);
|
2017-06-20 11:38:26 +00:00
|
|
|
* }
|
|
|
|
* @endcode
|
2018-10-23 13:34:43 +00:00
|
|
|
*
|
|
|
|
* While manually declaring private copy constructor and assignment functions
|
|
|
|
* works, it is not ideal as these declarations are usually not immediately
|
|
|
|
* visible, easy to forget and may be obscure for uninformed programmer.
|
|
|
|
*
|
|
|
|
* Using the NonCopyable class reduce the boilerplate required and express
|
|
|
|
* clearly the intent as class inheritance appears right after the class name
|
|
|
|
* declaration.
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* struct Connection : private NonCopyable<Connection> {
|
|
|
|
* // regular declarations
|
2017-06-20 11:38:26 +00:00
|
|
|
* }
|
2018-10-23 13:34:43 +00:00
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @par Implementation details
|
|
|
|
*
|
|
|
|
* Using a template type prevents cases where the empty base optimization cannot
|
|
|
|
* be applied and therefore ensure that the cost of the NonCopyable semantic
|
|
|
|
* sugar is null.
|
|
|
|
*
|
2017-11-14 10:06:38 +00:00
|
|
|
* 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
|
2017-06-20 11:38:26 +00:00
|
|
|
* struct A { };
|
2017-11-14 10:06:38 +00:00
|
|
|
* struct B : A {
|
2017-06-20 11:38:26 +00:00
|
|
|
* int foo;
|
|
|
|
* };
|
|
|
|
* // thanks to empty base optimization, sizeof(B) == sizeof(int)
|
2017-11-14 10:06:38 +00:00
|
|
|
*
|
|
|
|
* struct C : A {
|
2017-06-20 11:38:26 +00:00
|
|
|
* B b;
|
|
|
|
* };
|
2017-11-14 10:06:38 +00:00
|
|
|
*
|
2017-06-20 11:38:26 +00:00
|
|
|
* // empty base optimization cannot be applied here because A from C and A from
|
2018-01-16 21:08:14 +00:00
|
|
|
* // B shall have a different address. In that case, with the alignment
|
2017-06-20 11:38:26 +00:00
|
|
|
* // sizeof(C) == 2* sizeof(int)
|
|
|
|
* @endcode
|
2017-11-14 10:06:38 +00:00
|
|
|
*
|
|
|
|
* The solution to that problem is to templatize the empty class to makes it
|
|
|
|
* unique to the type it is applied to:
|
|
|
|
*
|
|
|
|
* @code
|
2017-06-20 11:38:26 +00:00
|
|
|
* template<typename T>
|
|
|
|
* struct A<T> { };
|
2017-11-14 10:06:38 +00:00
|
|
|
* struct B : A<B> {
|
2017-06-20 11:38:26 +00:00
|
|
|
* int foo;
|
|
|
|
* };
|
2017-11-14 10:06:38 +00:00
|
|
|
* struct C : A<C> {
|
2017-06-20 11:38:26 +00:00
|
|
|
* B b;
|
|
|
|
* };
|
2017-11-14 10:06:38 +00:00
|
|
|
*
|
|
|
|
* // empty base optimization can be applied B and C does not refer to the same
|
2017-06-20 11:38:26 +00:00
|
|
|
* // kind of A. sizeof(C) == sizeof(B) == sizeof(int).
|
|
|
|
* @endcode
|
2017-11-14 10:09:07 +00:00
|
|
|
*
|
2018-10-23 13:34:43 +00:00
|
|
|
* @tparam T The type that should be made non copyable.
|
|
|
|
*
|
2017-11-14 10:09:07 +00:00
|
|
|
* @note Compile time errors are disabled if the develop or the release profile
|
|
|
|
* is used. To override this behavior and force compile time errors in all profile
|
|
|
|
* set the configuration parameter "platform.force-non-copyable-error" to true.
|
2017-06-20 11:38:26 +00:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2017-11-14 10:06:38 +00:00
|
|
|
class NonCopyable {
|
2018-10-23 11:11:02 +00:00
|
|
|
#ifndef DOXYGEN_ONLY
|
2017-06-20 11:38:26 +00:00
|
|
|
protected:
|
2017-11-14 10:06:38 +00:00
|
|
|
/**
|
2018-01-16 21:08:14 +00:00
|
|
|
* Disallow construction of NonCopyable objects from outside of its hierarchy.
|
2017-06-20 11:38:26 +00:00
|
|
|
*/
|
|
|
|
NonCopyable() { }
|
2017-11-14 10:06:38 +00:00
|
|
|
/**
|
2018-01-16 21:08:14 +00:00
|
|
|
* Disallow destruction of NonCopyable objects from outside of its hierarchy.
|
2017-06-20 11:38:26 +00:00
|
|
|
*/
|
|
|
|
~NonCopyable() { }
|
|
|
|
|
2017-11-14 10:09:07 +00:00
|
|
|
#if (!defined(MBED_DEBUG) && (MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR == 0))
|
|
|
|
/**
|
|
|
|
* NonCopyable copy constructor.
|
|
|
|
*
|
|
|
|
* A compile time warning is issued when this function is used and a runtime
|
|
|
|
* warning is printed when the copy construction of the non copyable happens.
|
|
|
|
*
|
|
|
|
* If you see this warning, your code is probably doing something unspecified.
|
|
|
|
* Copy of non copyable resources can lead to resource leak and random error.
|
|
|
|
*/
|
|
|
|
MBED_DEPRECATED("Invalid copy construction of a NonCopyable resource.")
|
2018-06-27 14:09:15 +00:00
|
|
|
NonCopyable(const NonCopyable &)
|
2017-11-14 10:09:07 +00:00
|
|
|
{
|
|
|
|
debug("Invalid copy construction of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NonCopyable copy assignment operator.
|
|
|
|
*
|
|
|
|
* A compile time warning is issued when this function is used and a runtime
|
|
|
|
* warning is printed when the copy construction of the non copyable happens.
|
|
|
|
*
|
|
|
|
* If you see this warning, your code is probably doing something unspecified.
|
|
|
|
* Copy of non copyable resources can lead to resource leak and random error.
|
|
|
|
*/
|
|
|
|
MBED_DEPRECATED("Invalid copy assignment of a NonCopyable resource.")
|
2018-06-27 14:09:15 +00:00
|
|
|
NonCopyable &operator=(const NonCopyable &)
|
2017-11-14 10:09:07 +00:00
|
|
|
{
|
|
|
|
debug("Invalid copy assignment of a NonCopyable resource: %s\r\n", MBED_PRETTY_FUNCTION);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2017-11-14 10:06:38 +00:00
|
|
|
private:
|
2017-06-20 11:38:26 +00:00
|
|
|
/**
|
2017-11-14 10:06:38 +00:00
|
|
|
* Declare copy constructor as private, any attempt to copy construct
|
2017-06-20 11:38:26 +00:00
|
|
|
* a NonCopyable will fail at compile time.
|
|
|
|
*/
|
2018-06-27 14:09:15 +00:00
|
|
|
NonCopyable(const NonCopyable &);
|
2017-06-20 11:38:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-16 21:08:14 +00:00
|
|
|
* Declare copy assignment operator as private, any attempt to copy assign
|
2017-06-20 11:38:26 +00:00
|
|
|
* a NonCopyable will fail at compile time.
|
|
|
|
*/
|
2018-06-27 14:09:15 +00:00
|
|
|
NonCopyable &operator=(const NonCopyable &);
|
2017-11-14 10:09:07 +00:00
|
|
|
#endif
|
2018-10-23 11:11:02 +00:00
|
|
|
#endif
|
2017-06-20 11:38:26 +00:00
|
|
|
};
|
|
|
|
|
2017-11-14 10:06:38 +00:00
|
|
|
} // namespace mbed
|
2017-06-20 11:38:26 +00:00
|
|
|
|
|
|
|
#endif /* MBED_NONCOPYABLE_H_ */
|