Eigen  5.0.1
 
Loading...
Searching...
No Matches
ReturnByValue.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_RETURNBYVALUE_H
13#define EIGEN_RETURNBYVALUE_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22template <typename Derived>
23struct traits<ReturnByValue<Derived> > : public traits<typename traits<Derived>::ReturnType> {
24 enum {
25 // We're disabling the DirectAccess because e.g. the constructor of
26 // the Block-with-DirectAccess expression requires to have a coeffRef method.
27 // Also, this avoids having to implement stride support.
28 Flags = (traits<typename traits<Derived>::ReturnType>::Flags | EvalBeforeNestingBit) & ~DirectAccessBit
29 };
30};
31
32/* The ReturnByValue object doesn't even have a coeff() method.
33 * So the only way that nesting it in an expression can work, is by evaluating it into a plain matrix.
34 * So internal::nested always gives the plain return matrix type.
35 *
36 * FIXME: this specialization may be redundant with EvalBeforeNestingBit.
37 * Answer: EvalBeforeNestingBit should be deprecated since we have the evaluators
38 */
39template <typename Derived, int n, typename PlainObject>
40struct nested_eval<ReturnByValue<Derived>, n, PlainObject> {
41 using type = typename traits<Derived>::ReturnType;
42};
43
44} // end namespace internal
45
50template <typename Derived>
51class ReturnByValue : public internal::dense_xpr_base<ReturnByValue<Derived> >::type, internal::no_assignment_operator {
52 public:
53 using ReturnType = typename internal::traits<Derived>::ReturnType;
54
55 using Base = typename internal::dense_xpr_base<ReturnByValue>::type;
56 EIGEN_DENSE_PUBLIC_INTERFACE(ReturnByValue)
57
58 template <typename Dest>
59 EIGEN_DEVICE_FUNC inline void evalTo(Dest& dst) const {
60 static_cast<const Derived*>(this)->evalTo(dst);
61 }
62 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return static_cast<const Derived*>(this)->rows(); }
63 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return static_cast<const Derived*>(this)->cols(); }
64
65#ifndef EIGEN_PARSED_BY_DOXYGEN
66#define Unusable \
67 YOU_ARE_TRYING_TO_ACCESS_A_SINGLE_COEFFICIENT_IN_A_SPECIAL_EXPRESSION_WHERE_THAT_IS_NOT_ALLOWED_BECAUSE_THAT_WOULD_BE_INEFFICIENT
68 class Unusable {
69 Unusable(const Unusable&) {}
70 Unusable& operator=(const Unusable&) { return *this; }
71 };
72 const Unusable& coeff(Index) const { return *reinterpret_cast<const Unusable*>(this); }
73 const Unusable& coeff(Index, Index) const { return *reinterpret_cast<const Unusable*>(this); }
74 Unusable& coeffRef(Index) { return *reinterpret_cast<Unusable*>(this); }
75 Unusable& coeffRef(Index, Index) { return *reinterpret_cast<Unusable*>(this); }
76#undef Unusable
77#endif
78};
79
80template <typename Derived>
81template <typename OtherDerived>
82EIGEN_DEVICE_FUNC Derived& DenseBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other) {
83 other.evalTo(derived());
84 return derived();
85}
86
87namespace internal {
88
89// Expression is evaluated in a temporary; default implementation of Assignment is bypassed so that
90// when a ReturnByValue expression is assigned, the evaluator is not constructed.
91// TODO: Finalize port to new regime; ReturnByValue should not exist in the expression world
92
93template <typename Derived>
94struct evaluator<ReturnByValue<Derived> > : public evaluator<typename internal::traits<Derived>::ReturnType> {
95 using XprType = ReturnByValue<Derived>;
96 using PlainObject = typename internal::traits<Derived>::ReturnType;
97 using Base = evaluator<PlainObject>;
98
99 EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
100 internal::construct_at<Base>(this, m_result);
101 xpr.evalTo(m_result);
102 }
103
104 protected:
105 PlainObject m_result;
106};
107
108} // end namespace internal
109
110} // end namespace Eigen
111
112#endif // EIGEN_RETURNBYVALUE_H
constexpr Derived & operator=(const DenseBase< OtherDerived > &other)
Definition Assign.h:41
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int EvalBeforeNestingBit
Definition Constants.h:75