mirror of
https://github.com/espressif/openthread.git
synced 2026-08-09 04:07:47 +00:00
[core] add Clearable class providing simple Clear() method (#5029)
This commit adds new class `Clearable` (inherited by other types) providing simple implementation of `Clear()` which sets all the instance bytes to zero (using `memset()`).
This commit is contained in:
committed by
Jonathan Hui
parent
71575ff76a
commit
1ce2a07afd
@@ -319,6 +319,7 @@ HEADERS_COMMON = \
|
||||
coap/coap.hpp \
|
||||
coap/coap_message.hpp \
|
||||
coap/coap_secure.hpp \
|
||||
common/clearable.hpp \
|
||||
common/code_utils.hpp \
|
||||
common/crc16.hpp \
|
||||
common/debug.hpp \
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
#include <openthread/coap.h>
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/code_utils.hpp"
|
||||
#include "common/encoding.hpp"
|
||||
#include "common/message.hpp"
|
||||
@@ -628,10 +629,8 @@ private:
|
||||
* This structure represents a HelpData used by this CoAP message.
|
||||
*
|
||||
*/
|
||||
struct HelpData
|
||||
struct HelpData : public Clearable<HelpData>
|
||||
{
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
Header mHeader;
|
||||
uint16_t mOptionLast;
|
||||
uint16_t mHeaderOffset; ///< The byte offset for the CoAP Header
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 definitions for Clearable class for OpenThread objects.
|
||||
*/
|
||||
|
||||
#ifndef CLEARABLE_HPP_
|
||||
#define CLEARABLE_HPP_
|
||||
|
||||
#include "openthread-core-config.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace ot {
|
||||
|
||||
/**
|
||||
* This template class defines a Clearable object which provides `Clear()` method.
|
||||
*
|
||||
* The `Clear` implementation simply sets all the bytes of a `Type` instance to zero.
|
||||
*
|
||||
* Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit
|
||||
* from `Clearable<Type>`.
|
||||
*
|
||||
*/
|
||||
template <typename Type> class Clearable
|
||||
{
|
||||
public:
|
||||
void Clear(void) { memset(reinterpret_cast<void *>(this), 0, sizeof(Type)); }
|
||||
};
|
||||
|
||||
} // namespace ot
|
||||
|
||||
#endif // CLEARABLE_HPP_
|
||||
@@ -42,6 +42,7 @@
|
||||
#include <openthread/link.h>
|
||||
#include <openthread/thread.h>
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/equatable.hpp"
|
||||
#include "common/string.hpp"
|
||||
|
||||
@@ -87,7 +88,7 @@ PanId GenerateRandomPanId(void);
|
||||
*
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class ExtAddress : public otExtAddress, public Equatable<ExtAddress>
|
||||
class ExtAddress : public otExtAddress, public Equatable<ExtAddress>, public Clearable<ExtAddress>
|
||||
{
|
||||
public:
|
||||
enum
|
||||
@@ -111,12 +112,6 @@ public:
|
||||
kReverseByteOrder, // Copy address bytes in reverse byte order.
|
||||
};
|
||||
|
||||
/**
|
||||
* This method clears the Extended Address (sets all bytes to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { Fill(0); }
|
||||
|
||||
/**
|
||||
* This method fills all bytes of address with a given byte value.
|
||||
*
|
||||
@@ -426,7 +421,7 @@ private:
|
||||
*
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class Key : public otMacKey, public Equatable<Key>
|
||||
class Key : public otMacKey, public Equatable<Key>, public Clearable<Key>
|
||||
{
|
||||
public:
|
||||
enum
|
||||
@@ -434,12 +429,6 @@ public:
|
||||
kSize = OT_MAC_KEY_SIZE, // Key size in bytes.
|
||||
};
|
||||
|
||||
/**
|
||||
* This method clears the key (set all bytes to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(m8, 0, kSize); }
|
||||
|
||||
/**
|
||||
* This method gets a pointer to the buffer containing the key.
|
||||
*
|
||||
@@ -455,7 +444,7 @@ public:
|
||||
*
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class ExtendedPanId : public otExtendedPanId, public Equatable<ExtendedPanId>
|
||||
class ExtendedPanId : public otExtendedPanId, public Equatable<ExtendedPanId>, public Clearable<ExtendedPanId>
|
||||
{
|
||||
public:
|
||||
enum
|
||||
@@ -469,12 +458,6 @@ public:
|
||||
*/
|
||||
typedef String<kInfoStringSize> InfoString;
|
||||
|
||||
/**
|
||||
* This method clears the Extended PAN Identifier (sets all bytes to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
/**
|
||||
* This method converts an address to a string.
|
||||
*
|
||||
|
||||
@@ -45,11 +45,6 @@ using ot::Encoding::BigEndian::HostSwap32;
|
||||
namespace ot {
|
||||
namespace Ip6 {
|
||||
|
||||
void Address::Clear(void)
|
||||
{
|
||||
memset(mFields.m8, 0, sizeof(mFields));
|
||||
}
|
||||
|
||||
bool Address::IsUnspecified(void) const
|
||||
{
|
||||
return (mFields.m32[0] == 0 && mFields.m32[1] == 0 && mFields.m32[2] == 0 && mFields.m32[3] == 0);
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/encoding.hpp"
|
||||
#include "common/equatable.hpp"
|
||||
#include "common/string.hpp"
|
||||
@@ -61,7 +62,7 @@ namespace Ip6 {
|
||||
*
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class Address : public otIp6Address, public Equatable<Address>
|
||||
class Address : public otIp6Address, public Equatable<Address>, public Clearable<Address>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
@@ -105,12 +106,6 @@ public:
|
||||
*/
|
||||
typedef String<kIp6AddressStringSize> InfoString;
|
||||
|
||||
/**
|
||||
* This method clears the IPv6 address by setting it to the Unspecified Address "::".
|
||||
*
|
||||
*/
|
||||
void Clear(void);
|
||||
|
||||
/**
|
||||
* This method indicates whether or not the IPv6 address is the Unspecified Address.
|
||||
*
|
||||
|
||||
+7
-14
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "openthread-core-config.h"
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/linked_list.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/message.hpp"
|
||||
@@ -64,17 +65,13 @@ class Ip6;
|
||||
* This class implements an IPv6 network interface unicast address.
|
||||
*
|
||||
*/
|
||||
class NetifUnicastAddress : public otNetifAddress, public LinkedListEntry<NetifUnicastAddress>
|
||||
class NetifUnicastAddress : public otNetifAddress,
|
||||
public LinkedListEntry<NetifUnicastAddress>,
|
||||
public Clearable<NetifUnicastAddress>
|
||||
{
|
||||
friend class Netif;
|
||||
|
||||
public:
|
||||
/**
|
||||
* This method clears the object (setting all fields to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
/**
|
||||
* This method returns the unicast address.
|
||||
*
|
||||
@@ -113,17 +110,13 @@ private:
|
||||
* This class implements an IPv6 network interface multicast address.
|
||||
*
|
||||
*/
|
||||
class NetifMulticastAddress : public otNetifMulticastAddress, public LinkedListEntry<NetifMulticastAddress>
|
||||
class NetifMulticastAddress : public otNetifMulticastAddress,
|
||||
public LinkedListEntry<NetifMulticastAddress>,
|
||||
public Clearable<NetifMulticastAddress>
|
||||
{
|
||||
friend class Netif;
|
||||
|
||||
public:
|
||||
/**
|
||||
* This method clears the object (setting all fields to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
/**
|
||||
* This method returns the multicast address.
|
||||
*
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "openthread-core-config.h"
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "net/ip6_address.hpp"
|
||||
|
||||
namespace ot {
|
||||
@@ -204,7 +205,7 @@ public:
|
||||
* This class implements a socket address.
|
||||
*
|
||||
*/
|
||||
class SockAddr : public otSockAddr
|
||||
class SockAddr : public otSockAddr, public Clearable<SockAddr>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
@@ -213,12 +214,6 @@ public:
|
||||
*/
|
||||
SockAddr(void) { Clear(); }
|
||||
|
||||
/**
|
||||
* This method clears the object (sets all fields to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
/**
|
||||
* This method returns a reference to the IPv6 address.
|
||||
*
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
|
||||
#include <openthread/dataset.h>
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/equatable.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/random.hpp"
|
||||
@@ -73,15 +74,9 @@ class MasterKey : public otMasterKey, public Equatable<MasterKey>
|
||||
*
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class Pskc : public otPskc, public Equatable<Pskc>
|
||||
class Pskc : public otPskc, public Equatable<Pskc>, public Clearable<Pskc>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This method clears the PSKc (sets all bytes to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
#if !OPENTHREAD_RADIO
|
||||
/**
|
||||
* This method generates a cryptographically secure random sequence to populate the Thread PSKc.
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
|
||||
#include <openthread/thread.h>
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/encoding.hpp"
|
||||
#include "common/equatable.hpp"
|
||||
#include "common/string.hpp"
|
||||
@@ -448,15 +449,9 @@ public:
|
||||
* This class represents the Thread Leader Data.
|
||||
*
|
||||
*/
|
||||
class LeaderData : public otLeaderData
|
||||
class LeaderData : public otLeaderData, public Clearable<LeaderData>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This method clears the Leader Data (setting all the fields to zero).
|
||||
*
|
||||
*/
|
||||
void Clear(void) { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
/**
|
||||
* This method returns the Partition ID value.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user