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/util/leak_detector.h"
5#include <boost/core/noncopyable.hpp>
6#include <boost/serialization/nvp.hpp>
7
8namespace sbk::core
9{
10 enum object_flags
11 {
12 object_flag_none = 0x00000000,
13 object_flag_loading = 0x00000001 //< Serializing is loading property data
14 };
15
22 class SB_CLASS object : public object_owner, public std::enable_shared_from_this<object>, public boost::noncopyable
23 {
24 REGISTER_REFLECTION(object)
25 LEAK_DETECTOR(object)
26
27 public:
28 object() = default;
29 virtual ~object();
30
31 template <typename T>
32 [[nodiscard]] auto casted_shared_from_this() -> std::shared_ptr<T>;
33
37 template <typename T>
38 [[nodiscard]] auto try_convert_object() noexcept -> T*;
39
43 template <typename T>
44 [[nodiscard]] auto try_convert_object() const noexcept -> const T*;
45
46 auto destroy() -> void;
47
48 [[nodiscard]] auto get_object_type() const -> rttr::type;
49 [[nodiscard]] auto get_owner() const -> object_owner*;
50 [[nodiscard]] auto get_owner_object() const -> object*;
51 [[nodiscard]] auto get_on_destroy() -> MulticastDelegate<object*>&;
52
53 [[nodiscard]] auto get_flags() const -> object_flags;
54 [[nodiscard]] auto set_flags(object_flags flagsToSet) -> void;
55 [[nodiscard]] auto clear_flags(object_flags flagsToClear) -> void;
56 [[nodiscard]] auto has_flag(object_flags flagsToCheck) -> bool;
57
58 template <class archive_class>
59 void serialize(archive_class& archive, const unsigned int fileVersion)
60 {
61 const rttr::type type = get_object_type();
62 BOOST_ASSERT(type.is_valid());
63
64 for (rttr::property property : type.get_properties())
65 {
66 BOOST_VERIFY(property.is_valid());
67 std::string propertyName = property.get_name().data();
68 std::replace(propertyName.begin(), propertyName.end(), ' ', '_');
69
70 if (typename archive_class::is_saving())
71 {
72 rttr::variant propertyVariant = property.get_value(rttr::instance(this));
73 archive & boost::serialization::make_nvp(propertyName.c_str(), propertyVariant);
74 }
75 else if (typename archive_class::is_loading())
76 {
77 rttr::variant loadedVariant = property.get_value(rttr::instance(this));
78 BOOST_VERIFY(loadedVariant.is_valid());
79 BOOST_VERIFY(loadedVariant.get_type().is_valid());
80 archive & boost::serialization::make_nvp(propertyName.c_str(), loadedVariant);
81
82 if (property.get_type() == rttr::type::get<std::string_view>() &&
83 loadedVariant.get_type() == rttr::type::get<std::string>())
84 {
85 std::string loadedString = loadedVariant.convert<std::string>();
86 std::string_view loadedStringView = loadedString;
87
88 if (has_flag(object_flag_loading))
89 {
90 property.set_value(rttr::instance(this), loadedStringView);
91 }
92 }
93 else
94 {
95 if (has_flag(object_flag_loading))
96 {
97 loadedVariant.convert(property.get_type());
98 BOOST_ASSERT(loadedVariant.get_type() == property.get_type());
99 property.set_value(rttr::instance(this), loadedVariant);
100 }
101 }
102 }
103 }
104 }
105
106 private:
107 friend class object_owner;
108
109 auto set_owner(object_owner* newOwner) -> void;
110 auto cache_type() -> void;
111
112 object_owner* m_owner = nullptr;
113 object_flags m_flags = object_flag_none;
114
119 mutable std::optional<rttr::type> m_type = std::nullopt;
120
121 MulticastDelegate<object*> m_onDestroyEvent;
122 };
123
124#include "object.inl"
125} // namespace sbk::core
Creates, owns and tracks objects.
Definition object_owner.h:13
Base object that all sound Bakery objects should inherit from.
Definition object.h:23
auto try_convert_object() noexcept -> T *
Gets the most derived type of this object and upcasts it to T.
Definition property.h:9