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:
parent
e99e2408e9
commit
f54bb68f47
|
@ -34,12 +34,12 @@ uint32_t Align(uint32_t value, size_t alignment);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* AlignPtr(T* ptr, size_t alignment) {
|
T* AlignPtr(T* ptr, size_t alignment) {
|
||||||
return reinterpret_cast<T*>(AlignVoidPtr(ptr, alignment));
|
return static_cast<T*>(AlignVoidPtr(ptr, alignment));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T* AlignPtr(const T* ptr, size_t alignment) {
|
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>
|
template <typename destType, typename sourceType>
|
||||||
|
|
|
@ -22,18 +22,18 @@ DawnSwapChainImplementation CreateSwapChainImplementation(T* swapChain) {
|
||||||
DawnSwapChainImplementation impl = {};
|
DawnSwapChainImplementation impl = {};
|
||||||
impl.userData = swapChain;
|
impl.userData = swapChain;
|
||||||
impl.Init = [](void* userData, void* wsiContext) {
|
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);
|
reinterpret_cast<T*>(userData)->Init(ctx);
|
||||||
};
|
};
|
||||||
impl.Destroy = [](void* userData) { delete reinterpret_cast<T*>(userData); };
|
impl.Destroy = [](void* userData) { delete reinterpret_cast<T*>(userData); };
|
||||||
impl.Configure = [](void* userData, DawnTextureFormat format, DawnTextureUsageBit allowedUsage,
|
impl.Configure = [](void* userData, DawnTextureFormat format, DawnTextureUsageBit allowedUsage,
|
||||||
uint32_t width, uint32_t height) {
|
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) {
|
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;
|
return impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -210,7 +210,7 @@ namespace dawn_native {
|
||||||
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
||||||
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer ||
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer ||
|
||||||
mLayout->GetBindingInfo().types[binding] == dawn::BindingType::StorageBuffer);
|
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]};
|
return {buffer, mOffsets[binding], mSizes[binding]};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ namespace dawn_native {
|
||||||
ASSERT(binding < kMaxBindingsPerGroup);
|
ASSERT(binding < kMaxBindingsPerGroup);
|
||||||
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
||||||
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::Sampler);
|
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) {
|
TextureViewBase* BindGroupBase::GetBindingAsTextureView(size_t binding) {
|
||||||
|
@ -227,7 +227,7 @@ namespace dawn_native {
|
||||||
ASSERT(binding < kMaxBindingsPerGroup);
|
ASSERT(binding < kMaxBindingsPerGroup);
|
||||||
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
||||||
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::SampledTexture);
|
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
|
} // namespace dawn_native
|
||||||
|
|
|
@ -237,7 +237,7 @@ namespace dawn_native {
|
||||||
mLastAllocationSize =
|
mLastAllocationSize =
|
||||||
std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384)));
|
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)) {
|
if (DAWN_UNLIKELY(block == nullptr)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,11 +76,11 @@ namespace dawn_native {
|
||||||
}
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* NextCommand() {
|
T* NextCommand() {
|
||||||
return reinterpret_cast<T*>(NextCommand(sizeof(T), alignof(T)));
|
return static_cast<T*>(NextCommand(sizeof(T), alignof(T)));
|
||||||
}
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* NextData(size_t count) {
|
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.
|
// Needs to be called if iteration was stopped early.
|
||||||
|
|
|
@ -55,8 +55,7 @@ namespace dawn_native {
|
||||||
T* const* buffers,
|
T* const* buffers,
|
||||||
uint64_t const* offsets) {
|
uint64_t const* offsets) {
|
||||||
static_assert(std::is_base_of<BufferBase, T>::value, "");
|
static_assert(std::is_base_of<BufferBase, T>::value, "");
|
||||||
SetVertexBuffers(startSlot, count, reinterpret_cast<BufferBase* const*>(buffers),
|
SetVertexBuffers(startSlot, count, buffers, offsets);
|
||||||
offsets);
|
|
||||||
}
|
}
|
||||||
void SetVertexBuffers(uint32_t startSlot,
|
void SetVertexBuffers(uint32_t startSlot,
|
||||||
uint32_t count,
|
uint32_t count,
|
||||||
|
|
|
@ -44,7 +44,7 @@ namespace dawn_native { namespace d3d12 {
|
||||||
return nullptr;
|
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);
|
return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ namespace dawn_native { namespace opengl {
|
||||||
|
|
||||||
ASSERT(optionsBase->backendType == BackendType::OpenGL);
|
ASSERT(optionsBase->backendType == BackendType::OpenGL);
|
||||||
const AdapterDiscoveryOptions* options =
|
const AdapterDiscoveryOptions* options =
|
||||||
reinterpret_cast<const AdapterDiscoveryOptions*>(optionsBase);
|
static_cast<const AdapterDiscoveryOptions*>(optionsBase);
|
||||||
|
|
||||||
if (options->getProc == nullptr) {
|
if (options->getProc == nullptr) {
|
||||||
return DAWN_VALIDATION_ERROR("AdapterDiscoveryOptions::getProc must be set");
|
return DAWN_VALIDATION_ERROR("AdapterDiscoveryOptions::getProc must be set");
|
||||||
|
|
|
@ -132,7 +132,7 @@ namespace dawn_native { namespace opengl {
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* ShaderModule::GetSource() const {
|
const char* ShaderModule::GetSource() const {
|
||||||
return reinterpret_cast<const char*>(mGlslSource.data());
|
return mGlslSource.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
const ShaderModule::CombinedSamplerInfo& ShaderModule::GetCombinedSamplerInfo() const {
|
const ShaderModule::CombinedSamplerInfo& ShaderModule::GetCombinedSamplerInfo() const {
|
||||||
|
|
|
@ -121,7 +121,7 @@ namespace dawn_native { namespace vulkan {
|
||||||
|
|
||||||
allocation->mMemory = allocatedMemory;
|
allocation->mMemory = allocatedMemory;
|
||||||
allocation->mOffset = 0;
|
allocation->mOffset = 0;
|
||||||
allocation->mMappedPointer = reinterpret_cast<uint8_t*>(mappedPointer);
|
allocation->mMappedPointer = static_cast<uint8_t*>(mappedPointer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ namespace dawn_wire { namespace server {
|
||||||
cmd.requestSerial = data->requestSerial;
|
cmd.requestSerial = data->requestSerial;
|
||||||
cmd.status = status;
|
cmd.status = status;
|
||||||
cmd.dataLength = 0;
|
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) {
|
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
|
||||||
cmd.dataLength = dataLength;
|
cmd.dataLength = dataLength;
|
||||||
|
|
|
@ -545,7 +545,7 @@ void DawnTest::ResolveExpectations() {
|
||||||
|
|
||||||
// Get a pointer to the mapped copy of the data for the expectation.
|
// Get a pointer to the mapped copy of the data for the expectation.
|
||||||
const char* data =
|
const char* data =
|
||||||
reinterpret_cast<const char*>(mReadbackSlots[expectation.readbackSlot].mappedData);
|
static_cast<const char*>(mReadbackSlots[expectation.readbackSlot].mappedData);
|
||||||
data += expectation.readbackOffset;
|
data += expectation.readbackOffset;
|
||||||
|
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
|
@ -652,7 +652,7 @@ namespace detail {
|
||||||
testing::AssertionResult ExpectEq<T>::Check(const void* data, size_t size) {
|
testing::AssertionResult ExpectEq<T>::Check(const void* data, size_t size) {
|
||||||
DAWN_ASSERT(size == sizeof(T) * mExpected.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();
|
testing::AssertionResult failure = testing::AssertionFailure();
|
||||||
for (size_t i = 0; i < mExpected.size(); ++i) {
|
for (size_t i = 0; i < mExpected.size(); ++i) {
|
||||||
|
|
Loading…
Reference in New Issue