diff --git a/src/core/Makefile.am b/src/core/Makefile.am index b7733746a..5ef1df610 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -331,6 +331,7 @@ HEADERS_COMMON = \ common/logging.hpp \ common/message.hpp \ common/new.hpp \ + common/non_copyable.hpp \ common/notifier.hpp \ common/random.hpp \ common/random_manager.hpp \ diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index a230aa271..834cb3e69 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -38,6 +38,7 @@ #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/message.hpp" +#include "common/non_copyable.hpp" #include "common/timer.hpp" #include "net/ip6.hpp" #include "net/netif.hpp" @@ -268,7 +269,7 @@ private: * This class implements the CoAP client and server. * */ -class CoapBase : public InstanceLocator +class CoapBase : public InstanceLocator, private NonCopyable { friend class ResponsesQueue; diff --git a/src/core/common/extension.hpp b/src/core/common/extension.hpp index 2a5a543ca..01fde91c3 100644 --- a/src/core/common/extension.hpp +++ b/src/core/common/extension.hpp @@ -37,6 +37,7 @@ #include "openthread-core-config.h" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #if OPENTHREAD_ENABLE_VENDOR_EXTENSION @@ -65,7 +66,7 @@ namespace Extension { * Support for vendor extension can be enabled using `OPENTHREAD_ENABLE_VENDOR_EXTENSION` configuration option. * */ -class ExtensionBase : public InstanceLocator +class ExtensionBase : public InstanceLocator, private NonCopyable { public: /** diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 124964c9d..8642f81f4 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -46,6 +46,7 @@ #include #endif +#include "common/non_copyable.hpp" #include "common/random_manager.hpp" #include "common/tasklet.hpp" #include "common/timer.hpp" @@ -119,7 +120,7 @@ namespace ot { * This class contains all the components used by OpenThread. * */ -class Instance : public otInstance +class Instance : public otInstance, private NonCopyable { public: #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 9fe5651ca..611e860e7 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -44,6 +44,7 @@ #include "common/code_utils.hpp" #include "common/encoding.hpp" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "mac/mac_types.hpp" #include "thread/link_quality.hpp" @@ -1086,7 +1087,7 @@ private: * This class represents a message pool * */ -class MessagePool : public InstanceLocator +class MessagePool : public InstanceLocator, private NonCopyable { friend class Message; friend class MessageQueue; diff --git a/src/core/common/non_copyable.hpp b/src/core/common/non_copyable.hpp new file mode 100644 index 000000000..e57afe4c0 --- /dev/null +++ b/src/core/common/non_copyable.hpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes a common base class for disabling copying. + */ + +#ifndef NON_COPYABLE_HPP_ +#define NON_COPYABLE_HPP_ + +namespace ot { + +/** + * This class makes any class that derives from it non-copyable. It is + * intended to be used as a private base class. + */ +class NonCopyable +{ +protected: + NonCopyable(void) {} + ~NonCopyable(void) {} + +private: + NonCopyable(const NonCopyable &); + NonCopyable &operator=(const NonCopyable &); +}; + +} // namespace ot + +#endif // NON_COPYABLE_HPP_ diff --git a/src/core/common/notifier.hpp b/src/core/common/notifier.hpp index 65e98fda9..6bbc6599a 100644 --- a/src/core/common/notifier.hpp +++ b/src/core/common/notifier.hpp @@ -44,6 +44,7 @@ #include "common/linked_list.hpp" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/tasklet.hpp" namespace ot { @@ -73,7 +74,7 @@ namespace ot { * registered at the same time is specified by `OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS` configuration parameter. * */ -class Notifier : public InstanceLocator +class Notifier : public InstanceLocator, private NonCopyable { public: /** diff --git a/src/core/common/random_manager.hpp b/src/core/common/random_manager.hpp index ea11bdb4c..7106033a0 100644 --- a/src/core/common/random_manager.hpp +++ b/src/core/common/random_manager.hpp @@ -44,6 +44,8 @@ #include #endif +#include "common/non_copyable.hpp" + #if (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || defined(MBEDTLS_HAVEGE_C) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT))) #define OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT @@ -55,7 +57,7 @@ namespace ot { * This class manages random number generator initialization/deinitialization. * */ -class RandomManager +class RandomManager : private NonCopyable { public: /** diff --git a/src/core/common/settings.hpp b/src/core/common/settings.hpp index 573b8b511..5f7c1bd29 100644 --- a/src/core/common/settings.hpp +++ b/src/core/common/settings.hpp @@ -38,6 +38,7 @@ #include "common/encoding.hpp" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "mac/mac_types.hpp" #include "utils/flash.hpp" #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE @@ -50,7 +51,7 @@ namespace MeshCoP { class Dataset; } -class SettingsDriver : public InstanceLocator +class SettingsDriver : public InstanceLocator, private NonCopyable { public: /** @@ -151,7 +152,7 @@ private: * This class provides structure definitions for different settings keys. * */ -class SettingsBase : public InstanceLocator +class SettingsBase : public InstanceLocator, private NonCopyable { public: /** diff --git a/src/core/common/tasklet.hpp b/src/core/common/tasklet.hpp index 34e743983..cc5f666c2 100644 --- a/src/core/common/tasklet.hpp +++ b/src/core/common/tasklet.hpp @@ -41,6 +41,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" namespace ot { @@ -149,7 +150,7 @@ private: * This class implements the tasklet scheduler. * */ -class TaskletScheduler +class TaskletScheduler : private NonCopyable { friend class Tasklet; diff --git a/src/core/common/timer.hpp b/src/core/common/timer.hpp index 1b899a98d..8e71999a7 100644 --- a/src/core/common/timer.hpp +++ b/src/core/common/timer.hpp @@ -45,6 +45,7 @@ #include "common/debug.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/tasklet.hpp" #include "common/time.hpp" @@ -251,7 +252,7 @@ private: * This class implements the base timer scheduler. * */ -class TimerScheduler : public InstanceLocator +class TimerScheduler : public InstanceLocator, private NonCopyable { friend class Timer; diff --git a/src/core/crypto/mbedtls.hpp b/src/core/crypto/mbedtls.hpp index d4386b5b7..697e7b96c 100644 --- a/src/core/crypto/mbedtls.hpp +++ b/src/core/crypto/mbedtls.hpp @@ -38,6 +38,8 @@ #include +#include "common/non_copyable.hpp" + namespace ot { namespace Crypto { @@ -52,7 +54,7 @@ namespace Crypto { * This class implements mbedTLS memory. * */ -class MbedTls +class MbedTls : private NonCopyable { public: /** diff --git a/src/core/diags/factory_diags.hpp b/src/core/diags/factory_diags.hpp index 45a1b83e7..cc0352d7e 100644 --- a/src/core/diags/factory_diags.hpp +++ b/src/core/diags/factory_diags.hpp @@ -41,13 +41,14 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" namespace ot { namespace FactoryDiags { #if OPENTHREAD_CONFIG_DIAG_ENABLE -class Diags : public InstanceLocator +class Diags : public InstanceLocator, private NonCopyable { public: /** diff --git a/src/core/mac/link_raw.hpp b/src/core/mac/link_raw.hpp index 003893a14..4cfd99c6a 100644 --- a/src/core/mac/link_raw.hpp +++ b/src/core/mac/link_raw.hpp @@ -39,6 +39,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "mac/mac_frame.hpp" #include "mac/sub_mac.hpp" @@ -51,7 +52,7 @@ namespace Mac { * This class defines the raw link-layer object. * */ -class LinkRaw : public InstanceLocator +class LinkRaw : public InstanceLocator, private NonCopyable { friend class ot::Instance; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 701ece102..66cda0039 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -44,6 +44,7 @@ #include "common/encoding.hpp" #include "common/locator.hpp" #include "common/message.hpp" +#include "common/non_copyable.hpp" #include "common/timer.hpp" #include "net/icmp6.hpp" #include "net/ip6_address.hpp" @@ -98,7 +99,7 @@ using ot::Encoding::BigEndian::HostSwap32; * This class implements the core IPv6 message processing. * */ -class Ip6 : public InstanceLocator +class Ip6 : public InstanceLocator, private NonCopyable { friend class ot::Instance; diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp index 84b857b5f..35710c166 100644 --- a/src/core/net/netif.hpp +++ b/src/core/net/netif.hpp @@ -39,6 +39,7 @@ #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/message.hpp" +#include "common/non_copyable.hpp" #include "common/tasklet.hpp" #include "mac/mac_types.hpp" #include "net/ip6_address.hpp" @@ -169,7 +170,7 @@ private: * This class implements an IPv6 network interface. * */ -class Netif : public InstanceLocator, public LinkedListEntry +class Netif : public InstanceLocator, public LinkedListEntry, private NonCopyable { friend class Ip6; friend class Address; diff --git a/src/core/radio/radio.hpp b/src/core/radio/radio.hpp index 2fae17da7..3d18e7a13 100644 --- a/src/core/radio/radio.hpp +++ b/src/core/radio/radio.hpp @@ -39,6 +39,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "mac/mac_frame.hpp" #include "utils/static_assert.hpp" @@ -58,7 +59,7 @@ namespace ot { * This class represents an OpenThread radio abstraction. * */ -class Radio : public InstanceLocator +class Radio : public InstanceLocator, private NonCopyable { friend class Instance; diff --git a/src/core/thread/announce_sender.hpp b/src/core/thread/announce_sender.hpp index 419b327d8..42c66d152 100644 --- a/src/core/thread/announce_sender.hpp +++ b/src/core/thread/announce_sender.hpp @@ -37,6 +37,7 @@ #include "openthread-core-config.h" #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/notifier.hpp" #include "common/timer.hpp" #include "mac/mac.hpp" @@ -49,7 +50,7 @@ namespace ot { * This class provides APIs to schedule periodic transmission of MLE Announcement messages for a given number * transmissions per channel. */ -class AnnounceSenderBase : public InstanceLocator +class AnnounceSenderBase : public InstanceLocator, private NonCopyable { protected: /** diff --git a/src/core/utils/channel_manager.hpp b/src/core/utils/channel_manager.hpp index 534d485ef..18e8f9e66 100644 --- a/src/core/utils/channel_manager.hpp +++ b/src/core/utils/channel_manager.hpp @@ -39,6 +39,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/notifier.hpp" #include "common/timer.hpp" #include "mac/mac.hpp" @@ -63,7 +64,7 @@ namespace Utils { * This class implements the Channel Manager. * */ -class ChannelManager : public InstanceLocator +class ChannelManager : public InstanceLocator, private NonCopyable { public: enum @@ -295,7 +296,7 @@ private: #else // OPENTHREAD_FTD -class ChannelManager +class ChannelManager : private NonCopyable { public: explicit ChannelManager(Instance &) {} diff --git a/src/core/utils/channel_monitor.hpp b/src/core/utils/channel_monitor.hpp index 750c2d415..26899beb2 100644 --- a/src/core/utils/channel_monitor.hpp +++ b/src/core/utils/channel_monitor.hpp @@ -39,6 +39,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/timer.hpp" #include "mac/mac.hpp" #include "radio/radio.hpp" @@ -70,7 +71,7 @@ namespace Utils { * window (referred to as "channel occupancy"). * */ -class ChannelMonitor : public InstanceLocator +class ChannelMonitor : public InstanceLocator, private NonCopyable { public: enum diff --git a/src/core/utils/heap.hpp b/src/core/utils/heap.hpp index 13d58ada1..99bbdad3c 100644 --- a/src/core/utils/heap.hpp +++ b/src/core/utils/heap.hpp @@ -40,6 +40,7 @@ #include #include +#include "common/non_copyable.hpp" #include "utils/static_assert.hpp" namespace ot { @@ -175,7 +176,7 @@ private: * +--------------------------------------------------------------------------+ * */ -class Heap +class Heap : private NonCopyable { public: /** diff --git a/src/core/utils/otns.hpp b/src/core/utils/otns.hpp index bd93b546f..ac23fedff 100644 --- a/src/core/utils/otns.hpp +++ b/src/core/utils/otns.hpp @@ -43,6 +43,7 @@ #include #include "common/locator.hpp" +#include "common/non_copyable.hpp" #include "common/notifier.hpp" #include "mac/mac_types.hpp" #include "net/ip6_address.hpp" @@ -55,7 +56,7 @@ namespace Utils { * This class implements the OTNS Stub that interacts with OTNS. * */ -class Otns : public InstanceLocator +class Otns : public InstanceLocator, private NonCopyable { public: /**