LCOV - code coverage report
Current view: top level - src - cxxtimer.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 27 30 90.0 %
Date: 2025-02-23 09:33:43 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /*
       2             : MIT License
       3             : Copyright (c) 2017 André L. Maravilha
       4             : Permission is hereby granted, free of charge, to any person obtaining a copy
       5             : of this software and associated documentation files (the "Software"), to deal
       6             : in the Software without restriction, including without limitation the rights
       7             : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       8             : copies of the Software, and to permit persons to whom the Software is
       9             : furnished to do so, subject to the following conditions:
      10             : The above copyright notice and this permission notice shall be included in all
      11             : copies or substantial portions of the Software.
      12             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      13             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      14             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      15             : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      16             : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      17             : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      18             : SOFTWARE.
      19             : */
      20             : 
      21             : /*
      22             :  * https://github.com/andremaravilha/cxxtimer
      23             :  * commit: a48bd81d9168a4e258123a84fc150912dd65ce79
      24             :  */
      25             : 
      26             : #ifndef CXX_TIMER_HPP
      27             : #define CXX_TIMER_HPP
      28             : 
      29             : #include <chrono>
      30             : 
      31             : 
      32             : namespace cxxtimer {
      33             : 
      34             : /**
      35             :  * This class works as a stopwatch.
      36             :  */
      37             : class Timer {
      38             : 
      39             : public:
      40             : 
      41             :     /**
      42             :      * Constructor.
      43             :      *
      44             :      * @param   start
      45             :      *          If true, the timer is started just after construction.
      46             :      *          Otherwise, it will not be automatically started.
      47             :      */
      48             :     explicit Timer(bool start = false);
      49             : 
      50             :     /**
      51             :      * Copy constructor.
      52             :      *
      53             :      * @param   other
      54             :      *          The object to be copied.
      55             :      */
      56          56 :     Timer(const Timer& other) = default;
      57             : 
      58             :     /**
      59             :      * Transfer constructor.
      60             :      *
      61             :      * @param   other
      62             :      *          The object to be transferred.
      63             :      */
      64         112 :     Timer(Timer&& other) = default;
      65             : 
      66             :     /**
      67             :      * Destructor.
      68             :      */
      69        4326 :     virtual ~Timer() = default;
      70             : 
      71             :     /**
      72             :      * Assignment operator by copy.
      73             :      *
      74             :      * @param   other
      75             :      *          The object to be copied.
      76             :      *
      77             :      * @return  A reference to this object.
      78             :      */
      79             :     Timer& operator=(const Timer& other) = default;
      80             : 
      81             :     /**
      82             :      * Assignment operator by transfer.
      83             :      *
      84             :      * @param   other
      85             :      *          The object to be transferred.
      86             :      *
      87             :      * @return  A reference to this object.
      88             :      */
      89             :     Timer& operator=(Timer&& other) = default;
      90             : 
      91             :     /**
      92             :      * Start/resume the timer.
      93             :      */
      94             :     void start();
      95             : 
      96             :     /**
      97             :      * Stop/pause the timer.
      98             :      */
      99             :     void stop();
     100             : 
     101             :     /**
     102             :      * Reset the timer.
     103             :      */
     104             :     void reset();
     105             : 
     106             :     /**
     107             :      * Return the elapsed time.
     108             :      *
     109             :      * @param   duration_t
     110             :      *          The duration type used to return the time elapsed. If not
     111             :      *          specified, it returns the time as represented by
     112             :      *          std::chrono::milliseconds.
     113             :      *
     114             :      * @return  The elapsed time.
     115             :      */
     116             :     template <class duration_t = std::chrono::milliseconds>
     117             :     typename duration_t::rep count() const;
     118             : 
     119             : private:
     120             : 
     121             :     bool started_;
     122             :     bool paused_;
     123             :     std::chrono::steady_clock::time_point reference_;
     124             :     std::chrono::duration<long double> accumulated_;
     125             : };
     126             : 
     127             : }
     128             : 
     129             : 
     130        4937 : inline cxxtimer::Timer::Timer(bool start) :
     131             :         started_(false), paused_(false),
     132        4937 :         reference_(std::chrono::steady_clock::now()),
     133        4881 :         accumulated_(std::chrono::duration<long double>(0)) {
     134        4937 :     if (start) {
     135        4937 :         this->start();
     136             :     }
     137             : }
     138             : 
     139        4937 : inline void cxxtimer::Timer::start() {
     140        4937 :     if (!started_) {
     141        4937 :         started_ = true;
     142        4937 :         paused_ = false;
     143        4937 :         accumulated_ = std::chrono::duration<long double>(0);
     144        4937 :         reference_ = std::chrono::steady_clock::now();
     145           0 :     } else if (paused_) {
     146           0 :         reference_ = std::chrono::steady_clock::now();
     147           0 :         paused_ = false;
     148             :     }
     149        4937 : }
     150             : 
     151        2757 : inline void cxxtimer::Timer::stop() {
     152        2757 :     if (started_ && !paused_) {
     153        2757 :         std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
     154        2757 :         accumulated_ = accumulated_ + std::chrono::duration_cast< std::chrono::duration<long double> >(now - reference_);
     155        2757 :         paused_ = true;
     156             :     }
     157        2757 : }
     158             : 
     159             : inline void cxxtimer::Timer::reset() {
     160             :     if (started_) {
     161             :         started_ = false;
     162             :         paused_ = false;
     163             :         reference_ = std::chrono::steady_clock::now();
     164             :         accumulated_ = std::chrono::duration<long double>(0);
     165             :     }
     166             : }
     167             : 
     168             : template <class duration_t>
     169        4846 : typename duration_t::rep cxxtimer::Timer::count() const {
     170        4846 :     if (started_) {
     171        4846 :         if (paused_) {
     172        2700 :             return std::chrono::duration_cast<duration_t>(accumulated_).count();
     173             :         } else {
     174        2146 :             return std::chrono::duration_cast<duration_t>(
     175        2146 :                     accumulated_ + (std::chrono::steady_clock::now() - reference_)).count();
     176             :         }
     177             :     } else {
     178             :         return duration_t(0).count();
     179             :     }
     180             : }
     181             : 
     182             : 
     183             : #endif

Generated by: LCOV version 1.14