Skip to content

Commit

Permalink
Use SFINAE to collapse numeric pust_table_value impls
Browse files Browse the repository at this point in the history
Signed-off-by: Tin <[email protected]>
  • Loading branch information
Caellian committed Nov 8, 2023
1 parent 4b67529 commit 091efc3
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/mouse-events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,28 @@
#include <string>
#include <lua.h>
#include <time.h>
#include <type_traits>

/* Lua helper functions */
template <typename T>
void push_table_value(lua_State *L, std::string key, T value);

void push_table_value(lua_State *L, std::string key, std::string value) {
lua_pushstring(L, key.c_str());
lua_pushstring(L, value.c_str());
lua_settable(L, -3);
}

void push_table_value(lua_State *L, std::string key, int value) {
lua_pushstring(L, key.c_str());
lua_pushinteger(L, value);
lua_settable(L, -3);
}

void push_table_value(lua_State *L, std::string key, uint32_t value) {
lua_pushstring(L, key.c_str());
lua_pushinteger(L, value);
lua_settable(L, -3);
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
push_table_value(lua_State *L, std::string key, T value) {
lua_pushstring(L, key.c_str());
lua_pushinteger(L, value);
lua_settable(L, -3);
}

void push_table_value(lua_State *L, std::string key, uint64_t value) {
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
push_table_value(lua_State *L, std::string key, T value) {
lua_pushstring(L, key.c_str());
lua_pushinteger(L, value);
lua_pushnumber(L, value);
lua_settable(L, -3);
}

Expand All @@ -59,18 +55,6 @@ void push_table_value(lua_State *L, std::string key, bool value) {
lua_settable(L, -3);
}

void push_table_value(lua_State *L, std::string key, float value) {
lua_pushstring(L, key.c_str());
lua_pushnumber(L, value);
lua_settable(L, -3);
}

void push_table_value(lua_State *L, std::string key, double value) {
lua_pushstring(L, key.c_str());
lua_pushnumber(L, value);
lua_settable(L, -3);
}

template <size_t N>
void push_bitset(lua_State *L, std::bitset<N> it,
std::array<std::string, N> labels) {
Expand Down

0 comments on commit 091efc3

Please sign in to comment.