Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
property.h
1#pragma once
2
3#include "Delegates.h"
4#include "sound_bakery/sound_bakery_internal.h"
5
6namespace SB::Core
7{
8 template <typename T>
9 class SB_CLASS Property
10 {
11 static_assert(std::is_arithmetic<T>::value);
12
13 public:
15
16 public:
17 Property() : m_property(T()), m_min(0), m_max(1) {}
18
19 Property(T value) : m_property(value), m_min(value), m_max(value + 1) {}
20
21 Property(T value, T min, T max) : m_property(value), m_min(min), m_max(max)
22 {
23 assert(value >= min);
24 assert(value <= max);
25 assert(min < max);
26 }
27
32 : m_property(other.m_property), m_min(other.m_min), m_max(other.m_max), m_delegate(other.m_delegate)
33 {
34 }
35
40 Property(Property&& other) = default;
41
42 public:
51 {
52 if (this != &other)
53 {
54 m_property = other.m_property;
55 m_min = other.m_min;
56 m_max = other.m_max;
57 m_delegate = other.m_delegate;
58 }
59
60 return *this;
61 }
62
63 Property& operator=(Property&& other) = default;
64
65 public:
66 void set(T value)
67 {
68 if (value != m_property)
69 {
70 if (value >= m_min && value <= m_max)
71 {
72 m_delegate.Broadcast(m_property, value);
73 m_property = value;
74 }
75 }
76 }
77
78 [[nodiscard]] T get() const { return m_property; }
79
80 PropertyChangedDelegate& getDelegate() { return m_delegate; }
81
82 [[nodiscard]] T getMin() const { return m_min; }
83
84 [[nodiscard]] T getMax() const { return m_max; }
85
86 [[nodiscard]] std::pair<T, T> getMinMaxPair() const { return std::pair<T, T>(m_min, m_max); }
87
88 private:
89 T m_property;
90 T m_min;
91 T m_max;
92 PropertyChangedDelegate m_delegate;
93 };
94
95 using IntProperty = Property<int32_t>;
96 using IdProperty = Property<SB_ID>;
97 using FloatProperty = Property<float>;
98} // namespace SB::Core
Definition database_ptr.h:23
Definition property.h:10
Property & operator=(const Property &other)
Assignment operator.
Definition property.h:50
Property(Property &&other)=default
Move constructor.
Property(const Property &other)
Copy constructor.
Definition property.h:31