Skip to content

Commit

Permalink
Tidy: Fix const-ness of variables and functions.
Browse files Browse the repository at this point in the history
The const_cast conversion should never be used.  Casting a const
variable to a non-const variable and then operating on it is undefined
behavior.

One surprising revelation is that calling MythTimer::elapsed to see if
the timer has expired can restart the timer.  Changing this function
take a non-const timer causes ripples through the code.

Another changes that causes a few ripples is that
ProgramInfo::SetPathname modifies the ProgramInfo object.

https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-const-cast.html
  • Loading branch information
linuxdude42 committed Dec 1, 2019
1 parent 6c1b08a commit 272b843
Show file tree
Hide file tree
Showing 31 changed files with 52 additions and 54 deletions.
10 changes: 5 additions & 5 deletions mythtv/libs/libmyth/programinfo.cpp
Expand Up @@ -2348,12 +2348,12 @@ static ProgramInfoType discover_program_info_type(
return pit;
}

void ProgramInfo::SetPathname(const QString &pn) const
void ProgramInfo::SetPathname(const QString &pn)
{
m_pathname = pn;

ProgramInfoType pit = discover_program_info_type(m_chanid, m_pathname, false);
const_cast<ProgramInfo*>(this)->SetProgramInfoType(pit);
SetProgramInfoType(pit);
}

ProgramInfoType ProgramInfo::DiscoverProgramInfoType(void) const
Expand Down Expand Up @@ -2457,7 +2457,7 @@ QString ProgramInfo::QueryBasename(void) const
* call and so should not be called from the UI thread.
*/
QString ProgramInfo::GetPlaybackURL(
bool checkMaster, bool forceCheckLocal) const
bool checkMaster, bool forceCheckLocal)
{
// return the original path if BD or DVD URI
if (IsVideoBD() || IsVideoDVD())
Expand Down Expand Up @@ -4743,7 +4743,7 @@ void ProgramInfo::SaveInetRef(const QString &inet)
* call and so should not be called from the UI thread.
* \return true iff file is readable
*/
bool ProgramInfo::IsFileReadable(void) const
bool ProgramInfo::IsFileReadable(void)
{
if (IsLocal() && QFileInfo(m_pathname).isReadable())
return true;
Expand Down Expand Up @@ -4813,7 +4813,7 @@ uint ProgramInfo::QueryTranscoderID(void) const
* \note This method sometimes initiates a QUERY_CHECKFILE MythProto
* call and so should not be called from the UI thread.
*/
QString ProgramInfo::DiscoverRecordingDirectory(void) const
QString ProgramInfo::DiscoverRecordingDirectory(void)
{
if (!IsLocal())
{
Expand Down
8 changes: 4 additions & 4 deletions mythtv/libs/libmyth/programinfo.h
Expand Up @@ -348,7 +348,7 @@ class MPUBLIC ProgramInfo
bool IsMythStream(void) const { return m_pathname.startsWith("myth://"); }
bool IsPathSet(void) const { return GetBasename() != m_pathname; }
bool HasPathname(void) const { return !GetPathname().isEmpty(); }
bool IsFileReadable(void) const;
bool IsFileReadable(void);

QString GetTitle(void) const { return m_title; }
QString GetSortTitle(void) const { return m_sortTitle; }
Expand Down Expand Up @@ -503,7 +503,7 @@ class MPUBLIC ProgramInfo
void SetSubtitle(const QString &st, const QString &sst = nullptr);
void SetProgramInfoType(ProgramInfoType t)
{ m_programflags &= ~FL_TYPEMASK; m_programflags |= ((uint32_t)t<<20); }
void SetPathname(const QString&) const;
void SetPathname(const QString&);
void SetChanID(uint _chanid) { m_chanid = _chanid; }
void SetScheduledStartTime(const QDateTime &dt) { m_startts = dt; }
void SetScheduledEndTime( const QDateTime &dt) { m_endts = dt; }
Expand Down Expand Up @@ -626,9 +626,9 @@ class MPUBLIC ProgramInfo
void SaveInetRef(const QString &inet);

// Extremely slow functions that cannot be called from the UI thread.
QString DiscoverRecordingDirectory(void) const;
QString DiscoverRecordingDirectory(void);
QString GetPlaybackURL(bool checkMaster = false,
bool forceCheckLocal = false) const;
bool forceCheckLocal = false);
ProgramInfoType DiscoverProgramInfoType(void) const;

// Edit flagging map
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmyth/remoteutil.cpp
Expand Up @@ -87,7 +87,7 @@ bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
return false;
}

bool RemoteCheckFile(const ProgramInfo *pginfo, bool checkSlaves)
bool RemoteCheckFile(ProgramInfo *pginfo, bool checkSlaves)
{
QStringList strlist("QUERY_CHECKFILE");
strlist << QString::number((int)checkSlaves);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmyth/remoteutil.h
Expand Up @@ -20,7 +20,7 @@ MPUBLIC bool RemoteGetUptime(time_t &uptime);
MPUBLIC
bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM);
MPUBLIC bool RemoteCheckFile(
const ProgramInfo *pginfo, bool checkSlaves = true);
ProgramInfo *pginfo, bool checkSlaves = true);
MPUBLIC bool RemoteDeleteRecording( uint recordingID, bool forceMetadataDelete,
bool forgetHistory);
MPUBLIC
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythbase/mythsocket.cpp
Expand Up @@ -564,7 +564,7 @@ bool MythSocket::IsConnected(void) const
return m_connected;
}

