001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:






















































































































































































































































/*
* This file is part of the Omni C++ framework
*
* Copyright (c) 2016, Zeriph Enterprises, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of Zeriph, Zeriph Enterprises, LLC, nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <omni/chrono/stopwatch.hpp>

omni::stopwatch::stopwatch() :
    OMNI_CTOR_FW(omni::stopwatch)
    m_end(),
    m_init(),
    m_isrun(false),
    m_isstrt(false)
{
    std::memset(&this->m_init, 0, sizeof(omni::chrono::tick_t));
    std::memset(&this->m_end, 0, sizeof(omni::chrono::tick_t));
    this->m_isrun = false;
    OMNI_D5_FW("created");
}

omni::stopwatch::stopwatch(const omni::stopwatch &cp) :
    OMNI_CPCTOR_FW(cp)
    m_end(),
    m_init(),
    m_isrun(false),
    m_isstrt(false)
{
    #if defined(OMNI_OS_WIN) || defined(OMNI_OS_APPLE)
        this->m_init = cp.m_init;
        this->m_end = cp.m_end;
    #else
        this->m_init.tv_sec = cp.m_init.tv_sec;
        this->m_init.tv_nsec = cp.m_init.tv_nsec;
        this->m_end.tv_sec = cp.m_end.tv_sec;
        this->m_end.tv_nsec = cp.m_end.tv_nsec;
    #endif
    this->m_isrun = cp.m_isrun;
    this->m_isstrt = cp.m_isstrt;
    OMNI_D5_FW("copied");
}

omni::stopwatch::~stopwatch()
{
    OMNI_TRY_FW
    OMNI_DTOR_FW
    OMNI_CATCH_FW
    OMNI_D5_FW("destroyed");
}

// TODO: omni::timespan elapsed();

std::size_t omni::stopwatch::elapsed_us() const
{
    return omni::chrono::elapsed_us(this->m_init,
        (this->m_isrun ? omni::chrono::monotonic_tick() : this->m_end)
    );
}

std::size_t omni::stopwatch::elapsed_ms() const
{
    return omni::chrono::elapsed_ms(this->m_init,
        (this->m_isrun ? omni::chrono::monotonic_tick() : this->m_end)
    );
}

std::size_t omni::stopwatch::elapsed_s() const
{
    return omni::chrono::elapsed_s(this->m_init,
        (this->m_isrun ? omni::chrono::monotonic_tick() : this->m_end)
    );
}

bool omni::stopwatch::is_running() const
{
    return this->m_isrun;
}

omni::stopwatch& omni::stopwatch::reset()
{
    this->stop();
    this->_zero();
    OMNI_D2_FW("reset");
    return *this;
}

omni::stopwatch& omni::stopwatch::restart()
{
    return this->restart(0);
}

omni::stopwatch& omni::stopwatch::restart(unsigned long offset_ms)
{
    this->reset();
    OMNI_D2_FW("restarted");
    return this->start(offset_ms);
}

omni::stopwatch& omni::stopwatch::start()
{
    return this->start(0);
}

omni::stopwatch& omni::stopwatch::start(unsigned long offset_ms)
{
    if (this->m_isrun) {
        // Don't 'start' if we are already running
        OMNI_D2_FW("stopwatch already running");
        return *this;
    }
    if (!this->m_isstrt) {
        omni::chrono::tick_t syst = omni::chrono::monotonic_tick();
        // if the user specifies an offset, we need to adjust the 'syst' var to
        // be as if it were 'behind' by 'offset_ms'
        #if defined(OMNI_OS_WIN) || defined(OMNI_OS_APPLE)
            if (offset_ms > 0) {                        
                #if defined(OMNI_OS_WIN)
                    #if defined(OMNI_WIN_MONO_TICK_QPC)
                        //elapsed_us(init,end) = (((end - init) * 1000000) / freq);
                        // init = end - ((off * freq) / 1000) <-- note: 1000 because of ms
                        this->m_init.QuadPart = syst.QuadPart - ((offset_ms * omni::chrono::monotonic::frequency().QuadPart) / 1000);
                    #else
                        // GetTickCount/GetTickCount64/clock: all represent ms so no adjust needed, just a cast
                        this->m_init = syst - static_cast<omni::chrono::tick_t>(offset_ms);
                    #endif
                #else // OMNI_OS_APPLE
                    this->m_init = syst - static_cast<omni::chrono::tick_t>((offset_ms*1000000));
                #endif
            } else {
                this->m_init = syst;
            }
        #else
            if (offset_ms > 0) {
                if (offset_ms >= 1000) {
                    this->m_init.tv_sec = syst.tv_sec - (offset_ms / 1000);
                } else {
                    this->m_init.tv_sec = syst.tv_sec;
                }
                this->m_init.tv_nsec = syst.tv_nsec - ((offset_ms % 1000)*1000000);
            } else {
                this->m_init.tv_sec = syst.tv_sec;
                this->m_init.tv_nsec = syst.tv_nsec;
            }
        #endif
        this->m_end = this->m_init;
        this->m_isstrt = true;
        #if defined(OMNI_OS_WIN) || defined(OMNI_OS_APPLE)
            #if defined(OMNI_OS_WIN) && defined(OMNI_WIN_MONO_TICK_QPC)
                OMNI_DV2_FW("initial clock = ", this->m_init.QuadPart);
            #else
                OMNI_DV2_FW("initial clock = ", this->m_init);
            #endif
        #else
            OMNI_DV2_FW("initial clock = ", this->m_init.tv_nsec);
        #endif
    }
    this->m_isrun = true;
    return *this;
}

omni::stopwatch& omni::stopwatch::stop()
{
    if (this->m_isrun) {
        this->m_end = omni::chrono::monotonic_tick();
        this->m_isrun = false;
        #if defined(OMNI_OS_WIN) || defined(OMNI_OS_APPLE)
            #if defined(OMNI_OS_WIN) && defined(OMNI_WIN_MONO_TICK_QPC)
                OMNI_DV2_FW("end clock = ", this->m_end.QuadPart);
            #else
                OMNI_DV2_FW("end clock = ", this->m_end);
            #endif
        #else
            OMNI_DV2_FW("end clock = ", this->m_end.tv_nsec);
        #endif
    }
    #if defined(OMNI_DBG_L2)
        else { OMNI_D2_FW("stopwatch not running"); }
    #endif
    return *this;
}

omni::stopwatch& omni::stopwatch::operator=(const omni::stopwatch &other)
{
    if (this != &other) {
        this->stop();
        OMNI_ASSIGN_FW(other)
        this->m_end = other.m_end;
        this->m_init = other.m_init;
        this->m_isrun = other.m_isrun;
        this->m_isstrt = other.m_isstrt;
    }
    return *this;
}

omni::stopwatch& omni::stopwatch::operator=(bool enable)
{
    if (enable) { this->start(); }
    else { this->stop(); }
    return *this;
}

bool omni::stopwatch::operator==(const omni::stopwatch &o) const
{
    if (this == &o) { return true; }
    return (omni::chrono::equal(this->m_init, o.m_init) &&
            omni::chrono::equal(this->m_end, o.m_end) &&
            this->m_isrun == o.m_isrun &&
            this->m_isstrt == o.m_isstrt)
            OMNI_EQUAL_FW(o);
}

bool omni::stopwatch::operator!=(const omni::stopwatch &o) const
{
    return !(*this == o);
}

void omni::stopwatch::_zero()
{
    std::memset(&this->m_init, 0, sizeof(omni::chrono::tick_t));
    this->m_end = this->m_init;
    this->m_isstrt = false;
}