summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-10-08 18:59:35 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-10-08 18:59:35 +0100
commitfa16a96443f4b3a7b62ca05338e88d0ce13a849f (patch)
tree3e0a682889caed767990861c4b320a91b83849e8
parentMove to C++23 (for std::byteswap) (diff)
downloadlibdbpp-postgresql-fa16a96443f4b3a7b62ca05338e88d0ce13a849f.tar.bz2
libdbpp-postgresql-fa16a96443f4b3a7b62ca05338e88d0ce13a849f.tar.xz
libdbpp-postgresql-fa16a96443f4b3a7b62ca05338e88d0ce13a849f.zip
Move valueAs to BinaryColumn, implement with std::byteswap
Removes the need to cast to/from BSD endian supported types.
-rw-r--r--libpqpp/pq-binarycolumn.cpp19
-rw-r--r--libpqpp/pq-binarycolumn.h3
-rw-r--r--libpqpp/pq-column.h9
3 files changed, 19 insertions, 12 deletions
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 <bit>
#include <cstdint>
#include <endian.h>
#include <error.h>
@@ -9,6 +10,18 @@
PQ::BinaryColumn::BinaryColumn(const PQ::SelectBase * s, unsigned int f) : PQ::Column(s, f) { }
+template<std::integral T>
+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<bool>());
break;
case INT2OID:
- h.integer(static_cast<int64_t>(be16toh(valueAs<uint16_t>())));
+ h.integer(valueAs<int16_t>());
break;
case INT4OID:
- h.integer(static_cast<int64_t>(be32toh(valueAs<uint32_t>())));
+ h.integer(valueAs<int32_t>());
break;
case INT8OID:
- h.integer(static_cast<int64_t>(be64toh(valueAs<uint64_t>())));
+ h.integer(valueAs<int64_t>());
break;
case BYTEAOID:
h.blob(DB::Blob(value(), length()));
diff --git a/libpqpp/pq-binarycolumn.h b/libpqpp/pq-binarycolumn.h
index e2a8044..e44d7e2 100644
--- a/libpqpp/pq-binarycolumn.h
+++ b/libpqpp/pq-binarycolumn.h
@@ -15,6 +15,9 @@ namespace PQ {
BinaryColumn(const SelectBase *, unsigned int field);
void apply(DB::HandleField &) const override;
+
+ private:
+ template<std::integral T> [[nodiscard]] inline T valueAs() const;
};
}
diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h
index 6bf2669..fa4a78a 100644
--- a/libpqpp/pq-column.h
+++ b/libpqpp/pq-column.h
@@ -17,15 +17,6 @@ namespace PQ {
void apply(DB::HandleField &) const override;
protected:
- template<typename T>
- inline T
- valueAs() const
- {
- T v {};
- std::memcpy(&v, value(), sizeof(T));
- return v;
- }
-
const char * value() const;
std::size_t length() const;