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:
13 using property_changed_delegate = MulticastDelegate<T, T>;
14
15 property() : m_value(T()), m_min(0), m_max(1) {}
16 property(T value) : m_value(value), m_min(value), m_max(value + 1) {}
17 property(T value, T min, T max) : m_value(value), m_min(min), m_max(max)
18 {
19 BOOST_ASSERT(value >= min);
20 BOOST_ASSERT(value <= max);
21 BOOST_ASSERT(min < max);
22 }
23
24 property(const property& other)
25 : m_value(other.m_value), m_min(other.m_min), m_max(other.m_max), m_delegate(other.m_delegate)
26 {
27 }
28
29 property(property&& other) = default;
30 ~property() = default;
31
32 property& operator=(const property& other)
33 {
34 if (this != &other)
35 {
36 m_value = other.m_value;
37 m_min = other.m_min;
38 m_max = other.m_max;
39 m_delegate = other.m_delegate;
40 }
41
42 return *this;
43 }
44
45 property& operator=(property&& other) = default;
46
47 auto set(T value) -> bool
48 {
49 if (value != m_value)
50 {
51 if (value >= m_min && value <= m_max)
52 {
53 m_delegate.Broadcast(m_value, value);
54 m_value = value;
55 return true;
56 }
57 }
58 return false;
59 }
60
61 [[nodiscard]] auto get() const -> T { return m_value; }
62 [[nodiscard]] auto get_min() const -> T { return m_min; }
63 [[nodiscard]] auto get_max() const -> T { return m_max; }
64 [[nodiscard]] auto get_min_max_pair() const -> std::pair<T, T> { return std::pair<T, T>(m_min, m_max); }
65 [[nodiscard]] auto get_delegate() -> property_changed_delegate& { return m_delegate; }
66
67 private:
68 T m_value;
69 T m_min;
70 T m_max;
71 property_changed_delegate m_delegate;
72 };
73
77} // namespace sbk::core
Definition property.h:9