Skip to content

Commit

Permalink
Replace decay_t with more appropriate type conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Possseidon committed Sep 26, 2021
1 parent 64c4ece commit 8d7373c
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions dang-lua/include/dang-lua/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "dang-utils/utils.h"

// TODO: Replace decay_t with remove_reference_t or whatever is appropriate.
// TODO: More explicit about which index parameters must be positive

namespace dang::lua {
Expand Down Expand Up @@ -2425,9 +2424,9 @@ class StateBase {
using ConvertValue = Convert<std::remove_reference_t<TValue>>;

static_assert(ConvertValue::push_count == 1, "Supplied value must take up a single stack position.");
static_assert(is_index_v<std::decay_t<TIndex>>, "Supplied index must be an index.");
static_assert(is_index_v<std::remove_reference_t<TIndex>>, "Supplied index must be an index.");

if constexpr (is_index_v<std::decay_t<TValue>>) {
if constexpr (is_index_v<std::remove_reference_t<TValue>>) {
assertPushable();
lua_copy(state_, value.index(), index.index());
if constexpr (is_any_moved_stack_index_result_v<TValue&&>)
Expand Down Expand Up @@ -2761,8 +2760,8 @@ class StateBase {
static_assert(ConvertLeft::push_count == 1, "Left operand must take up a single stack position.");
static_assert(ConvertRight::push_count == 1, "Right operand must take up a single stack position.");

constexpr bool left_is_index = is_index_v<std::decay_t<TLeft>>;
constexpr bool right_is_index = is_index_v<std::decay_t<TRight>>;
constexpr bool left_is_index = is_index_v<std::remove_reference_t<TLeft>>;
constexpr bool right_is_index = is_index_v<std::remove_reference_t<TRight>>;
if constexpr (left_is_index) {
if constexpr (right_is_index)
return lua_compare(state_, lhs.index(), rhs.index(), static_cast<int>(operation)) != 0;
Expand Down Expand Up @@ -2835,7 +2834,8 @@ class StateBase {

static_assert(ConvertKey::push_count == 1, "Supplied key must take up a single stack position.");

if constexpr (std::is_integral_v<std::decay_t<TKey>> && !std::is_same_v<std::decay_t<TKey>, bool>) {
if constexpr (std::is_integral_v<std::remove_reference_t<TKey>> &&
!std::is_same_v<dutils::remove_cvref_t<TKey>, bool>) {
assertPushable();
// lua_Integer{ key } disallows narrowing conversions, which is perfect
auto type = static_cast<Type>(lua_geti(state_, table.index(), lua_Integer{key}));
Expand All @@ -2853,7 +2853,7 @@ class StateBase {
notifyPush();
return std::tuple{type, top().asResult()};
}
else if constexpr (std::is_same_v<std::decay_t<TKey>, std::string>) {
else if constexpr (std::is_same_v<dutils::remove_cvref_t<TKey>, std::string>) {
return getTable(table, key.c_str());
}
else {
Expand Down Expand Up @@ -2886,7 +2886,8 @@ class StateBase {
static_assert(ConvertKey::push_count == 1, "Supplied key must take up a single stack position.");
static_assert(ConvertValue::push_count == 1, "Supplied value must take up a single stack position.");

if constexpr (std::is_integral_v<std::decay_t<TKey>> && !std::is_same_v<std::decay_t<TKey>, bool>) {
if constexpr (std::is_integral_v<std::remove_reference_t<TKey>> &&
!std::is_same_v<dutils::remove_cvref_t<TKey>, bool>) {
push(std::forward<TValue>(value));
// lua_Integer{ key } disallows narrowing conversions, which is perfect
lua_seti(state_, table.index(), lua_Integer{key});
Expand All @@ -2901,7 +2902,7 @@ class StateBase {
// -1, +0
notifyPush(-1);
}
else if constexpr (std::is_same_v<std::decay_t<TKey>, std::string>) {
else if constexpr (std::is_same_v<dutils::remove_cvref_t<TKey>, std::string>) {
setTable(table, key.c_str(), std::forward<TValue>(value));
}
else {
Expand All @@ -2921,7 +2922,8 @@ class StateBase {

static_assert(ConvertKey::push_count == 1, "Supplied key must take up a single stack position.");

if constexpr (std::is_integral_v<std::decay_t<TKey>> && !std::is_same_v<std::decay_t<TKey>, bool>) {
if constexpr (std::is_integral_v<std::remove_reference_t<TKey>> &&
!std::is_same_v<dutils::remove_cvref_t<TKey>, bool>) {
assertPushable();
// lua_Integer{ key } disallows narrowing conversions, which is perfect
auto type = static_cast<Type>(lua_rawgeti(state_, table.index(), lua_Integer{key}));
Expand Down Expand Up @@ -2966,7 +2968,8 @@ class StateBase {
static_assert(ConvertKey::push_count == 1, "Supplied key must take up a single stack position.");
static_assert(ConvertValue::push_count == 1, "Supplied value must take up a single stack position.");

if constexpr (std::is_integral_v<std::decay_t<TKey>> && !std::is_same_v<std::decay_t<TKey>, bool>) {
if constexpr (std::is_integral_v<std::remove_reference_t<TKey>> &&
!std::is_same_v<dutils::remove_cvref_t<TKey>, bool>) {
push(std::forward<TValue>(value));
// lua_Integer{ key } disallows narrowing conversions, which is perfect
lua_rawseti(state_, table.index(), lua_Integer{key});
Expand Down Expand Up @@ -3002,7 +3005,7 @@ class StateBase {
notifyPush(1);
return std::tuple{type, top().asResult()};
}
else if constexpr (std::is_same_v<std::decay_t<TKey>, std::string>) {
else if constexpr (std::is_same_v<dutils::remove_cvref_t<TKey>, std::string>) {
return getGlobalWithType(key.c_str());
}
else {
Expand Down Expand Up @@ -3032,7 +3035,7 @@ class StateBase {
lua_setglobal(state_, key);
notifyPush(-1);
}
else if constexpr (std::is_same_v<std::decay_t<TKey>, std::string>) {
else if constexpr (std::is_same_v<dutils::remove_cvref_t<TKey>, std::string>) {
return setGlobal(key.c_str(), std::forward<TValue>(value));
}
else {
Expand Down Expand Up @@ -4520,7 +4523,7 @@ template <typename... TIterators>
class iterator_variant : public std::variant<TIterators...> {
public:
using difference_type = lua_Integer;
using value_type = std::decay_t<decltype((*std::declval<TIterators>(), ...))>;
using value_type = dutils::remove_cvref_t<decltype((*std::declval<TIterators>(), ...))>;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::input_iterator_tag;
Expand Down Expand Up @@ -4871,15 +4874,15 @@ namespace detail {
template <typename TLeft, typename TRight>
inline auto& stateOf(TLeft& lhs, TRight& rhs)
{
if constexpr (is_index_v<std::decay_t<TLeft>>)
if constexpr (is_index_v<TLeft>)
return lhs.state();
else
return rhs.state();
}

/// @brief Whether any of the type parameters is an index.
template <typename... TArgs>
using any_is_index = std::disjunction<is_index<std::decay_t<TArgs>>...>;
using any_is_index = std::disjunction<is_index<std::remove_reference_t<TArgs>>...>;

template <typename... TArgs>
inline constexpr auto any_is_index_v = any_is_index<TArgs...>::value;
Expand Down Expand Up @@ -4964,7 +4967,8 @@ std::forward<TRight>(rhs));

template <typename TLeft, typename TRight>
inline auto operator<<(TLeft&& lhs, TRight&& rhs)
-> std::enable_if_t<detail::any_is_index_v<TLeft, TRight> && !std::is_same_v<std::decay_t<TLeft>, std::ostream>,
-> std::enable_if_t<detail::any_is_index_v<TLeft, TRight> &&
!std::is_same_v<dutils::remove_cvref_t<TLeft>, std::ostream>,
StackIndexResult>
{
return detail::stateOf(lhs, rhs).template arith<ArithOp::LeftShift>(std::forward<TLeft>(lhs),
Expand Down

0 comments on commit 8d7373c

Please sign in to comment.