From 716a50be7875cb4e5764137196fba285e0ce31a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Mon, 2 Apr 2012 07:14:01 +0200 Subject: [PATCH] Added inline cast operators to the Column::getXxx() appropriate functions + added a standard std::ostream inserter --- src/SQLiteC++/Column.cpp | 8 +++++++ src/SQLiteC++/Column.h | 47 ++++++++++++++++++++++++++++++---------- src/example1/main.cpp | 8 ++++++- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/SQLiteC++/Column.cpp b/src/SQLiteC++/Column.cpp index 005fe95..b4e0bba 100644 --- a/src/SQLiteC++/Column.cpp +++ b/src/SQLiteC++/Column.cpp @@ -53,3 +53,11 @@ const char * Column::getText(void) const // throw(SQLite::Exception) }; // namespace SQLite + + +// Standard std::ostream inserter +std::ostream& operator<<(std::ostream &stream, SQLite::Column const& column) +{ + stream << column.getText(); + return stream; +} diff --git a/src/SQLiteC++/Column.h b/src/SQLiteC++/Column.h index 89dfc05..5d93836 100644 --- a/src/SQLiteC++/Column.h +++ b/src/SQLiteC++/Column.h @@ -33,25 +33,44 @@ public: * @brief Compile and register the SQL query for the provided SQLite Database Connection */ explicit Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, int aIndex) throw(); // nothrow + /// Basic destructor virtual ~Column(void) throw(); // nothrow - /** - * @brief Return the integer value of the column - */ + /// Return the integer value of the column. int getInt (void) const; // throw(SQLite::Exception); - /** - * @brief Return the 64bits integer value of the column - */ + // Return the 64bits integer value of the column. sqlite3_int64 getInt64 (void) const; // throw(SQLite::Exception); - /** - * @brief Return the double (64bits float) value of the column - */ + // Return the double (64bits float) value of the column. double getDouble(void) const; // throw(SQLite::Exception); - /** - * @brief Return the text value (NULL terminated string) of the column - */ + // Return the text value (NULL terminated string) of the column. const char* getText (void) const; // throw(SQLite::Exception); + /// Inline cast operator to int + inline operator const int() const + { + return getInt(); + } + /// Inline cast operator to 64bits integer + inline operator const sqlite3_int64() const + { + return getInt64(); + } + /// Inline cast operator to double + inline operator const double() const + { + return getDouble(); + } + /// Inline cast operator to char* + inline operator const char*() const + { + return getText(); + } + /// Inline cast operator to std::string + inline operator const std::string() const + { + return getText(); + } + private: // Column is copyable, but copy should not be used elsewhere than in return form getColumn Column(void); @@ -65,3 +84,7 @@ private: }; // namespace SQLite + + +/// Standard std::ostream inserter +std::ostream& operator<<(std::ostream &stream, SQLite::Column const& column); diff --git a/src/example1/main.cpp b/src/example1/main.cpp index c4d9f72..47ae027 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -17,7 +17,13 @@ int main (void) while (query.executeStep()) { - std::cout << "row : (" << query.getColumn(0).getInt() << ", " << query.getColumn(1).getText() << ", " << query.getColumn(2).getInt() << ")\n"; + int id = query.getColumn(0); // = query.getColumn(0).getInt() + //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText() + std::string value = query.getColumn(1); // = query.getColumn(1).getText() + int size = query.getColumn(2); // = query.getColumn(2).getInt() + + std::cout << "row : (" << id << ", " << value << ", " << size << ")\n"; + std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ", " << query.getColumn(2) << ")\n"; } } catch (std::exception& e)