TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
expression.hpp
Go to the documentation of this file.
1// Copyright (c) 2024--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./utils.hpp"
14
15#include <tuple>
16#include <utility>
17
18namespace nda::clef {
19
20 namespace tags {
21
26
28 struct function {};
29
31 struct subscript {};
32
34 struct terminal {};
35
37 struct if_else {};
38
40 struct unary_op {};
41
43 struct binary_op {};
44
46
47 } // namespace tags
48
53
64 template <typename Tag, typename... Ts>
65 struct expr {
67 using childs_t = std::tuple<Ts...>;
68
71
73 expr(expr const &) = default;
74
79 expr(expr &&ex) noexcept : childs(std::move(ex.childs)) {}
80
82 expr &operator=(expr const &) = delete;
83
85 expr &operator=(expr &&) = default;
86
93 template <typename... Us>
94 expr(Tag, Us &&...us) : childs(std::forward<Us>(us)...) {}
95
104#ifdef __cpp_explicit_this_parameter
105 template <typename Self, typename... Args>
106 auto operator[](this Self &&self, Args &&...args) {
107 return expr<tags::subscript, expr, expr_storage_t<Args>...>{tags::subscript(), std::forward<Self>(self), std::forward<Args>(args)...};
108 }
109#else
110 template <typename... Args>
111 auto operator[](Args &&...args) const & {
112 return expr<tags::subscript, expr, expr_storage_t<Args>...>{tags::subscript(), *this, std::forward<Args>(args)...};
113 }
114 template <typename... Args>
115 auto operator[](Args &&...args) & {
116 return expr<tags::subscript, expr, expr_storage_t<Args>...>{tags::subscript(), *this, std::forward<Args>(args)...};
117 }
118
119 template <typename... Args>
120 auto operator[](Args &&...args) && {
121 return expr<tags::subscript, expr, expr_storage_t<Args>...>{tags::subscript(), std::move(*this), std::forward<Args>(args)...};
122 }
123#endif
124
133#ifdef __cpp_explicit_this_parameter
134 template <typename Self, typename... Args>
135 auto operator()(this Self &&self, Args &&...args) {
136 return expr<tags::function, expr, expr_storage_t<Args>...>{tags::function(), std::forward<Self>(self), std::forward<Args>(args)...};
137 }
138#else
139 template <typename... Args>
140 auto operator()(Args &&...args) const & {
141 return expr<tags::function, expr, expr_storage_t<Args>...>{tags::function(), *this, std::forward<Args>(args)...};
142 }
143
144 template <typename... Args>
145 auto operator()(Args &&...args) & {
146 return expr<tags::function, expr, expr_storage_t<Args>...>{tags::function(), *this, std::forward<Args>(args)...};
147 }
148
149 template <typename... Args>
150 auto operator()(Args &&...args) && {
151 return expr<tags::function, expr, expr_storage_t<Args>...>{tags::function(), std::move(*this), std::forward<Args>(args)...};
152 }
153#endif
154 };
155
156 namespace detail {
157
158 // Specialization of ph_set for nda::clef::expr types.
159 template <typename Tag, typename... Ts>
160 struct ph_set<expr<Tag, Ts...>> : ph_set<Ts...> {};
161
162 // Specialization of is_lazy_impl for nda::clef::expr types (always true).
163 template <typename Tag, typename... Ts>
164 constexpr bool is_lazy_impl<expr<Tag, Ts...>> = true;
165
166 // Specialization of force_copy_in_expr_impl for nda::clef::expr types (always true).
167 template <typename Tag, typename... Ts>
168 constexpr bool force_copy_in_expr_impl<expr<Tag, Ts...>> = true;
169
170 } // namespace detail
171
173
174} // namespace nda::clef
Provides some utility functions and type traits for the CLEF library.
Single node of the expression tree.
expr & operator=(expr const &)=delete
Copy assignment operator is deleted.
auto operator()(Args &&...args) const &
Function call operator.
expr(expr &&ex) noexcept
Move constructor simply moves the child nodes from the source expression.
std::tuple< Ts... > childs_t
Tuple type for storing the child nodes.
expr & operator=(expr &&)=default
Default move assignment operator.
auto operator[](Args &&...args) const &
Subscript operator.
expr(expr const &)=default
Default copy constructor.
expr(Tag, Us &&...us)
Construct an expression node with a given tag and child nodes.
childs_t childs
Child nodes of the current expression node.
Tag for binary operator expressions.
Tag for function call expressions.
Tag for conditional expressions.
Tag for subscript expressions.
Tag to indicate a terminal node in the expression tree.
Tag for unary operator expressions.