Skip to content

Commit

Permalink
update customization points and track un-specialized structs to trigg…
Browse files Browse the repository at this point in the history
…er some safe-guards as a back-compatible safety net
  • Loading branch information
ThePhD committed Feb 20, 2017
1 parent 889a45d commit ab9126d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
5 changes: 5 additions & 0 deletions docs/source/tutorial/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ These are template class/structs, so you'll override them using a technique C++
template <>
struct lua_size<two_things> : std::integral_constant<int, 2> {};
// Then, specialize the type to match:
// this has multiple types, so we consider it a "poly" type.
template <>
struct lua_type_of<two_things> : std::integral_constant<sol::type, sol::type::poly> {};
// Now, specialize various stack structures
namespace stack {
Expand Down
17 changes: 12 additions & 5 deletions examples/customization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace sol {
template <>
struct lua_size<two_things> : std::integral_constant<int, 2> {};

// Then, specialize the type
// this makes sure Sol can return it properly
template <>
struct lua_type_of<two_things> : std::integral_constant<sol::type, sol::type::poly> {};

// Now, specialize various stack structures
namespace stack {

Expand Down Expand Up @@ -67,22 +72,24 @@ namespace sol {
}

int main() {
std::cout << "=== customization example ===" << std::endl;
std::cout << std::boolalpha;

sol::state lua;
lua.open_libraries(sol::lib::base);

// Create a pass-through style of function
lua.script("function f ( a, b ) return a, b end");
lua.script("function f ( a, b ) print(a, b) return a, b end");

// get the function out of Lua
sol::function f = lua["f"];

two_things things = f(two_things{ 24, true });
two_things things = f(two_things{ 24, false });
assert(things.a == 24);
assert(things.b == true);
assert(things.b == false);
// things.a == 24
// things.b == true

std::cout << "=== customization example ===" << std::endl;
std::cout << std::boolalpha;
std::cout << "things.a: " << things.a << std::endl;
std::cout << "things.b: " << things.b << std::endl;
std::cout << std::endl;
Expand Down
28 changes: 23 additions & 5 deletions single/sol/sol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 2017-02-20 08:45:30.715552 UTC
// This header was generated with sol v2.15.9 (revision b7b6366)
// Generated 2017-02-20 23:06:29.737387 UTC
// This header was generated with sol v2.15.9 (revision 889a45d)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_INCLUDE_HPP
Expand Down Expand Up @@ -3686,21 +3686,39 @@ namespace sol {
struct is_unique_usertype : std::integral_constant<bool, unique_usertype_traits<T>::value> {};

template <typename T>
struct lua_type_of : detail::lua_type_of<T> {};
struct lua_type_of : detail::lua_type_of<T> {
typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
};

template <typename T>
struct lua_size : std::integral_constant<int, 1> { };
struct lua_size : std::integral_constant<int, 1> {
typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
};

template <typename A, typename B>
struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> { };

template <typename... Args>
struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> { };

namespace detail {
template <typename...>
struct void_ { typedef void type; };
template <typename T, typename = void>
struct has_internal_marker_impl : std::false_type {};
template <typename T>
struct has_internal_marker_impl<T, typename void_<typename T::SOL_INTERNAL_UNSPECIALIZED_MARKER_>::type> : std::true_type {};

template <typename T>
struct has_internal_marker : has_internal_marker_impl<T> {};
}

template <typename T>
struct is_lua_primitive : std::integral_constant<bool,
type::userdata != lua_type_of<meta::unqualified_t<T>>::value
|| (lua_size<T>::value > 1)
|| ((type::userdata == lua_type_of<meta::unqualified_t<T>>::value)
&& detail::has_internal_marker<lua_type_of<meta::unqualified_t<T>>>::value
&& !detail::has_internal_marker<lua_size<meta::unqualified_t<T>>>::value)
|| std::is_base_of<reference, meta::unqualified_t<T>>::value
|| std::is_base_of<stack_reference, meta::unqualified_t<T>>::value
|| meta::is_specialization_of<std::tuple, meta::unqualified_t<T>>::value
Expand Down
24 changes: 21 additions & 3 deletions sol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,21 +704,39 @@ namespace sol {
struct is_unique_usertype : std::integral_constant<bool, unique_usertype_traits<T>::value> {};

template <typename T>
struct lua_type_of : detail::lua_type_of<T> {};
struct lua_type_of : detail::lua_type_of<T> {
typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
};

template <typename T>
struct lua_size : std::integral_constant<int, 1> { };
struct lua_size : std::integral_constant<int, 1> {
typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
};

template <typename A, typename B>
struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> { };

template <typename... Args>
struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> { };

namespace detail {
template <typename...>
struct void_ { typedef void type; };
template <typename T, typename = void>
struct has_internal_marker_impl : std::false_type {};
template <typename T>
struct has_internal_marker_impl<T, typename void_<typename T::SOL_INTERNAL_UNSPECIALIZED_MARKER_>::type> : std::true_type {};

template <typename T>
struct has_internal_marker : has_internal_marker_impl<T> {};
}

template <typename T>
struct is_lua_primitive : std::integral_constant<bool,
type::userdata != lua_type_of<meta::unqualified_t<T>>::value
|| (lua_size<T>::value > 1)
|| ((type::userdata == lua_type_of<meta::unqualified_t<T>>::value)
&& detail::has_internal_marker<lua_type_of<meta::unqualified_t<T>>>::value
&& !detail::has_internal_marker<lua_size<meta::unqualified_t<T>>>::value)
|| std::is_base_of<reference, meta::unqualified_t<T>>::value
|| std::is_base_of<stack_reference, meta::unqualified_t<T>>::value
|| meta::is_specialization_of<std::tuple, meta::unqualified_t<T>>::value
Expand Down

0 comments on commit ab9126d

Please sign in to comment.