简体   繁体   中英

How to reset the high_resolution_clock::time_point

I'm develoiping a class Timer that some of its members are of type high_resolution_clock::time_point where time_point is defined as typedef chrono::time_point<system_clock> time_point;

Question

What is the default value of this object?

I need to be aware of this value from couple of reasons:

  1. Know if member was initialized
  2. Implement Timer::Reset() function

Background

class Timer
{
    void Start() { m_tpStop = high_resolution_clock::now(); }
    void Stop() { m_tpStart = high_resolution_clock::now(); }

    bool WasStarted() { /* TO-DO */ }

    void Reset();
    __int64 GetDuration();

    high_resolution_clock::time_point m_tpStart;
    high_resolution_clock::time_point m_tpStop;
};

So, can I implement Timer::WasStarted by looking only at member m_tpStart ? I'd like to refrain from adding a boolean member for that purpose.

So, can I implement Timer::WasStarted by looking only at member m_tpStart?

Well, if you define such invariant, that m_tpStart is zero (the epoch) if and only if the timer is reset (not started), then it's trivial. Simply check whether start is epoch to test if the timer is started.

Exactly how to set a time point to epoch, seems to be slightly convoluted - and I guess that's what you're referring to with " How to reset the high_resolution_clock::time_point ". You'll need to copy-assign a default constructed time point.

void Start() { m_tpStart = high_resolution_clock::now(); }
void Stop() {
    m_tpStop = high_resolution_clock::now();
}
bool WasStarted() {
    return m_tpStart.time_since_epoch().count(); // unit doesn't matter
}
void Reset() {
    m_tpStart = m_tpStop = {};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM