Expression Templates Library (ETL)
serial_expr.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include "etl/wrapper_traits.hpp"
16 
17 namespace etl {
18 
22 template <typename Expr>
23 struct serial_expr final {
24  using expr_t = Expr;
26 
27 private:
28  Expr value;
29 
30  friend struct wrapper_traits<serial_expr>;
31 
32 public:
33  //Cannot be constructed with no args
34  serial_expr() = delete;
35 
40  explicit serial_expr(Expr l) : value(std::forward<Expr>(l)) {
41  //Nothing else to init
42  }
43 
44  //Expresison can be copied and moved
45  serial_expr(const serial_expr& e) = default;
46  serial_expr(serial_expr&& e) noexcept = default;
47 
48  //Expressions are invariant
49  serial_expr& operator=(const serial_expr& e) = delete;
50  serial_expr& operator=(serial_expr&& e) = delete;
51 
57  template <typename E>
58  bool alias(const E& other) const noexcept {
59  return value.alias(other);
60  }
61 
62  // Assignment functions
63 
68  template <typename L>
69  void assign_to(L&& lhs) const {
70  auto old_serial = local_context().serial;
71 
72  local_context().serial = true;
73 
74  value.assign_to(lhs);
75 
76  local_context().serial = old_serial;
77  }
78 
83  template <typename L>
84  void assign_add_to(L&& lhs) const {
85  auto old_serial = local_context().serial;
86 
87  local_context().serial = true;
88 
89  value.assign_add_to(lhs);
90 
91  local_context().serial = old_serial;
92  }
93 
98  template <typename L>
99  void assign_sub_to(L&& lhs) const {
100  auto old_serial = local_context().serial;
101 
102  local_context().serial = true;
103 
104  value.assign_sub_to(lhs);
105 
106  local_context().serial = old_serial;
107  }
108 
113  template <typename L>
114  void assign_mul_to(L&& lhs) const {
115  auto old_serial = local_context().serial;
116 
117  local_context().serial = true;
118 
119  value.assign_mul_to(lhs);
120 
121  local_context().serial = old_serial;
122  }
123 
128  template <typename L>
129  void assign_div_to(L&& lhs) const {
130  auto old_serial = local_context().serial;
131 
132  local_context().serial = true;
133 
134  value.assign_div_to(lhs);
135 
136  local_context().serial = old_serial;
137  }
138 
143  template <typename L>
144  void assign_mod_to(L&& lhs) const {
145  auto old_serial = local_context().serial;
146 
147  local_context().serial = true;
148 
149  value.assign_mod_to(lhs);
150 
151  local_context().serial = old_serial;
152  }
153 
160  friend std::ostream& operator<<(std::ostream& os, const serial_expr& expr) {
161  return os << "serial(" << expr.value << ")";
162  }
163 };
164 
169 template <typename Expr>
170 struct etl_traits<etl::serial_expr<Expr>> : wrapper_traits<etl::serial_expr<Expr>> {};
171 
172 } //end of namespace etl
Contains traits for wrapper expressions.
friend std::ostream & operator<<(std::ostream &os, const serial_expr &expr)
Prints the type of the optimized expression to the stream.
Definition: serial_expr.hpp:160
bool serial
Force serial execution.
Definition: context.hpp:27
serial_expr(Expr l)
Construt a new optimized expression around the given ETL expression.
Definition: serial_expr.hpp:40
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: serial_expr.hpp:129
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: serial_expr.hpp:84
bool alias(const E &other) const noexcept
Test if this expression aliases with the given expression.
Definition: serial_expr.hpp:58
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: serial_expr.hpp:99
value_t< Expr > value_type
The value type.
Definition: serial_expr.hpp:25
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: serial_expr.hpp:144
A wrapper for expressions that is forced to be serial.
Definition: serial_expr.hpp:23
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: serial_expr.hpp:114
Expr expr_t
The wrapped expression type.
Definition: serial_expr.hpp:24
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
Traits for wrapper expressions.
Definition: wrapper_traits.hpp:21
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: serial_expr.hpp:69