Freeciv21
Develop your civilization from humble roots to a global empire
timing.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include "timing.h"
15 #include <QElapsedTimer>
16 
17 // utility
18 #include "log.h"
19 
20 Q_LOGGING_CATEGORY(timers_category, "freeciv.timers")
21 
23 
24 class civtimer : public QElapsedTimer {
25 public:
27  enum timer_state state;
28  enum timer_timetype type;
29  enum timer_use use;
30  double sec;
31  int msec;
32 };
33 
35  : QElapsedTimer(), state(TIMER_STOPPED), type(ttype), use(tuse),
36  sec(0.0), msec(0)
37 {
38 }
39 
44 {
45  return timer_renew(nullptr, type, use);
46 }
47 
52  enum timer_use use)
53 {
54  if (!t) {
55  t = new civtimer(type, use);
56  }
57  t->type = type;
58  t->use = use;
59  timer_clear(t);
60  return t;
61 }
62 
67 {
68  delete t;
69  t = nullptr;
70 }
71 
76 bool timer_in_use(civtimer *t) { return (t && t->use != TIMER_IGNORE); }
77 
84 {
85  fc_assert_ret(nullptr != t);
86  t->state = TIMER_STOPPED;
87  t->sec = 0.0;
88  t->msec = 0;
89 }
90 
96 {
97  fc_assert_ret(nullptr != t);
98 
99  if (t->use == TIMER_IGNORE) {
100  return;
101  }
102  if (t->state == TIMER_STARTED) {
103  qCritical("tried to start already started timer");
104  return;
105  }
106  t->state = TIMER_STARTED;
107  t->restart();
108 }
109 
117 {
118  fc_assert_ret(nullptr != t);
119 
120  if (t->use == TIMER_IGNORE) {
121  return;
122  }
123  if (t->state == TIMER_STOPPED) {
124  qCritical("tried to stop already stopped timer");
125  return;
126  }
127  t->msec = t->elapsed();
128  t->sec = (double(t->elapsed()) / 1000);
129  t->state = TIMER_STOPPED;
130 }
131 
138 {
139  fc_assert_ret_val(nullptr != t, -1.0);
140 
141  if (t->use == TIMER_IGNORE) {
142  return 0.0;
143  }
144  if (t->state == TIMER_STARTED) {
145  timer_stop(t);
146  t->state = TIMER_STARTED;
147  }
148  return t->sec;
149 }
enum timer_timetype type
Definition: timing.cpp:28
enum timer_use use
Definition: timing.cpp:29
enum timer_state state
Definition: timing.cpp:27
int msec
Definition: timing.cpp:31
double sec
Definition: timing.cpp:30
civtimer(enum timer_timetype type, enum timer_use use)
Definition: timing.cpp:34
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
Q_LOGGING_CATEGORY(tileset_category, "freeciv.tileset")
Functions for handling the tilespec files which describe the files and contents of tilesets.
void timer_destroy(civtimer *t)
Deletes timer.
Definition: timing.cpp:66
double timer_read_seconds(civtimer *t)
Read value from timer.
Definition: timing.cpp:137
civtimer * timer_new(enum timer_timetype type, enum timer_use use)
Allocate a new timer with specified "type" and "use".
Definition: timing.cpp:43
void timer_start(civtimer *t)
Start timing, adding to previous accumulated time if timer has not been cleared.
Definition: timing.cpp:95
timer_state
Definition: timing.cpp:22
@ TIMER_STARTED
Definition: timing.cpp:22
@ TIMER_STOPPED
Definition: timing.cpp:22
void timer_clear(civtimer *t)
Reset accumulated time to zero, and stop timer if going.
Definition: timing.cpp:83
civtimer * timer_renew(civtimer *t, enum timer_timetype type, enum timer_use use)
Allocate a new timer, or reuse t, with specified "type" and "use".
Definition: timing.cpp:51
bool timer_in_use(civtimer *t)
Return whether timer is in use.
Definition: timing.cpp:76
void timer_stop(civtimer *t)
Stop timing, and accumulate time so far.
Definition: timing.cpp:116
timer_use
Definition: timing.h:24
@ TIMER_IGNORE
Definition: timing.h:26
timer_timetype
Definition: timing.h:19