Improve usage of static_casts over reinterpret_casts

Static_casts are prefered over reinterpret_casts for better type
safety

Change-Id: I190cbee293591ebf8ab8035e900c081848eb1f30
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/6921
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Rafael Cintron 2019-05-03 00:58:27 +00:00 committed by Commit Bot service account
parent e99e2408e9
commit f54bb68f47
12 changed files with 20 additions and 21 deletions

View File

@ -34,12 +34,12 @@ uint32_t Align(uint32_t value, size_t alignment);
template <typename T>
T* AlignPtr(T* ptr, size_t alignment) {
return reinterpret_cast<T*>(AlignVoidPtr(ptr, alignment));
return static_cast<T*>(AlignVoidPtr(ptr, alignment));
}
template <typename T>
const T* AlignPtr(const T* ptr, size_t alignment) {
return reinterpret_cast<const T*>(AlignVoidPtr(const_cast<T*>(ptr), alignment));
return static_cast<const T*>(AlignVoidPtr(const_cast<T*>(ptr), alignment));
}
template <typename destType, typename sourceType>

View File

@ -22,18 +22,18 @@ DawnSwapChainImplementation CreateSwapChainImplementation(T* swapChain) {
DawnSwapChainImplementation impl = {};
impl.userData = swapChain;
impl.Init = [](void* userData, void* wsiContext) {
auto* ctx = reinterpret_cast<typename T::WSIContext*>(wsiContext);
auto* ctx = static_cast<typename T::WSIContext*>(wsiContext);
reinterpret_cast<T*>(userData)->Init(ctx);
};
impl.Destroy = [](void* userData) { delete reinterpret_cast<T*>(userData); };
impl.Configure = [](void* userData, DawnTextureFormat format, DawnTextureUsageBit allowedUsage,
uint32_t width, uint32_t height) {
return reinterpret_cast<T*>(userData)->Configure(format, allowedUsage, width, height);
return static_cast<T*>(userData)->Configure(format, allowedUsage, width, height);
};
impl.GetNextTexture = [](void* userData, DawnSwapChainNextTexture* nextTexture) {
return reinterpret_cast<T*>(userData)->GetNextTexture(nextTexture);
return static_cast<T*>(userData)->GetNextTexture(nextTexture);
};
impl.Present = [](void* userData) { return reinterpret_cast<T*>(userData)->Present(); };
impl.Present = [](void* userData) { return static_cast<T*>(userData)->Present(); };
return impl;
}

View File

@ -210,7 +210,7 @@ namespace dawn_native {
ASSERT(mLayout->GetBindingInfo().mask[binding]);
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer ||
mLayout->GetBindingInfo().types[binding] == dawn::BindingType::StorageBuffer);
BufferBase* buffer = reinterpret_cast<BufferBase*>(mBindings[binding].Get());
BufferBase* buffer = static_cast<BufferBase*>(mBindings[binding].Get());
return {buffer, mOffsets[binding], mSizes[binding]};
}
@ -219,7 +219,7 @@ namespace dawn_native {
ASSERT(binding < kMaxBindingsPerGroup);
ASSERT(mLayout->GetBindingInfo().mask[binding]);
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::Sampler);
return reinterpret_cast<SamplerBase*>(mBindings[binding].Get());
return static_cast<SamplerBase*>(mBindings[binding].Get());
}
TextureViewBase* BindGroupBase::GetBindingAsTextureView(size_t binding) {
@ -227,7 +227,7 @@ namespace dawn_native {
ASSERT(binding < kMaxBindingsPerGroup);
ASSERT(mLayout->GetBindingInfo().mask[binding]);
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::SampledTexture);
return reinterpret_cast<TextureViewBase*>(mBindings[binding].Get());
return static_cast<TextureViewBase*>(mBindings[binding].Get());
}
} // namespace dawn_native

View File

