Skip to content

Commit

Permalink
More and better MSVC warnings fixes.
Browse files Browse the repository at this point in the history
Fix warnings by changing the code whenever possible, in particular remove
firebird_rowid_backend class and src/backends/firebird/row-id.cpp file in
which it was defined entirely as it only resulted in "unreachable code"
warnings but was otherwise unused.

Also avoid implicit conversions by either using the correct types or by making
the casts explicit using static_cast<> in places where the casts are really
needed.

Add helpful SOCI_NOT_COPYABLE, SOCI_NOT_ASSIGNABLE and SOCI_UNUSED macros and
use them to suppress the corresponding warnings.

Finally, combine soci-config.h and soci-platform.h in a single file, always
include the latter as the first header and keep the former as a wrapper for
the latter just for compatibility.

Remove the MSVC "#pragma warning(disable: *)" as they are not needed any more.

Closes SOCI#355.
  • Loading branch information
Miha Ravselj authored and vadz committed Jul 15, 2015
1 parent caa2370 commit 7623f76
Show file tree
Hide file tree
Showing 64 changed files with 192 additions and 251 deletions.
54 changes: 37 additions & 17 deletions include/private/firebird/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void setTextParam(char const * s, std::size_t size, char * buf_,
std::string getTextParam(XSQLVAR const *var);

template <typename IntType>
const char *str2dec(const char * s, IntType &out, int &scale)
const char *str2dec(const char * s, IntType &out, short &scale)
{
int sign = 1;
if ('+' == *s)
Expand All @@ -65,7 +65,7 @@ const char *str2dec(const char * s, IntType &out, int &scale)
int d = *s - '0';
if (d < 0 || d > 9)
return s;
res = res * 10 + d * sign;
res = res * 10 + static_cast<IntType>(d * sign);
if (1 == sign)
{
if (res < out)
Expand Down Expand Up @@ -97,19 +97,30 @@ double round_for_isc(double value)
return value < 0 ? value - 0.5 : value + 0.5;
}

//helper template to generate proper code based on compile time type check
template<bool cond> struct cond_to_isc {};
template<> struct cond_to_isc<false>
{
static void checkInteger(short scale, short type)
{
if( scale >= 0 && (type == SQL_SHORT || type == SQL_LONG || type == SQL_INT64) )
throw soci_error("Can't convert non-integral value to integral column type");
}
};
template<> struct cond_to_isc<true>
{
static void checkInteger(short scale,short type) { SOCI_UNUSED(scale) SOCI_UNUSED(type) }
};

template<typename T1>
void to_isc(void * val, XSQLVAR * var, int x_scale = 0)
void to_isc(void * val, XSQLVAR * var, short x_scale = 0)
{
T1 value = *reinterpret_cast<T1*>(val);
short scale = var->sqlscale + x_scale;
short type = var->sqltype & ~1;
long long divisor = 1, multiplier = 1;

if ((std::numeric_limits<T1>::is_integer == false) && scale >= 0 &&
(type == SQL_SHORT || type == SQL_LONG || type == SQL_INT64))
{
throw soci_error("Can't convert non-integral value to integral column type");
}
cond_to_isc<std::numeric_limits<T1>::is_integer>::checkInteger(scale,type);

for (int i = 0; i > scale; --i)
multiplier *= 10;
Expand Down Expand Up @@ -156,7 +167,7 @@ void to_isc(void * val, XSQLVAR * var, int x_scale = 0)
template<typename IntType, typename UIntType>
void parse_decimal(void * val, XSQLVAR * var, const char * s)
{
int scale;
short scale;
UIntType t1;
IntType t2;
if (!*str2dec(s, t1, scale))
Expand Down Expand Up @@ -189,6 +200,22 @@ std::string format_decimal(const void *sqldata, int sqlscale)
return r + std::string(sqlscale, '0');
}


template<bool cond> struct cond_from_isc {};
template<> struct cond_from_isc<true> {
static void checkInteger(short scale)
{
std::ostringstream msg;
msg << "Can't convert value with scale " << -scale
<< " to integral type";
throw soci_error(msg.str());
}
};
template<> struct cond_from_isc<false>
{
static void checkInteger(short scale) { SOCI_UNUSED(scale) }
};

template<typename T1>
T1 from_isc(XSQLVAR * var)
{
Expand All @@ -197,14 +224,7 @@ T1 from_isc(XSQLVAR * var)

if (scale < 0)
{
if (std::numeric_limits<T1>::is_integer)
{
std::ostringstream msg;
msg << "Can't convert value with scale " << -scale
<< " to integral type";
throw soci_error(msg.str());
}

cond_from_isc<std::numeric_limits<T1>::is_integer>::checkInteger(scale);
for (int i = 0; i > scale; --i)
{
tens *= 10;
Expand Down
8 changes: 8 additions & 0 deletions include/soci/bind-values.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class use_type_vector: public std::vector<use_type_base *>

use_type_vector &p;
Indicator &ind;
private:
SOCI_NOT_COPYABLE(use_sequence)
};

template <typename T>
Expand All @@ -73,6 +75,8 @@ class use_type_vector: public std::vector<use_type_base *>
}

use_type_vector &p;
private:
SOCI_NOT_COPYABLE(use_sequence)
};

template <typename T, typename Indicator>
Expand Down Expand Up @@ -144,6 +148,8 @@ class into_type_vector: public std::vector<details::into_type_base *>

into_type_vector &p;
Indicator &ind;
private:
SOCI_NOT_COPYABLE(into_sequence)
};

template <typename T>
Expand All @@ -159,6 +165,8 @@ class into_type_vector: public std::vector<details::into_type_base *>
}

into_type_vector &p;
private:
SOCI_NOT_COPYABLE(into_sequence)
};

