WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
Time.h
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Time class header file
18
19Author: Alex Reynolds
20*/
21#ifndef ARCHITECTURE_TIME_H
22#define ARCHITECTURE_TIME_H
23
24#include "configuration.h"
25
26#include <time.h>
27#if CLOCKWERK_ALLOW_STD_STRING
28#include <string>
29#endif
30
32#include "logging/SimLogger.h"
33
35#define NSEC_MAX 999999999
36
37namespace clockwerk {
38 const floating_point NSEC_PER_SEC_D = 1000000000.0;
39 const uint16 MSEC_PER_SEC_I = 1000;
40 const uint32 NSEC_PER_MSEC_I = 1000000;
41 const uint32 NSEC_PER_SEC_I = 1000000000;
42
44 //
53 class Time : public GraphTreeObject {
54 public:
56 Time(const struct timespec &t) {_tspec = t; _loggable=true;}
57 Time(int64 sec=0, int64 nsec=0) {_tspec.tv_sec=sec; _tspec.tv_nsec=nsec; _loggable=true;}
58 ~Time() {};
59
61 int64 sec() const {return _tspec.tv_sec;}
63 int64 nsec() const {return _tspec.tv_nsec;}
64
67 void setTime(const struct timespec &t) {_tspec = t;}
68
72 void setTime(uint32 sec, uint32 nsec) {_tspec.tv_sec=sec; _tspec.tv_nsec=nsec;}
73
76 void stepTime(uint64 nsec);
77
79 const timespec& asTimespec() const {return _tspec;}
80 floating_point asFloatingPoint() const {return (floating_point)_tspec.tv_sec + ((floating_point)_tspec.tv_nsec)/NSEC_PER_SEC_D;}
81 unsigned long long asMilliseconds() const {return _tspec.tv_sec*MSEC_PER_SEC_I + _tspec.tv_nsec/NSEC_PER_MSEC_I;}
82
83 int16 fromStr(const char* str) override;
84
91 int16 str(char* output, size_t size) const override;
92
93#if CLOCKWERK_ALLOW_STD_STRING
94#if CLOCKWERK_ALLOW_VECTOR
98 int16 log(void* logger) {
99 SimLogger* log_ptr = (SimLogger*)logger;
100 log_ptr->writeToBuffer(asFloatingPoint());
101 return NO_ERROR;
102 }
103
106 std::vector<std::string> header_info() const {
107 return {""};
108 }
109#endif
110#endif
111
114 floating_point asFrequency();
115
118 int16 fromFrequency(uint16 rate_hz);
121 void fromFloatingPoint(floating_point val);
125
130 int16 add(const Time &a, Time &res) const;
131
136 int16 subtract(const Time &b, Time &res) const;
137
142 int16 multiply(unsigned int m, Time &res) const;
143
148 int16 divide(unsigned int d, Time &res) const;
149
153 bool operator>=(const Time& B) const;
154
158 bool operator<=(const Time& B) const;
159
163 bool operator>(const Time& B) const;
164
168 bool operator<(const Time& B) const;
169
173 bool operator==(const Time& B) const;
174 protected:
175 struct timespec _tspec;
176 };
177
178}
179
180#endif
bool _loggable
Variable to indicate whether the selected object is loggable. Set to false by default.
Definition GraphTreeObject.h:273
GraphTreeObject(const char *gt_nme="", GraphTreeObject **storage_array=nullptr, uint32 storage_size=0)
Name-based constructor for GraphTreeObject which will have no children by default.
Definition GraphTreeObject.cpp:23
void setTime(const struct timespec &t)
Function to set the time in the time object.
Definition Time.h:67
floating_point asFloatingPoint() const
Definition Time.h:80
struct timespec _tspec
Definition Time.h:175
bool operator<=(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:200
int16 multiply(unsigned int m, Time &res) const
Function to multiply time by int.
Definition Time.cpp:128
Time(const struct timespec &t)
Default, copy constructors and default destructor.
Definition Time.h:56
int16 subtract(const Time &b, Time &res) const
Function to subtract a - b = res.
Definition Time.cpp:102
bool operator==(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:214
int16 add(const Time &a, Time &res) const
Set the time object from a string.
Definition Time.cpp:87
~Time()
Definition Time.h:58
bool operator>(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:158
void fromFloatingPoint(floating_point val)
Function to set a time object from floating point.
Definition Time.cpp:63
Time(int64 sec=0, int64 nsec=0)
Definition Time.h:57
floating_point asFrequency()
Function to convert time to frequency in Hz.
Definition Time.cpp:36
int64 nsec() const
Get time nanoseconds.
Definition Time.h:63
const timespec & asTimespec() const
Functions to return various time formats.
Definition Time.h:79
bool operator<(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:172
int16 fromStr(const char *str) override
Definition Time.cpp:78
void stepTime(uint64 nsec)
Function to step time forward by a set amount of nanoseconds.
Definition Time.cpp:24
void setTime(uint32 sec, uint32 nsec)
Function to set the time in the time object.
Definition Time.h:72
int16 str(char *output, size_t size) const override
Output time as string.
Definition Time.cpp:74
int16 fromFrequency(uint16 rate_hz)
Function to set a time object from a frequency, in Hz.
Definition Time.cpp:44
bool operator>=(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:186
unsigned long long asMilliseconds() const
Definition Time.h:81
int64 sec() const
Get time seconds.
Definition Time.h:61
int16 divide(unsigned int d, Time &res) const
Function to divide time by int.
Definition Time.cpp:141
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
Definition CircularBuffer.hpp:28
const uint32 NSEC_PER_SEC_I
Definition Time.h:41
const floating_point NSEC_PER_SEC_D
Definition Time.h:38
const uint32 NSEC_PER_MSEC_I
Definition Time.h:40
const uint16 MSEC_PER_SEC_I
Definition Time.h:39