object.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <memory>
  7. #include <string>
  8. #include "common/common_types.h"
  9. namespace Kernel {
  10. class KernelCore;
  11. using Handle = u32;
  12. enum class HandleType : u32 {
  13. Unknown,
  14. WritableEvent,
  15. ReadableEvent,
  16. SharedMemory,
  17. TransferMemory,
  18. Thread,
  19. Process,
  20. ResourceLimit,
  21. ClientPort,
  22. ServerPort,
  23. ClientSession,
  24. ServerSession,
  25. };
  26. class Object : NonCopyable, public std::enable_shared_from_this<Object> {
  27. public:
  28. explicit Object(KernelCore& kernel);
  29. virtual ~Object();
  30. /// Returns a unique identifier for the object. For debugging purposes only.
  31. u32 GetObjectId() const {
  32. return object_id.load(std::memory_order_relaxed);
  33. }
  34. virtual std::string GetTypeName() const {
  35. return "[BAD KERNEL OBJECT TYPE]";
  36. }
  37. virtual std::string GetName() const {
  38. return "[UNKNOWN KERNEL OBJECT]";
  39. }
  40. virtual HandleType GetHandleType() const = 0;
  41. /**
  42. * Check if a thread can wait on the object
  43. * @return True if a thread can wait on the object, otherwise false
  44. */
  45. bool IsWaitable() const;
  46. protected:
  47. /// The kernel instance this object was created under.
  48. KernelCore& kernel;
  49. private:
  50. std::atomic<u32> object_id{0};
  51. };
  52. template <typename T>
  53. std::shared_ptr<T> SharedFrom(T* raw) {
  54. if (raw == nullptr)
  55. return nullptr;
  56. return std::static_pointer_cast<T>(raw->shared_from_this());
  57. }
  58. /**
  59. * Attempts to downcast the given Object pointer to a pointer to T.
  60. * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
  61. */
  62. template <typename T>
  63. inline std::shared_ptr<T> DynamicObjectCast(std::shared_ptr<Object> object) {
  64. if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
  65. return std::static_pointer_cast<T>(object);
  66. }
  67. return nullptr;
  68. }
  69. } // namespace Kernel