From f31b16e7e4c701323f00696f38c6961a70a433c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Sun, 3 Mar 2019 09:11:55 +0100 Subject: [PATCH] Add implicit cast operators to char & short and their unsigned variants Fix #179 error: conversion from 'SQLite::Column' to 'unsigned char' is ambiguous --- CHANGELOG.md | 5 +++-- include/SQLiteCpp/Column.h | 21 +++++++++++++++++++++ tests/Column_test.cpp | 20 ++++++++++++++------ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6a318f7..45f1afff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,7 +116,7 @@ Version 2.2.0 - Sept 19 2017 - Fix warnings #134 - Deprecated Statement::IsOk() to Statement::HasRow() -Version 2.3.0 +Version 2.3.0 - March 3 2019 - Update SQLite3 from 3.20.1 to latest 3.27.2 (2019-02-25) #183 #187 - Add Statement binding for long int values #147 - Allows long int for bind when used with name #148 @@ -124,6 +124,7 @@ Version 2.3.0 - Add comparison with sqlite_orm #141 - Fix Statement::bind truncates long integer to 32 bits on x86_64 Linux #155 - Add a move constructor to Database #157 -- Added tests for all MSVC compilers available on AppVeyor #169 +- Added tests for all MSVC compilers available on AppVeyor (2013, 2015, 2017) #169 - Update VariadicBind.h #172 - Better CMake compatibility #170 +- Add implicit cast operator to char and short types \ No newline at end of file diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h index 1143e69e..c22a8293 100644 --- a/include/SQLiteCpp/Column.h +++ b/include/SQLiteCpp/Column.h @@ -166,6 +166,27 @@ class Column return getBytes (); } + /// Inline cast operator to char + inline operator char() const + { + return static_cast(getInt()); + } + /// Inline cast operator to unsigned char + inline operator unsigned char() const + { + return static_cast(getInt()); + } + /// Inline cast operator to short + inline operator short() const + { + return static_cast(getInt()); + } + /// Inline cast operator to unsigned short + inline operator unsigned short() const + { + return static_cast(getInt()); + } + /// Inline cast operator to int inline operator int() const { diff --git a/tests/Column_test.cpp b/tests/Column_test.cpp index f1815a84..9eb0ad37 100644 --- a/tests/Column_test.cpp +++ b/tests/Column_test.cpp @@ -57,12 +57,16 @@ TEST(Column, basis) { // validates every variant of cast operators, and conversions of types { - const sqlite3_int64 id1 = query.getColumn(0); // operator int64_t() - const int64_t id2 = query.getColumn(0); // operator int64_t() - const long long id3 = query.getColumn(0); // operator int64_t() - const long id4 = query.getColumn(0); // operator int64_t() or long() depending on compiler/architecture - const unsigned int uint1 = query.getColumn(0); // operator uint32_t() - const uint32_t uint2 = query.getColumn(0); // operator uint32_t() + const sqlite3_int64 id1 = query.getColumn(0); // operator long long() + const int64_t id2 = query.getColumn(0); // operator long long() + const long long id3 = query.getColumn(0); // operator long long() + const long id4 = query.getColumn(0); // operator long long() or long() depending on compiler/architecture + const char id5 = query.getColumn(0); // operator char() + const short id6 = query.getColumn(0); // operator short() + const unsigned int uint1 = query.getColumn(0); // operator unsigned int() + const uint32_t uint2 = query.getColumn(0); // operator unsigned int() + const unsigned char uint3 = query.getColumn(0); // operator unsigned char() + const unsigned short uint4 = query.getColumn(0); // operator unsigned short() const char* ptxt = query.getColumn(1); // operator const char*() const std::string msg = query.getColumn(1); // operator std::string() (or const char* with MSVC) const int integer = query.getColumn(2); // operator int() @@ -74,8 +78,12 @@ TEST(Column, basis) { EXPECT_EQ(1, id2); EXPECT_EQ(1, id3); EXPECT_EQ(1, id4); + EXPECT_EQ(1, id5); + EXPECT_EQ(1, id6); EXPECT_EQ(1U, uint1); EXPECT_EQ(1U, uint2); + EXPECT_EQ(1U, uint3); + EXPECT_EQ(1U, uint4); EXPECT_STREQ("first", ptxt); EXPECT_EQ("first", msg); EXPECT_EQ(-123, integer);