Add presentation from Lua Workshop 2016

Have proper detection for numbers (even if it comes at a higher cost)
This commit is contained in:
ThePhD 2016-10-15 18:14:19 -04:00
parent f3fbd24226
commit 940f44db55
5 changed files with 83 additions and 3 deletions

3
.gitignore vendored
View File

@ -66,6 +66,7 @@ lua53.dll
main.exe
main.o
lua-5.3.3/
*.pdf
main.lua
LuaJIT-2.1.0/
lua-5.3.3-cxx/
lua-5.3.3.vcxproj-cxx.filters

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2016-10-11 10:53:03.759428 UTC
// This header was generated with sol v2.14.10 (revision 866a297)
// Generated 2016-10-15 21:59:41.897573 UTC
// This header was generated with sol v2.14.10 (revision f3fbd24)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -4696,6 +4696,34 @@ namespace sol {
}
};
template<typename T>
struct checker<T, type::number, std::enable_if_t<std::is_integral<T>::value>> {
template <typename Handler>
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
tracking.use(1);
bool success = lua_isinteger(L, index) == 1;
if (!success) {
// expected type, actual type
handler(L, index, type::number, type_of(L, index));
}
return success;
}
};
template<typename T>
struct checker<T, type::number, std::enable_if_t<std::is_floating_point<T>::value>> {
template <typename Handler>
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
tracking.use(1);
bool success = lua_isnumber(L, index) == 1;
if (!success) {
// expected type, actual type
handler(L, index, type::number, type_of(L, index));
}
return success;
}
};
template <type expected, typename C>
struct checker<nil_t, expected, C> {
template <typename Handler>

View File

@ -77,6 +77,34 @@ namespace sol {
}
};
template<typename T>
struct checker<T, type::number, std::enable_if_t<std::is_integral<T>::value>> {
template <typename Handler>
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
tracking.use(1);
bool success = lua_isinteger(L, index) == 1;
if (!success) {
// expected type, actual type
handler(L, index, type::number, type_of(L, index));
}
return success;
}
};
template<typename T>
struct checker<T, type::number, std::enable_if_t<std::is_floating_point<T>::value>> {
template <typename Handler>
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
tracking.use(1);
bool success = lua_isnumber(L, index) == 1;
if (!success) {
// expected type, actual type
handler(L, index, type::number, type_of(L, index));
}
return success;
}
};
template <type expected, typename C>
struct checker<nil_t, expected, C> {
template <typename Handler>

View File

@ -678,3 +678,26 @@ TEST_CASE("compilation/const-regression", "make sure constness in tables is resp
State* s = state.state_.registry()["state"];
REQUIRE(s == &state);
}
TEST_CASE("numbers/integers", "make sure integers are detectable on most platforms") {
sol::state lua;
lua["a"] = 50; // int
lua["b"] = 50.5; // double
sol::object a = lua["a"];
sol::object b = lua["b"];
bool a_is_int = a.is<int>();
bool a_is_double = a.is<double>();
bool b_is_int = b.is<int>();
bool b_is_double = b.is<double>();
REQUIRE(a_is_int);
REQUIRE(a_is_double);
// TODO: will this fail on certain lower Lua versions?
REQUIRE_FALSE(a_is_int);
REQUIRE(a_is_double);
}