object.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Event,
  15. WritableEvent,
  16. ReadableEvent,
  17. SharedMemory,
  18. TransferMemory,
  19. Thread,
  20. Process,
  21. ResourceLimit,
  22. ClientPort,
  23. ServerPort,
  24. ClientSession,
  25. ServerSession,
  26. Session,
  27. };
  28. class Object : NonCopyable, public std::enable_shared_from_this<Object> {
  29. public:
  30. explicit Object(KernelCore& kernel_);
  31. explicit Object(KernelCore& kernel_, std::string&& name_);
  32. virtual ~Object();
  33. /// Returns a unique identifier for the object. For debugging purposes only.
  34. u32 GetObjectId() const {
  35. return object_id.load(std::memory_order_relaxed);
  36. }
  37. virtual std::string GetTypeName() const {
  38. return "[BAD KERNEL OBJECT TYPE]";
  39. }
  40. virtual std::string GetName() const {
  41. return name;
  42. }
  43. virtual HandleType GetHandleType() const = 0;
  44. void Close() {
  45. // TODO(bunnei): This is a placeholder to decrement the reference count, which we will use
  46. // when we implement KAutoObject instead of using shared_ptr.
  47. }
  48. /**
  49. * Check if a thread can wait on the object
  50. * @return True if a thread can wait on the object, otherwise false
  51. */
  52. bool IsWaitable() const;
  53. virtual void Finalize() = 0;
  54. protected:
  55. /// The kernel instance this object was created under.
  56. KernelCore& kernel;
  57. private:
  58. std::atomic<u32> object_id{0};
  59. std::string name;
  60. };
  61. template <typename T>
  62. std::shared_ptr<T> SharedFrom(T* raw) {
  63. if (raw == nullptr)
  64. return nullptr;
  65. return std::static_pointer_cast<T>(raw->shared_from_this());
  66. }
  67. /**
  68. * Attempts to downcast the given Object pointer to a pointer to T.
  69. * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
  70. */
  71. template <typename T>
  72. inline std::shared_ptr<T> DynamicObjectCast(std::shared_ptr<Object> object) {
  73. if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
  74. return std::static_pointer_cast<T>(object);
  75. }
  76. return nullptr;
  77. }
  78. } // namespace Kernel