Skip to content
Snippets Groups Projects
Commit 6cbe0d9b authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

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.
parent 36dafba0
No related branches found
No related tags found
1 merge request!11Feature/mpi
...@@ -57,6 +57,19 @@ class CastArray ...@@ -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 PASTIS_INLINE
CastArray(const CastArray&) = default; CastArray(const CastArray&) = default;
...@@ -79,4 +92,16 @@ struct cast_array_to ...@@ -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 #endif // CAST_ARRAY_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment