From 93d532094edc7cfa57a0cf9073bb71c67ee1c668 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Tue, 20 Oct 2015 21:38:28 -0400 Subject: [PATCH] object is now more flexible while still supporting the same semantics indentation an spacing fixes for everything --- sol/demangle.hpp | 2 +- sol/deprecate.hpp | 8 ++++---- sol/function.hpp | 6 +++--- sol/function_types.hpp | 2 +- sol/object.hpp | 39 +++++++++++++++++++++++++++++++-------- sol/proxy.hpp | 18 ++++++++++-------- sol/resolve.hpp | 2 +- sol/stack.hpp | 2 +- sol/state.hpp | 2 +- sol/table.hpp | 18 +++++++++--------- sol/traits.hpp | 10 +++++----- sol/types.hpp | 6 +++--- sol/usertype.hpp | 4 ++-- 13 files changed, 72 insertions(+), 47 deletions(-) diff --git a/sol/demangle.hpp b/sol/demangle.hpp index 56a132ea..90bc0320 100644 --- a/sol/demangle.hpp +++ b/sol/demangle.hpp @@ -59,7 +59,7 @@ inline std::string demangle(const std::type_info& id) { while(found != std::string::npos) { realname.erase(found, removals[r].size()); found = realname.find(removals[r]); - } + } } for(std::size_t r = 0; r < replacements.size(); r+=2) { diff --git a/sol/deprecate.hpp b/sol/deprecate.hpp index 0d20c23a..b59cc74a 100644 --- a/sol/deprecate.hpp +++ b/sol/deprecate.hpp @@ -34,10 +34,10 @@ namespace sol { namespace detail { - template - struct SOL_DEPRECATED deprecate_type { - using type = T; - }; + template + struct SOL_DEPRECATED deprecate_type { + using type = T; + }; } // detail } // sol diff --git a/sol/function.hpp b/sol/function.hpp index 06e12072..0aa8373f 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -110,11 +110,11 @@ private: function_result invoke(indices<>, types<>, std::ptrdiff_t n) const { const int stacksize = lua_gettop(state()); - const int firstreturn = std::max(0, stacksize - static_cast(n) - 1); + const int firstreturn = std::max(0, stacksize - static_cast(n) - 1); luacall(n, LUA_MULTRET); const int poststacksize = lua_gettop(state()); const int returncount = poststacksize - firstreturn; - return function_result(state(), firstreturn + 1, returncount); + return function_result(state(), firstreturn + 1, returncount); } public: @@ -172,7 +172,7 @@ struct pusher> { template static void set(lua_State* L, Sig* fxptr){ - set_fx(std::false_type(), L, fxptr); + set_fx(std::false_type(), L, fxptr); } template diff --git a/sol/function_types.hpp b/sol/function_types.hpp index 131c41a2..747fcf48 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -47,7 +47,7 @@ struct functor { functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward(fxargs)...) {} bool check () const { - return invocation != nullptr; + return invocation != nullptr; } template diff --git a/sol/object.hpp b/sol/object.hpp index 38db7243..02759208 100644 --- a/sol/object.hpp +++ b/sol/object.hpp @@ -24,6 +24,7 @@ #include "reference.hpp" #include "stack.hpp" +#include "function.hpp" namespace sol { class object : public reference { @@ -32,10 +33,13 @@ public: object() = default; template - auto as() const -> decltype(stack::get(state())) { + auto as() const -> decltype(stack::get(state())) const { push(); type actual = stack::get(state()); - type_assert(state(), -1, type_of(), actual); + // This code is actually present + // in almost all of the type-getters, + // and it thus insanely redundant + // type_assert(state(), -1, type_of(), actual); return stack::pop(state()); } @@ -46,25 +50,44 @@ public: return (expected == actual) || (expected == type::poly); } - explicit operator bool() const { - return !is(); + bool valid() const { + return !this->is(); + } + + operator const char* () const { + return this->as(); + } + + template, const char*>>, Not, char>>, Not, std::string>>, Not, std::initializer_list>>> = 0> + operator T () const { + return this->as(); + } + + template + stack::get_return_or call( Args&&... args ) { + return this->as()(types(), std::forward( args )...); + } + + template + function_result operator()( Args&&... args ) { + return this->as()(std::forward( args )...); } }; inline bool operator==(const object& lhs, const nil_t&) { - return lhs.is(); + return !lhs.valid(); } inline bool operator==(const nil_t&, const object& rhs) { - return rhs.is(); + return !rhs.valid(); } inline bool operator!=(const object& lhs, const nil_t&) { - return !lhs.is(); + return lhs.valid(); } inline bool operator!=(const nil_t&, const object& rhs) { - return !rhs.is(); + return rhs.valid(); } } // sol diff --git a/sol/proxy.hpp b/sol/proxy.hpp index a314a017..1a69dc57 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -31,18 +31,13 @@ template struct proxy { private: Table& tbl; - Key& key; + If>, Key&, Unqualified> key; public: template proxy(Table& table, T&& key) : tbl(table), key(std::forward(key)) {} - template - T get() const { - return tbl.template get(key); - } - template proxy& set(T&& item) { tbl.set(key, std::forward(item)); @@ -56,13 +51,20 @@ public: } template>> = 0> - void operator=(U&& other) { + proxy& operator=(U&& other) { tbl.set_function(key, std::forward(other)); + return *this; } template>> = 0> - void operator=(U&& other) { + proxy& operator=(U&& other) { tbl.set(key, std::forward(other)); + return *this; + } + + template + T get() const { + return tbl.template get( key ); } operator const char* () const { diff --git a/sol/resolve.hpp b/sol/resolve.hpp index e8702b5c..8fa8aafd 100644 --- a/sol/resolve.hpp +++ b/sol/resolve.hpp @@ -43,7 +43,7 @@ inline auto resolve_f(std::true_type, F&& f) template inline void resolve_f(std::false_type, F&&) { static_assert(has_deducible_signature::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> diff --git a/sol/stack.hpp b/sol/stack.hpp index 4c9610f7..138bc851 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -378,7 +378,7 @@ struct pusher { template> = 0> static int push(lua_State*, T& ref) { - return ref.push(); + return ref.push(); } template, EnableIf>, Not>, Not>, Not>> = 0> diff --git a/sol/state.hpp b/sol/state.hpp index f339e3ec..9e39b240 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -153,7 +153,7 @@ public: template auto get(Keys&&... keys) const -> decltype(global.get(std::forward(keys)...)) { - return global.get(std::forward(keys)...); + return global.get(std::forward(keys)...); } template diff --git a/sol/table.hpp b/sol/table.hpp index a35a1cb5..174bee83 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -58,7 +58,7 @@ public: template stack::get_return get( Keys&&... keys ) const { - return tuple_get(types(), build_indices(), std::tie(keys...)); + return tuple_get(types(), build_indices(), std::tie(keys...)); } template @@ -114,16 +114,16 @@ public: size_t result = lua_rawlen(state(), -1); pop(); return result; - } - - template + } + + template proxy operator[]( T&& key ) { - return proxy( *this, std::forward( key ) ); - } - - template + return proxy( *this, std::forward( key ) ); + } + + template proxy operator[]( T&& key ) const { - return proxy( *this, std::forward( key ) ); + return proxy( *this, std::forward( key ) ); } void pop(int n = 1) const noexcept { diff --git a/sol/traits.hpp b/sol/traits.hpp index 22ebcc14..1bdf99cb 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -286,8 +286,8 @@ struct member_traits : detail::member_traits { struct has_begin_end_impl { template, - typename B = decltype(std::declval().begin()), - typename E = decltype(std::declval().end())> + typename B = decltype(std::declval().begin()), + typename E = decltype(std::declval().end())> static std::true_type test(int); template @@ -299,9 +299,9 @@ struct has_begin_end : decltype(has_begin_end_impl::test(0)) {}; struct has_key_value_pair_impl { template, - typename V = typename U::value_type, - typename F = decltype(std::declval().first), - typename S = decltype(std::declval().second)> + typename V = typename U::value_type, + typename F = decltype(std::declval().first), + typename S = decltype(std::declval().second)> static std::true_type test(int); template diff --git a/sol/types.hpp b/sol/types.hpp index f4e8e69c..9bbb350a 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -73,7 +73,7 @@ enum class type : int { userdata = LUA_TUSERDATA, lightuserdata = LUA_TLIGHTUSERDATA, table = LUA_TTABLE, - poly = none | nil | string | number | thread | + poly = none | nil | string | number | thread | 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) { 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) { int actual = lua_type(L, index); if(expected != type::poly && static_cast(expected) != actual) { - type_error(L, static_cast(expected), actual); + type_error(L, static_cast(expected), actual); } } diff --git a/sol/usertype.hpp b/sol/usertype.hpp index 77d8538e..c9eaccca 100644 --- a/sol/usertype.hpp +++ b/sol/usertype.hpp @@ -358,11 +358,11 @@ public: // but leave the regular T table on last // so it can be linked to a type for usage with `.new(...)` or `:new(...)` push_metatable(L, usertype_traits::metatable, - metafunctions, ptrmetafunctiontable); + metafunctions, ptrmetafunctiontable); lua_pop(L, 1); push_metatable(L, usertype_traits::metatable, - metafunctions, metafunctiontable); + metafunctions, metafunctiontable); set_global_deleter(L); return 1; }