Skip to content

Commit 45f4133

Browse files
committed
Unify Datatype equality semantics
1 parent d107a95 commit 45f4133

3 files changed

Lines changed: 56 additions & 42 deletions

File tree

include/openPMD/Datatype.hpp

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ inline size_t toBits(Datatype d)
420420
return toBytes(d) * CHAR_BIT;
421421
}
422422

423+
constexpr bool isSigned(Datatype d);
424+
423425
/** Compare if a Datatype is a vector type
424426
*
425427
* @param d Datatype to test
@@ -714,40 +716,7 @@ constexpr bool isSameChar(Datatype d1, Datatype d2);
714716
* some platforms, e.g. if long and long long are the same or double and
715717
* long double will also return true.
716718
*/
717-
inline bool isSame(openPMD::Datatype const d, openPMD::Datatype const e)
718-
{
719-
// exact same type
720-
if (static_cast<int>(d) == static_cast<int>(e))
721-
return true;
722-
723-
bool d_is_vec = isVector(d);
724-
bool e_is_vec = isVector(e);
725-
726-
// same int
727-
bool d_is_int, d_is_sig;
728-
std::tie(d_is_int, d_is_sig) = isInteger(d);
729-
bool e_is_int, e_is_sig;
730-
std::tie(e_is_int, e_is_sig) = isInteger(e);
731-
if (d_is_int && e_is_int && d_is_vec == e_is_vec && d_is_sig == e_is_sig &&
732-
toBits(d) == toBits(e))
733-
return true;
734-
735-
// same float
736-
bool d_is_fp = isFloatingPoint(d);
737-
bool e_is_fp = isFloatingPoint(e);
738-
739-
if (d_is_fp && e_is_fp && d_is_vec == e_is_vec && toBits(d) == toBits(e))
740-
return true;
741-
742-
// same complex floating point
743-
bool d_is_cfp = isComplexFloatingPoint(d);
744-
bool e_is_cfp = isComplexFloatingPoint(e);
745-
746-
if (d_is_cfp && e_is_cfp && d_is_vec == e_is_vec && toBits(d) == toBits(e))
747-
return true;
748-
749-
return false;
750-
}
719+
constexpr bool isSame(openPMD::Datatype d, openPMD::Datatype e);
751720

752721
/**
753722
* @brief basicDatatype Strip openPMD Datatype of std::vector, std::array et.

include/openPMD/Datatype.tpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// comment to prevent clang-format from moving this #include up
2626
// datatype macros may be included and un-included in other headers
2727
#include "openPMD/DatatypeMacros.hpp"
28+
#include "openPMD/auxiliary/TypeTraits.hpp"
2829

2930
#include <string>
3031
#include <type_traits> // std::void_t
@@ -253,6 +254,56 @@ constexpr inline bool isSameChar(Datatype d)
253254
{
254255
return switchType<detail::IsSameChar<T_Char>>(d);
255256
}
257+
258+
namespace detail
259+
{
260+
struct IsSigned
261+
{
262+
template <typename T>
263+
static constexpr bool call()
264+
{
265+
if constexpr (auxiliary::IsVector_v<T> || auxiliary::IsArray_v<T>)
266+
{
267+
return call<typename T::value_type>();
268+
}
269+
else if constexpr (std::is_same_v<T, std::string>)
270+
{
271+
return call<char>();
272+
}
273+
else
274+
{
275+
return std::is_signed_v<T>;
276+
}
277+
}
278+
279+
static constexpr char const *errorMsg = "IsSigned";
280+
};
281+
} // namespace detail
282+
283+
constexpr inline bool isSigned(Datatype d)
284+
{
285+
return switchType<detail::IsSigned>(d);
286+
}
287+
288+
constexpr inline bool isSameChar(Datatype d, Datatype e)
289+
{
290+
return isChar(d) && isChar(e) && isSigned(d) == isSigned(e);
291+
}
292+
293+
constexpr bool isSame(openPMD::Datatype const d, openPMD::Datatype const e)
294+
{
295+
return
296+
// exact same type
297+
static_cast<int>(d) == static_cast<int>(e)
298+
// same int
299+
|| isSameInteger(d, e)
300+
// same float
301+
|| isSameFloatingPoint(d, e)
302+
// same complex floating point
303+
|| isSameComplexFloatingPoint(d, e)
304+
// same char
305+
|| isSameChar(d, e);
306+
}
256307
} // namespace openPMD
257308

258309
#include "openPMD/UndefDatatypeMacros.hpp"

include/openPMD/backend/PatchRecordComponent.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ template <typename T>
160160
inline void PatchRecordComponent::store(uint64_t idx, T data)
161161
{
162162
Datatype dtype = determineDatatype<T>();
163-
if (dtype != getDatatype() && !isSameInteger<T>(getDatatype()) &&
164-
!isSameFloatingPoint<T>(getDatatype()) &&
165-
!isSameComplexFloatingPoint<T>(getDatatype()) &&
166-
!isSameChar<T>(getDatatype()))
163+
if (dtype != getDatatype())
167164
{
168165
std::ostringstream oss;
169166
oss << "Datatypes of patch data (" << dtype << ") and dataset ("
@@ -190,10 +187,7 @@ template <typename T>
190187
inline void PatchRecordComponent::store(T data)
191188
{
192189
Datatype dtype = determineDatatype<T>();
193-
if (dtype != getDatatype() && !isSameInteger<T>(getDatatype()) &&
194-
!isSameFloatingPoint<T>(getDatatype()) &&
195-
!isSameComplexFloatingPoint<T>(getDatatype()) &&
196-
!isSameChar<T>(getDatatype()))
190+
if (dtype != getDatatype())
197191
{
198192
std::ostringstream oss;
199193
oss << "Datatypes of patch data (" << dtype << ") and dataset ("

0 commit comments

Comments
 (0)