in place modification function there, but it won't be documented because it's a BAD IDEA

This commit is contained in:
ThePhD 2018-03-30 04:51:29 -04:00
parent 035c2f8fad
commit 0c380da637
4 changed files with 92 additions and 4 deletions

View File

@ -0,0 +1,52 @@
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp>
#include <iostream>
struct ree {
int value = 1;
ree() {}
ree(int v) : value(v) {}
};
int main() {
std::cout << "=== special pointers -- modify in place ===" << std::endl;
sol::state lua;
auto new_shared_ptr = [](sol::stack_object obj) {
// works just fine
sol::stack::modify_unique_usertype(obj, [](std::shared_ptr<ree>& sptr) {
sptr = std::make_shared<ree>(sptr->value + 1);
});
};
auto reset_shared_ptr = [](sol::stack_object obj) {
sol::stack::modify_unique_usertype(obj, [](std::shared_ptr<ree>& sptr) {
// THIS IS SUCH A BAD IDEA AAAGH
sptr.reset();
// DO NOT reset to nullptr:
// change it to an actual NEW value...
// otherwise you will inject a nullptr into the userdata representation...
// which will NOT compare == to nil
});
};
lua.set_function("f", new_shared_ptr);
lua.set_function("f2", reset_shared_ptr);
lua.set_function("g", [](ree* r) {
std::cout << r->value << std::endl;
});
lua["p"] = std::make_shared<ree>();
lua.script("g(p) -- okay");
lua.script("f(p)");
lua.script("g(p) -- okay");
// uncomment the below for
// segfault/read-access violation
lua.script("f2(p)");
//lua.script("g(p) -- kaboom");
return 0;
}

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 2018-03-29 13:24:07.253227 UTC
// This header was generated with sol v2.19.5 (revision b879eb0)
// Generated 2018-03-30 08:50:17.354157 UTC
// This header was generated with sol v2.19.5 (revision 035c2f8)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -7683,6 +7683,24 @@ namespace sol {
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
}
template <typename T, typename F>
inline void modify_unique_usertype_as(stack_object obj, F&& f) {
typedef unique_usertype_traits<T> u_traits;
void* raw = lua_touserdata(obj.lua_state(), obj.stack_index());
void* ptr_memory = detail::align_usertype_pointer(raw);
void* uu_memory = detail::align_usertype_unique<T>(raw);
T& uu = *static_cast<T*>(uu_memory);
f(uu);
*static_cast<void**>(ptr_memory) = static_cast<void*>(u_traits::get(uu));
}
template <typename F>
inline void modify_unique_usertype(stack_object obj, F&& f) {
typedef meta::bind_traits<meta::unqualified_t<F>> bt;
typedef typename bt::template arg_at<0> T;
modify_unique_usertype_as<meta::unqualified_t<T>>(obj, std::forward<F>(f));
}
} // namespace stack
} // namespace sol

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 2018-03-29 13:24:07.463940 UTC
// This header was generated with sol v2.19.5 (revision b879eb0)
// Generated 2018-03-30 08:50:17.586274 UTC
// This header was generated with sol v2.19.5 (revision 035c2f8)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP

View File

@ -870,6 +870,24 @@ namespace sol {
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
}
template <typename T, typename F>
inline void modify_unique_usertype_as(stack_object obj, F&& f) {
typedef unique_usertype_traits<T> u_traits;
void* raw = lua_touserdata(obj.lua_state(), obj.stack_index());
void* ptr_memory = detail::align_usertype_pointer(raw);
void* uu_memory = detail::align_usertype_unique<T>(raw);
T& uu = *static_cast<T*>(uu_memory);
f(uu);
*static_cast<void**>(ptr_memory) = static_cast<void*>(u_traits::get(uu));
}
template <typename F>
inline void modify_unique_usertype(stack_object obj, F&& f) {
typedef meta::bind_traits<meta::unqualified_t<F>> bt;
typedef typename bt::template arg_at<0> T;
modify_unique_usertype_as<meta::unqualified_t<T>>(obj, std::forward<F>(f));
}
} // namespace stack
} // namespace sol