From be0442c6207cc10fca079a803bbdd8985959b657 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 11 Sep 2019 06:47:56 -0700 Subject: Avoid C-style casts in C++ --- CMakeLists.txt | 2 +- al/state.cpp | 16 ++++++++-------- alc/alc.cpp | 18 ++++++++---------- alc/backends/sdl2.cpp | 2 +- alc/backends/sndio.cpp | 2 +- alc/backends/wasapi.cpp | 9 ++++++--- alc/backends/winmm.cpp | 8 +++++--- alc/helpers.cpp | 2 +- utils/makemhr/loaddef.cpp | 12 ++++++------ utils/makemhr/loadsofa.cpp | 2 +- utils/sofa-info.cpp | 2 +- 11 files changed, 39 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7626c1ef..c37a2d18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,7 +220,7 @@ IF(MSVC) ENDFOREACH(flag_var) ENDIF() ELSE() - SET(C_FLAGS ${C_FLAGS} -Winline -Wall) + SET(C_FLAGS ${C_FLAGS} -Winline -Wall -Wold-style-cast) CHECK_C_COMPILER_FLAG(-Wextra HAVE_W_EXTRA) IF(HAVE_W_EXTRA) SET(C_FLAGS ${C_FLAGS} -Wextra) diff --git a/al/state.cpp b/al/state.cpp index 64cbae6e..cc5d8bac 100644 --- a/al/state.cpp +++ b/al/state.cpp @@ -371,36 +371,36 @@ START_API_FUNC switch(pname) { case AL_DOPPLER_FACTOR: - value = (ALint64SOFT)context->mDopplerFactor; + value = static_cast(context->mDopplerFactor); break; case AL_DOPPLER_VELOCITY: - value = (ALint64SOFT)context->mDopplerVelocity; + value = static_cast(context->mDopplerVelocity); break; case AL_DISTANCE_MODEL: - value = (ALint64SOFT)context->mDistanceModel; + value = static_cast(context->mDistanceModel); break; case AL_SPEED_OF_SOUND: - value = (ALint64SOFT)context->mSpeedOfSound; + value = static_cast(context->mSpeedOfSound); break; case AL_DEFERRED_UPDATES_SOFT: if(context->mDeferUpdates.load(std::memory_order_acquire)) - value = (ALint64SOFT)AL_TRUE; + value = AL_TRUE; break; case AL_GAIN_LIMIT_SOFT: - value = (ALint64SOFT)(GAIN_MIX_MAX/context->mGainBoost); + value = static_cast(GAIN_MIX_MAX/context->mGainBoost); break; case AL_NUM_RESAMPLERS_SOFT: - value = (ALint64SOFT)(ResamplerMax + 1); + value = static_cast(ResamplerMax + 1); break; case AL_DEFAULT_RESAMPLER_SOFT: - value = (ALint64SOFT)ResamplerDefault; + value = static_cast(ResamplerDefault); break; default: diff --git a/alc/alc.cpp b/alc/alc.cpp index 5f52ca26..10ab3b94 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -214,7 +215,7 @@ BackendFactory *CaptureFactory{}; /************************************************ * Functions, enums, and errors ************************************************/ -#define DECL(x) { #x, (ALCvoid*)(x) } +#define DECL(x) { #x, reinterpret_cast(x) } const struct { const ALCchar *funcName; ALCvoid *address; @@ -1228,14 +1229,11 @@ BOOL APIENTRY DllMain(HINSTANCE module, DWORD reason, LPVOID /*reserved*/) { switch(reason) { - case DLL_PROCESS_ATTACH: - /* Pin the DLL so we won't get unloaded until the process terminates */ - GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - (WCHAR*)module, &module); - break; - - case DLL_PROCESS_DETACH: - break; + case DLL_PROCESS_ATTACH: + /* Pin the DLL so we won't get unloaded until the process terminates */ + GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + reinterpret_cast(module), &module); + break; } return TRUE; } @@ -2087,7 +2085,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList) TRACE("Output limiter enabled, %.4fdB limit\n", thrshld_dB); } - TRACE("Fixed device latency: %ldns\n", (long)device->FixedLatency.count()); + TRACE("Fixed device latency: %" PRId64 "ns\n", int64_t{device->FixedLatency.count()}); /* Need to delay returning failure until replacement Send arrays have been * allocated with the appropriate size. diff --git a/alc/backends/sdl2.cpp b/alc/backends/sdl2.cpp index 29d27c05..4b3b5e63 100644 --- a/alc/backends/sdl2.cpp +++ b/alc/backends/sdl2.cpp @@ -133,7 +133,7 @@ ALCenum Sdl2Backend::open(const ALCchar *name) mDevice->FmtChans = DevFmtStereo; else { - ERR("Got unhandled SDL channel count: %d\n", (int)have.channels); + ERR("Got unhandled SDL channel count: %d\n", int{have.channels}); return ALC_INVALID_VALUE; } switch(have.format) diff --git a/alc/backends/sndio.cpp b/alc/backends/sndio.cpp index 587f67bb..c7a86670 100644 --- a/alc/backends/sndio.cpp +++ b/alc/backends/sndio.cpp @@ -396,7 +396,7 @@ ALCenum SndioCapture::open(const ALCchar *name) (mDevice->FmtType == DevFmtUShort && par.bits == 16 && par.sig == 0) || (mDevice->FmtType == DevFmtInt && par.bits == 32 && par.sig != 0) || (mDevice->FmtType == DevFmtUInt && par.bits == 32 && par.sig == 0)) || - mDevice->channelsFromFmt() != (ALsizei)par.rchan || + mDevice->channelsFromFmt() != static_cast(par.rchan) || mDevice->Frequency != par.rate) { ERR("Failed to set format %s %s %uhz, got %c%u %u-channel %uhz instead\n", diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp index ec1ee936..55c95146 100644 --- a/alc/backends/wasapi.cpp +++ b/alc/backends/wasapi.cpp @@ -81,6 +81,9 @@ DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x namespace { +inline constexpr REFERENCE_TIME operator "" _reftime(unsigned long long int n) noexcept +{ return static_cast(n); } + #define MONO SPEAKER_FRONT_CENTER #define STEREO (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT) #define QUAD (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT) @@ -90,7 +93,7 @@ namespace { #define X7DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT) #define X7DOT1_WIDE (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT|SPEAKER_FRONT_LEFT_OF_CENTER|SPEAKER_FRONT_RIGHT_OF_CENTER) -#define REFTIME_PER_SEC ((REFERENCE_TIME)10000000) +#define REFTIME_PER_SEC 10000000_reftime #define DEVNAME_HEAD "OpenAL Soft on " @@ -1051,7 +1054,7 @@ HRESULT WasapiPlayback::resetProxy() /* Find the nearest multiple of the period size to the update size */ if(min_per < per_time) min_per *= maxi64((per_time + min_per/2) / min_per, 1); - min_len = (UINT32)ScaleCeil(min_per, mDevice->Frequency, REFTIME_PER_SEC); + min_len = static_cast(ScaleCeil(min_per, mDevice->Frequency, REFTIME_PER_SEC)); min_len = minu(min_len, buffer_len/2); mDevice->UpdateSize = min_len; @@ -1680,7 +1683,7 @@ void WasapiCapture::stopProxy() ALCuint WasapiCapture::availableSamples() -{ return (ALCuint)mRing->readSpace(); } +{ return static_cast(mRing->readSpace()); } ALCenum WasapiCapture::captureSamples(void *buffer, ALCuint samples) { diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index b6787a24..76e6fe3b 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -245,7 +245,8 @@ retry_open: mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign; mFormat.cbSize = 0; - MMRESULT res{waveOutOpen(&mOutHdl, DeviceID, &mFormat, (DWORD_PTR)&WinMMPlayback::waveOutProcC, + MMRESULT res{waveOutOpen(&mOutHdl, DeviceID, &mFormat, + reinterpret_cast(&WinMMPlayback::waveOutProcC), reinterpret_cast(this), CALLBACK_FUNCTION)}; if(res != MMSYSERR_NOERROR) { @@ -506,7 +507,8 @@ ALCenum WinMMCapture::open(const ALCchar *name) mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign; mFormat.cbSize = 0; - MMRESULT res{waveInOpen(&mInHdl, DeviceID, &mFormat, (DWORD_PTR)&WinMMCapture::waveInProcC, + MMRESULT res{waveInOpen(&mInHdl, DeviceID, &mFormat, + reinterpret_cast(&WinMMCapture::waveInProcC), reinterpret_cast(this), CALLBACK_FUNCTION)}; if(res != MMSYSERR_NOERROR) { @@ -590,7 +592,7 @@ ALCenum WinMMCapture::captureSamples(void *buffer, ALCuint samples) } ALCuint WinMMCapture::availableSamples() -{ return (ALCuint)mRing->readSpace(); } +{ return static_cast(mRing->readSpace()); } } // namespace diff --git a/alc/helpers.cpp b/alc/helpers.cpp index ba95c0f8..84787637 100644 --- a/alc/helpers.cpp +++ b/alc/helpers.cpp @@ -255,7 +255,7 @@ auto filebuf::underflow() -> int_type { // Read in the next chunk of data, and set the pointers on success DWORD got{}; - if(ReadFile(mFile, mBuffer.data(), (DWORD)mBuffer.size(), &got, nullptr)) + if(ReadFile(mFile, mBuffer.data(), static_cast(mBuffer.size()), &got, nullptr)) setg(mBuffer.data(), mBuffer.data(), mBuffer.data()+got); } if(gptr() == egptr()) diff --git a/utils/makemhr/loaddef.cpp b/utils/makemhr/loaddef.cpp index 89893e38..5f6d76d8 100644 --- a/utils/makemhr/loaddef.cpp +++ b/utils/makemhr/loaddef.cpp @@ -1047,7 +1047,7 @@ static int LoadWaveSource(FILE *fp, SourceRefT *src, const uint hrirRate, const // Load a Spatially Oriented Format for Accoustics (SOFA) file. static MYSOFA_EASY* LoadSofaFile(SourceRefT *src, const uint hrirRate, const uint n) { - struct MYSOFA_EASY *sofa{mysofa_cache_lookup(src->mPath, (float)hrirRate)}; + struct MYSOFA_EASY *sofa{mysofa_cache_lookup(src->mPath, static_cast(hrirRate))}; if(sofa) return sofa; sofa = static_cast(calloc(1, sizeof(*sofa))); @@ -1096,7 +1096,7 @@ static MYSOFA_EASY* LoadSofaFile(SourceRefT *src, const uint hrirRate, const uin fprintf(stderr, "\nError: Out of memory.\n"); return nullptr; } - return mysofa_cache_store(sofa, src->mPath, (float)hrirRate); + return mysofa_cache_store(sofa, src->mPath, static_cast(hrirRate)); } // Copies the HRIR data from a particular SOFA measurement. @@ -1532,7 +1532,7 @@ static int ReadSourceRef(TokenReaderT *tr, SourceRefT *src) src->mType = ET_NONE; src->mSize = 0; src->mBits = 0; - src->mChannel = (uint)intVal; + src->mChannel = static_cast(intVal); src->mSkip = 0; } else if(src->mFormat == SF_WAVE) @@ -1666,7 +1666,7 @@ static int ReadSofaRef(TokenReaderT *tr, SourceRefT *src) TrReadOperator(tr, "@"); if(!TrReadInt(tr, 0, 0x7FFFFFFF, &intVal)) return 0; - src->mOffset = (uint)intVal; + src->mOffset = static_cast(intVal); } else src->mOffset = 0; @@ -1834,13 +1834,13 @@ static int ProcessSources(TokenReaderT *tr, HrirDataT *hData) continue; double ef{(90.0 + aer[1]) * (hData->mFds[fi].mEvCount - 1) / 180.0}; - ei = (uint)std::round(ef); + ei = static_cast(std::round(ef)); ef = (ef - ei) * 180.0f / (hData->mFds[fi].mEvCount - 1); if(std::abs(ef) >= 0.1) continue; double af{aer[0] * hData->mFds[fi].mEvs[ei].mAzCount / 360.0f}; - ai = (uint)std::round(af); + ai = static_cast(std::round(af)); af = (af - ai) * 360.0f / hData->mFds[fi].mEvs[ei].mAzCount; ai = ai % hData->mFds[fi].mEvs[ei].mAzCount; if(std::abs(af) >= 0.1) diff --git a/utils/makemhr/loadsofa.cpp b/utils/makemhr/loadsofa.cpp index 7fb169e2..e82376aa 100644 --- a/utils/makemhr/loadsofa.cpp +++ b/utils/makemhr/loadsofa.cpp @@ -221,7 +221,7 @@ static bool PrepareLayout(const uint m, const float *xyzs, HrirDataT *hData) float ev{90.0f + elems[ei]}; float eif{std::round(ev / step)}; - if(std::fabs(eif - (uint)eif) < (0.1f / step)) + if(std::fabs(eif - static_cast(eif)) < (0.1f / step)) { evStart = static_cast(eif); break; diff --git a/utils/sofa-info.cpp b/utils/sofa-info.cpp index 83e3d065..e9f2257c 100644 --- a/utils/sofa-info.cpp +++ b/utils/sofa-info.cpp @@ -242,7 +242,7 @@ static void PrintCompatibleLayout(const uint m, const float *xyzs) float ev{90.0f + elems[ei]}; float eif{std::round(ev / step)}; - if(std::fabs(eif - (uint)eif) < (0.1f / step)) + if(std::fabs(eif - static_cast(eif)) < (0.1f / step)) { evStart = static_cast(eif); break; -- cgit v1.2.3