template <typename T, typename Indicator>
Expand Down
2 changes: 1 addition & 1 deletion include/soci/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef SOCI_BLOB_H_INCLUDED
#define SOCI_BLOB_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <cstddef>

Expand Down
2 changes: 1 addition & 1 deletion include/soci/connection-parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef SOCI_CONNECTION_PARAMETERS_H_INCLUDED
#define SOCI_CONNECTION_PARAMETERS_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"

#include <map>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion include/soci/connection-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef SOCI_CONNECTION_POOL_H_INCLUDED
#define SOCI_CONNECTION_POOL_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <cstddef>

Expand Down
2 changes: 1 addition & 1 deletion include/soci/db2/soci-db2.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct SOCI_DB2_DECL db2_statement_backend : details::statement_backend

int prepare_for_describe();
void describe_column(int colNum, data_type& dtype, std::string& columnName);
std::size_t column_size(int col);
size_t column_size(int col);

db2_standard_into_type_backend* make_into_type_backend();
db2_standard_use_type_backend* make_use_type_backend();
Expand Down
2 changes: 1 addition & 1 deletion include/soci/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef SOCI_ERROR_H_INCLUDED
#define SOCI_ERROR_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <stdexcept>
#include <string>
Expand Down
11 changes: 2 additions & 9 deletions include/soci/firebird/soci-firebird.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ struct firebird_statement_backend : details::statement_backend
long long rowsAffectedBulk_; // number of rows affected by the last bulk operation

virtual void exchangeData(bool gotData, int row);
virtual void prepareSQLDA(XSQLDA ** sqldap, int size = 10);
virtual void prepareSQLDA(XSQLDA ** sqldap, short size = 10);
virtual void rewriteQuery(std::string const & query,
std::vector<char> & buffer);
virtual void rewriteParameters(std::string const & src,
Expand All @@ -238,13 +238,6 @@ struct firebird_statement_backend : details::statement_backend
bool procedure_;
};

struct firebird_rowid_backend : details::rowid_backend
{
firebird_rowid_backend(firebird_session_backend &session);

~firebird_rowid_backend();
};

struct firebird_blob_backend : details::blob_backend
{
firebird_blob_backend(firebird_session_backend &session);
Expand Down Expand Up @@ -314,7 +307,7 @@ struct firebird_session_backend : details::session_backend
void cleanUp();

virtual firebird_statement_backend * make_statement_backend();
virtual firebird_rowid_backend * make_rowid_backend();
virtual details::rowid_backend* make_rowid_backend();
virtual firebird_blob_backend * make_blob_backend();

bool get_option_decimals_as_strings() { return decimals_as_strings_; }
Expand Down
4 changes: 4 additions & 0 deletions include/soci/into.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct into_container

T &t;
Indicator &ind;
private:
SOCI_NOT_ASSIGNABLE(into_container)
};

