LCOV - code coverage report
Current view: top level - src - cxxtimer.hpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 0 39 0.0 %
Date: 2026-06-25 07:23:51 Functions: 0 12 0.0 %

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

Generated by: LCOV version 1.16