Skip to content

Commit

Permalink
Use SFINAE to collapse all 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 c64aab2
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 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 Expand Up @@ -177,7 +161,7 @@ void mouse_event::push_lua_table(lua_State *L) const {

void mouse_positioned_event::push_lua_data(lua_State *L) const {
push_table_value(L, "x", this->x);
push_table_value(L, "y", this->y);
push_table_value<uint64_t>(L, "y", this->y);
push_table_value(L, "x_abs", this->x_abs);
push_table_value(L, "y_abs", this->y_abs);
}
Expand Down

0 comments on commit c64aab2

Please sign in to comment.