LCOV - code coverage report
Current view: top level - lib/support - CHIPCounter.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 9 13 69.2 %
Date: 2024-02-15 08:20:41 Functions: 6 9 66.7 %

          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           1 :     Counter() {}
      45           1 :     virtual ~Counter() {}
      46             : 
      47             :     /**
      48             :      *  @brief
      49             :      *  Advance the value of the counter.
      50             :      *
      51             :      *  @return A CHIP error code if anything failed, CHIP_NO_ERROR otherwise.
      52             :      */
      53             :     virtual CHIP_ERROR Advance() = 0;
      54             : 
      55             :     /**
      56             :      *  @brief
      57             :      *  Get the current value of the counter.
      58             :      *
      59             :      *  @return The current value of the counter.
      60             :      */
      61             :     virtual T GetValue() = 0;
      62             : };
      63             : 
      64             : /**
      65             :  * @class MonotonicallyIncreasingCounter
      66             :  *
      67             :  * @brief
      68             :  *   A class for managing a monotonically-increasing counter as an integer value.
      69             :  */
      70             : 
      71             : template <typename T>
      72             : class MonotonicallyIncreasingCounter : public Counter<T>
      73             : {
      74             : public:
      75           1 :     MonotonicallyIncreasingCounter() : mCounterValue(0) {}
      76           1 :     ~MonotonicallyIncreasingCounter() override{};
      77             : 
      78             :     /**
      79             :      *  @brief
      80             :      *    Initialize a MonotonicallyIncreasingCounter object.
      81             :      *
      82             :      *  @param[in] aStartValue  The starting value of the counter.
      83             :      *
      84             :      *  @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
      85             :      */
      86           1 :     CHIP_ERROR Init(T aStartValue)
      87             :     {
      88           1 :         CHIP_ERROR err = CHIP_NO_ERROR;
      89             : 
      90           1 :         mCounterValue = aStartValue;
      91             : 
      92           1 :         return err;
      93             :     }
      94             : 
      95             :     /**
      96             :      *  @brief
      97             :      *  Advance the value of the counter.
      98             :      *
      99             :      *  @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise
     100             :      */
     101           0 :     CHIP_ERROR Advance() override
     102             :     {
     103           0 :         CHIP_ERROR err = CHIP_NO_ERROR;
     104             : 
     105           0 :         mCounterValue++;
     106             : 
     107           0 :         return err;
     108             :     }
     109             : 
     110             :     /**
     111             :      *  @brief
     112             :      *  Get the current value of the counter.
     113             :      *
     114             :      *  @return The current value of the counter.
     115             :      */
     116           1 :     T GetValue() override { return mCounterValue; }
     117             : 
     118             : protected:
     119             :     T mCounterValue;
     120             : };
     121             : 
     122             : } // namespace chip

Generated by: LCOV version 1.14