Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
object.h
1#pragma once
2
3#include "sound_bakery/core/object/object_owner.h"
4#include "sound_bakery/pch.h"
5#include "sound_bakery/util/macros.h"
6
7namespace sbk::engine
8{
9 class system;
10}
11
12namespace sbk::core
13{
20 class SB_CLASS object : public object_owner, public std::enable_shared_from_this<object>
21 {
22 REGISTER_REFLECTION(object)
23 NOT_COPYABLE(object)
24
25 public:
26 object() = default;
27 virtual ~object();
28
29 [[nodiscard]] object_owner* owner() const { return m_owner; }
30
36 template <typename T>
38 {
39 if (getType().is_derived_from(T::type()) || getType() == T::type())
40 {
41 return sbk::reflection::cast<T*, object*>(this);
42 }
43 return nullptr;
44 }
45
46 template <typename T>
47 const T* try_convert_object() const noexcept
48 {
49 if (getType().is_derived_from(T::type()) || getType() == T::type())
50 {
51 return sbk::reflection::cast<const T*, const object*>(this);
52 }
53 return nullptr;
54 }
55
56 rttr::type getType() const
57 {
58 if (this == nullptr)
59 {
60 return rttr::type::get<void>();
61 }
62
63 if (!m_type.has_value())
64 {
65 m_type = get_type();
66 }
67
68 assert(m_type.has_value());
69 assert(m_type.value().is_valid());
70
71 return m_type.value();
72 }
73
74 void destroy();
75
76 [[nodiscard]] MulticastDelegate<object*>& get_on_destroy() { return m_onDestroyEvent; }
77
78 private:
79 friend class object_owner;
80
81 void set_owner(object_owner* newOwner)
82 {
83 assert(m_owner == nullptr);
84 m_owner = newOwner;
85 }
86
87 object_owner* m_owner = nullptr;
88
93 mutable std::optional<rttr::type> m_type = std::nullopt;
94
95 MulticastDelegate<object*> m_onDestroyEvent;
96 };
97} // namespace sbk::core
Definition database_ptr.h:23
Creates, owns and tracks objects.
Definition object_owner.h:13
Base object that all sound Bakery objects should inherit from.
Definition object.h:21
T * try_convert_object() noexcept
Gets the most derived type of this object and upcasts it to T.
Definition object.h:37