Skip to content

Commit

Permalink
[support] Templatize Counter class (#17883)
Browse files Browse the repository at this point in the history
* [support] Templatize Counter class

* Fix build failure

* Address comments

* Address comments
  • Loading branch information
erjiaqing authored and pull[bot] committed Dec 18, 2023
1 parent eba3d55 commit 1576249
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 311 deletions.
6 changes: 3 additions & 3 deletions src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct CopyAndAdjustDeltaTimeContext

void EventManagement::Init(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers,
CircularEventBuffer * apCircularEventBuffer, const LogStorageResources * const apLogStorageResources,
MonotonicallyIncreasingCounter * apEventNumberCounter)
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter)
{
CircularEventBuffer * current = nullptr;
CircularEventBuffer * prev = nullptr;
Expand Down Expand Up @@ -335,7 +335,7 @@ CHIP_ERROR EventManagement::ConstructEvent(EventLoadOutContext * apContext, Even
void EventManagement::CreateEventManagement(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers,
CircularEventBuffer * apCircularEventBuffer,
const LogStorageResources * const apLogStorageResources,
MonotonicallyIncreasingCounter * apEventNumberCounter)
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter)
{

sInstance.Init(apExchangeManager, aNumBuffers, apCircularEventBuffer, apLogStorageResources, apEventNumberCounter);
Expand Down Expand Up @@ -401,7 +401,7 @@ void EventManagement::VendEventNumber()
}

// Assign event Number to the buffer's counter's value.
mLastEventNumber = static_cast<EventNumber>(mpEventNumberCounter->GetValue());
mLastEventNumber = mpEventNumberCounter->GetValue();
}

CHIP_ERROR EventManagement::LogEvent(EventLoggingDelegate * apDelegate, const EventOptions & aEventOptions,
Expand Down
7 changes: 4 additions & 3 deletions src/app/EventManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class EventManagement
*
*/
void Init(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers, CircularEventBuffer * apCircularEventBuffer,
const LogStorageResources * const apLogStorageResources, MonotonicallyIncreasingCounter * apEventNumberCounter);
const LogStorageResources * const apLogStorageResources,
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter);

static EventManagement & GetInstance();

Expand Down Expand Up @@ -224,7 +225,7 @@ class EventManagement
static void CreateEventManagement(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers,
CircularEventBuffer * apCircularEventBuffer,
const LogStorageResources * const apLogStorageResources,
MonotonicallyIncreasingCounter * apEventNumberCounter);
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter);

static void DestroyEventManagement();

Expand Down Expand Up @@ -512,7 +513,7 @@ class EventManagement
#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING

// The counter we're going to use for event numbers.
MonotonicallyIncreasingCounter * mpEventNumberCounter = nullptr;
MonotonicallyIncreasingCounter<EventNumber> * mpEventNumberCounter = nullptr;

EventNumber mLastEventNumber = 0; ///< Last event Number vended
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Server Server::sServer;
static uint8_t sInfoEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE];
static uint8_t sDebugEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE];
static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE];
static ::chip::PersistedCounter sGlobalEventIdCounter;
static ::chip::PersistedCounter<chip::EventNumber> sGlobalEventIdCounter;
static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS];
#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT

Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/TestEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class TestContext : public chip::Test::AppContext
}

private:
chip::MonotonicallyIncreasingCounter mEventCounter;
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
};

void ENFORCE_FORMAT(1, 2) SimpleDumpWriter(const char * aFormat, ...)
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/TestEventOverflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class TestContext : public chip::Test::AppContext
}

private:
chip::MonotonicallyIncreasingCounter mEventCounter;
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
};

class TestEventGenerator : public chip::app::EventLoggingDelegate
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TestContext : public chip::Test::AppContext
}

private:
chip::MonotonicallyIncreasingCounter mEventCounter;
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
};

TestContext sContext;
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ uint8_t gInfoEventBuffer[2048];
uint8_t gCritEventBuffer[2048];
chip::app::CircularEventBuffer gCircularEventBuffer[3];

chip::MonotonicallyIncreasingCounter gEventCounter;
chip::MonotonicallyIncreasingCounter<chip::EventNumber> gEventCounter;

CHIP_ERROR InitializeEventLogging(chip::Messaging::ExchangeManager * apMgr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/controller/tests/TestEventCaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TestContext : public chip::Test::AppContext
}

private:
MonotonicallyIncreasingCounter mEventCounter;
MonotonicallyIncreasingCounter<EventNumber> mEventCounter;
};

nlTestSuite * gSuite = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/controller/tests/TestEventChunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TestContext : public chip::Test::AppContext
}

private:
MonotonicallyIncreasingCounter mEventCounter;
MonotonicallyIncreasingCounter<EventNumber> mEventCounter;
};

