Mercurial > code
changeset 599:9016afda8527
ElapsedTimer: use Boost.Timer instead
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 02 Dec 2016 22:20:36 +0100 |
parents | ce684e9e2151 |
children | af299c1729cf |
files | CMakeLists.txt modules/elapsed-timer/CMakeLists.txt modules/elapsed-timer/elapsed-timer.hpp modules/elapsed-timer/test/main.cpp |
diffstat | 4 files changed, 0 insertions(+), 181 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Fri Dec 02 22:20:10 2016 +0100 +++ b/CMakeLists.txt Fri Dec 02 22:20:36 2016 +0100 @@ -49,7 +49,6 @@ ) endif () -add_subdirectory(modules/elapsed-timer) add_subdirectory(modules/js) add_subdirectory(modules/net) add_subdirectory(modules/signals)
--- a/modules/elapsed-timer/CMakeLists.txt Fri Dec 02 22:20:10 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -# -# CMakeLists.txt -- code building for common code -# -# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -code_define_module( - NAME elapsed-timer - SOURCES elapsed-timer.hpp -)
--- a/modules/elapsed-timer/elapsed-timer.hpp Fri Dec 02 22:20:10 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -/* - * elapsed-timer.hpp -- measure elapsed time - * - * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef ELAPSED_TIMER_HPP -#define ELAPSED_TIMER_HPP - -/** - * \file elapsed-timer.hpp - * \brief Measure elapsed time - */ - -#include <chrono> - -/** - * \brief Measure elapsed time - * - * This class provides an abstraction to measure elapsed time since the - * construction of the object. It uses std::chrono::high_resolution_clock for - * more precision and uses milliseconds only. - */ -class ElapsedTimer { -private: - using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>; - - TimePoint m_last; - bool m_paused{false}; - unsigned m_elapsed{0}; - -public: - /** - * Construct the elapsed timer, start counting. - */ - inline ElapsedTimer() noexcept - : m_last(std::chrono::high_resolution_clock::now()) - { - } - - /** - * Put the timer on pause, the already elapsed time is stored. - */ - inline void pause() noexcept - { - /* - * When we put the timer on pause, do not forget to set the already - * elapsed time. - */ - elapsed(); - m_paused = true; - } - - /** - * Restart the timer, does not reset it. - */ - inline void restart() noexcept - { - m_paused = false; - m_last = std::chrono::high_resolution_clock::now(); - } - - /** - * Reset the timer to 0. - */ - inline void reset() noexcept - { - m_elapsed = 0; - m_last = std::chrono::high_resolution_clock::now(); - } - - /** - * Get the number of elapsed milliseconds. - * - * \return the milliseconds - */ - inline unsigned elapsed() noexcept - { - using std::chrono::duration_cast; - using std::chrono::high_resolution_clock; - using std::chrono::milliseconds; - - if (!m_paused) { - m_elapsed += duration_cast<milliseconds>(high_resolution_clock::now() - m_last).count(); - m_last = high_resolution_clock::now(); - } - - return m_elapsed; - } -}; - -#endif // !ELAPSED_TIMER_HPP
--- a/modules/elapsed-timer/test/main.cpp Fri Dec 02 22:20:10 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* - * main.cpp -- test ElapsedTimer - * - * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <thread> - -#include <gtest/gtest.h> - -#include <elapsed-timer.hpp> - -using namespace std::chrono_literals; - -TEST(TestElapsedTimer, standard) -{ - ElapsedTimer timer; - - std::this_thread::sleep_for(300ms); - - ASSERT_GE(timer.elapsed(), 250U); - ASSERT_LE(timer.elapsed(), 350U); -} - -TEST(TestElapsedTimer, reset) -{ - ElapsedTimer timer; - - std::this_thread::sleep_for(300ms); - - timer.reset(); - - ASSERT_LE(timer.elapsed(), 100U); -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -} -