From b17754f998f225f3de9df59c917c7b88f81aaf22 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Mon, 29 May 2017 14:42:27 -0700 Subject: [PATCH] Kernel: Extract dynamic Object pointer cast into its own function --- src/core/hle/kernel/kernel.h | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 94f2025a07..d8929259eb 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -121,6 +121,18 @@ inline void intrusive_ptr_release(Object* object) { template using SharedPtr = boost::intrusive_ptr; +/** + * Attempts to downcast the given Object pointer to a pointer to T. + * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. + */ +template +inline SharedPtr DynamicObjectCast(SharedPtr object) { + if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { + return boost::static_pointer_cast(std::move(object)); + } + return nullptr; +} + /// Class that represents a Kernel object that a thread can be waiting on class WaitObject : public Object { public: @@ -163,6 +175,15 @@ private: std::vector> waiting_threads; }; +// Specialization of DynamicObjectCast for WaitObjects +template <> +inline SharedPtr DynamicObjectCast(SharedPtr object) { + if (object != nullptr && object->IsWaitable()) { + return boost::static_pointer_cast(std::move(object)); + } + return nullptr; +} + /** * This class allows the creation of Handles, which are references to objects that can be tested * for validity and looked up. Here they are used to pass references to kernel objects to/from the @@ -224,15 +245,11 @@ public: /** * Looks up a handle while verifying its type. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its - * type differs from the handle type `T::HANDLE_TYPE`. + * type differs from the requested one. */ template SharedPtr Get(Handle handle) const { - SharedPtr object = GetGeneric(handle); - if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { - return boost::static_pointer_cast(std::move(object)); - } - return nullptr; + return DynamicObjectCast(GetGeneric(handle)); } /** @@ -241,11 +258,7 @@ public: * not a waitable object. */ SharedPtr GetWaitObject(Handle handle) const { - SharedPtr object = GetGeneric(handle); - if (object != nullptr && object->IsWaitable()) { - return boost::static_pointer_cast(std::move(object)); - } - return nullptr; + return DynamicObjectCast(GetGeneric(handle)); } /// Closes all handles held in this table.