uint32_t gIterationCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class GenericConfigurationManagerImpl : public ConfigurationManager

protected:
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
chip::LifetimePersistedCounter mLifetimePersistedCounter;
chip::LifetimePersistedCounter<uint32_t> mLifetimePersistedCounter;
#endif

#if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
Expand Down
27 changes: 27 additions & 0 deletions src/lib/core/CHIPEncoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ inline void Write8(uint8_t *& p, uint8_t v)
*/
namespace LittleEndian {

/**
* This conditionally performs, as necessary for the target system, a
* byte order swap by value of the specified value, presumed to be in
* little endian byte ordering to the target system (i.e. host)
* byte ordering.
*/
template <typename T>
inline T HostSwap(T v);

/**
* This conditionally performs, as necessary for the target system, a
* byte order swap by value of the specified 16-bit value, presumed to
Expand All @@ -225,6 +234,12 @@ inline uint16_t HostSwap16(uint16_t v)
return nl::ByteOrder::Swap16LittleToHost(v);
}

template <>
inline uint16_t HostSwap<uint16_t>(uint16_t v)
{
return HostSwap16(v);
}

/**
* This conditionally performs, as necessary for the target system, a
* byte order swap by value of the specified 32-bit value, presumed to
Expand All @@ -243,6 +258,12 @@ inline uint32_t HostSwap32(uint32_t v)
return nl::ByteOrder::Swap32LittleToHost(v);
}

template <>
inline uint32_t HostSwap<uint32_t>(uint32_t v)
{
return HostSwap32(v);
}

/**
* This conditionally performs, as necessary for the target system, a
* byte order swap by value of the specified 64-bit value, presumed to
Expand All @@ -261,6 +282,12 @@ inline uint64_t HostSwap64(uint64_t v)
return nl::ByteOrder::Swap64LittleToHost(v);
}

template <>
inline uint64_t HostSwap<uint64_t>(uint64_t v)
{
return HostSwap64(v);
}

/**
* Perform a, potentially unaligned, memory read of the little endian
* byte ordered 16-bit value from the specified pointer address,
Expand Down
3 changes: 0 additions & 3 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static_library("support") {
"BytesToHex.cpp",
"BytesToHex.h",
"CHIPArgParser.cpp",
"CHIPCounter.cpp",
"CHIPCounter.h",
"CHIPMem.cpp",
"CHIPMem.h",
Expand All @@ -87,10 +86,8 @@ static_library("support") {
"FixedBufferAllocator.cpp",
"FixedBufferAllocator.h",
"Iterators.h",
"LifetimePersistedCounter.cpp",
"LifetimePersistedCounter.h",
"ObjectLifeCycle.h",
"PersistedCounter.cpp",
"PersistedCounter.h",
"PersistentStorageMacros.h",
"Pool.cpp",
Expand Down
53 changes: 0 additions & 53 deletions src/lib/support/CHIPCounter.cpp

This file was deleted.

32 changes: 24 additions & 8 deletions src/lib/support/CHIPCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace chip {
* An interface for managing a counter as an integer value.
*/

template <typename T>
class Counter
{
public:
Expand All @@ -57,7 +58,7 @@ class Counter
*
* @return The current value of the counter.
*/
virtual uint32_t GetValue() = 0;
virtual T GetValue() = 0;
};

/**
Expand All @@ -67,11 +68,12 @@ class Counter
* A class for managing a monotonically-increasing counter as an integer value.
*/

class MonotonicallyIncreasingCounter : public Counter
template <typename T>
class MonotonicallyIncreasingCounter : public Counter<T>
{
public:
MonotonicallyIncreasingCounter();
~MonotonicallyIncreasingCounter() override;
MonotonicallyIncreasingCounter() : mCounterValue(0) {}
~MonotonicallyIncreasingCounter() override{};

/**
* @brief
Expand All @@ -81,26 +83,40 @@ class MonotonicallyIncreasingCounter : public Counter
*
* @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
*/
CHIP_ERROR Init(uint32_t aStartValue);
CHIP_ERROR Init(T aStartValue)
{
CHIP_ERROR err = CHIP_NO_ERROR;

mCounterValue = aStartValue;

return err;
}

/**
* @brief
* Advance the value of the counter.
*
* @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
*/
CHIP_ERROR Advance() override;
CHIP_ERROR Advance() override
{
CHIP_ERROR err = CHIP_NO_ERROR;

mCounterValue++;

return err;
}

/**
* @brief
* Get the current value of the counter.
*
* @return The current value of the counter.
*/
uint32_t GetValue() override;
T GetValue() override { return mCounterValue; }

protected:
uint32_t mCounterValue;
T mCounterValue;
};

} // namespace chip
Loading

0 comments on commit 1576249

Please sign in to comment.