diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2015-04-05 01:52:52 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2015-04-05 01:52:52 +0200 |
commit | 62d0f7e8e031251a4622cd0c9bc8bdc17048cfcd (patch) | |
tree | d4c74b5ca58db570a7856e08abd736652d98b492 | |
parent | fe9648fa1cdc8f9cf5216a88fadb8b96604047fd (diff) | |
download | usdx-62d0f7e8e031251a4622cd0c9bc8bdc17048cfcd.tar.gz usdx-62d0f7e8e031251a4622cd0c9bc8bdc17048cfcd.tar.xz usdx-62d0f7e8e031251a4622cd0c9bc8bdc17048cfcd.zip |
compat/chrono_literals: Add C++14 backport.cpp
Add user-defined literals for duration types (like the ones from C++14),
Because these are user-defined they start with an underscore.
Diffstat (limited to '')
-rw-r--r-- | src/menu/mouse_event.cpp | 7 | ||||
-rw-r--r-- | src/menu/software_mouse_pointer.cpp | 3 | ||||
-rw-r--r-- | src/utils/compat/chrono_literals.hpp | 81 | ||||
-rw-r--r-- | test/base/timestamp.cpp | 9 |
4 files changed, 92 insertions, 8 deletions
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 <cstddef> #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 <chrono> + +namespace usdx +{ + constexpr std::chrono::milliseconds + operator "" _ms (unsigned long long arg) + { + return std::chrono::milliseconds {arg}; + } + + constexpr std::chrono::duration<long double, std::milli> + operator "" _ms (long double arg) + { + return std::chrono::duration<long double, std::milli>(arg); + } + + constexpr std::chrono::seconds + operator "" _s (unsigned long long arg) + { + return std::chrono::seconds {arg}; + } + + constexpr std::chrono::duration<long double> + operator "" _s (long double arg) + { + return std::chrono::duration<long double>(arg); + } + + constexpr std::chrono::minutes + operator "" _min (unsigned long long arg) + { + return std::chrono::minutes {arg}; + } + + constexpr std::chrono::duration<long double, std::ratio<60,1>> + operator "" _min (long double arg) + { + return std::chrono::duration<long double, std::ratio<60,1>>(arg); + } + + constexpr std::chrono::hours + operator "" _h (unsigned long long arg) + { + return std::chrono::hours {arg}; + } + + constexpr std::chrono::duration<long double, std::ratio<3600,1>> + operator "" _h (long double arg) + { + return std::chrono::duration<long double, std::ratio<3600,1>>(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 <thread> #include "base/timestamp.hpp" +#include "utils/compat/chrono_literals.hpp" #include <cppunit/extensions/HelperMacros.h> #include <log4cpp/Category.hh> @@ -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); } }; |