From 408722c9f6c380051842a10e3a3552348ee03a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sat, 23 Jun 2018 21:56:18 +0300 Subject: [PATCH] libcore|Time: Disallow TimeSpan construction from an integer An integer might mistakenly be milliseconds where TimeSpans should be seconds. --- doomsday/libs/core/include/de/data/time.h | 4 +++- doomsday/libs/core/include/de/widgets/animation.h | 11 +++++++---- .../libs/core/include/de/widgets/animationrule.h | 4 ++-- .../libs/core/include/de/widgets/rulerectangle.h | 2 +- doomsday/libs/core/src/core/loop.cpp | 8 ++++---- doomsday/libs/core/src/net/socket.cpp | 2 +- doomsday/libs/core/src/scriptsys/process.cpp | 2 +- doomsday/libs/core/src/scriptsys/scriptsystem.cpp | 4 ++-- doomsday/libs/core/src/widgets/animation.cpp | 7 +++---- doomsday/libs/core/src/widgets/animationrule.cpp | 2 +- .../libs/shell/include/de/shell/abstractlink.h | 2 +- doomsday/libs/shell/include/de/shell/link.h | 2 +- doomsday/libs/shell/src/abstractlink.cpp | 4 ++-- doomsday/libs/shell/src/localserver.cpp | 2 +- doomsday/libs/shell/src/serverfinder.cpp | 4 ++-- doomsday/tests/test_timer/main.cpp | 4 ++-- doomsday/tools/shell-text/src/shellapp.cpp | 2 +- doomsday/tools/shell-text/src/statuswidget.cpp | 15 ++++++--------- 18 files changed, 41 insertions(+), 40 deletions(-) diff --git a/doomsday/libs/core/include/de/data/time.h b/doomsday/libs/core/include/de/data/time.h index dee3c8a121..df55d7e720 100644 --- a/doomsday/libs/core/include/de/data/time.h +++ b/doomsday/libs/core/include/de/data/time.h @@ -62,10 +62,12 @@ class DE_PUBLIC Time : public ISerializable * * @param seconds Length of the time span. */ - Span(ddouble seconds = 0) + Span(ddouble seconds = 0.0) : _seconds(seconds) {} + Span(int) = delete; // ambiguous + /** * Conversion to the numeric type (floating-point seconds). */ diff --git a/doomsday/libs/core/include/de/widgets/animation.h b/doomsday/libs/core/include/de/widgets/animation.h index 2b0077c502..7a1d55830b 100644 --- a/doomsday/libs/core/include/de/widgets/animation.h +++ b/doomsday/libs/core/include/de/widgets/animation.h @@ -71,9 +71,9 @@ class DE_PUBLIC Animation : public ISerializable, public Deletable * This includes @a startDelay. * @param startDelay Number of seconds to wait before starting the transition. */ - void setValue(float toValue, TimeSpan transitionSpan = 0, TimeSpan startDelay = 0); + void setValue(float toValue, TimeSpan transitionSpan = 0.0, TimeSpan startDelay = 0.0); - void setValue(int toValue, TimeSpan transitionSpan = 0, TimeSpan startDelay = 0); + void setValue(int toValue, TimeSpan transitionSpan = 0.0, TimeSpan startDelay = 0.0); /** * Starts a new transition. @@ -84,7 +84,10 @@ class DE_PUBLIC Animation : public ISerializable, public Deletable * This includes @a startDelay. * @param startDelay Number of seconds to wait before starting the transition. */ - void setValueFrom(float fromValue, float toValue, TimeSpan transitionSpan = 0, TimeSpan startDelay = 0); + void setValueFrom(float fromValue, + float toValue, + TimeSpan transitionSpan = 0.0, + TimeSpan startDelay = 0.0); /** * Current value. @@ -178,7 +181,7 @@ class DE_PUBLIC Animation : public ISerializable, public Deletable static TimeSpan currentTime(); - static Animation range(Style style, float from, float to, TimeSpan span, TimeSpan delay = 0); + static Animation range(Style style, float from, float to, TimeSpan span, TimeSpan delay = 0.0); private: DE_PRIVATE(d) diff --git a/doomsday/libs/core/include/de/widgets/animationrule.h b/doomsday/libs/core/include/de/widgets/animationrule.h index c876ca1309..fa4c979efb 100644 --- a/doomsday/libs/core/include/de/widgets/animationrule.h +++ b/doomsday/libs/core/include/de/widgets/animationrule.h @@ -47,9 +47,9 @@ class DE_PUBLIC AnimationRule : public Rule, DE_OBSERVES(Clock, TimeChange) */ explicit AnimationRule(Rule const &target, TimeSpan transition, Animation::Style style = Animation::EaseOut); - void set(float target, TimeSpan transition = 0, TimeSpan delay = 0); + void set(float target, TimeSpan transition = 0.0, TimeSpan delay = 0.0); - void set(Rule const &target, TimeSpan transition = 0, TimeSpan delay = 0); + void set(Rule const &target, TimeSpan transition = 0.0, TimeSpan delay = 0.0); /** * Sets the animation style of the rule. diff --git a/doomsday/libs/core/include/de/widgets/rulerectangle.h b/doomsday/libs/core/include/de/widgets/rulerectangle.h index 1878332eb4..7e0735d87b 100644 --- a/doomsday/libs/core/include/de/widgets/rulerectangle.h +++ b/doomsday/libs/core/include/de/widgets/rulerectangle.h @@ -123,7 +123,7 @@ class DE_PUBLIC RuleRectangle : public ISizeRule * (1, 1) to the bottom right. * @param transition Transition time for the change. */ - void setAnchorPoint(Vec2f const &normalizedPoint, TimeSpan const &transition = 0); + void setAnchorPoint(Vec2f const &normalizedPoint, TimeSpan const &transition = 0.0); /** * Returns the current rectangle as defined by the input rules. diff --git a/doomsday/libs/core/src/core/loop.cpp b/doomsday/libs/core/src/core/loop.cpp index 84249688f6..a078adc25c 100644 --- a/doomsday/libs/core/src/core/loop.cpp +++ b/doomsday/libs/core/src/core/loop.cpp @@ -31,12 +31,12 @@ static Loop *loopSingleton = nullptr; DE_PIMPL(Loop) { - TimeSpan interval; - bool running; - Timer timer; + TimeSpan interval; + bool running; + Timer timer; LoopCallback mainCall; - Impl(Public *i) : Base(i), interval(0), running(false) + Impl(Public *i) : Base(i), running(false) { DE_ASSERT(!loopSingleton); loopSingleton = i; diff --git a/doomsday/libs/core/src/net/socket.cpp b/doomsday/libs/core/src/net/socket.cpp index 105327b4dd..492bc6bdc8 100644 --- a/doomsday/libs/core/src/net/socket.cpp +++ b/doomsday/libs/core/src/net/socket.cpp @@ -86,7 +86,7 @@ struct Counters Time periodStartedAt; }; static LockableT counters; -static TimeSpan const sendPeriodDuration = 5; +static TimeSpan const sendPeriodDuration = 5.0; /// Maximum number of channels. static duint const MAX_CHANNELS = 2; diff --git a/doomsday/libs/core/src/scriptsys/process.cpp b/doomsday/libs/core/src/scriptsys/process.cpp index f2dce133d7..f3d23e9c8d 100644 --- a/doomsday/libs/core/src/scriptsys/process.cpp +++ b/doomsday/libs/core/src/scriptsys/process.cpp @@ -147,7 +147,7 @@ DE_PIMPL(Process) }; /// If execution continues for longer than this, a HangError is thrown. -static TimeSpan const MAX_EXECUTION_TIME = 10; +static TimeSpan const MAX_EXECUTION_TIME = 10.0; Process::Process(Record *externalGlobalNamespace) : d(new Impl(this)) { diff --git a/doomsday/libs/core/src/scriptsys/scriptsystem.cpp b/doomsday/libs/core/src/scriptsys/scriptsystem.cpp index aa6baffaba..7f561d3d4a 100644 --- a/doomsday/libs/core/src/scriptsys/scriptsystem.cpp +++ b/doomsday/libs/core/src/scriptsys/scriptsystem.cpp @@ -165,9 +165,9 @@ DE_PIMPL(ScriptSystem) // Compile a list of all possible import locations. importPaths.clear(); - DE_FOR_EACH_CONST(ArrayValue::Elements, i, importPath->elements()) + for (const auto *i : importPath->elements()) { - importPaths << (*i)->asText(); + importPaths << i->asText(); } for (const Path &path : additionalImportPaths) { diff --git a/doomsday/libs/core/src/widgets/animation.cpp b/doomsday/libs/core/src/widgets/animation.cpp index d5625b10a7..d772ba0db1 100644 --- a/doomsday/libs/core/src/widgets/animation.cpp +++ b/doomsday/libs/core/src/widgets/animation.cpp @@ -96,7 +96,6 @@ DE_PIMPL_NOREF(Animation) Impl(float val, Style s) : value(val) , target(val) - , startDelay(0) , setTime (theTime.now) , targetTime(theTime.now) , style(s) @@ -113,8 +112,8 @@ DE_PIMPL_NOREF(Animation) TimeSpan span = targetTime - setTime; float s2 = 0; - TimeSpan peak = 0; - TimeSpan peak2 = 0; + TimeSpan peak; + TimeSpan peak2; // Spring values. if (style == Bounce || style == FixedBounce) @@ -302,7 +301,7 @@ TimeSpan Animation::remainingTime() const TimeSpan const now = d->currentTime(); if (now >= d->targetTime) { - return 0; + return 0.0; } return d->targetTime - now; } diff --git a/doomsday/libs/core/src/widgets/animationrule.cpp b/doomsday/libs/core/src/widgets/animationrule.cpp index d83d9a1cad..34e926f2ce 100644 --- a/doomsday/libs/core/src/widgets/animationrule.cpp +++ b/doomsday/libs/core/src/widgets/animationrule.cpp @@ -131,7 +131,7 @@ void AnimationRule::update() TimeSpan span = _animation.transitionTime(); if (_behavior.testFlag(DontAnimateFromZero) && fequal(_animation.target(), 0)) { - span = 0; + span = 0.0; } _animation.setValue(_targetRule->value(), span); _animation.clock().audienceForPriorityTimeChange += this; diff --git a/doomsday/libs/shell/include/de/shell/abstractlink.h b/doomsday/libs/shell/include/de/shell/abstractlink.h index 674d75cdfb..a7e1ec5e3f 100644 --- a/doomsday/libs/shell/include/de/shell/abstractlink.h +++ b/doomsday/libs/shell/include/de/shell/abstractlink.h @@ -54,7 +54,7 @@ class LIBSHELL_PUBLIC AbstractLink * @param domain Domain/IP address of the server. * @param timeout Keep trying until this much time has passed. */ - virtual void connectDomain(String const &domain, TimeSpan const &timeout = 0); + virtual void connectDomain(String const &domain, TimeSpan const &timeout = 0.0); /** * Opens a connection to a server over the network. diff --git a/doomsday/libs/shell/include/de/shell/link.h b/doomsday/libs/shell/include/de/shell/link.h index eefa168a0d..c8a6afe431 100644 --- a/doomsday/libs/shell/include/de/shell/link.h +++ b/doomsday/libs/shell/include/de/shell/link.h @@ -45,7 +45,7 @@ class LIBSHELL_PUBLIC Link : public AbstractLink * @param domain Domain/IP address of the server. * @param timeout Keep trying until this much time has passed. */ - Link(String const &domain, TimeSpan const &timeout = 0); + Link(String const &domain, TimeSpan const &timeout = 0.0); /** * Opens a connection to a server over the network. diff --git a/doomsday/libs/shell/src/abstractlink.cpp b/doomsday/libs/shell/src/abstractlink.cpp index b56ac8dd85..9224b4a18e 100644 --- a/doomsday/libs/shell/src/abstractlink.cpp +++ b/doomsday/libs/shell/src/abstractlink.cpp @@ -163,7 +163,7 @@ void AbstractLink::connectHost(Address const &address) d->status = Connecting; d->startedTryingAt = Time(); - d->timeout = 0; + d->timeout = 0.0; } void AbstractLink::takeOver(Socket *openSocket) @@ -189,7 +189,7 @@ void AbstractLink::disconnect() { DE_ASSERT(d->socket.get() != nullptr); - d->timeout = 0; + d->timeout = 0.0; d->socket->close(); // emits signal d->status = Disconnected; diff --git a/doomsday/libs/shell/src/localserver.cpp b/doomsday/libs/shell/src/localserver.cpp index c063139cc4..c735c4043e 100644 --- a/doomsday/libs/shell/src/localserver.cpp +++ b/doomsday/libs/shell/src/localserver.cpp @@ -175,7 +175,7 @@ bool LocalServer::isRunning() const Link *LocalServer::openLink() { if (!isRunning()) return nullptr; - return new Link(String::format("localhost:%d", d->port), 30); + return new Link(String::format("localhost:%d", d->port), 30.0); } NativePath LocalServer::errorLogPath() const diff --git a/doomsday/libs/shell/src/serverfinder.cpp b/doomsday/libs/shell/src/serverfinder.cpp index 73d7f696b0..3d5e916867 100644 --- a/doomsday/libs/shell/src/serverfinder.cpp +++ b/doomsday/libs/shell/src/serverfinder.cpp @@ -29,7 +29,7 @@ namespace de { namespace shell { -static TimeSpan MSG_EXPIRATION_SECS = 4; +static TimeSpan MSG_EXPIRATION_SECS = 4.0; DE_PIMPL(ServerFinder) , DE_OBSERVES(Beacon, Discovery) @@ -137,7 +137,7 @@ ServerFinder::ServerFinder() : d(new Impl(this)) if (!App::appExists() || !App::commandLine().has("-nodiscovery")) { - d->beacon.discover(0 /* no timeout */, 2); + d->beacon.discover(0.0 /* no timeout */, 2.0); } } catch (Beacon::PortError const &er) diff --git a/doomsday/tests/test_timer/main.cpp b/doomsday/tests/test_timer/main.cpp index 1ab7d6f660..350c886f26 100644 --- a/doomsday/tests/test_timer/main.cpp +++ b/doomsday/tests/test_timer/main.cpp @@ -35,14 +35,14 @@ int main(int argc, char **argv) Timer quittingTime; debug("Timer %p created", &quittingTime); - quittingTime.setInterval(3); + quittingTime.setInterval(3.0); quittingTime.setSingleShot(true); quittingTime.audienceForTrigger() += [&](){ app.quit(12345); }; quittingTime.start(); Timer test; test.audienceForTrigger() += [](){ debug("Testing!"); }; - test.start(1); + test.start(1.0); int code = app.exec(); debug("Event loop returned %i", code); diff --git a/doomsday/tools/shell-text/src/shellapp.cpp b/doomsday/tools/shell-text/src/shellapp.cpp index 2460b1dc5c..7def3c98a0 100644 --- a/doomsday/tools/shell-text/src/shellapp.cpp +++ b/doomsday/tools/shell-text/src/shellapp.cpp @@ -178,7 +178,7 @@ void ShellApp::openConnection(String const &address) LOG_NET_NOTE("Opening connection to %s") << address; // Keep trying to connect to 30 seconds. - d->link = new Link(address, 30); + d->link = new Link(address, 30.0); d->status->setShellLink(d->link); d->link->audienceForPacketsReady() += [this]() { handleIncomingPackets(); }; diff --git a/doomsday/tools/shell-text/src/statuswidget.cpp b/doomsday/tools/shell-text/src/statuswidget.cpp index 70fd12cc4c..ce4ba62a63 100644 --- a/doomsday/tools/shell-text/src/statuswidget.cpp +++ b/doomsday/tools/shell-text/src/statuswidget.cpp @@ -25,16 +25,13 @@ using namespace de::shell; DE_PIMPL(StatusWidget) { - Link * link; + Link * link{nullptr}; Timer updateTimer; String gameMode; String rules; String mapId; - Impl(Public &i) : Base(i), link(nullptr) - { - //updateTimer = new QTimer(thisPublic); - } + Impl(Public * i) : Base(i) {} void refresh() { @@ -43,7 +40,7 @@ DE_PIMPL(StatusWidget) void linkConnected() { - updateTimer.start(1000); + updateTimer.start(1.0); self().redraw(); } @@ -55,7 +52,7 @@ DE_PIMPL(StatusWidget) }; StatusWidget::StatusWidget(String const &name) - : Widget(name), d(new Impl(*this)) + : Widget(name), d(new Impl(this)) { d->updateTimer.audienceForTrigger() += [this]() { d->refresh(); }; } @@ -78,8 +75,8 @@ void StatusWidget::setShellLink(Link *link) void StatusWidget::setGameState(String const &mode, String const &rules, String const &mapId) { d->gameMode = mode; - d->rules = rules; - d->mapId = mapId; + d->rules = rules; + d->mapId = mapId; redraw(); }