ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
Time.h
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 TIME_CLASS_H
22#define TIME_CLASS_H
23
24#include <time.h>
25#include <string>
26
27#include "data_management/GraphTreeObject.h"
28#include "logging/SimLogger.h"
29
30/// Constants for this class
31#define NSEC_PER_SEC_D 1000000000.0
32#define MSEC_PER_SEC_I 1000
33#define NSEC_PER_MSEC_I 1000000
34#define NSEC_PER_SEC_I 1000000000
35#define NSEC_MAX 999999999
36
37namespace clockwerk {
38
39 /// @brief Wrapper to manage and convert time as timespce
40 //
41 /// The time class is a simple wrapper class designed to make
42 /// working with time simple. It wraps around a struct timespec
43 /// as its core, and contains functions to perform time math
44 /// and conversions into various formats.
45 class Time : public GraphTreeObject {
46 public:
47 /// Default, copy constructors and default destructor
48 Time() {_tspec.tv_sec = 0; _tspec.tv_nsec = 0; _loggable=true;}
49 Time(const struct timespec &t) {_tspec = t; _loggable=true;}
50 Time(long unsigned int sec, long unsigned int nsec=0) {_tspec.tv_sec=sec; _tspec.tv_nsec=nsec; _loggable=true;}
51 ~Time() {};
52
53 /// @brief Function to set the time in the time object
54 /// @param t Timespec to set the time to11
55 void setTime(const struct timespec &t) {_tspec = t;}
56
57 /// @brief Function to step time forward by a set amount of nanoseconds
58 /// @param nsec Number of nanoseconds to step by
59 void stepTime(const unsigned long long &nsec);
60
61 /// Functions to return various time formats
62 const timespec& asTimespec() const {return _tspec;}
63 double asDouble() const {return (double)_tspec.tv_sec + ((double)_tspec.tv_nsec)/NSEC_PER_SEC_D;}
64 unsigned long long asMilliseconds() const {return _tspec.tv_sec*MSEC_PER_SEC_I + _tspec.tv_nsec/NSEC_PER_MSEC_I;}
65 std::string asString() const {return std::to_string(_tspec.tv_sec) + ":" + std::to_string(_tspec.tv_nsec);}
66
67 /// @brief Function to set a time object from a frequency, in Hz
68 /// @param rate_hz The rate to be converted to time
69 void fromFrequency(unsigned int rate_hz);
70 /// @brief Function to set a time object from double
71 /// @param val The double value to set time from
72 void fromDouble(double val);
73
74 /// @brief Function to log data from our Time class
75 /// @param logger Pointer to the logger to write to
76 /// @note Overwrites virtual function
77 int log(void* logger) {
78 SimLogger* log_ptr = (SimLogger*)logger;
79 log_ptr->writeToBuffer(asDouble());
80 return NO_ERROR;
81 }
82
83 /// @brief Function to write header data from our Time class
84 /// @note Overwrites virtual function
85 std::vector<std::string> header_info() const {
86 return {""};
87 }
88
89 /// @brief Function to add two times together a + b
90 /// @param b Time to add
91 /// @param res Result of the addition operation
92 /// @return Error code corresponding to success/failure
93 int add(const Time &a, Time &res) const;
94
95 /// @brief Function to subtract a - b = res
96 /// @param b Time to subtract
97 /// @param res Result of the subtraction operation
98 /// @return Error code corresponding to success/failure
99 int subtract(const Time &b, Time &res) const;
100
101 /// @brief Function to get half of a struct timespec
102 /// @return timespec corresponding to
103 int divide(unsigned int d, Time &res) const;
104
105 /// @brief Overloaded operator to compare two time objects
106 /// @return True/false corresponding to greater than calculation
107 bool operator>=(const Time& B) const;
108
109 /// @brief Overloaded operator to compare two time objects
110 /// @return True/false corresponding to greater than calculation
111 bool operator<=(const Time& B) const;
112
113 /// @brief Overloaded operator to compare two time objects
114 /// @return True/false corresponding to greater than calculation
115 bool operator>(const Time& B) const;
116
117 /// @brief Overloaded operator to compare two time objects
118 /// @return True/false corresponding to greater than calculation
119 bool operator<(const Time& B) const;
120
121 /// @brief Overloaded operator to compare two time objects
122 /// @return True/false corresponding to equals calculation
123 bool operator==(const Time& B) const;
124 protected:
125 struct timespec _tspec;
126 };
127
128}
129
130#endif
#define NSEC_PER_SEC_D
Definition Time.h:31
#define MSEC_PER_SEC_I
Definition Time.h:32
#define NSEC_PER_MSEC_I
Definition Time.h:33
Base class for object organization.
Definition GraphTreeObject.h:87
bool _loggable
Variable to indicate whether the selected object is loggable. Set to false by default.
Definition GraphTreeObject.h:257
Class for logging data to a file.
Definition SimLogger.h:67
Wrapper to manage and convert time as timespce.
Definition Time.h:45
void setTime(const struct timespec &t)
Function to set the time in the time object.
Definition Time.h:55
bool operator<=(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:159
int divide(unsigned int d, Time &res) const
Function to get half of a struct timespec.
Definition Time.cpp:100
bool operator==(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:173
int add(const Time &a, Time &res) const
Function to add two times together a + b.
Definition Time.cpp:59
void stepTime(const unsigned long long &nsec)
Function to step time forward by a set amount of nanoseconds.
Definition Time.cpp:23
bool operator>(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:117
const timespec & asTimespec() const
Functions to return various time formats.
Definition Time.h:62
bool operator<(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:131
Time()
Default, copy constructors and default destructor.
Definition Time.h:48
void fromFrequency(unsigned int rate_hz)
Function to set a time object from a frequency, in Hz.
Definition Time.cpp:35
int subtract(const Time &b, Time &res) const
Function to subtract a - b = res.
Definition Time.cpp:74
bool operator>=(const Time &B) const
Overloaded operator to compare two time objects.
Definition Time.cpp:145
std::vector< std::string > header_info() const
Function to write header data from our Time class.
Definition Time.h:85
void fromDouble(double val)
Function to set a time object from double.
Definition Time.cpp:48
int log(void *logger)
Function to log data from our Time class.
Definition Time.h:77
#define NO_ERROR
Definition clockwerkerrors.h:31