Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
object_owner.h
1#pragma once
2
3#include "sound_bakery/pch.h"
4
5namespace sbk::core
6{
12 class SB_CLASS object_owner
13 {
14 public:
15 std::shared_ptr<object> create_runtime_object(const rttr::type& type);
16 std::shared_ptr<object> load_object(YAML::Node& node);
17
21 std::shared_ptr<database_object> create_database_object(const rttr::type& type, bool addToDatabase = true);
22
23 template <typename T>
24 std::shared_ptr<T> create_runtime_object();
25
26 template <typename T>
27 std::shared_ptr<T> create_database_object();
28
29 void remove_object(const std::shared_ptr<object>& object);
30 void destroy_all() { m_objects.clear(); }
31
32 std::vector<std::shared_ptr<object>>& get_objects() { return m_objects; }
33 const std::vector<std::shared_ptr<object>>& get_objects() const { return m_objects; }
34
35 private:
36 std::vector<std::shared_ptr<object>> m_objects;
37 };
38
39 template <typename T>
40 std::shared_ptr<T> object_owner::create_runtime_object()
41 {
42 if (!rttr::type::get<T>().is_derived_from(rttr::type::get<sbk::core::object>()))
43 {
44 SPDLOG_ERROR("Cannot create object. Objects must derive from the base object type");
45 return {};
46 }
47
48 if (std::shared_ptr<object> object = create_runtime_object(rttr::type::get<T>()))
49 {
50 if (std::shared_ptr<T> castedObject = std::static_pointer_cast<T>(object))
51 {
52 return castedObject;
53 }
54 }
55
56 return {};
57 }
58
59 template <typename T>
60 std::shared_ptr<T> object_owner::create_database_object()
61 {
62 if (!rttr::type::get<T>().is_derived_from(rttr::type::get<sbk::core::database_object>()))
63 {
64 SPDLOG_ERROR("Cannot create object. Database objects must derive from the base database object type");
65 return {};
66 }
67
68 if (std::shared_ptr<database_object> object = create_database_object(rttr::type::get<T>()))
69 {
70 if (std::shared_ptr<T> castedObject = std::static_pointer_cast<T>(object))
71 {
72 return castedObject;
73 }
74 }
75
76 return {};
77 }
78} // namespace sbk::core
Definition database_ptr.h:23
Creates, owns and tracks objects.
Definition object_owner.h:13
std::shared_ptr< database_object > create_database_object(const rttr::type &type, bool addToDatabase=true)
Definition object_owner.cpp:109