2
3
4
42ITimer::
ITimer(
int type,
const timeval &interval,
const timeval &value)
noexcept
64 }
catch (
const std::exception &e) {
65 std::cerr <<
"Exception in destructor (" <<
__PRETTY_FUNCTION__ <<
"): " << e.what() <<
'\n';
72 if (!
running)
throw std::runtime_error(
"timer not running");
75 if (new_interval.tv_sec == 0 && new_interval.tv_usec == 0)
76 throw std::runtime_error(
"invalid timer values due to a too small speed factor");
80 if (tmp)
throw std::system_error(errno, std::generic_category(),
"call of setitimer failed");
83 val.it_interval = new_interval;
89 tmp = setitimer(
type, &val,
nullptr);
90 if (tmp)
throw std::system_error(errno, std::generic_category(),
"call of setitimer failed");
97 if (
running)
throw std::logic_error(
"timer already started");
102 if (timer_val.it_interval.tv_sec < 0 || timer_val.it_interval.tv_usec < 0)
103 throw std::runtime_error(
"timer interval is negative");
105 if (timer_val.it_value.tv_sec < 0 || timer_val.it_value.tv_usec < 0)
106 throw std::runtime_error(
"timer value is negative");
108 if (timer_val.it_interval.tv_sec == 0 && timer_val.it_interval.tv_usec == 0)
109 throw std::runtime_error(
"invalid timer values due to a too small speed factor");
111 if (timer_val.it_value.tv_sec == 0 && timer_val.it_value.tv_usec == 0)
112 throw std::runtime_error(
"timer value is zero (would disarm the timer)");
115 int tmp = setitimer(
type, &timer_val,
nullptr);
116 if (tmp < 0)
throw std::system_error(errno, std::generic_category(),
"call of setitimer failed");
122 if (!
running)
throw std::logic_error(
"timer already stopped");
125 itimerval timer_val {};
127 if (tmp < 0)
throw std::system_error(errno, std::generic_category(),
"call of setitimer failed");
137 if (factor <= 0.0)
throw std::invalid_argument(
"negative values not allowed");
139 if (std::isnan(factor) || std::isinf(factor))
throw std::invalid_argument(
"invalid double value");
147 if (
running)
throw std::logic_error(
"cannot set interval/value if timer is running");
167 int tmp = getitimer(
type, &val);
168 if (tmp < 0)
throw std::system_error(errno, std::generic_category(),
"call of getitimer failed");
177 fstream.write(
reinterpret_cast<
char *>(&val),
sizeof(val));
181 if (
running)
throw std::logic_error(
"timer is running");
184 fstream.read(
reinterpret_cast<
char *>(&val),
sizeof(val));
192 int tmp = getitimer(
type, &temp);
193 if (tmp < 0)
throw std::system_error(errno, std::generic_category(),
"call of getitimer failed");
287timeval &
operator*=(timeval &left,
double right)
noexcept {
293itimerval &
operator*=(itimerval &left,
double right)
noexcept {
294 left.it_interval
*= right;
295 left.it_value
*= right;
299timeval
operator*(
const timeval &left,
double right)
noexcept {
305itimerval
operator*(
const itimerval &left,
double right)
noexcept {
311timeval &
operator/=(timeval &left,
double right)
noexcept {
317itimerval &
operator/=(itimerval &left,
double right)
noexcept {
318 left.it_interval
/= right;
319 left.it_value
/= right;
323timeval
operator/(
const timeval &left,
double right)
noexcept {
329itimerval
operator/(
const itimerval &left,
double right)
noexcept {
336 double ret_val =
static_cast<
double>(time.tv_sec) +
static_cast<
double>(time.tv_usec) /
USEC_PER_SEC;
341 auto sec =
static_cast<time_t>(time);
342 auto usec =
static_cast<suseconds_t>((time -
static_cast<
double>(sec)) *
USEC_PER_SEC);
347 return timeval {.tv_sec = sec, .tv_usec = usec};
ITimer_Prof(double interval)
create ITimer_Prof instance
ITimer_Prof(double interval, double value)
create ITimer_Prof instance
ITimer_Prof(const timeval &interval={.tv_sec=1,.tv_usec=0})
create ITimer_Prof instance
static bool instance_exists
ITimer_Prof(const timeval &interval, const timeval &value)
create ITimer_Prof instance
ITimer_Real(const timeval &interval={.tv_sec=1,.tv_usec=0})
create ITimer_Real instance
ITimer_Real(double interval)
create ITimer_Real instance
ITimer_Real(const timeval &interval, const timeval &value)
create ITimer_Real instance
static bool instance_exists
ITimer_Real(double interval, double value)
create ITimer_Real instance
static bool instance_exists
ITimer_Virtual(const timeval &interval, const timeval &value)
create ITimer_Virtual instance
~ITimer_Virtual() override
ITimer_Virtual(const timeval &interval={.tv_sec=1,.tv_usec=0})
create ITimer_Virtual instance
ITimer_Virtual(double interval, double value)
create ITimer_Virtual instance
ITimer_Virtual(double interval)
create ITimer_Virtual instance
void to_fstream(std::ofstream &fstream) const
write to binary file stream
double speed_factor
speed adjustment factor
ITimer(int type, double interval, double value) noexcept
ITimer(int type, const timeval &interval={.tv_sec=1,.tv_usec=0}) noexcept
ITimer(int type, double interval) noexcept
timeval get_timer_value() const
get timer value
void from_fstream(std::ifstream &fstream)
read from binary filestream
void set_interval_value(double interval, double value)
set interval and value
virtual ~ITimer()
Destroy the timer instance.
void set_speed_to_normal()
set speed to normal
void set_speed_factor(double factor)
set speed factor
ITimer(int type, const timeval &interval, const timeval &value) noexcept
void set_interval_value(const timeval &interval, const timeval &value)
set interval and value
virtual void adjust_speed(double new_factor)
itimerval & operator/=(itimerval &left, double right) noexcept
timeval double_to_timeval(const double time) noexcept
timeval & operator*=(timeval &left, double right) noexcept
constexpr auto USEC_PER_SEC
timeval operator*(const timeval &left, double right) noexcept
itimerval operator*(const itimerval &left, double right) noexcept
itimerval operator/(const itimerval &left, double right) noexcept
itimerval & operator*=(itimerval &left, double right) noexcept
timeval & operator/=(timeval &left, double right) noexcept
double timeval_to_double(const timeval &time) noexcept
static constexpr itimerval STOP_TIMER
timeval operator/(const timeval &left, double right) noexcept