VCTR
Loading...
Searching...
No Matches
TransformedByStaticCast.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr::expressions
24{
25
26template <size_t extent, class SrcType, std::constructible_from<ValueType<SrcType>> DstValueType>
28{
29public:
30 using value_type = DstValueType;
31
32 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (TransformedByStaticCast, src)
33
34 VCTR_FORCEDINLINE constexpr DstValueType operator[] (size_t i) const
35 {
36 return static_cast<DstValueType> (src[i]);
37 }
38
39 VCTR_FORCEDINLINE const DstValueType* evalNextVectorOpInExpressionChain (DstValueType* dst) const
40 requires (sizeof (DstValueType) == sizeof (ValueType<SrcType>) || is::anyVctr<SrcType>) &&
42 std::is_trivially_destructible_v<ValueType<SrcType>> &&
43 std::is_trivially_constructible_v<DstValueType>
44 {
45 // Since the constraints verified that we can use a different typed destination memory to
46 // evaluate an intermediate chain result, we cast the destination memory pointer to a pointer
47 // of type SrcType::value_type*
48 const auto* s = src.evalNextVectorOpInExpressionChain (static_cast<ValueType<SrcType>*> (static_cast<void*> (dst)));
49
50 const auto n = src.size();
51 for (size_t i = 0; i < n; ++i)
52 dst[i] = static_cast<DstValueType> (s[i]);
53
54 return dst;
55 }
56};
57
58} // namespace vctr::expressions
59
60namespace vctr
61{
66template <class DstType>
68
69} // namespace vctr
70
Definition: TransformedByStaticCast.h:28
Constrains a type to have a member function evalNextVectorOpInExpressionChain (value_type*) const.
Definition: ContainerAndExpressionConcepts.h:94
Constrains a type to be any derived instance of VctrBase.
Definition: ContainerAndExpressionConcepts.h:192
constexpr ExpressionChainBuilder< expressions::TransformedByStaticCast, DstType > transformedByStaticCastTo
Transforms all source elements to DstValueType by applying a static_cast<DstValueType> to them.
Definition: TransformedByStaticCast.h:67
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
An expression chain builder is an object which supplies various operator<< overloads which build chai...
Definition: ExpressionChainBuilder.h:136
The base class to every expression template.
Definition: ExpressionTemplate.h:37