CppXAML
Loading...
Searching...
No Matches
XamlProperty.h
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3#include <cppxaml/utils.h>
4
13namespace winrt {
14 template<typename T, typename I>
15 T try_as(I* i) {
16 T t;
17 if (i->QueryInterface(winrt::guid_of<T>(), winrt::put_abi(t)) == S_OK) {
18 return t;
19 }
20 return nullptr;
21 }
22}
23
27namespace cppxaml {
28
29 namespace details {
30 template<typename T>
31 struct XamlEvent_t {
32 winrt::event_token operator()(T const& handler) {
33 return m_handler.add(handler);
34 }
35 auto operator()(const winrt::event_token& token) noexcept { return m_handler.remove(token); }
36
37 template<typename... TArgs>
38 auto invoke(TArgs&&... args) {
39 return m_handler(args...);
40 }
41 private:
42 winrt::event<T> m_handler;
43 };
44 }
49 template<typename T>
50 struct XamlEvent : cppxaml::details::XamlEvent_t<winrt::Windows::Foundation::EventHandler<T>> {};
51
62 template<typename TSender, typename TArgs>
63 struct TypedXamlEvent : cppxaml::details::XamlEvent_t<winrt::Windows::Foundation::TypedEventHandler<TSender, TArgs>> {};
64
77 template<typename T>
79 public:
80 using Type = T;
81 auto PropertyChanged(cppxaml::xaml::Data::PropertyChangedEventHandler const& value) {
82 return m_propertyChanged.add(value);
83 }
84 void PropertyChanged(winrt::event_token const& token) {
85 m_propertyChanged.remove(token);
86 }
87 Type& self() {
88 return *static_cast<Type*>(this);
89 }
90
112 auto RaisePropertyChanged(std::wstring_view name) {
113 return m_propertyChanged(self(), cppxaml::xaml::Data::PropertyChangedEventHandler{name});
114 }
115 protected:
116 winrt::event<cppxaml::xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
117
118 template<typename TProperty>
119 friend struct XamlProperty;
120 };
121
126 template<typename T>
128 using Type = T;
129 T operator()() const {
130 return m_value;
131 }
132 void operator()(const T& value) {
133 m_value = value;
134 }
135
136 operator T() {
137 return m_value;
138 }
139
140 XamlProperty<T>& operator=(const T& t) {
141 operator()(t);
142 return *this;
143 }
144
145 XamlProperty(const XamlProperty&) = delete;
146 XamlProperty(XamlProperty&&) = delete;
147
148 template<typename... TArgs>
149 XamlProperty(TArgs&&... args) : m_value(std::forward<TArgs>(args)...) {}
150
151 private:
152 Type m_value{};
153 };
154
160 template<typename T>
162 using Type = T;
163
164 T operator()() const {
166 }
167
168 void operator()(const T& value) {
169 if (value != operator()()) {
171 if (m_npc) {
172 (*m_npc)(m_sender, cppxaml::xaml::Data::PropertyChangedEventArgs{ m_name });
173 }
174 }
175 }
176 template<typename... TArgs>
178 winrt::event<cppxaml::xaml::Data::PropertyChangedEventHandler>* npc,
179 winrt::Windows::Foundation::IInspectable sender,
180 std::wstring_view name,
181 TArgs&&... args) :
182 XamlProperty(std::forward<TArgs...>(args)...) {
183 m_name = name;
184 m_npc = npc;
185 m_sender = sender;
186 }
187
190 private:
191 std::wstring m_name;
192 winrt::event<cppxaml::xaml::Data::PropertyChangedEventHandler>* m_npc;
193 winrt::Windows::Foundation::IInspectable m_sender;
194 };
195}
196
201#define INIT_PROPERTY(NAME, VALUE) \
202 NAME(&m_propertyChanged, winrt::try_as<winrt::Windows::Foundation::IInspectable>(this), std::wstring_view{ L#NAME }, VALUE)
203
The main CppXAML namespace.
Helper base class to inherit from to have a simple implementation of INotifyPropertyChanged.
Definition: XamlProperty.h:78
auto RaisePropertyChanged(std::wstring_view name)
Raises a property change notification event.
Definition: XamlProperty.h:112
A default event handler that maps to Windows.Foundation.TypedEventHandler.
Definition: XamlProperty.h:63
A default event handler that maps to Windows.Foundation.EventHandler.
Definition: XamlProperty.h:50
Implements a simple property (without change notifications).
Definition: XamlProperty.h:127
Implements a property type with notifications.
Definition: XamlProperty.h:161