Skip to content

Commit

Permalink
refactor pusher::push as constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
peacalm committed Jun 25, 2023
1 parent 3879e5f commit c9b8969
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions include/peacalm/luaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ struct luaw::pusher {
static const size_t size = 1;

template <typename Y>
static int push(luaw& l, Y&& v) {
static constexpr int push(luaw& l, Y&& v) {
static_assert(std::is_same<T, std::decay_t<Y>>::value,
"Decayed Y should be same type as T");

Expand All @@ -2112,7 +2112,7 @@ struct luaw::pusher<
std::is_class<std::decay_t<T>>::value>> {
static const size_t size = 1;

static int push(luaw& l, T* v) {
static constexpr int push(luaw& l, T* v) {
l.pushlightuserdata(
reinterpret_cast<void*>(const_cast<std::remove_cv_t<T>*>(v)));

Expand All @@ -2133,7 +2133,7 @@ struct luaw::pusher<
!std::is_class<std::decay_t<T>>::value>> {
static const size_t size = 1;

static int push(luaw& l, T* v) {
static constexpr int push(luaw& l, T* v) {
l.pushlightuserdata(
reinterpret_cast<void*>(const_cast<std::remove_cv_t<T>*>(v)));
return 1;
Expand All @@ -2148,7 +2148,7 @@ struct luaw::pusher<
std::enable_if_t<luaw_detail::is_std_unique_ptr<std::decay_t<T>>::value>> {
static const size_t size = 1;

static int push(luaw& l, T* v) {
static constexpr int push(luaw& l, T* v) {
using E = std::conditional_t<
luaw_detail::is_std_unique_ptr_of_array<std::decay_t<T>>::value,
typename T::element_type[],
Expand Down Expand Up @@ -2185,7 +2185,7 @@ struct luaw::pusher<
static const size_t size = 1;

template <typename Y>
static int push(luaw& l, Y&& v) {
static constexpr int push(luaw& l, Y&& v) {
static_assert(luaw_detail::is_std_unique_ptr<std::decay_t<Y>>::value,
"Decayed Y should be std::unique_ptr");

Expand Down Expand Up @@ -2223,7 +2223,7 @@ struct luaw::pusher<luaw::class_tag> {
static const size_t size = 1;

template <typename Y>
static int push(luaw& l, Y&& v) {
static constexpr int push(luaw& l, Y&& v) {
using SolidY = std::remove_reference_t<Y>;
static_assert(std::is_class<std::remove_pointer_t<SolidY>>::value,
"Only class or it's pointer");
Expand Down Expand Up @@ -2256,7 +2256,7 @@ template <>
struct luaw::pusher<luaw::newtable_tag> {
static const size_t size = 1;

static int push(luaw& l, newtable_tag) {
static constexpr int push(luaw& l, newtable_tag) {
l.newtable();
return 1;
}
Expand All @@ -2268,7 +2268,7 @@ struct luaw::pusher<luaw::function_tag> {
static const size_t size = 1;

template <typename F>
static int push(luaw& l, F&& f) {
static constexpr int push(luaw& l, F&& f) {
using DecayF = std::decay_t<F>;
static_assert(luaw_detail::is_callable<DecayF>::value,
"Should push a callable value");
Expand All @@ -2290,7 +2290,7 @@ struct luaw::pusher<std::function<Return(Args...)>> {
static const size_t size = 1;

template <typename F>
static int push(luaw& l, F&& f) {
static constexpr int push(luaw& l, F&& f) {
using CFunctionPtr = Return (*)(Args...);
return luaw::pusher<CFunctionPtr>::push(l, std::forward<F>(f));
}
Expand All @@ -2301,7 +2301,7 @@ template <>
struct luaw::pusher<luaw::lua_cfunction_t> {
static const size_t size = 1;

static int push(luaw& l, lua_cfunction_t f) {
static constexpr int push(luaw& l, lua_cfunction_t f) {
l.pushcfunction(f);
return 1;
}
Expand All @@ -2314,7 +2314,7 @@ struct luaw::pusher<Return (*)(Args...)> {

// function object with non-trivially destructor
template <typename F>
static std::
static constexpr std::
enable_if_t<!std::is_trivially_destructible<std::decay_t<F>>::value, int>
push(luaw& l, F&& f) {
using SolidF = std::remove_reference_t<F>;
Expand Down Expand Up @@ -2361,7 +2361,7 @@ struct luaw::pusher<Return (*)(Args...)> {

// function object with trivially destructor
template <typename F>
static std::
static constexpr std::
enable_if_t<std::is_trivially_destructible<std::decay_t<F>>::value, int>
push(luaw& l, F&& f) {
using SolidF = std::remove_reference_t<F>;
Expand All @@ -2385,7 +2385,7 @@ struct luaw::pusher<Return (*)(Args...)> {
}

// function pointer
static int push(luaw& l, Return (*f)(Args...)) {
static constexpr int push(luaw& l, Return (*f)(Args...)) {
auto closure = [](lua_State* L) -> int {
auto callee = reinterpret_cast<Return (*)(Args...)>(
lua_touserdata(L, lua_upvalueindex(1)));
Expand Down Expand Up @@ -2466,7 +2466,7 @@ template <>
struct luaw::pusher<bool> {
static const size_t size = 1;

static int push(luaw& l, bool v) {
static constexpr int push(luaw& l, bool v) {
lua_pushboolean(l.L(), v);
return 1;
}
Expand All @@ -2478,7 +2478,7 @@ struct luaw::pusher<IntType,
std::enable_if_t<std::is_integral<IntType>::value>> {
static const size_t size = 1;

static int push(luaw& l, IntType v) {
static constexpr int push(luaw& l, IntType v) {
lua_pushinteger(l.L(), v);
return 1;
}
Expand All @@ -2491,7 +2491,7 @@ struct luaw::pusher<
std::enable_if_t<std::is_floating_point<FloatType>::value>> {
static const size_t size = 1;

static int push(luaw& l, FloatType v) {
static constexpr int push(luaw& l, FloatType v) {
lua_pushnumber(l.L(), v);
return 1;
}
Expand All @@ -2502,7 +2502,7 @@ template <>
struct luaw::pusher<std::string> {
static const size_t size = 1;

static int push(luaw& l, const std::string& v) {
static constexpr int push(luaw& l, const std::string& v) {
lua_pushstring(l.L(), v.c_str());
return 1;
}
Expand All @@ -2513,7 +2513,7 @@ template <>
struct luaw::pusher<const char*> {
static const size_t size = 1;

static int push(luaw& l, const char* v) {
static constexpr int push(luaw& l, const char* v) {
// lua_pushstring will push nil if v is NULL, we forbid this
PEACALM_LUAW_ASSERT(v);
lua_pushstring(l.L(), v);
Expand All @@ -2526,7 +2526,7 @@ template <>
struct luaw::pusher<std::nullptr_t> {
static const size_t size = 1;

static int push(luaw& l, std::nullptr_t) {
static constexpr int push(luaw& l, std::nullptr_t) {
l.pushnil();
return 1;
}
Expand All @@ -2537,7 +2537,7 @@ template <typename T, typename U>
struct luaw::pusher<std::pair<T, U>> {
static const size_t size = 1;

static int push(luaw& l, const std::pair<T, U>& p) {
static constexpr int push(luaw& l, const std::pair<T, U>& p) {
l.newtable();
l.push(p.first);
l.rawseti(-2, 1);
Expand All @@ -2551,7 +2551,7 @@ namespace luaw_detail {

// Implementation for all list like containers
template <typename Container>
static int __push_list(luaw& l, const Container& v) {
static constexpr int __push_list(luaw& l, const Container& v) {
l.newtable();
int cnt = 0;
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
Expand All @@ -2564,7 +2564,7 @@ static int __push_list(luaw& l, const Container& v) {
// Implementation for all set like containers
// Make a Key-True table in Lua to represent set
template <typename Container>
static int __push_set(luaw& l, const Container& v) {
static constexpr int __push_set(luaw& l, const Container& v) {
l.newtable();
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
l.push(*b);
Expand All @@ -2576,7 +2576,7 @@ static int __push_set(luaw& l, const Container& v) {

// Implementation for all map like containers
template <typename Container>
static int __push_map(luaw& l, const Container& v) {
static constexpr int __push_map(luaw& l, const Container& v) {
l.newtable();
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
l.push(b->first);
Expand All @@ -2593,7 +2593,7 @@ template <typename T, std::size_t N>
struct luaw::pusher<std::array<T, N>> {
static const size_t size = 1;

static int push(luaw& l, const std::array<T, N>& v) {
static constexpr int push(luaw& l, const std::array<T, N>& v) {
return luaw_detail::__push_list(l, v);
}
};
Expand All @@ -2603,7 +2603,7 @@ template <typename T, typename Allocator>
struct luaw::pusher<std::vector<T, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::vector<T, Allocator>& v) {
static constexpr int push(luaw& l, const std::vector<T, Allocator>& v) {
return luaw_detail::__push_list(l, v);
}
};
Expand All @@ -2613,7 +2613,7 @@ template <typename T, typename Allocator>
struct luaw::pusher<std::deque<T, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::deque<T, Allocator>& v) {
static constexpr int push(luaw& l, const std::deque<T, Allocator>& v) {
return luaw_detail::__push_list(l, v);
}
};
Expand All @@ -2623,7 +2623,7 @@ template <typename T, typename Allocator>
struct luaw::pusher<std::forward_list<T, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::forward_list<T, Allocator>& v) {
static constexpr int push(luaw& l, const std::forward_list<T, Allocator>& v) {
return luaw_detail::__push_list(l, v);
}
};
Expand All @@ -2633,7 +2633,7 @@ template <typename T, typename Allocator>
struct luaw::pusher<std::list<T, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::list<T, Allocator>& v) {
static constexpr int push(luaw& l, const std::list<T, Allocator>& v) {
return luaw_detail::__push_list(l, v);
}
};
Expand All @@ -2643,7 +2643,8 @@ template <typename Key, typename Compare, typename Allocator>
struct luaw::pusher<std::set<Key, Compare, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::set<Key, Compare, Allocator>& v) {
static constexpr int push(luaw& l,
const std::set<Key, Compare, Allocator>& v) {
return luaw_detail::__push_set(l, v);
}
};
Expand All @@ -2653,8 +2654,8 @@ template <typename Key, typename Hash, typename KeyEqual, typename Allocator>
struct luaw::pusher<std::unordered_set<Key, Hash, KeyEqual, Allocator>> {
static const size_t size = 1;

static int push(luaw& l,
const std::unordered_set<Key, Hash, KeyEqual, Allocator>& v) {
static constexpr int push(
luaw& l, const std::unordered_set<Key, Hash, KeyEqual, Allocator>& v) {
return luaw_detail::__push_set(l, v);
}
};
Expand All @@ -2664,7 +2665,8 @@ template <typename Key, typename Compare, typename Allocator>
struct luaw::pusher<std::map<Key, Compare, Allocator>> {
static const size_t size = 1;

static int push(luaw& l, const std::map<Key, Compare, Allocator>& v) {
static constexpr int push(luaw& l,
const std::map<Key, Compare, Allocator>& v) {
return luaw_detail::__push_map(l, v);
}
};
Expand All @@ -2674,8 +2676,8 @@ template <typename Key, typename Hash, typename KeyEqual, typename Allocator>
struct luaw::pusher<std::unordered_map<Key, Hash, KeyEqual, Allocator>> {
static const size_t size = 1;

static int push(luaw& l,
const std::unordered_map<Key, Hash, KeyEqual, Allocator>& v) {
static constexpr int push(
luaw& l, const std::unordered_map<Key, Hash, KeyEqual, Allocator>& v) {
return luaw_detail::__push_map(l, v);
}
};
Expand All @@ -2687,25 +2689,26 @@ template <typename... Ts>
struct luaw::pusher<std::tuple<Ts...>> {
static const size_t size = std::tuple_size<std::tuple<Ts...>>::value;

static int push(luaw& l, const std::tuple<Ts...>& v) {
static constexpr int push(luaw& l, const std::tuple<Ts...>& v) {
const size_t N = size;
int ret_num = __push<0, N>(l, v, std::integral_constant<bool, 0 < N>{});
PEACALM_LUAW_ASSERT(ret_num == N);
return ret_num;
const int ret = __push<0, N>(l, v, std::integral_constant<bool, 0 < N>{});
PEACALM_LUAW_ASSERT(N == ret);
return ret;
}

private:
template <size_t I, size_t N, typename T>
static int __push(luaw& l, const T& t, std::true_type) {
static constexpr int __push(luaw& l, const T& t, std::true_type) {
static_assert(
!luaw_detail::decay_is_stdtuple<std::tuple_element_t<I, T>>::value,
"Recursive tuple is not allowed");
int x = l.push(std::get<I>(t));
int y = __push<I + 1, N>(l, t, std::integral_constant<bool, I + 1 < N>{});
const int x = l.push(std::get<I>(t));
const int y =
__push<I + 1, N>(l, t, std::integral_constant<bool, I + 1 < N>{});
return x + y;
}
template <size_t I, size_t N, typename T>
static int __push(luaw& l, const T& t, std::false_type) {
static constexpr int __push(luaw& l, const T& t, std::false_type) {
return 0;
}
};
Expand All @@ -2714,7 +2717,7 @@ template <>
struct luaw::pusher<luaw::luavalueidx> {
static const size_t size = 1;

static int push(luaw& l, const luaw::luavalueidx& r) {
static constexpr int push(luaw& l, const luaw::luavalueidx& r) {
PEACALM_LUAW_ASSERT(l.L() == r.L);
l.pushvalue(r.idx);
return 1;
Expand All @@ -2725,7 +2728,7 @@ template <>
struct luaw::pusher<luaw::luavalueref> {
static const size_t size = 1;

static int push(luaw& l, const luaw::luavalueref& r) {
static constexpr int push(luaw& l, const luaw::luavalueref& r) {
PEACALM_LUAW_ASSERT(l.L() == r.L);
l.rawgeti(LUA_REGISTRYINDEX, r.ref_id);
return 1;
Expand Down

0 comments on commit c9b8969

Please sign in to comment.