mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
in place modification function there, but it won't be documented because it's a BAD IDEA
This commit is contained in:
parent
035c2f8fad
commit
0c380da637
52
examples/shared_ptr_modify_in_place.cpp
Normal file
52
examples/shared_ptr_modify_in_place.cpp
Normal 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;
|
||||||
|
}
|
|
@ -20,8 +20,8 @@
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2018-03-29 13:24:07.253227 UTC
|
// Generated 2018-03-30 08:50:17.354157 UTC
|
||||||
// This header was generated with sol v2.19.5 (revision b879eb0)
|
// This header was generated with sol v2.19.5 (revision 035c2f8)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||||
|
@ -7683,6 +7683,24 @@ namespace sol {
|
||||||
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
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);
|
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 stack
|
||||||
} // namespace sol
|
} // namespace sol
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2018-03-29 13:24:07.463940 UTC
|
// Generated 2018-03-30 08:50:17.586274 UTC
|
||||||
// This header was generated with sol v2.19.5 (revision b879eb0)
|
// This header was generated with sol v2.19.5 (revision 035c2f8)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||||
|
|
|
@ -870,6 +870,24 @@ namespace sol {
|
||||||
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
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);
|
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 stack
|
||||||
} // namespace sol
|
} // namespace sol
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user