From 62d0f7e8e031251a4622cd0c9bc8bdc17048cfcd Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Sun, 5 Apr 2015 01:52:52 +0200 Subject: compat/chrono_literals: Add C++14 backport. Add user-defined literals for duration types (like the ones from C++14), Because these are user-defined they start with an underscore. --- src/menu/mouse_event.cpp | 7 ++-- src/menu/software_mouse_pointer.cpp | 3 +- src/utils/compat/chrono_literals.hpp | 81 ++++++++++++++++++++++++++++++++++++ test/base/timestamp.cpp | 9 ++-- 4 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 src/utils/compat/chrono_literals.hpp diff --git a/src/menu/mouse_event.cpp b/src/menu/mouse_event.cpp index 3f4104a0..ddcc00f2 100644 --- a/src/menu/mouse_event.cpp +++ b/src/menu/mouse_event.cpp @@ -25,6 +25,7 @@ #include #include "mouse_event.hpp" +#include "utils/compat/chrono_literals.hpp" namespace usdx { @@ -48,17 +49,17 @@ namespace usdx { if (direction == UP) { if (this->direction == DOWN) { - if (time.since(std::chrono::milliseconds{5000})) + if (time.since(5000_ms)) return CLICK; } else if (this->direction == CLICK) { - if (time.since(std::chrono::milliseconds{5000})) + if (time.since(5000_ms)) return DBL_CLICK; } } else if (direction == DOWN) { if (this->direction == CLICK && - time.since(std::chrono::milliseconds{5000})) + time.since(5000_ms)) return CLICK; } } diff --git a/src/menu/software_mouse_pointer.cpp b/src/menu/software_mouse_pointer.cpp index 2708917c..fd102fa8 100644 --- a/src/menu/software_mouse_pointer.cpp +++ b/src/menu/software_mouse_pointer.cpp @@ -28,13 +28,14 @@ #include "software_mouse_pointer.hpp" #include "utils/activator.hpp" +#include "utils/compat/chrono_literals.hpp" namespace usdx { SoftwareMousePointer::SoftwareMousePointer(Container* parent, EventManager* event_manager) : DrawableControl(parent), color(255,255,255,100), down(false) { - fade_inactive = new Timer(this, std::chrono::seconds{2}, true); + fade_inactive = new Timer(this, 2_s, true); this->vertices[0] = 0.0f; this->vertices[1] = 40.0f; diff --git a/src/utils/compat/chrono_literals.hpp b/src/utils/compat/chrono_literals.hpp new file mode 100644 index 00000000..7bd28e22 --- /dev/null +++ b/src/utils/compat/chrono_literals.hpp @@ -0,0 +1,81 @@ +/* + * UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef CHRONO_LITERALS_HPP +#define CHRONO_LITERALS_HPP + +#include + +namespace usdx +{ + constexpr std::chrono::milliseconds + operator "" _ms (unsigned long long arg) + { + return std::chrono::milliseconds {arg}; + } + + constexpr std::chrono::duration + operator "" _ms (long double arg) + { + return std::chrono::duration(arg); + } + + constexpr std::chrono::seconds + operator "" _s (unsigned long long arg) + { + return std::chrono::seconds {arg}; + } + + constexpr std::chrono::duration + operator "" _s (long double arg) + { + return std::chrono::duration(arg); + } + + constexpr std::chrono::minutes + operator "" _min (unsigned long long arg) + { + return std::chrono::minutes {arg}; + } + + constexpr std::chrono::duration> + operator "" _min (long double arg) + { + return std::chrono::duration>(arg); + } + + constexpr std::chrono::hours + operator "" _h (unsigned long long arg) + { + return std::chrono::hours {arg}; + } + + constexpr std::chrono::duration> + operator "" _h (long double arg) + { + return std::chrono::duration>(arg); + } +} + +#endif diff --git a/test/base/timestamp.cpp b/test/base/timestamp.cpp index 23ea3b04..87b8ef8f 100644 --- a/test/base/timestamp.cpp +++ b/test/base/timestamp.cpp @@ -24,6 +24,7 @@ #include #include "base/timestamp.hpp" +#include "utils/compat/chrono_literals.hpp" #include #include @@ -49,15 +50,15 @@ namespace usdx void testWaitShorter() { Timestamp t; - std::this_thread::sleep_for(std::chrono::milliseconds{500}); - CPPUNIT_ASSERT_EQUAL(t.since(std::chrono::milliseconds{600}), true); + std::this_thread::sleep_for(500_ms); + CPPUNIT_ASSERT_EQUAL(t.since(600_ms), true); } void testWaitLonger() { Timestamp t; - std::this_thread::sleep_for(std::chrono::milliseconds{500}); - CPPUNIT_ASSERT_EQUAL(t.since(std::chrono::milliseconds{400}), false); + std::this_thread::sleep_for(500_ms); + CPPUNIT_ASSERT_EQUAL(t.since(400_ms), false); } }; -- cgit v1.2.3