Cppcheck
timer.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 //---------------------------------------------------------------------------
19 #ifndef timerH
20 #define timerH
21 //---------------------------------------------------------------------------
22 
23 #include "config.h"
24 
25 #include <ctime>
26 #include <map>
27 #include <mutex>
28 #include <string>
29 
30 enum class SHOWTIME_MODES {
37 };
38 
40 public:
41  virtual ~TimerResultsIntf() = default;
42 
43  virtual void addResults(const std::string& str, std::clock_t clocks) = 0;
44 };
45 
47  std::clock_t mClocks{};
49 
50  double seconds() const {
51  const double ret = (double)((unsigned long)mClocks) / (double)CLOCKS_PER_SEC;
52  return ret;
53  }
54 };
55 
57 public:
58  TimerResults() = default;
59 
60  void showResults(SHOWTIME_MODES mode) const;
61  void addResults(const std::string& str, std::clock_t clocks) override;
62 
63  void reset();
64 
65 private:
66  std::map<std::string, TimerResultsData> mResults;
67  mutable std::mutex mResultsSync;
68 };
69 
71 public:
72  Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults = nullptr);
73  Timer(bool fileTotal, std::string filename);
74  ~Timer();
75 
76  Timer(const Timer&) = delete;
77  Timer& operator=(const Timer&) = delete;
78 
79  void stop();
80 
81 private:
82  const std::string mStr;
83  TimerResultsIntf* mTimerResults{};
84  std::clock_t mStart = std::clock();
86  bool mStopped{};
87 };
88 //---------------------------------------------------------------------------
89 #endif // timerH
virtual ~TimerResultsIntf()=default
virtual void addResults(const std::string &str, std::clock_t clocks)=0
TimerResults()=default
std::map< std::string, TimerResultsData > mResults
Definition: timer.h:66
std::mutex mResultsSync
Definition: timer.h:67
Definition: timer.h:70
Timer & operator=(const Timer &)=delete
const std::string mStr
Definition: timer.h:82
Timer(const Timer &)=delete
#define CPPCHECKLIB
Definition: config.h:35
std::clock_t mClocks
Definition: timer.h:47
double seconds() const
Definition: timer.h:50
long mNumberOfResults
Definition: timer.h:48
SHOWTIME_MODES
Definition: timer.h:30