bool MythSocket::IsDataAvailable(void) const
bool MythSocket::IsDataAvailable(void)
{
if (QThread::currentThread() == m_thread->qthread())
return m_tcpSocket->bytesAvailable() > 0;
Expand All @@ -575,7 +575,7 @@ bool MythSocket::IsDataAvailable(void) const
bool ret = false;

QMetaObject::invokeMethod(
const_cast<MythSocket*>(this), "IsDataAvailableReal",
this, "IsDataAvailableReal",
Qt::BlockingQueuedConnection,
Q_ARG(bool*, &ret));

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythsocket.h
Expand Up @@ -57,7 +57,7 @@ class MBASE_PUBLIC MythSocket : public QObject, public ReferenceCounter
bool WriteStringList(const QStringList &list);

bool IsConnected(void) const;
bool IsDataAvailable(void) const;
bool IsDataAvailable(void);

QHostAddress GetPeerAddress(void) const;
int GetPeerPort(void) const;
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythbase/mythtimer.cpp
Expand Up @@ -87,7 +87,7 @@ void MythTimer::stop(void)
* \note If addMSecs() has been called and the total offset applied
* is negative then this can return a negative number.
*/
int MythTimer::elapsed(void) const
int MythTimer::elapsed(void)
{
if (!m_timer.isValid())
{
Expand All @@ -100,7 +100,7 @@ int MythTimer::elapsed(void) const
qint64 e = m_timer.elapsed();
if (!QElapsedTimer::isMonotonic() && (e > 86300000))
{
const_cast<MythTimer*>(this)->start();
start();
e = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythtimer.h
Expand Up @@ -26,7 +26,7 @@ class MBASE_PUBLIC MythTimer

void addMSecs(int ms);

int elapsed(void) const;
int elapsed(void);
int64_t nsecsElapsed(void) const;
bool isRunning(void) const;

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/DVD/mythdvdplayer.cpp
Expand Up @@ -470,7 +470,7 @@ long long MythDVDPlayer::CalcMaxFFTime(long long ff, bool setjump) const
return MythPlayer::CalcMaxFFTime(ff, setjump);
}

int64_t MythDVDPlayer::GetSecondsPlayed(bool /*honorCutList*/, int divisor) const
int64_t MythDVDPlayer::GetSecondsPlayed(bool /*honorCutList*/, int divisor)
{
if (!player_ctx->m_buffer->IsDVD())
return 0;
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/DVD/mythdvdplayer.h
Expand Up @@ -24,7 +24,7 @@ class MythDVDPlayer : public MythPlayer
// Gets
uint64_t GetBookmark(void) override; // MythPlayer
int64_t GetSecondsPlayed(bool honorCutList,
int divisor = 1000) const override; // MythPlayer
int divisor = 1000) override; // MythPlayer
int64_t GetTotalSeconds(bool honorCutList,
int divisor = 1000) const override; // MythPlayer

Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/decoders/avformatdecoder.cpp
Expand Up @@ -3049,8 +3049,8 @@ void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
{
if (bufptr + 11 >= pkt->data + pkt->size)
continue; // not enough valid data...
SequenceHeader *seq = reinterpret_cast<SequenceHeader*>(
const_cast<uint8_t*>(bufptr));
const SequenceHeader *seq =
reinterpret_cast<const SequenceHeader*>(bufptr);

int width = static_cast<int>(seq->width()) >> context->lowres;
int height = static_cast<int>(seq->height()) >> context->lowres;
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/dvdstream.cpp
Expand Up @@ -96,7 +96,7 @@ bool DVDStream::OpenFile(const QString &filename, uint /*retry_ms*/)
{
// Locate the start block of the requested title
uint32_t len;
m_start = UDFFindFile(m_reader, const_cast<char*>(qPrintable(path)), &len);
m_start = UDFFindFile(m_reader, qPrintable(path), &len);
if (m_start == 0)
{
LOG(VB_GENERAL, LOG_ERR, QString("DVDStream(%1) UDFFindFile(%2) failed").
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/livetvchain.cpp
Expand Up @@ -706,10 +706,10 @@ void LiveTVChain::SetHostSocket(MythSocket *sock)
m_inUseSocks.append(sock);
}

bool LiveTVChain::IsHostSocket(const MythSocket *sock) const
bool LiveTVChain::IsHostSocket(MythSocket *sock)
{
QMutexLocker lock(&m_sockLock);
return m_inUseSocks.contains(const_cast<MythSocket*>(sock));
return m_inUseSocks.contains(sock);
}

uint LiveTVChain::HostSocketCount(void) const
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/livetvchain.h
Expand Up @@ -85,7 +85,7 @@ class MTV_PUBLIC LiveTVChain : public ReferenceCounter

// socket stuff
void SetHostSocket(MythSocket *sock);
bool IsHostSocket(const MythSocket *sock) const;
bool IsHostSocket(MythSocket *sock);
uint HostSocketCount(void) const;
void DelHostSocket(MythSocket *sock);

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythccextractorplayer.cpp
Expand Up @@ -663,7 +663,7 @@ void MythCCExtractorPlayer::IngestDVBSubtitles(void)

while (!avsubtitles->m_buffers.empty())
{
const AVSubtitle subtitle = avsubtitles->m_buffers.front();
AVSubtitle subtitle = avsubtitles->m_buffers.front();
avsubtitles->m_buffers.pop_front();

const QSize v_size =
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythplayer.cpp
Expand Up @@ -5066,7 +5066,7 @@ void MythPlayer::GetPlaybackData(InfoMap &infoMap)
GetCodecDescription(infoMap);
}

int64_t MythPlayer::GetSecondsPlayed(bool honorCutList, int divisor) const
int64_t MythPlayer::GetSecondsPlayed(bool honorCutList, int divisor)
{
int64_t pos = TranslatePositionFrameToMs(framesPlayed, honorCutList);
LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/mythplayer.h
Expand Up @@ -212,7 +212,7 @@ class MTV_PUBLIC MythPlayer
// divisor can be passed in as an argument, e.g. pass divisor=1 to
// return the time in milliseconds.
virtual int64_t GetSecondsPlayed(bool honorCutList,
int divisor = 1000) const;
int divisor = 1000);
virtual int64_t GetTotalSeconds(bool honorCutList,
int divisor = 1000) const;
int64_t GetLatestVideoTimecode() const { return m_latestVideoTimecode; }
Expand Down
5 changes: 2 additions & 3 deletions mythtv/libs/libmythtv/recorders/recorderbase.cpp
Expand Up @@ -839,7 +839,7 @@ void RecorderBase::SetTotalFrames(uint64_t total_frames)
RecorderBase *RecorderBase::CreateRecorder(
TVRec *tvrec,
ChannelBase *channel,
const RecordingProfile &profile,
RecordingProfile &profile,
const GeneralDBOptions &genOpt)
{
if (!channel)
Expand Down Expand Up @@ -957,8 +957,7 @@ RecorderBase *RecorderBase::CreateRecorder(

if (recorder)
{
recorder->SetOptionsFromProfile(
const_cast<RecordingProfile*>(&profile),
recorder->SetOptionsFromProfile(&profile,
genOpt.m_videoDev, genOpt.m_audioDev, genOpt.m_vbiDev);
// Override the samplerate defined in the profile if this card
// was configured with a fixed rate.
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/recorders/recorderbase.h
Expand Up @@ -245,7 +245,7 @@ class MTV_PUBLIC RecorderBase : public QRunnable
static RecorderBase *CreateRecorder(
TVRec *tvrec,
ChannelBase *channel,
const RecordingProfile &profile,
RecordingProfile &profile,
const GeneralDBOptions &genOpt);

protected:
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/subtitlereader.cpp
Expand Up @@ -23,7 +23,7 @@ void SubtitleReader::EnableRawTextSubtitles(bool enable)
m_RawTextSubtitlesEnabled = enable;
}

bool SubtitleReader::AddAVSubtitle(const AVSubtitle &subtitle,
bool SubtitleReader::AddAVSubtitle(AVSubtitle &subtitle,
bool fix_position,
bool allow_forced)
{
Expand Down Expand Up @@ -79,9 +79,9 @@ void SubtitleReader::ClearAVSubtitles(void)
m_AVSubtitles.m_lock.unlock();
}

void SubtitleReader::FreeAVSubtitle(const AVSubtitle &subtitle)
void SubtitleReader::FreeAVSubtitle(AVSubtitle &subtitle)
{
avsubtitle_free(const_cast<AVSubtitle*>(&subtitle));
avsubtitle_free(&subtitle);
}

void SubtitleReader::LoadExternalSubtitles(const QString &subtitleFileName,
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythtv/subtitlereader.h
Expand Up @@ -40,10 +40,10 @@ class SubtitleReader
void EnableRawTextSubtitles(bool enable);

AVSubtitles* GetAVSubtitles(void) { return &m_AVSubtitles; }
bool AddAVSubtitle(const AVSubtitle& subtitle, bool fix_position,
bool AddAVSubtitle(AVSubtitle& subtitle, bool fix_position,
bool allow_forced);
void ClearAVSubtitles(void);
static void FreeAVSubtitle(const AVSubtitle &sub);
static void FreeAVSubtitle(AVSubtitle &sub);

TextSubtitles* GetTextSubtitles(void) { return &m_TextSubtitles; }
bool HasTextSubtitles(void);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/subtitlescreen.cpp
Expand Up @@ -1815,7 +1815,7 @@ void SubtitleScreen::DisplayAVSubtitles(void)

while (!subs->m_buffers.empty())
{
const AVSubtitle subtitle = subs->m_buffers.front();
AVSubtitle subtitle = subs->m_buffers.front();
if (subtitle.start_display_time > currentFrame->timecode)
break;

Expand Down
5 changes: 2 additions & 3 deletions mythtv/libs/libmythtv/tv_play.cpp
Expand Up @@ -8052,7 +8052,7 @@ void TV::UpdateOSDInput(const PlayerContext *ctx)
/** \fn TV::UpdateOSDSignal(const PlayerContext*, const QStringList&)
* \brief Updates Signal portion of OSD...
*/
void TV::UpdateOSDSignal(const PlayerContext *ctx, const QStringList &strlist)
void TV::UpdateOSDSignal(PlayerContext *ctx, const QStringList &strlist)
{
OSD *osd = GetOSDLock(ctx);
if (!osd || m_browseHelper->IsBrowsing() || !m_queuedChanNum.isEmpty())
Expand All @@ -8062,8 +8062,7 @@ void TV::UpdateOSDSignal(const PlayerContext *ctx, const QStringList &strlist)
ReturnOSDLock(ctx, osd);

QMutexLocker locker(&m_timerIdLock);
m_signalMonitorTimerId[StartTimer(1, __LINE__)] =
const_cast<PlayerContext*>(ctx);
m_signalMonitorTimerId[StartTimer(1, __LINE__)] = ctx;
return;
}
ReturnOSDLock(ctx, osd);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythtv/tv_play.h
Expand Up @@ -624,7 +624,7 @@ class MTV_PUBLIC TV : public QObject, public MenuItemDisplayer
void UpdateOSDSeekMessage(const PlayerContext*,
const QString &mesg, enum OSDTimeout timeout);
void UpdateOSDInput(const PlayerContext*);
void UpdateOSDSignal(const PlayerContext*, const QStringList &strlist);
void UpdateOSDSignal(PlayerContext*, const QStringList &strlist);
void UpdateOSDTimeoutMessage(PlayerContext*);
void UpdateOSDAskAllowDialog(PlayerContext*);
void SetUpdateOSDPosition(bool set_it);
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythui/lirc.cpp
Expand Up @@ -339,14 +339,14 @@ bool LIRC::IsDoRunSet(void) const
return doRun;
}

void LIRC::Process(const QByteArray &data)
void LIRC::Process(QByteArray &data)
{
QMutexLocker static_lock(&lirclib_lock);

// lirc_code2char will make code point to a static datafer..
char *code = nullptr;
int ret = lirc_code2char(
d->m_lircState, d->m_lircConfig, const_cast<char*>(data.constData()), &code);
d->m_lircState, d->m_lircConfig, data.data(), &code);

while ((0 == ret) && code)
{
Expand Down Expand Up @@ -401,7 +401,7 @@ void LIRC::Process(const QByteArray &data)
QCoreApplication::postEvent(m_mainWindow, keyReleases[i]);

ret = lirc_code2char(
d->m_lircState, d->m_lircConfig, const_cast<char*>(data.constData()), &code);
d->m_lircState, d->m_lircConfig, data.data(), &code);
}
}

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythui/lirc.h
Expand Up @@ -41,7 +41,7 @@ class LIRC : public QObject, public MThread
bool IsDoRunSet(void) const;
void run(void) override; // MThread
QList<QByteArray> GetCodes(void);
void Process(const QByteArray &data);
void Process(QByteArray &data);

mutable QMutex lock;
static QMutex lirclib_lock;
Expand Down
8 changes: 4 additions & 4 deletions mythtv/libs/libmythui/mythuiimage.cpp
Expand Up @@ -478,7 +478,7 @@ QEvent::Type ImageLoadEvent::kEventType =
class ImageLoadThread : public QRunnable
{
public:
ImageLoadThread(const MythUIImage *parent, MythPainter *painter,
ImageLoadThread(MythUIImage *parent, MythPainter *painter,
const ImageProperties &imProps, QString basefile,
int number, ImageCacheMode mode) :
m_parent(parent), m_painter(painter), m_imageProperties(imProps),
Expand Down Expand Up @@ -507,7 +507,7 @@ class ImageLoadThread : public QRunnable
m_basefile,
m_imageProperties.m_filename,
aborted);
QCoreApplication::postEvent(const_cast<MythUIImage*>(m_parent), le);
QCoreApplication::postEvent(m_parent, le);

return;
}
Expand All @@ -522,11 +522,11 @@ class ImageLoadThread : public QRunnable
ImageLoadEvent *le = new ImageLoadEvent(m_parent, image, m_basefile,
m_imageProperties.m_filename,
m_number, aborted);
QCoreApplication::postEvent(const_cast<MythUIImage*>(m_parent), le);
QCoreApplication::postEvent(m_parent, le);
}

private:
const MythUIImage *m_parent {nullptr};
MythUIImage *m_parent {nullptr};
MythPainter *m_painter {nullptr};
ImageProperties m_imageProperties;
QString m_basefile;
Expand Down

0 comments on commit 272b843

Please sign in to comment.