From c5974cfe2726d8088b08dc52edd4a825dc86e147 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 5 Jun 2021 17:31:57 +0100 Subject: Add conversion operators to get common types from DbValues --- lib/dbTypes.h | 74 ++++++++++++++++++++++++++++++++++++++++++++ lib/input/mysqlConn.cpp | 1 - lib/input/mysqlConn.h | 4 ++- lib/input/mysqlStmt.h | 4 ++- lib/output/dumpToConsole.cpp | 1 - lib/output/pq/pqConn.cpp | 1 - lib/output/pq/pqConn.h | 4 ++- lib/output/pq/pqStmt.h | 4 ++- 8 files changed, 86 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/dbTypes.h b/lib/dbTypes.h index 068d642..332b896 100644 --- a/lib/dbTypes.h +++ b/lib/dbTypes.h @@ -2,6 +2,7 @@ #define MYGRATE_DBTYPES_H #include "bitset.h" +#include #include #include #include @@ -36,6 +37,60 @@ namespace MyGrate { using DbValueV = std::variant; + namespace detail { + template + concept HasToString = requires + { + std::to_string(I {}); + }; + + template struct is_false { + static constexpr bool value {false}; + }; + + template typename ConceptT> struct SafeExtract { + R + operator()(const R & i) const + { + return i; + } + + template + R + operator()(const I & i) const + { + if constexpr (ConceptT::value) { + return boost::numeric_cast(i); + } + else { + throw std::logic_error("Unreasonable conversion requested"); + } + } + }; + + struct ToString { + std::string + operator()(const std::string_view & i) const + { + return std::string {i}; + } + + template + std::string + operator()(const I & i) const + { + return std::to_string(i); + } + + template + std::string + operator()(const I &) const + { + throw std::logic_error("Unreasonable to_string requested"); + } + }; + } + class DbValue : public DbValueV { public: using DbValueV::DbValueV; @@ -54,6 +109,25 @@ namespace MyGrate { { return std::get(static_cast(*this)); } + + template operator R() const + { + if constexpr (std::is_integral_v) { + return visit(detail::SafeExtract {}); + } + else if constexpr (std::is_floating_point_v) { + return visit(detail::SafeExtract {}); + } + else if constexpr (std::is_same_v) { + return get(); + } + else if constexpr (std::is_same_v) { + return visit(detail::ToString {}); + } + else { + static_assert(detail::is_false::value, "Cannot extract one of these"); + } + } }; } diff --git a/lib/input/mysqlConn.cpp b/lib/input/mysqlConn.cpp index 46ed7c6..4899deb 100644 --- a/lib/input/mysqlConn.cpp +++ b/lib/input/mysqlConn.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/lib/input/mysqlConn.h b/lib/input/mysqlConn.h index 29a34db..db30ff0 100644 --- a/lib/input/mysqlConn.h +++ b/lib/input/mysqlConn.h @@ -3,10 +3,12 @@ #include #include -#include #include #include +namespace MyGrate { + class DbValue; +} namespace MyGrate::Input { class MySQLConn : public MYSQL, public DbConn { public: diff --git a/lib/input/mysqlStmt.h b/lib/input/mysqlStmt.h index a42e0db..69e62d4 100644 --- a/lib/input/mysqlStmt.h +++ b/lib/input/mysqlStmt.h @@ -3,12 +3,14 @@ #include "dbConn.h" #include "dbRecordSet.h" -#include "dbTypes.h" #include #include #include #include +namespace MyGrate { + class DbValue; +} namespace MyGrate::Input { using StmtPtr = std::unique_ptr; diff --git a/lib/output/dumpToConsole.cpp b/lib/output/dumpToConsole.cpp index 96850f3..949c16f 100644 --- a/lib/output/dumpToConsole.cpp +++ b/lib/output/dumpToConsole.cpp @@ -10,7 +10,6 @@ #include #include #include -#include namespace MyGrate::Output { void diff --git a/lib/output/pq/pqConn.cpp b/lib/output/pq/pqConn.cpp index 81d9610..b3a6ca6 100644 --- a/lib/output/pq/pqConn.cpp +++ b/lib/output/pq/pqConn.cpp @@ -2,7 +2,6 @@ #include "pqBindings.h" #include "pqStmt.h" #include -#include #include #include #include diff --git a/lib/output/pq/pqConn.h b/lib/output/pq/pqConn.h index ae5fc9a..5c9effb 100644 --- a/lib/output/pq/pqConn.h +++ b/lib/output/pq/pqConn.h @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -11,6 +10,9 @@ #include #include +namespace MyGrate { + class DbValue; +} namespace MyGrate::Output::Pq { class PqConn : public DbConn { public: diff --git a/lib/output/pq/pqStmt.h b/lib/output/pq/pqStmt.h index b806617..6531180 100644 --- a/lib/output/pq/pqStmt.h +++ b/lib/output/pq/pqStmt.h @@ -3,13 +3,15 @@ #include "dbConn.h" #include "dbRecordSet.h" -#include "dbTypes.h" #include #include #include #include #include +namespace MyGrate { + class DbValue; +} namespace MyGrate::Output::Pq { class PqConn; -- cgit v1.2.3