From fa16a96443f4b3a7b62ca05338e88d0ce13a849f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Oct 2023 18:59:35 +0100 Subject: Move valueAs to BinaryColumn, implement with std::byteswap Removes the need to cast to/from BSD endian supported types. --- libpqpp/pq-binarycolumn.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'libpqpp/pq-binarycolumn.cpp') diff --git a/libpqpp/pq-binarycolumn.cpp b/libpqpp/pq-binarycolumn.cpp index 514a145..761bf0b 100644 --- a/libpqpp/pq-binarycolumn.cpp +++ b/libpqpp/pq-binarycolumn.cpp @@ -2,6 +2,7 @@ #include "column.h" #include "dbTypes.h" #include "pq-column.h" +#include #include #include #include @@ -9,6 +10,18 @@ PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : PQ::Column(s, f) { } +template +inline T +PQ::BinaryColumn::valueAs() const +{ + T v {}; + std::memcpy(&v, value(), sizeof(T)); + if constexpr (std::endian::native != std::endian::big && sizeof(T) > 1) { + return std::byteswap(v); + } + return v; +} + void PQ::BinaryColumn::apply(DB::HandleField & h) const { @@ -27,13 +40,13 @@ PQ::BinaryColumn::apply(DB::HandleField & h) const h.boolean(valueAs()); break; case INT2OID: - h.integer(static_cast(be16toh(valueAs()))); + h.integer(valueAs()); break; case INT4OID: - h.integer(static_cast(be32toh(valueAs()))); + h.integer(valueAs()); break; case INT8OID: - h.integer(static_cast(be64toh(valueAs()))); + h.integer(valueAs()); break; case BYTEAOID: h.blob(DB::Blob(value(), length())); -- cgit v1.2.3