2013-11-25 17:56:27 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2013 Danny Y., Rapptz
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_TYPES_HPP
|
|
|
|
#define SOL_TYPES_HPP
|
|
|
|
|
|
|
|
#include <lua.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
struct nil_t {};
|
2013-12-03 12:33:23 +08:00
|
|
|
const nil_t nil {};
|
|
|
|
struct void_type {};
|
|
|
|
const void_type Void {};
|
2014-04-26 08:20:35 +08:00
|
|
|
struct lightuserdata_t {
|
|
|
|
void* value;
|
|
|
|
lightuserdata_t(void* data) : value(data) {}
|
|
|
|
operator void* () const { return value; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct userdata_t {
|
|
|
|
void* value;
|
|
|
|
userdata_t(void* data) : value(data) {}
|
|
|
|
operator void* () const { return value; }
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
enum class call_syntax {
|
|
|
|
dot = 0,
|
|
|
|
colon = 1
|
|
|
|
};
|
|
|
|
|
2013-11-25 17:56:27 +08:00
|
|
|
enum class type : int {
|
2013-12-03 12:33:23 +08:00
|
|
|
none = LUA_TNONE,
|
|
|
|
nil = LUA_TNIL,
|
|
|
|
string = LUA_TSTRING,
|
|
|
|
number = LUA_TNUMBER,
|
|
|
|
thread = LUA_TTHREAD,
|
|
|
|
boolean = LUA_TBOOLEAN,
|
|
|
|
function = LUA_TFUNCTION,
|
|
|
|
userdata = LUA_TUSERDATA,
|
2013-11-25 17:56:27 +08:00
|
|
|
lightuserdata = LUA_TLIGHTUSERDATA,
|
2013-12-03 12:33:23 +08:00
|
|
|
table = LUA_TTABLE,
|
|
|
|
poly = none | nil | string | number | thread |
|
|
|
|
table | boolean | function | userdata | lightuserdata
|
2013-11-25 17:56:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
inline void type_error(lua_State* L, int expected, int actual) {
|
|
|
|
luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void type_assert(lua_State* L, int index, type expected) {
|
|
|
|
int actual = lua_type(L, index);
|
2013-12-01 10:12:50 +08:00
|
|
|
if(expected != type::poly && static_cast<int>(expected) != actual) {
|
2013-11-25 17:56:27 +08:00
|
|
|
type_error(L, static_cast<int>(expected), actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
inline std::string type_name(lua_State*L, type t) {
|
|
|
|
return lua_typename(L, static_cast<int>(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2014-04-26 08:20:35 +08:00
|
|
|
class userdata;
|
2013-11-25 17:56:27 +08:00
|
|
|
class table;
|
|
|
|
class function;
|
2013-12-01 10:12:50 +08:00
|
|
|
class object;
|
2013-11-25 17:56:27 +08:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
|
|
|
inline type arithmetic(std::true_type) {
|
|
|
|
return type::number;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline type arithmetic(std::false_type) {
|
|
|
|
return type::none;
|
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline type type_of() {
|
|
|
|
return detail::arithmetic<T>(std::is_arithmetic<T>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<table>() {
|
|
|
|
return type::table;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<function>() {
|
|
|
|
return type::function;
|
|
|
|
}
|
|
|
|
|
2013-12-01 10:12:50 +08:00
|
|
|
template<>
|
|
|
|
inline type type_of<object>() {
|
|
|
|
return type::poly;
|
|
|
|
}
|
|
|
|
|
2013-11-25 17:56:27 +08:00
|
|
|
template<>
|
|
|
|
inline type type_of<const char*>() {
|
|
|
|
return type::string;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<std::string>() {
|
|
|
|
return type::string;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<nil_t>() {
|
|
|
|
return type::nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<bool>() {
|
|
|
|
return type::boolean;
|
|
|
|
}
|
2013-12-14 13:15:14 +08:00
|
|
|
|
2014-04-26 08:20:35 +08:00
|
|
|
template<>
|
|
|
|
inline type type_of<lightuserdata_t>() {
|
|
|
|
return type::lightuserdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline type type_of<userdata_t>() {
|
|
|
|
return type::userdata;
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:15:14 +08:00
|
|
|
inline bool operator==(nil_t, nil_t) { return true; }
|
2013-12-15 09:16:28 +08:00
|
|
|
inline bool operator!=(nil_t, nil_t) { return false; }
|
2013-11-25 17:56:27 +08:00
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_TYPES_HPP
|