xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
property.hpp
浏览该文件的文档.
1#pragma once
2#include <xcal/public.h>
3
4#include <unordered_map>
5
6#define XCAL_PROPERTY_TYPE(type) \
7 Type type_() const override { return Type::type; }
8
9namespace xcal::property {
10
11enum class Type {
16 Scalar,
17 Color,
20 Vec,
21 User,
22};
23
25 virtual Type type_() const = 0;
26
27 private:
28 mutable bool_t is_changed_{true};
29
30 protected:
31 template <typename T>
33 private:
34 T value_;
35 static std::unordered_map<void *, MProperty *> proxy_to_self_;
36
37 public:
38 template <typename... Args>
39 requires(std::is_constructible_v<T, Args...>)
40 explicit Proxy(MProperty *self, Args &&...args)
41 : value_(std::forward<Args>(args)...) {
42 proxy_to_self_[this] = self;
43 }
44
45 Proxy(MProperty *self, T &&value) : value_(std::move(value)) {
46 proxy_to_self_[this] = self;
47 }
48 Proxy(MProperty *self, const T &value) : value_(value) {
49 proxy_to_self_[this] = self;
50 }
51 ~Proxy() { proxy_to_self_.erase(this); }
52 void on_changed() {
53 if (auto it = proxy_to_self_.find(this);
54 it != proxy_to_self_.end()) {
55 it->second->set_changed();
56 }
57 }
58 Proxy &operator=(const T &value) {
59 value_ = value;
60 on_changed();
61 return *this;
62 }
63 Proxy &operator=(T &&value) {
64 value_ = std::move(value);
65 on_changed();
66 return *this;
67 }
68 Proxy(const Proxy &other) = delete;
69 Proxy(Proxy &&other) : value_(std::move(other.value_)) {
70 proxy_to_self_[this] = proxy_to_self_[&other];
71 }
72 Proxy &operator=(const Proxy &other) {
73 value_ = other.value_;
74 on_changed();
75 return *this;
76 }
77 Proxy &operator=(Proxy &&other) {
78 value_ = std::move(other.value_);
79 on_changed();
80 return *this;
81 }
82
83 // NOLINTNEXTLINE(google-explicit-constructor)
84 operator T &() {
85 on_changed();
86 return value_;
87 }
88 // NOLINTNEXTLINE(google-explicit-constructor)
89 operator const T &() const { return value_; }
90
91 template <typename... Args>
92 auto &operator[](Args &&...args) {
93 on_changed();
94 return value_[std::forward<Args>(args)...];
95 }
96 template <typename... Args>
97 auto &operator[](Args &&...args) const {
98 return value_[std::forward<Args>(args)...];
99 }
100 template <typename... Args>
101 auto &operator()(Args &&...args) {
102 on_changed();
103 return value_(std::forward<Args>(args)...);
104 }
105 template <typename... Args>
106 const auto &operator()(Args &&...args) const {
107 return value_(std::forward<Args>(args)...);
108 }
109#define XCAL_PROPERTY_PROXY_OPERATOR(op) \
110 template <typename Arg> \
111 auto &operator op(const Arg & arg) { \
112 on_changed(); \
113 value_ op arg; \
114 return *this; \
115 }
126#undef XCAL_PROPERTY_PROXY_OPERATOR
127 };
128
129 public:
130 MProperty() = default;
131 MProperty(const MProperty &) = delete;
132 MProperty(MProperty &&) = delete;
133 MProperty &operator=(const MProperty &) = delete;
135 bool_t is_changed() const { return is_changed_; }
136 void set_changed() const { is_changed_ = true; }
137 void reset_changed() const { is_changed_ = false; }
138 Type type() const { return type_(); };
139 virtual ~MProperty() = default;
140};
141template <typename T>
142std::unordered_map<void *, MProperty *> MProperty::Proxy<T>::proxy_to_self_{};
143
144} // namespace xcal::property
颜色属性类
Definition color.hpp:16
Proxy(MProperty *self, T &&value)
Definition property.hpp:45
Proxy & operator=(Proxy &&other)
Definition property.hpp:77
Proxy(MProperty *self, const T &value)
Definition property.hpp:48
const auto & operator()(Args &&...args) const
Definition property.hpp:106
Proxy & operator=(const Proxy &other)
Definition property.hpp:72
auto & operator[](Args &&...args) const
Definition property.hpp:97
auto & operator()(Args &&...args)
Definition property.hpp:101
Proxy(const Proxy &other)=delete
auto & operator[](Args &&...args)
Definition property.hpp:92
Proxy & operator=(T &&value)
Definition property.hpp:63
Proxy & operator=(const T &value)
Definition property.hpp:58
Proxy(MProperty *self, Args &&...args)
Definition property.hpp:40
MProperty(MProperty &&)=delete
bool_t is_changed() const
Definition property.hpp:135
virtual ~MProperty()=default
MProperty(const MProperty &)=delete
MProperty & operator=(MProperty &&)=delete
MProperty & operator=(const MProperty &)=delete
时间点属性类
_PositionList< xcmath::vec< float_t, 2 >, Type::PositionList > PositionList
_PositionList< xcmath::vec< float_t, 3 >, Type::ThreeDPositionList > ThreeDPositionList
bool bool_t
Definition public.h:28
#define XCAL_PROPERTY_PROXY_OPERATOR(op)
Definition property.hpp:109
#define XCAL_API
Definition public.h:69