Skip to content

Commit

Permalink
Use std::chrono literals whenever possible.
Browse files Browse the repository at this point in the history
The "chrono" header file includes literal strings that can be use to
mark a number as a time interval. This lets you write function calls
like this:

    sleep_for(std::chrono::milliseconds(25))

much more simply as:

    sleep_for(25ms)

Take advantage of this wherever possible.
  • Loading branch information
linuxdude42 committed Jan 22, 2021
1 parent 93f2eec commit b252506
Show file tree
Hide file tree
Showing 95 changed files with 183 additions and 231 deletions.
2 changes: 0 additions & 2 deletions mythplugins/mytharchive/mytharchive/logviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "archiveutil.h"
#include "logviewer.h"

using namespace std::chrono_literals;

void showLogViewer(void)
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mytharchive/mytharchive/videoselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include "archiveutil.h"
#include "videoselector.h"

using namespace std::chrono_literals;

VideoSelector::VideoSelector(MythScreenStack *parent, QList<ArchiveItem *> *archiveList)
:MythScreenType(parent, "VideoSelector"),
m_archiveList(archiveList)
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythmusic/mythmusic/avfdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ extern "C" {
#include "libavutil/opt.h"
}

using namespace std::chrono_literals;

/****************************************************************************/

using ShoutCastMetaMap = QMap<QString,QString>;
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythmusic/mythmusic/cdrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
#include "mythlogging.h"
#include "vorbisencoder.h"

using namespace std::chrono_literals;

#ifdef HAVE_CDIO
// libparanoia compatibility
#ifndef cdrom_paranoia
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythmusic/mythmusic/miniplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include <QTimer>

using namespace std::chrono_literals;


MiniPlayer::MiniPlayer(MythScreenStack *parent)
: MusicCommon(parent, nullptr, "music_miniplayer")
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythmusic/mythmusic/streamview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include "musicplayer.h"
#include "streamview.h"

using namespace std::chrono_literals;