@ -237,7 +237,7 @@ namespace dawn_native {
mLastAllocationSize =
std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384)));
uint8_t* block = reinterpret_cast<uint8_t*>(malloc(mLastAllocationSize));
uint8_t* block = static_cast<uint8_t*>(malloc(mLastAllocationSize));
if (DAWN_UNLIKELY(block == nullptr)) {
return false;
}

View File

@ -76,11 +76,11 @@ namespace dawn_native {
}
template <typename T>
T* NextCommand() {
return reinterpret_cast<T*>(NextCommand(sizeof(T), alignof(T)));
return static_cast<T*>(NextCommand(sizeof(T), alignof(T)));
}
template <typename T>
T* NextData(size_t count) {
return reinterpret_cast<T*>(NextData(sizeof(T) * count, alignof(T)));
return static_cast<T*>(NextData(sizeof(T) * count, alignof(T)));
}
// Needs to be called if iteration was stopped early.

View File

@ -55,8 +55,7 @@ namespace dawn_native {
T* const* buffers,
uint64_t const* offsets) {
static_assert(std::is_base_of<BufferBase, T>::value, "");
SetVertexBuffers(startSlot, count, reinterpret_cast<BufferBase* const*>(buffers),
offsets);
SetVertexBuffers(startSlot, count, buffers, offsets);
}
void SetVertexBuffers(uint32_t startSlot,
uint32_t count,

View File

@ -44,7 +44,7 @@ namespace dawn_native { namespace d3d12 {
return nullptr;
}
ID3D12Resource* nativeTexture = reinterpret_cast<ID3D12Resource*>(next.texture.ptr);
ID3D12Resource* nativeTexture = static_cast<ID3D12Resource*>(next.texture.ptr);
return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture);
}

View File

@ -67,7 +67,7 @@ namespace dawn_native { namespace opengl {
ASSERT(optionsBase->backendType == BackendType::OpenGL);
const AdapterDiscoveryOptions* options =
reinterpret_cast<const AdapterDiscoveryOptions*>(optionsBase);
static_cast<const AdapterDiscoveryOptions*>(optionsBase);
if (options->getProc == nullptr) {
return DAWN_VALIDATION_ERROR("AdapterDiscoveryOptions::getProc must be set");

View File

@ -132,7 +132,7 @@ namespace dawn_native { namespace opengl {
}
const char* ShaderModule::GetSource() const {
return reinterpret_cast<const char*>(mGlslSource.data());
return mGlslSource.c_str();
}
const ShaderModule::CombinedSamplerInfo& ShaderModule::GetCombinedSamplerInfo() const {

View File

@ -121,7 +121,7 @@ namespace dawn_native { namespace vulkan {
allocation->mMemory = allocatedMemory;
allocation->mOffset = 0;
allocation->mMappedPointer = reinterpret_cast<uint8_t*>(mappedPointer);
allocation->mMappedPointer = static_cast<uint8_t*>(mappedPointer);
return true;
}

View File

@ -114,7 +114,7 @@ namespace dawn_wire { namespace server {
cmd.requestSerial = data->requestSerial;
cmd.status = status;
cmd.dataLength = 0;
cmd.data = reinterpret_cast<const uint8_t*>(ptr);
cmd.data = static_cast<const uint8_t*>(ptr);
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
cmd.dataLength = dataLength;

View File

@ -545,7 +545,7 @@ void DawnTest::ResolveExpectations() {
// Get a pointer to the mapped copy of the data for the expectation.
const char* data =
reinterpret_cast<const char*>(mReadbackSlots[expectation.readbackSlot].mappedData);
static_cast<const char*>(mReadbackSlots[expectation.readbackSlot].mappedData);
data += expectation.readbackOffset;
uint32_t size;
@ -652,7 +652,7 @@ namespace detail {
testing::AssertionResult ExpectEq<T>::Check(const void* data, size_t size) {
DAWN_ASSERT(size == sizeof(T) * mExpected.size());
const T* actual = reinterpret_cast<const T*>(data);
const T* actual = static_cast<const T*>(data);
testing::AssertionResult failure = testing::AssertionFailure();
for (size_t i = 0; i < mExpected.size(); ++i) {