Matter SDK Coverage Report
Current view: top level - lib/support - CHIPCounter.h (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 56.2 % 16 9
Test Date: 2025-01-17 19:00:11 Functions: 52.6 % 19 10

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    Copyright (c) 2016-2017 Nest Labs, Inc.
       5              :  *    All rights reserved.
       6              :  *
       7              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       8              :  *    you may not use this file except in compliance with the License.
       9              :  *    You may obtain a copy of the License at
      10              :  *
      11              :  *        http://www.apache.org/licenses/LICENSE-2.0
      12              :  *
      13              :  *    Unless required by applicable law or agreed to in writing, software
      14              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      15              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16              :  *    See the License for the specific language governing permissions and
      17              :  *    limitations under the License.
      18              :  */
      19              : 
      20              : /**
      21              :  * @file
      22              :  *
      23              :  * @brief
      24              :  *   Class declarations for a Counter base class, and a monotonically-increasing counter.
      25              :  */
      26              : 
      27              : #pragma once
      28              : 
      29              : #include <lib/core/CHIPError.h>
      30              : 
      31              : namespace chip {
      32              : 
      33              : /**
      34              :  * @class Counter
      35              :  *
      36              :  * @brief
      37              :  *   An interface for managing a counter as an integer value.
      38              :  */
      39              : 
      40              : template <typename T>
      41              : class Counter
      42              : {
      43              : public:
      44           76 :     Counter() {}
      45           76 :     virtual ~Counter() {}
      46              : 
      47              :     /**
      48              :      *  @brief Advance the value of the counter.
      49              :      *
      50              :      *  @return A CHIP error code if anything failed, CHIP_NO_ERROR otherwise.
      51              :      */
      52              :     virtual CHIP_ERROR Advance() = 0;
      53              : 
      54              :     /**
      55              :      * @brief Advances the current counter value by N
      56              :      *
      57              :      * @param value value of N
      58              :      *
      59              :      * @return A CHIP error code if anything failed, CHIP_NO_ERROR otherwise.
      60              :      */
      61              :     virtual CHIP_ERROR AdvanceBy(T value) = 0;
      62              : 
      63              :     /**
      64              :      *  @brief Get the current value of the counter.
      65              :      *
      66              :      *  @return The current value of the counter.
      67              :      */
      68              :     virtual T GetValue() = 0;
      69              : };
      70              : 
      71              : /**
      72              :  * @class MonotonicallyIncreasingCounter
      73              :  *
      74              :  * @brief
      75              :  *   A class for managing a monotonically-increasing counter as an integer value.
      76              :  */
      77              : 
      78              : template <typename T>
      79              : class MonotonicallyIncreasingCounter : public Counter<T>
      80              : {
      81              : public:
      82           76 :     MonotonicallyIncreasingCounter() : mCounterValue(0) {}
      83           76 :     ~MonotonicallyIncreasingCounter() override{};
      84              : 
      85              :     /**
      86              :      *  @brief Initialize a MonotonicallyIncreasingCounter object.
      87              :      *
      88              :      *  @param[in] aStartValue  The starting value of the counter.
      89              :      *
      90              :      *  @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
      91              :      */
      92            1 :     CHIP_ERROR Init(T aStartValue)
      93              :     {
      94            1 :         CHIP_ERROR err = CHIP_NO_ERROR;
      95              : 
      96            1 :         mCounterValue = aStartValue;
      97              : 
      98            1 :         return err;
      99              :     }
     100              : 
     101              :     /**
     102              :      *  @brief Advance the value of the counter.
     103              :      *
     104              :      *  @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
     105              :      */
     106            0 :     CHIP_ERROR Advance() override
     107              :     {
     108            0 :         CHIP_ERROR err = CHIP_NO_ERROR;
     109              : 
     110            0 :         mCounterValue++;
     111              : 
     112            0 :         return err;
     113              :     }
     114              : 
     115              :     /**
     116              :      * @brief Advances the current counter value by N
     117              :      *
     118              :      * @param value value of N
     119              :      *
     120              :      * @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
     121              :      */
     122            0 :     CHIP_ERROR AdvanceBy(T value) override
     123              :     {
     124            0 :         mCounterValue = static_cast<T>(mCounterValue + value);
     125            0 :         return CHIP_NO_ERROR;
     126              :     }
     127              : 
     128              :     /**
     129              :      *  @brief Get the current value of the counter.
     130              :      *
     131              :      *  @return The current value of the counter.
     132              :      */
     133            1 :     T GetValue() override { return mCounterValue; }
     134              : 
     135              : protected:
     136              :     T mCounterValue;
     137              : };
     138              : 
     139              : } // namespace chip
        

Generated by: LCOV version 2.0-1