| // Copyright 2017 The Dawn Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <gtest/gtest.h> |
| |
| #include "dawn_native/RefCounted.h" |
| #include "dawn_native/ToBackend.h" |
| |
| #include <type_traits> |
| |
| // Make our own Base - Backend object pair, reusing the CommandBuffer name |
| namespace backend { |
| class CommandBufferBase : public RefCounted { |
| }; |
| } |
| |
| using namespace backend; |
| |
| class MyCommandBuffer : public CommandBufferBase { |
| }; |
| |
| struct MyBackendTraits { |
| using CommandBufferType = MyCommandBuffer; |
| }; |
| |
| // Instanciate ToBackend for our "backend" |
| template<typename T> |
| auto ToBackend(T&& common) -> decltype(ToBackendBase<MyBackendTraits>(common)) { |
| return ToBackendBase<MyBackendTraits>(common); |
| } |
| |
| // Test that ToBackend correctly converts pointers to base classes. |
| TEST(ToBackend, Pointers) { |
| { |
| MyCommandBuffer* cmdBuf = new MyCommandBuffer; |
| const CommandBufferBase* base = cmdBuf; |
| |
| auto backendCmdBuf = ToBackend(base); |
| static_assert(std::is_same<decltype(backendCmdBuf), const MyCommandBuffer*>::value, ""); |
| ASSERT_EQ(cmdBuf, backendCmdBuf); |
| |
| cmdBuf->Release(); |
| } |
| { |
| MyCommandBuffer* cmdBuf = new MyCommandBuffer; |
| CommandBufferBase* base = cmdBuf; |
| |
| auto backendCmdBuf = ToBackend(base); |
| static_assert(std::is_same<decltype(backendCmdBuf), MyCommandBuffer*>::value, ""); |
| ASSERT_EQ(cmdBuf, backendCmdBuf); |
| |
| cmdBuf->Release(); |
| } |
| } |
| |
| // Test that ToBackend correctly converts Refs to base classes. |
| TEST(ToBackend, Ref) { |
| { |
| MyCommandBuffer* cmdBuf = new MyCommandBuffer; |
| const Ref<CommandBufferBase> base(cmdBuf); |
| |
| const auto& backendCmdBuf = ToBackend(base); |
| static_assert(std::is_same<decltype(ToBackend(base)), const Ref<MyCommandBuffer>&>::value, ""); |
| ASSERT_EQ(cmdBuf, backendCmdBuf.Get()); |
| |
| cmdBuf->Release(); |
| } |
| { |
| MyCommandBuffer* cmdBuf = new MyCommandBuffer; |
| Ref<CommandBufferBase> base(cmdBuf); |
| |
| auto backendCmdBuf = ToBackend(base); |
| static_assert(std::is_same<decltype(ToBackend(base)), Ref<MyCommandBuffer>&>::value, ""); |
| ASSERT_EQ(cmdBuf, backendCmdBuf.Get()); |
| |
| cmdBuf->Release(); |
| } |
| } |