Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
property.h
1#pragma once
2
3#include "sound_bakery/pch.h"
4
5namespace sbk::core
6{
7 template <typename T>
8 class SB_CLASS property
9 {
10 static_assert(std::is_arithmetic<T>::value);
11
12 public:
14
15 property() : m_property(T()), m_min(0), m_max(1) {}
16
17 property(T value) : m_property(value), m_min(value), m_max(value + 1) {}
18
19 property(T value, T min, T max) : m_property(value), m_min(min), m_max(max)
20 {
21 assert(value >= min);
22 assert(value <= max);
23 assert(min < max);
24 }
25
30 : m_property(other.m_property), m_min(other.m_min), m_max(other.m_max), m_delegate(other.m_delegate)
31 {
32 }
33
38 property(property&& other) = default;
39
40 ~property() = default;
41
49 property& operator=(const property& other)
50 {
51 if (this != &other)
52 {
53 m_property = other.m_property;
54 m_min = other.m_min;
55 m_max = other.m_max;
56 m_delegate = other.m_delegate;
57 }
58
59 return *this;
60 }
61
62 property& operator=(property&& other) = default;
63
64 void set(T value)
65 {
66 if (value != m_property)
67 {
68 if (value >= m_min && value <= m_max)
69 {
70 m_delegate.Broadcast(m_property, value);
71 m_property = value;
72 }
73 }
74 }
75
76 [[nodiscard]] T get() const { return m_property; }
77 property_changed_delegate& get_delegate() { return m_delegate; }
78 [[nodiscard]] T get_min() const { return m_min; }
79 [[nodiscard]] T get_max() const { return m_max; }
80 [[nodiscard]] std::pair<T, T> get_min_max_pair() const { return std::pair<T, T>(m_min, m_max); }
81
82 private:
83 T m_property;
84 T m_min;
85 T m_max;
86 property_changed_delegate m_delegate;
87 };
88
89 using int_property = property<int32_t>;
90 using id_property = property<sbk_id>;
91 using float_property = property<float>;
92} // namespace sbk::core
Definition database_ptr.h:23
Definition property.h:9
property(property &&other)=default
Move constructor.
property & operator=(const property &other)
Assignment operator.
Definition property.h:49
property(const property &other)
Copy constructor.
Definition property.h:29