0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Using portable static asserts for the basic expression operators now

The return type yet needs to be adjusted
This commit is contained in:
rbock 2015-10-09 07:24:05 +02:00
parent 407136c44e
commit 553dc5b016
5 changed files with 164 additions and 88 deletions

View File

@ -28,6 +28,8 @@
#define SQLPP_DETAIL_BASIC_EXPRESSION_OPERATORS_H
#include <sqlpp11/value_type_fwd.h>
#include <sqlpp11/portable_static_assert.h>
#include <sqlpp11/consistent.h>
#include <sqlpp11/alias.h>
#include <sqlpp11/sort_order.h>
#include <sqlpp11/expression_fwd.h>
@ -38,27 +40,33 @@
namespace sqlpp
{
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison");
template <typename LhsValueType, typename RhsType>
using check_rhs_comparison_operand_t = static_check_t<
(is_expression_t<RhsType>::value // expressions are OK
or
is_multi_expression_t<RhsType>::value) // multi-expressions like ANY are OK for comparisons, too
and
LhsValueType::template _is_valid_operand<RhsType>::value, // the correct value type is required, of course
assert_valid_rhs_comparison_operand_t>;
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid");
template <typename LhsValueType, typename... InTypes>
using check_rhs_in_arguments_t =
static_check_t<logic::all_t<check_rhs_comparison_operand_t<LhsValueType, InTypes>::value...>::value,
assert_valid_in_arguments_t>;
// basic operators
template <typename Expr, typename ValueType>
struct basic_expression_operators
{
template <typename T>
struct _is_valid_comparison_operand
{
static constexpr bool value =
(is_expression_t<T>::value // expressions are OK
or
is_multi_expression_t<T>::value) // multi-expressions like ANY are OK for comparisons, too
and
ValueType::template _is_valid_operand<T>::value // the correct value type is required, of course
;
};
template <typename T>
equal_to_t<Expr, wrap_operand_t<T>> operator==(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), {rhs{t}}};
}
@ -67,7 +75,7 @@ namespace sqlpp
not_equal_to_t<Expr, wrap_operand_t<T>> operator!=(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), {rhs{t}}};
}
@ -76,7 +84,7 @@ namespace sqlpp
less_than_t<Expr, wrap_operand_t<T>> operator<(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -85,7 +93,7 @@ namespace sqlpp
less_equal_t<Expr, wrap_operand_t<T>> operator<=(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -94,7 +102,7 @@ namespace sqlpp
greater_than_t<Expr, wrap_operand_t<T>> operator>(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -103,7 +111,7 @@ namespace sqlpp
greater_equal_t<Expr, wrap_operand_t<T>> operator>=(T t) const
{
using rhs = wrap_operand_t<T>;
static_assert(_is_valid_comparison_operand<rhs>::value, "invalid rhs operand in comparison");
check_rhs_comparison_operand_t<ValueType, rhs>{}._();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -132,16 +140,14 @@ namespace sqlpp
template <typename... T>
in_t<Expr, wrap_operand_t<T>...> in(T... t) const
{
static_assert(logic::all_t<_is_valid_comparison_operand<wrap_operand_t<T>>::value...>::value,
"at least one operand of in() is not valid");
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
}
template <typename... T>
not_in_t<Expr, wrap_operand_t<T>...> not_in(T... t) const
{
static_assert(logic::all_t<_is_valid_comparison_operand<wrap_operand_t<T>>::value...>::value,
"at least one operand of in() is not valid");
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
}
};

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_CONSISTENT_H
#define SQLPP_CONSISTENT_H
#include <type_traits>
namespace sqlpp
{
struct consistent_t
{
static constexpr bool value = true;
using type = std::true_type;
static void _(){};
};
}
#endif

View File

@ -28,6 +28,7 @@
#define SQLPP_INSERT_VALUE_LIST_H
#include <sqlpp11/type_traits.h>
#include <sqlpp11/portable_static_assert.h>
#include <sqlpp11/logic.h>
#include <sqlpp11/column_fwd.h>
#include <sqlpp11/assignment.h>
@ -127,64 +128,6 @@ namespace sqlpp
interpretable_list_t<Database> _dynamic_values;
};
#define SQLPP_PORTABLE_STATIC_ASSERT(name, message) \
struct name \
{ \
static constexpr bool value = false; \
using type = std::false_type; \
\
template <typename T = void> \
static void _() \
{ \
static_assert(wrong_t<T>::value, message); \
} \
}
namespace detail
{
template <bool Consistent, typename Assert>
struct static_check_impl
{
using type = Assert;
};
template <typename Assert>
struct static_check_impl<true, Assert>
{
using type = consistent_t;
};
}
template <bool Consistent, typename Assert>
using static_check_t = typename detail::static_check_impl<Consistent, Assert>::type;
namespace detail
{
template <typename... Asserts>
struct static_combined_check_impl;
template <typename Assert, typename... Rest>
struct static_combined_check_impl<Assert, Rest...>
{
using type = Assert;
};
template <typename... Rest>
struct static_combined_check_impl<consistent_t, Rest...>
{
using type = typename static_combined_check_impl<Rest...>::type;
};
template <>
struct static_combined_check_impl<>
{
using type = consistent_t;
};
}
template <typename... Asserts>
using static_combined_check_t = typename detail::static_combined_check_impl<Asserts...>::type;
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_assignments_t, "at least one argument is not an assignment in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_no_duplicates_t, "at least one duplicate column detected in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_prohibited_t,

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_PORTABLE_STATIC_ASSERT_H
#define SQLPP_PORTABLE_STATIC_ASSERT_H
namespace sqlpp
{
#define SQLPP_PORTABLE_STATIC_ASSERT(name, message) \
struct name \
{ \
static constexpr bool value = false; \
using type = std::false_type; \
\
template <typename T = void> \
static void _() \
{ \
static_assert(wrong_t<T>::value, message); \
} \
}
namespace detail
{
template <bool Consistent, typename Assert>
struct static_check_impl
{
using type = Assert;
};
template <typename Assert>
struct static_check_impl<true, Assert>
{
using type = consistent_t;
};
}
template <bool Consistent, typename Assert>
using static_check_t = typename detail::static_check_impl<Consistent, Assert>::type;
namespace detail
{
template <typename... Asserts>
struct static_combined_check_impl;
template <typename Assert, typename... Rest>
struct static_combined_check_impl<Assert, Rest...>
{
using type = Assert;
};
template <typename... Rest>
struct static_combined_check_impl<consistent_t, Rest...>
{
using type = typename static_combined_check_impl<Rest...>::type;
};
template <>
struct static_combined_check_impl<>
{
using type = consistent_t;
};
}
template <typename... Asserts>
using static_combined_check_t = typename detail::static_combined_check_impl<Asserts...>::type;
}
#endif

View File

@ -29,6 +29,7 @@
#include <type_traits>
#include <tuple>
#include <sqlpp11/consistent.h>
#include <sqlpp11/serializer.h>
#include <sqlpp11/detail/type_vector.h>
#include <sqlpp11/detail/type_set.h>
@ -306,14 +307,6 @@ namespace sqlpp
template <typename Policies>
using derived_statement_t = typename Policies::_statement_t;
struct consistent_t
{
static constexpr bool value = true;
using type = std::true_type;
static void _(){};
};
template <typename T>
using is_inconsistent_t =
typename std::conditional<std::is_same<consistent_t, T>::value, std::false_type, std::true_type>::type;