KDBindings API Documentation  1.0.95
make_node.h
Go to the documentation of this file.
1 /*
2  This file is part of KDBindings.
3 
4  SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5  Author: Sean Harmer <sean.harmer@kdab.com>
6 
7  SPDX-License-Identifier: MIT
8 
9  Contact KDAB at <info@kdab.com> for commercial licensing options.
10 */
11 
12 #pragma once
13 
14 #include <kdbindings/node.h>
15 #include <type_traits>
16 
17 namespace KDBindings {
18 
19 namespace Private {
20 
21 template<typename T>
22 struct bindable_value_type_ {
23  using type = T;
24 };
25 
26 template<typename T>
27 struct bindable_value_type_<Property<T>> {
28  using type = T;
29 };
30 
31 template<typename T>
32 struct bindable_value_type_<const Property<T>> {
33  using type = T;
34 };
35 
36 template<typename T>
37 struct bindable_value_type_<NodeInterface<T>> {
38  using type = T;
39 };
40 
41 template<typename T>
42 struct bindable_value_type_<Node<T>> {
43  using type = T;
44 };
45 
46 template<typename T>
47 struct bindable_value_type : bindable_value_type_<std::decay_t<T>> {
48 };
49 
50 template<typename T>
51 using bindable_value_type_t = typename bindable_value_type<T>::type;
52 
53 // Find the type of a Node wrapping an operator and arguments
54 template<typename Operator, typename... Ts>
55 using operator_node_result =
56  std::decay<
57  std::invoke_result_t<
58  std::decay_t<Operator>,
59  bindable_value_type_t<Ts>...>>;
60 
61 template<typename Operator, typename... Ts>
62 using operator_node_result_t = typename operator_node_result<Operator, Ts...>::type;
63 
64 // Node creation helpers
65 template<typename T>
66 inline Node<std::decay_t<T>> makeNode(T &&value)
67 {
68  return Node<std::decay_t<T>>(std::make_unique<ConstantNode<std::decay_t<T>>>(std::move(value)));
69 }
70 
71 template<typename T>
72 inline Node<T> makeNode(Property<T> &property)
73 {
74  return Node<T>(std::make_unique<PropertyNode<T>>(property));
75 }
76 
77 template<typename T>
78 inline Node<T> makeNode(const Property<T> &property)
79 {
80  return Node<T>(std::make_unique<PropertyNode<T>>(property));
81 }
82 
83 template<typename T>
84 inline Node<T> makeNode(Node<T> &&node)
85 {
86  return std::move(node);
87 }
88 
89 template<typename Operator, typename... Ts, typename = std::enable_if_t<sizeof...(Ts) >= 1>, typename ResultType = operator_node_result_t<Operator, Ts...>>
90 inline Node<ResultType> makeNode(Operator &&op, Ts &&...args)
91 {
92  return Node<ResultType>(std::make_unique<OperatorNode<ResultType, std::decay_t<Operator>, bindable_value_type_t<Ts>...>>(
93  std::forward<Operator>(op),
94  makeNode(std::forward<Ts>(args))...));
95 }
96 
97 // Needed by function and operator helpers
98 template<typename T>
99 struct is_bindable : std::integral_constant<
100  bool,
101  is_property<T>::value || is_node<T>::value> {
102 };
103 
104 } // namespace Private
105 
106 } // namespace KDBindings
The main namespace of the KDBindings library.
Definition: binding.h:21

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDBindings
Reactive programming & data binding in C++
https://github.com/KDAB/KDBindings/
Generated by doxygen 1.9.1