StreamView::StreamView(MythScreenStack *parent, MythScreenType *parentScreen)
: MusicCommon(parent, parentScreen, "streamview")
{
Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythzoneminder/mythzoneminder/zmconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include "zmclient.h"
#include "zmconsole.h"

using namespace std::chrono_literals;

constexpr std::chrono::milliseconds STATUS_UPDATE_TIME { 10s }; // update the status every 10 seconds
constexpr std::chrono::milliseconds TIME_UPDATE_TIME { 1s }; // update the time every 1 second

Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythzoneminder/mythzoneminder/zmminiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// mythzoneminder
#include "zmclient.h"

using namespace std::chrono_literals;

// the maximum image size we are ever likely to get from ZM
#define MAX_IMAGE_SIZE (2048*1536*3)

Expand Down
2 changes: 0 additions & 2 deletions mythplugins/mythzoneminder/mythzoneminder/zmplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include "zmclient.h"
#include "zmplayer.h"

using namespace std::chrono_literals;

ZMPlayer::ZMPlayer(MythScreenStack *parent, const char *name,
std::vector<Event *> *eventList, size_t *currentEvent)
:MythScreenType(parent, name),
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmyth/guistartup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@

#include "guistartup.h"

using namespace std::chrono_literals;

GUIStartup::GUIStartup(MythScreenStack *parent, QEventLoop *eventLoop)
:MythScreenType(parent, "GUIStartup"),
m_loop(eventLoop),
Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmyth/guistartup.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
// MythDB headers
#include "mythexp.h"

// MythBase headers
#include "mythchrono.h"

// MythUI headers
#include "mythscreentype.h"

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmyth/mythcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ bool MythContextPrivate::PromptForDatabaseParams(const QString &error)
{
DatabaseParams params = m_parent->GetDatabaseParams();
QString response;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(1s);
// give user chance to skip config
std::cout << std::endl << error.toLocal8Bit().constData() << std::endl << std::endl;
response = getResponse("Would you like to configure the database "
Expand Down Expand Up @@ -935,7 +935,7 @@ QString MythContextPrivate::TestDBconnection(bool prompt)
for (int temp = 0; temp < useTimeout * 2 ; temp++)
{
processEvents();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(500ms);
}
break;
}
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmythbase/housekeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
#include "housekeeper.h"
#include "mythcorecontext.h"

using namespace std::chrono_literals;

/** \class HouseKeeperTask
* \ingroup housekeeper
* \brief Definition for a single task to be run by the HouseKeeper
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmythbase/lcddevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include "mythsystemlegacy.h"
#include "exitcodes.h"

using namespace std::chrono_literals;


#define LOC QString("LCDdevice: ")

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythbase/libmythbase.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ INSTALLS = target
QMAKE_CLEAN += $(TARGET) $(TARGETA) $(TARGETD) $(TARGET0) $(TARGET1) $(TARGET2)

# Input
HEADERS += mthread.h mthreadpool.h
HEADERS += mthread.h mthreadpool.h mythchrono.h
HEADERS += mythsocket.h mythsocket_cb.h
HEADERS += mythbaseexp.h mythdbcon.h mythdb.h mythdbparams.h
HEADERS += verbosedefs.h mythversion.h compat.h mythconfig.h
Expand Down Expand Up @@ -89,7 +89,7 @@ inc.files += mythcoreutil.h mythlocale.h mythdownloadmanager.h
inc.files += mythtranslation.h iso639.h iso3166.h mythmedia.h mythmiscutil.h
inc.files += mythcdrom.h autodeletedeque.h dbutil.h mythdeque.h
inc.files += referencecounter.h referencecounterlist.h mythcommandlineparser.h
inc.files += mthread.h mthreadpool.h
inc.files += mthread.h mthreadpool.h mythchrono.h
inc.files += filesysteminfo.h hardwareprofile.h bonjourregister.h serverpool.h
inc.files += plist.h bswap.h signalhandling.h ffmpeg-mmx.h mythdate.h
inc.files += mythplugin.h mythpluginapi.h mythqtcompat.h
Expand Down
13 changes: 13 additions & 0 deletions mythtv/libs/libmythbase/mythchrono.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __MYTHCHRONO_H__
#define __MYTHCHRONO_H__

#include <cmath>
#include <sys/time.h> // For gettimeofday

// Include the std::chrono definitions, and set up to parse the
// std::chrono literal suffixes.
#include <chrono>
using std::chrono::duration_cast;
using namespace std::chrono_literals;

#endif // __MYTHCHRONO_H__
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/mythdbparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QString>

#include "mythbaseexp.h"
#include "mythchrono.h"

/// Structure containing the basic Database parameters
class MBASE_PUBLIC DatabaseParams
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/mythsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

// MythTV headers
#include "mythbaseexp.h"
#include "mythchrono.h"

enum MythSystemFlag {
kMSNone = 0x00000000,
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythbase/mythsystemwindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void MythSystemLegacyIOHandler::run(void)

while( run_system )
{
usleep(10000); // ~100x per second, for ~3MBps throughput
usleep(10ms); // ~100x per second, for ~3MBps throughput
m_pLock.lock();
if( m_pMap.isEmpty() )
{
Expand Down Expand Up @@ -205,7 +205,7 @@ void MythSystemLegacyIOHandler::Wait(HANDLE h)
while (m_pMap.contains(h))
{
locker.unlock();
usleep(10 * 1000);
usleep(10ms);
locker.relock();
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ void MythSystemLegacyManager::run(void)
if( m_childCount == 0 )
{
m_mapLock.unlock();
usleep( 100000 );
usleep( 100ms );
continue;
}

Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/mythtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cinttypes>
#include <QElapsedTimer>
#include "mythbaseexp.h"
#include "mythchrono.h"

/** A QElapsedTimer based timer to replace use of QTime as a timer.
*
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/portchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ bool PortChecker::checkPort(QString &host, int port, int timeLimit, bool linkLoc
// Check if user got impatient and canceled
if (m_cancelCheck)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(500ms);
state = socket.state();
LOG(VB_GENERAL, LOG_DEBUG, LOC + QString("socket state %1")
.arg(state));
Expand Down
22 changes: 11 additions & 11 deletions mythtv/libs/libmythbase/test/test_mythtimer/test_mythtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,49 @@ class TestMythTimer: public QObject
{
MythTimer t;
t.start();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
}

static void TimeElapsesAfterRestart(void)
{
MythTimer t;
t.restart();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
}

static void TimeDoesNotElapseImmediatelyAfterConstructionByDefault(void)
{
MythTimer t;
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() == 0);
}

static void TimeDoesNotElapsesImmediatelyAfterContructionIfIntended(void)
{
MythTimer t(MythTimer::kStartRunning);
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
}

static void TimeElapsesContinually(void)
{
MythTimer t;
t.start();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(500ms);
QVERIFY(t.elapsed() > 1000);
}

static void TimeResetsOnRestart(void)
{
MythTimer t;
t.start();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.restart() > 500);
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500 && t.elapsed() < 750);
}

Expand All @@ -98,7 +98,7 @@ class TestMythTimer: public QObject
MythTimer t;
t.start();
t.addMSecs(-250);
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 250 && t.elapsed() < 500);
}

Expand All @@ -107,7 +107,7 @@ class TestMythTimer: public QObject
MythTimer t;
t.addMSecs(-250);
t.start();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
}

Expand All @@ -116,7 +116,7 @@ class TestMythTimer: public QObject
MythTimer t;
t.addMSecs(-250);
t.restart();
std::this_thread::sleep_for(std::chrono::milliseconds(520));
std::this_thread::sleep_for(520ms);
QVERIFY(t.elapsed() > 500);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "requesthandler/outboundhandler.h"

using namespace std::chrono_literals;

OutboundRequestHandler::OutboundRequestHandler(void)
{
m_timer.setSingleShot(true);
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmythtv/AirPlay/mythairplayserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "bonjourregister.h"
#include "mythairplayserver.h"

using namespace std::chrono_literals;

MythAirplayServer* MythAirplayServer::gMythAirplayServer = nullptr;
MThread* MythAirplayServer::gMythAirplayServerThread = nullptr;
QMutex* MythAirplayServer::gMythAirplayServerMutex = new QMutex(QMutex::Recursive);
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmythtv/AirPlay/mythraopconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include "mythmainwindow.h"

using namespace std::chrono_literals;

#define LOC QString("RAOP Conn: ")
#define MAX_PACKET_SIZE 2048

Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/Bluray/mythbdbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ void MythBDBuffer::HandleBDEvent(BD_EVENT &Event)
case BD_EVENT_STILL_TIME:
// we use the clip information to determine the still frame status
// sleep a little
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
break;
case BD_EVENT_SEEK:
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("EVENT_SEEK"));
Expand Down Expand Up @@ -1132,7 +1132,7 @@ void MythBDBuffer::HandleBDEvent(BD_EVENT &Event)
case BD_EVENT_IDLE:
/* Nothing to do. Playlist is not playing, but title applet is running.
* Application should not call bd_read*() immediately again to avoid busy loop. */
std::this_thread::sleep_for(std::chrono::milliseconds(40));
std::this_thread::sleep_for(40ms);
break;

case BD_EVENT_MENU:
Expand Down Expand Up @@ -1227,7 +1227,7 @@ void MythBDBuffer::WaitForPlayer(void)
m_playerWait = true;
int count = 0;
while (m_playerWait && count++ < 200)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
if (m_playerWait)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Player wait state was not cleared");
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/Bluray/mythbdplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool MythBDPlayer::VideoLoop(void)
if (nbframes == 0)
{
LOG(VB_PLAYBACK, LOG_WARNING, LOC + "Warning: In BD Still but no video frames in queue");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
return !IsErrored();
}

Expand Down
Loading

0 comments on commit b252506

Please sign in to comment.