2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-03-26 00:45:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
struct my_type {
|
|
|
|
int value = 10;
|
|
|
|
|
|
|
|
my_type() {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being default constructed!"
|
|
|
|
<< std::endl;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my_type(const my_type& other) : value(other.value) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being copy constructed!" << std::endl;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my_type(my_type&& other) : value(other.value) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being move-constructed!" << std::endl;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my_type& operator=(const my_type& other) {
|
|
|
|
value = other.value;
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being copy-assigned to!" << std::endl;
|
2018-04-18 03:04:17 +08:00
|
|
|
return *this;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my_type& operator=(my_type&& other) {
|
|
|
|
value = other.value;
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being move-assigned to!" << std::endl;
|
2018-04-18 03:04:17 +08:00
|
|
|
return *this;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~my_type() {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "my_type at " << static_cast<void*>(this)
|
|
|
|
<< " being destructed!" << std::endl;
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
std::cout << "=== unique_ptr support ===" << std::endl;
|
|
|
|
|
|
|
|
sol::state lua;
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.new_usertype<my_type>(
|
|
|
|
"my_type", "value", &my_type::value);
|
2018-03-26 00:45:28 +08:00
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
std::unique_ptr<my_type> unique
|
|
|
|
= std::make_unique<my_type>();
|
2018-03-26 00:45:28 +08:00
|
|
|
lua["unique"] = std::move(unique);
|
|
|
|
}
|
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "getting reference to unique_ptr..."
|
|
|
|
<< std::endl;
|
|
|
|
std::unique_ptr<my_type>& ref_to_unique_ptr
|
|
|
|
= lua["unique"];
|
2018-03-26 00:45:28 +08:00
|
|
|
my_type& ref_to_my_type = lua["unique"];
|
|
|
|
my_type* ptr_to_my_type = lua["unique"];
|
|
|
|
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(
|
2021-03-06 23:14:48 +08:00
|
|
|
ptr_to_my_type == ref_to_unique_ptr.get());
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(
|
2021-03-06 23:14:48 +08:00
|
|
|
&ref_to_my_type == ref_to_unique_ptr.get());
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(ref_to_unique_ptr->value == 10);
|
2018-03-26 00:45:28 +08:00
|
|
|
|
|
|
|
// script affects all of them equally
|
|
|
|
lua.script("unique.value = 20");
|
|
|
|
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(ptr_to_my_type->value == 20);
|
|
|
|
SOL_ASSERT(ref_to_my_type.value == 20);
|
|
|
|
SOL_ASSERT(ref_to_unique_ptr->value == 20);
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "getting copy of unique_ptr..."
|
|
|
|
<< std::endl;
|
2018-03-26 00:45:28 +08:00
|
|
|
my_type copy_of_value = lua["unique"];
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(copy_of_value.value == 20);
|
2018-03-26 00:45:28 +08:00
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
// script still affects pointer, but does not affect
|
|
|
|
// copy of `my_type`
|
2018-03-26 00:45:28 +08:00
|
|
|
lua.script("unique.value = 30");
|
|
|
|
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(copy_of_value.value == 20);
|
2018-03-26 00:45:28 +08:00
|
|
|
}
|
|
|
|
// set to nil and collect garbage to destroy it
|
|
|
|
lua.script("unique = nil");
|
|
|
|
lua.collect_garbage();
|
|
|
|
lua.collect_garbage();
|
|
|
|
|
|
|
|
std::cout << "garbage has been collected" << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|