mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
object is now more flexible while still supporting the same semantics
indentation an spacing fixes for everything
This commit is contained in:
parent
8e0cc99215
commit
93d532094e
|
@ -59,7 +59,7 @@ inline std::string demangle(const std::type_info& id) {
|
||||||
while(found != std::string::npos) {
|
while(found != std::string::npos) {
|
||||||
realname.erase(found, removals[r].size());
|
realname.erase(found, removals[r].size());
|
||||||
found = realname.find(removals[r]);
|
found = realname.find(removals[r]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(std::size_t r = 0; r < replacements.size(); r+=2) {
|
for(std::size_t r = 0; r < replacements.size(); r+=2) {
|
||||||
|
|
|
@ -34,10 +34,10 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct SOL_DEPRECATED deprecate_type {
|
struct SOL_DEPRECATED deprecate_type {
|
||||||
using type = T;
|
using type = T;
|
||||||
};
|
};
|
||||||
} // detail
|
} // detail
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
|
@ -110,11 +110,11 @@ private:
|
||||||
|
|
||||||
function_result invoke(indices<>, types<>, std::ptrdiff_t n) const {
|
function_result invoke(indices<>, types<>, std::ptrdiff_t n) const {
|
||||||
const int stacksize = lua_gettop(state());
|
const int stacksize = lua_gettop(state());
|
||||||
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
||||||
luacall(n, LUA_MULTRET);
|
luacall(n, LUA_MULTRET);
|
||||||
const int poststacksize = lua_gettop(state());
|
const int poststacksize = lua_gettop(state());
|
||||||
const int returncount = poststacksize - firstreturn;
|
const int returncount = poststacksize - firstreturn;
|
||||||
return function_result(state(), firstreturn + 1, returncount);
|
return function_result(state(), firstreturn + 1, returncount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -172,7 +172,7 @@ struct pusher<function_sig_t<Sigs...>> {
|
||||||
|
|
||||||
template<typename Sig>
|
template<typename Sig>
|
||||||
static void set(lua_State* L, Sig* fxptr){
|
static void set(lua_State* L, Sig* fxptr){
|
||||||
set_fx(std::false_type(), L, fxptr);
|
set_fx(std::false_type(), L, fxptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args, typename R, typename C, typename T>
|
template<typename... Args, typename R, typename C, typename T>
|
||||||
|
|
|
@ -47,7 +47,7 @@ struct functor {
|
||||||
functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward<FxArgs>(fxargs)...) {}
|
functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward<FxArgs>(fxargs)...) {}
|
||||||
|
|
||||||
bool check () const {
|
bool check () const {
|
||||||
return invocation != nullptr;
|
return invocation != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
#include "reference.hpp"
|
#include "reference.hpp"
|
||||||
#include "stack.hpp"
|
#include "stack.hpp"
|
||||||
|
#include "function.hpp"
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
class object : public reference {
|
class object : public reference {
|
||||||
|
@ -32,10 +33,13 @@ public:
|
||||||
object() = default;
|
object() = default;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto as() const -> decltype(stack::get<T>(state())) {
|
auto as() const -> decltype(stack::get<T>(state())) const {
|
||||||
push();
|
push();
|
||||||
type actual = stack::get<type>(state());
|
type actual = stack::get<type>(state());
|
||||||
type_assert(state(), -1, type_of<T>(), actual);
|
// This code is actually present
|
||||||
|
// in almost all of the type-getters,
|
||||||
|
// and it thus insanely redundant
|
||||||
|
// type_assert(state(), -1, type_of<T>(), actual);
|
||||||
return stack::pop<T>(state());
|
return stack::pop<T>(state());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,25 +50,44 @@ public:
|
||||||
return (expected == actual) || (expected == type::poly);
|
return (expected == actual) || (expected == type::poly);
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit operator bool() const {
|
bool valid() const {
|
||||||
return !is<nil_t>();
|
return !this->is<nil_t>();
|
||||||
|
}
|
||||||
|
|
||||||
|
operator const char* () const {
|
||||||
|
return this->as<const char*>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, EnableIf<Not<std::is_same<Unqualified<T>, const char*>>, Not<std::is_same<Unqualified<T>, char>>, Not<std::is_same<Unqualified<T>, std::string>>, Not<std::is_same<Unqualified<T>, std::initializer_list<char>>>> = 0>
|
||||||
|
operator T () const {
|
||||||
|
return this->as<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Ret, typename... Args>
|
||||||
|
stack::get_return_or<function_result, Ret...> call( Args&&... args ) {
|
||||||
|
return this->as<function>()(types<Ret...>(), std::forward<Args>( args )...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
function_result operator()( Args&&... args ) {
|
||||||
|
return this->as<function>()(std::forward<Args>( args )...);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator==(const object& lhs, const nil_t&) {
|
inline bool operator==(const object& lhs, const nil_t&) {
|
||||||
return lhs.is<nil_t>();
|
return !lhs.valid();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator==(const nil_t&, const object& rhs) {
|
inline bool operator==(const nil_t&, const object& rhs) {
|
||||||
return rhs.is<nil_t>();
|
return !rhs.valid();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!=(const object& lhs, const nil_t&) {
|
inline bool operator!=(const object& lhs, const nil_t&) {
|
||||||
return !lhs.is<nil_t>();
|
return lhs.valid();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator!=(const nil_t&, const object& rhs) {
|
inline bool operator!=(const nil_t&, const object& rhs) {
|
||||||
return !rhs.is<nil_t>();
|
return rhs.valid();
|
||||||
}
|
}
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
|
@ -31,18 +31,13 @@ template<typename Table, typename Key>
|
||||||
struct proxy {
|
struct proxy {
|
||||||
private:
|
private:
|
||||||
Table& tbl;
|
Table& tbl;
|
||||||
Key& key;
|
If<std::is_array<Unqualified<Key>>, Key&, Unqualified<Key>> key;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy(Table& table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
|
proxy(Table& table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T get() const {
|
|
||||||
return tbl.template get<T>(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy& set(T&& item) {
|
proxy& set(T&& item) {
|
||||||
tbl.set(key, std::forward<T>(item));
|
tbl.set(key, std::forward<T>(item));
|
||||||
|
@ -56,13 +51,20 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U, EnableIf<Function<Unqualified<U>>> = 0>
|
template<typename U, EnableIf<Function<Unqualified<U>>> = 0>
|
||||||
void operator=(U&& other) {
|
proxy& operator=(U&& other) {
|
||||||
tbl.set_function(key, std::forward<U>(other));
|
tbl.set_function(key, std::forward<U>(other));
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U, DisableIf<Function<Unqualified<U>>> = 0>
|
template<typename U, DisableIf<Function<Unqualified<U>>> = 0>
|
||||||
void operator=(U&& other) {
|
proxy& operator=(U&& other) {
|
||||||
tbl.set(key, std::forward<U>(other));
|
tbl.set(key, std::forward<U>(other));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T get() const {
|
||||||
|
return tbl.template get<T>( key );
|
||||||
}
|
}
|
||||||
|
|
||||||
operator const char* () const {
|
operator const char* () const {
|
||||||
|
|
|
@ -43,7 +43,7 @@ inline auto resolve_f(std::true_type, F&& f)
|
||||||
template<typename F>
|
template<typename F>
|
||||||
inline void resolve_f(std::false_type, F&&) {
|
inline void resolve_f(std::false_type, F&&) {
|
||||||
static_assert(has_deducible_signature<F>::value,
|
static_assert(has_deducible_signature<F>::value,
|
||||||
"Cannot use no-template-parameter call with an overloaded functor: specify the signature");
|
"Cannot use no-template-parameter call with an overloaded functor: specify the signature");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F, typename U = Unqualified<F>>
|
template<typename F, typename U = Unqualified<F>>
|
||||||
|
|
|
@ -378,7 +378,7 @@ struct pusher {
|
||||||
|
|
||||||
template<typename U = T, EnableIf<std::is_base_of<reference, U>> = 0>
|
template<typename U = T, EnableIf<std::is_base_of<reference, U>> = 0>
|
||||||
static int push(lua_State*, T& ref) {
|
static int push(lua_State*, T& ref) {
|
||||||
return ref.push();
|
return ref.push();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
|
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
|
||||||
|
|
|
@ -153,7 +153,7 @@ public:
|
||||||
template<typename... Args, typename... Keys>
|
template<typename... Args, typename... Keys>
|
||||||
auto get(Keys&&... keys) const
|
auto get(Keys&&... keys) const
|
||||||
-> decltype(global.get<Args...>(std::forward<Keys>(keys)...)) {
|
-> decltype(global.get<Args...>(std::forward<Keys>(keys)...)) {
|
||||||
return global.get<Args...>(std::forward<Keys>(keys)...);
|
return global.get<Args...>(std::forward<Keys>(keys)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
|
|
|
@ -58,7 +58,7 @@ public:
|
||||||
|
|
||||||
template<typename... Ret, typename... Keys>
|
template<typename... Ret, typename... Keys>
|
||||||
stack::get_return<Ret...> get( Keys&&... keys ) const {
|
stack::get_return<Ret...> get( Keys&&... keys ) const {
|
||||||
return tuple_get(types<Ret...>(), build_indices<sizeof...(Ret)>(), std::tie(keys...));
|
return tuple_get(types<Ret...>(), build_indices<sizeof...(Ret)>(), std::tie(keys...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
|
@ -118,12 +118,12 @@ public:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy<table, T> operator[]( T&& key ) {
|
proxy<table, T> operator[]( T&& key ) {
|
||||||
return proxy<table, T>( *this, std::forward<T>( key ) );
|
return proxy<table, T>( *this, std::forward<T>( key ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy<const table, T> operator[]( T&& key ) const {
|
proxy<const table, T> operator[]( T&& key ) const {
|
||||||
return proxy<const table, T>( *this, std::forward<T>( key ) );
|
return proxy<const table, T>( *this, std::forward<T>( key ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop(int n = 1) const noexcept {
|
void pop(int n = 1) const noexcept {
|
||||||
|
|
|
@ -286,8 +286,8 @@ struct member_traits : detail::member_traits<Signature> {
|
||||||
|
|
||||||
struct has_begin_end_impl {
|
struct has_begin_end_impl {
|
||||||
template<typename T, typename U = Unqualified<T>,
|
template<typename T, typename U = Unqualified<T>,
|
||||||
typename B = decltype(std::declval<U&>().begin()),
|
typename B = decltype(std::declval<U&>().begin()),
|
||||||
typename E = decltype(std::declval<U&>().end())>
|
typename E = decltype(std::declval<U&>().end())>
|
||||||
static std::true_type test(int);
|
static std::true_type test(int);
|
||||||
|
|
||||||
template<typename...>
|
template<typename...>
|
||||||
|
@ -299,9 +299,9 @@ struct has_begin_end : decltype(has_begin_end_impl::test<T>(0)) {};
|
||||||
|
|
||||||
struct has_key_value_pair_impl {
|
struct has_key_value_pair_impl {
|
||||||
template<typename T, typename U = Unqualified<T>,
|
template<typename T, typename U = Unqualified<T>,
|
||||||
typename V = typename U::value_type,
|
typename V = typename U::value_type,
|
||||||
typename F = decltype(std::declval<V&>().first),
|
typename F = decltype(std::declval<V&>().first),
|
||||||
typename S = decltype(std::declval<V&>().second)>
|
typename S = decltype(std::declval<V&>().second)>
|
||||||
static std::true_type test(int);
|
static std::true_type test(int);
|
||||||
|
|
||||||
template<typename...>
|
template<typename...>
|
||||||
|
|
|
@ -73,7 +73,7 @@ enum class type : int {
|
||||||
userdata = LUA_TUSERDATA,
|
userdata = LUA_TUSERDATA,
|
||||||
lightuserdata = LUA_TLIGHTUSERDATA,
|
lightuserdata = LUA_TLIGHTUSERDATA,
|
||||||
table = LUA_TTABLE,
|
table = LUA_TTABLE,
|
||||||
poly = none | nil | string | number | thread |
|
poly = none | nil | string | number | thread |
|
||||||
table | boolean | function | userdata | lightuserdata
|
table | boolean | function | userdata | lightuserdata
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -96,14 +96,14 @@ inline void type_error(lua_State* L, type expected, type actual) {
|
||||||
|
|
||||||
inline void type_assert(lua_State* L, int index, type expected, type actual) {
|
inline void type_assert(lua_State* L, int index, type expected, type actual) {
|
||||||
if (expected != type::poly && expected != actual) {
|
if (expected != type::poly && expected != actual) {
|
||||||
type_panic(L, index, expected, actual);
|
type_panic(L, index, expected, actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void type_assert(lua_State* L, int index, type expected) {
|
inline void type_assert(lua_State* L, int index, type expected) {
|
||||||
int actual = lua_type(L, index);
|
int actual = lua_type(L, index);
|
||||||
if(expected != type::poly && static_cast<int>(expected) != actual) {
|
if(expected != type::poly && static_cast<int>(expected) != actual) {
|
||||||
type_error(L, static_cast<int>(expected), actual);
|
type_error(L, static_cast<int>(expected), actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -358,11 +358,11 @@ public:
|
||||||
// but leave the regular T table on last
|
// but leave the regular T table on last
|
||||||
// so it can be linked to a type for usage with `.new(...)` or `:new(...)`
|
// so it can be linked to a type for usage with `.new(...)` or `:new(...)`
|
||||||
push_metatable(L, usertype_traits<T*>::metatable,
|
push_metatable(L, usertype_traits<T*>::metatable,
|
||||||
metafunctions, ptrmetafunctiontable);
|
metafunctions, ptrmetafunctiontable);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
push_metatable(L, usertype_traits<T>::metatable,
|
push_metatable(L, usertype_traits<T>::metatable,
|
||||||
metafunctions, metafunctiontable);
|
metafunctions, metafunctiontable);
|
||||||
set_global_deleter(L);
|
set_global_deleter(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user