CppXAML
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2
15namespace cppxaml {
16
17#ifdef USE_WINUI3
18 namespace xaml = winrt::Microsoft::UI::Xaml;
19#else
20 namespace xaml = winrt::Windows::UI::Xaml;
21#endif
22
23
28 namespace utils {
34 inline auto tolower(std::wstring_view sv) {
35 std::wstring copy(sv);
36 std::transform(copy.begin(), copy.end(), copy.begin(), [](wchar_t x) { return (wchar_t)::tolower(x); });
37 return copy;
38 }
39
53 template<template<typename...> typename TOutContainer = std::vector, typename TInContainer, typename UnaryOp>
54 auto transform(TInContainer const& iterable, UnaryOp&& unary_op) {
55 using result_t = std::invoke_result_t<UnaryOp, typename TInContainer::value_type>;
56 TOutContainer<result_t> out{};
57 auto o = std::inserter(out, out.end());
58 for (auto i = iterable.cbegin(); i != iterable.cend(); i++) {
59 o = unary_op(*i);
60 }
61 return out;
62 }
63
99 template<template<typename...> typename TOutContainer = std::vector, typename TInContainer, typename UnaryOp>
100 auto transform_with_index(TInContainer const& iterable, UnaryOp&& unary_op) {
101 using result_t = std::invoke_result_t<UnaryOp, typename TInContainer::value_type, typename TInContainer::const_iterator::difference_type>;
102 TOutContainer<result_t> out{};
103 auto o = std::inserter(out, out.end());
104 for (auto i = iterable.cbegin(); i != iterable.cend(); i++) {
105 o = unary_op(*i, i - iterable.cbegin());
106 }
107 return out;
108 }
109
110 }
111
119 template<typename T = cppxaml::xaml::DependencyObject>
120 T FindChildByName(cppxaml::xaml::DependencyObject d, std::wstring_view name) {
121 if (auto fe = d.try_as<cppxaml::xaml::FrameworkElement>()) {
122 if (fe.Name() == name) return d.as<T>();
123 }
124 for (int i = 0; i < cppxaml::xaml::Media::VisualTreeHelper::GetChildrenCount(d); i++) {
125 auto r = FindChildByName<T>(cppxaml::xaml::Media::VisualTreeHelper::GetChild(d, i), name);
126 if (r) return r.as<T>();
127 }
128 return nullptr;
129 }
130
131
132}
133
134#ifndef DOXY
135#define IF_ASSIGNABLE_CONTROL(XAMLTYPE) std::enable_if_t<std::is_assignable_v<cppxaml::xaml::Controls::XAMLTYPE, T>, cppxaml::details::Wrapper<T>>
136#define IF_ASSIGNABLE_CONTROL_TITEMS(XAMLTYPE, TITEMS) std::enable_if_t<std::is_assignable_v<cppxaml::xaml::Controls::XAMLTYPE, T>, cppxaml::details::Wrapper<T, TITEMS>>
137#define DOXY_RT(...) auto
138#else
139#define IF_ASSIGNABLE_CONTROL(XAMLTYPE) cppxaml::details::Wrapper<T>
140#define IF_ASSIGNABLE_CONTROL_TITEMS(XAMLTYPE, TITEMS) cppxaml::details::Wrapper<T, TITEMS>
141#define DOXY_RT(...) __VA_ARGS__
142#endif
auto tolower(std::wstring_view sv)
Definition: utils.h:34
auto transform(TInContainer const &iterable, UnaryOp &&unary_op)
Maps each element in a container via a unary operation on each element.
Definition: utils.h:54
auto transform_with_index(TInContainer const &iterable, UnaryOp &&unary_op)
Maps each element in a container via a unary operation on each element.
Definition: utils.h:100
The main CppXAML namespace.
T FindChildByName(cppxaml::xaml::DependencyObject d, std::wstring_view name)
Finds a XAML element by name.
Definition: utils.h:120