I'm implementing my own time unit class. This class provides a function for fetching a specific time value (milliseconds, seconds etc.). In order to prevent defining several get() functions for each time unit, I tried to implement a single function to serve this purpose.
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned long long u_ll;
typedef u_ll DAYS_DTYPE;
typedef u_char HOURS_DTYPE;
typedef u_char MINUTES_DTYPE;
typedef u_char SECONDS_DTYPE;
typedef u_short MILLISECONDS_DTYPE;
// used class members; max. values are handled class-internal
DAYS_DTYPE m_days; // max.: 18.446.744.073.709.551.615
HOURS_DTYPE m_hours; // max.: 23
MINUTES_DTYPE m_minutes; // max.: 59
SECONDS_DTYPE m_seconds; // max.: 59
MILLISECONDS_DTYPE m_milliseconds; // max.: 999
// class-internal enum
enum TimeUnit { Milliseconds, Seconds, Minutes, Hours, Days };
// used member function
template <typename T>
T getTime(TimeUnit unit)
{
switch (unit)
{
case Milliseconds: return m_milliseconds;
case Seconds: return m_seconds;
case Minutes: return m_minutes;
case Hours: return m_hours;
case Days: return m_days;
default:
break;
}
}
In order to fetch a specific time unit I have to use sth. like this:
getTime<MILLISECONDS_DTYPE>(Time::Milliseconds))
Is this approach acceptable? In case it's not, what should I change?
std::chrono
library. \$\endgroup\$getTime<MILLISECONDS_DTYPE>(Time::Milliseconds))
is any better thangetMillisec()
. Actually it seems worse. So why do you want to just define a single function? \$\endgroup\$