From 6cbe0d9b881db8eeeca6947bffd7422e7da4dc5d Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Thu, 18 Oct 2018 16:53:49 +0200 Subject: [PATCH] Allow to cast of trivial values as array of a "smaller" type This is to be used with caution, since no data is copied. One has to ensure that the cast data lives longer than the associated CastArray. This might need to be reworked. --- src/utils/CastArray.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/utils/CastArray.hpp b/src/utils/CastArray.hpp index 9135489e6..57a05cbb5 100644 --- a/src/utils/CastArray.hpp +++ b/src/utils/CastArray.hpp @@ -57,6 +57,19 @@ class CastArray } } + PASTIS_INLINE + CastArray(DataType& value) + : m_size (sizeof(DataType)/sizeof(CastDataType)), + m_values(reinterpret_cast<CastDataType*>(&(value))) + { + static_assert((std::is_const_v<CastDataType> and std::is_const_v<DataType>) or + (not std::is_const_v<DataType>), "CastArray cannot remove const attribute"); + static_assert(std::is_trivial_v<DataType>, "Defining CastArray from non trivial type is not allowed"); + } + + PASTIS_INLINE + CastArray(DataType&& value) = delete; + PASTIS_INLINE CastArray(const CastArray&) = default; @@ -79,4 +92,16 @@ struct cast_array_to } }; +template <typename CastDataType> +struct cast_value_to +{ + template <typename DataType> + PASTIS_INLINE + static CastArray<DataType, CastDataType> + from(DataType& value) + { + return CastArray<DataType, CastDataType>(value); + } +}; + #endif // CAST_ARRAY_HPP -- GitLab