typedef void no_indicator;
Expand All @@ -42,6 +44,8 @@ struct into_container<T, no_indicator>
: t(_t) {}

T &t;
private:
SOCI_NOT_ASSIGNABLE(into_container)
};

} // namespace details
Expand Down
6 changes: 5 additions & 1 deletion include/soci/odbc/soci-odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
# define SOCI_ODBC_DECL
#endif

#include "soci/soci-platform.h"
#include <vector>
#include <soci/soci-backend.h>
#include <sstream>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include "soci/soci-platform.h"
#include <windows.h>
#endif
#include <sqlext.h> // ODBC
Expand Down Expand Up @@ -77,6 +77,8 @@ class odbc_standard_type_backend_base
};

odbc_statement_backend &statement_;
private:
SOCI_NOT_COPYABLE(odbc_standard_type_backend_base)
};

struct odbc_standard_into_type_backend : details::standard_into_type_backend,
Expand All @@ -101,6 +103,8 @@ struct odbc_standard_into_type_backend : details::standard_into_type_backend,
int position_;
SQLSMALLINT odbcType_;
SQLLEN valueLen_;
private:
SOCI_NOT_COPYABLE(odbc_standard_into_type_backend)
};

struct odbc_vector_into_type_backend : details::vector_into_type_backend,
Expand Down
8 changes: 1 addition & 7 deletions include/soci/postgresql/soci-postgresql.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
#include <libpq-fe.h>
#include <vector>

#ifdef _MSC_VER
#pragma warning(disable:4512 4511)
#endif

namespace soci
{

Expand Down Expand Up @@ -114,9 +110,7 @@ class postgresql_result

PGresult* result_;

// This class can't be copied as it owns result_ which can't be duplicated.
postgresql_result(postgresql_result const &);
postgresql_result& operator=(postgresql_result const &);
SOCI_NOT_COPYABLE(postgresql_result)
};

} // namespace details
Expand Down
2 changes: 1 addition & 1 deletion include/soci/query_transformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef SOCI_QUERY_TRANSFORMATION_H_INCLUDED
#define SOCI_QUERY_TRANSFORMATION_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#include <functional>
#include <string>

Expand Down
4 changes: 1 addition & 3 deletions include/soci/ref-counted-statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ class SOCI_DECL ref_counted_statement_base
session & session_;

private:
// noncopyable
ref_counted_statement_base(ref_counted_statement_base const&);
ref_counted_statement_base& operator=(ref_counted_statement_base const&);
SOCI_NOT_COPYABLE(ref_counted_statement_base)
};

// this class is supposed to be a vehicle for the "once" statements
Expand Down
2 changes: 2 additions & 0 deletions include/soci/row-exchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class into_type<row>
virtual void convert_from_base() {}

row & r_;

SOCI_NOT_COPYABLE(into_type)
};

template <>
Expand Down
4 changes: 1 addition & 3 deletions include/soci/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ class SOCI_DECL row
}

private:
// copy not supported
row(row const &);
void operator=(row const &);
SOCI_NOT_COPYABLE(row)

std::size_t find_column(std::string const& name) const;

Expand Down
2 changes: 1 addition & 1 deletion include/soci/rowid.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef SOCI_ROWID_H_INCLUDED
#define SOCI_ROWID_H_INCLUDED

#include "soci/soci-config.h"
#include "soci/soci-platform.h"

namespace soci
{
Expand Down
7 changes: 1 addition & 6 deletions include/soci/rowset.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,7 @@ class rowset_impl
const std::auto_ptr<statement> st_;
const std::auto_ptr<T> define_;
#endif


// Non-copyable
rowset_impl(rowset_impl const &);
rowset_impl & operator=(rowset_impl const &);

SOCI_NOT_COPYABLE(rowset_impl)
}; // class rowset_impl

} // namespace details
Expand Down
3 changes: 1 addition & 2 deletions include/soci/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ class SOCI_DECL session
details::blob_backend * make_blob_backend();

private:
session(session const &);
session& operator=(session const &);
SOCI_NOT_COPYABLE(session)

std::ostringstream query_stream_;
details::query_transformation_function* query_transformation_;
Expand Down
Loading

0 comments on commit 7623f76

Please sign in to comment.