Skip to content

kdbindings/node_operators.h

Namespaces

Name
KDBindings
The main namespace of the KDBindings library.

Defines

Name
KDBINDINGS_DEFINE_BINARY_OP(OP)
KDBINDINGS_DEFINE_UNARY_OP(OP)

Macros Documentation

define KDBINDINGS_DEFINE_BINARY_OP

1
2
3
#define KDBINDINGS_DEFINE_BINARY_OP(
    OP
)

define KDBINDINGS_DEFINE_UNARY_OP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#define KDBINDINGS_DEFINE_UNARY_OP(
    OP
)
    template<typename... T>                                                               \
    inline auto operator OP(Property<T...> &arg) noexcept(noexcept(OP arg.get()))         \
            ->Private::Node<std::decay_t<decltype(OP arg.get())>>                         \
    {                                                                                     \
        return Private::makeNode([](auto &&v) { return (OP v); }, arg);                   \
    }                                                                                     \
                                                                                          \
    template<typename T>                                                                  \
    inline auto operator OP(Private::Node<T> &&arg) noexcept(noexcept(OP arg.evaluate())) \
            ->Private::Node<std::decay_t<decltype(OP arg.evaluate())>>                    \
    {                                                                                     \
        return Private::makeNode([](auto &&v) { return (OP v); }, std::move(arg));        \
    }

Source code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
  This file is part of KDBindings.

  SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Sean Harmer <sean.harmer@kdab.com>

  SPDX-License-Identifier: MIT

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#pragma once

#include <kdbindings/node.h>
#include <kdbindings/make_node.h>

namespace KDBindings {

// Helper macro to declare free standing unary operators for Property and Node

#define KDBINDINGS_DEFINE_UNARY_OP(OP)                                                    \
    template<typename... T>                                                               \
    inline auto operator OP(Property<T...> &arg) noexcept(noexcept(OP arg.get()))         \
            ->Private::Node<std::decay_t<decltype(OP arg.get())>>                         \
    {                                                                                     \
        return Private::makeNode([](auto &&v) { return (OP v); }, arg);                   \
    }                                                                                     \
                                                                                          \
    template<typename T>                                                                  \
    inline auto operator OP(Private::Node<T> &&arg) noexcept(noexcept(OP arg.evaluate())) \
            ->Private::Node<std::decay_t<decltype(OP arg.evaluate())>>                    \
    {                                                                                     \
        return Private::makeNode([](auto &&v) { return (OP v); }, std::move(arg));        \
    }

KDBINDINGS_DEFINE_UNARY_OP(!)
KDBINDINGS_DEFINE_UNARY_OP(~) // Bitwise not
KDBINDINGS_DEFINE_UNARY_OP(+)
KDBINDINGS_DEFINE_UNARY_OP(-)

// Helper macro to declare free standing binary operators for Property and Node.
// The combinations we need are:
//
// operator op (Property<A> &a, B&& b)          [Property, value]
// operator op (A&& a, Property<B> &b)          [value, Property]
// operator op (Property<A> &a, Property<B> &b) [Property, Property]
//
// operator op (Node<A>&& a, B&& b)             [Node value]
// operator op (A&& a, Node<B>&& b)             [value, Node]
// operator op (Node<A>&& a, Node<B>&& b)       [Node, Node]
//
// operator op (Property<A> &a, Node<B>&& b)    [Property, Node]
// operaotr op (Node<A>&& a, Property<B> &b)    [Node, Property]

#define KDBINDINGS_DEFINE_BINARY_OP(OP)                                                                                  \
    template<typename B, typename... A>                                                                                  \
    inline auto operator OP(Property<A...> &a, B &&b) noexcept(noexcept(a.get() OP b))                                   \
            ->std::enable_if_t<!Private::is_bindable<B>::value,                                                          \
                               Private::Node<decltype(a.get() OP b)>>                                                    \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, a, std::forward<B>(b));                \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename... B>                                                                                  \
    inline auto operator OP(A &&a, Property<B...> &b) noexcept(noexcept(a OP b.get()))                                   \
            ->std::enable_if_t<!Private::is_bindable<A>::value,                                                          \
                               Private::Node<decltype(a OP b.get())>>                                                    \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, std::forward<A>(a), b);                \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename B>                                                                                     \
    inline auto operator OP(Property<A> &a, Property<B> &b) noexcept(noexcept(a.get() OP b.get()))                       \
            ->Private::Node<decltype(a.get() OP b.get())>                                                                \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, a, b);                                 \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename B>                                                                                     \
    inline auto operator OP(Private::Node<A> &&a, B &&b) noexcept(noexcept(a.evaluate() OP b))                           \
            ->std::enable_if_t<!Private::is_bindable<B>::value,                                                          \
                               Private::Node<decltype(a.evaluate() OP b)>>                                               \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, std::move(a), std::forward<B>(b));     \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename B>                                                                                     \
    inline auto operator OP(A &&a, Private::Node<B> &&b) noexcept(noexcept(a OP b.evaluate()))                           \
            ->std::enable_if_t<!Private::is_bindable<A>::value,                                                          \
                               Private::Node<decltype(a OP b.evaluate())>>                                               \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, std::forward<A>(a), std::move(b));     \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename B>                                                                                     \
    inline auto operator OP(Private::Node<A> &&a, Private::Node<B> &&b) noexcept(noexcept(a.evaluate() OP b.evaluate())) \
            ->Private::Node<decltype(a.evaluate() OP b.evaluate())>                                                      \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, std::move(a), std::move(b));           \
    }                                                                                                                    \
                                                                                                                         \
    template<typename B, typename A>                                                                                     \
    inline auto operator OP(Property<A> &a, Private::Node<B> &&b) noexcept(noexcept(a.get() OP b.evaluate()))            \
            ->Private::Node<decltype(a.get() OP b.evaluate())>                                                           \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, a, std::move(b));                      \
    }                                                                                                                    \
                                                                                                                         \
    template<typename A, typename B>                                                                                     \
    inline auto operator OP(Private::Node<A> &&a, Property<B> &b) noexcept(noexcept(a.evaluate() OP b.get()))            \
            ->Private::Node<decltype(a.evaluate() OP b.get())>                                                           \
    {                                                                                                                    \
        return Private::makeNode([](auto &&av, auto &&bv) { return (av OP bv); }, std::move(a), b);                      \
    }

KDBINDINGS_DEFINE_BINARY_OP(*)
KDBINDINGS_DEFINE_BINARY_OP(/)
KDBINDINGS_DEFINE_BINARY_OP(%)
KDBINDINGS_DEFINE_BINARY_OP(+)
KDBINDINGS_DEFINE_BINARY_OP(-)
KDBINDINGS_DEFINE_BINARY_OP(<<)
KDBINDINGS_DEFINE_BINARY_OP(>>)
KDBINDINGS_DEFINE_BINARY_OP(<)
KDBINDINGS_DEFINE_BINARY_OP(<=)
KDBINDINGS_DEFINE_BINARY_OP(>)
KDBINDINGS_DEFINE_BINARY_OP(>=)
KDBINDINGS_DEFINE_BINARY_OP(==)
KDBINDINGS_DEFINE_BINARY_OP(!=)
KDBINDINGS_DEFINE_BINARY_OP(&)
KDBINDINGS_DEFINE_BINARY_OP(^)
KDBINDINGS_DEFINE_BINARY_OP(|)
KDBINDINGS_DEFINE_BINARY_OP(&&)
KDBINDINGS_DEFINE_BINARY_OP(||)

} // namespace KDBindings

Updated on 2024-07-13 at 00:00:32 +0000