Fixed an issue with wrapping new objects into tl::Variants which are returned directly. For these objects, ownership needs to be transferred to the script.

This commit is contained in:
Matthias Koefferlein
2020-12-27 17:09:06 +01:00
parent 992be87812
commit 4ec00fb129
10 changed files with 222 additions and 108 deletions
+9 -4
View File
@@ -1106,8 +1106,8 @@ public:
// Variant adaptor framework
/**
* @brief A generic adaptor for strings
* This is the base class for implementing generic access to strings
* @brief A generic adaptor for variants
* This is the base class for implementing generic access to variants
*/
class GSI_PUBLIC VariantAdaptor
: public AdaptorBase
@@ -1145,7 +1145,7 @@ public:
};
/**
* @brief Generic string adaptor implementation
* @brief Generic variant adaptor implementation
*/
template <class X>
class GSI_PUBLIC_TEMPLATE VariantAdaptorImpl
@@ -1274,7 +1274,12 @@ public:
return *mp_v;
}
virtual void set (const tl::Variant &v)
tl::Variant &var_ref_nc ()
{
return *mp_v;
}
virtual void set (const tl::Variant &v)
{
if (! m_is_const) {
*mp_v = v;
+26 -7
View File
@@ -107,6 +107,11 @@ const char *A::a_static ()
return "static_a";
}
tl::Variant A::new_a_by_variant ()
{
return tl::Variant (A ());
}
static A *a_ctor (int i)
{
return new A (i);
@@ -128,9 +133,11 @@ A *A::a20_get ()
// Implementation of B
B *B::b_inst = 0;
static int b_count = 0;
B::B ()
{
++b_count;
av.push_back (A (100));
av.push_back (A (121));
av.push_back (A (144));
@@ -142,7 +149,7 @@ B::B ()
av_nc.push_back (new A_NC (7169));
}
B::~B()
B::~B ()
{
while (! av_nc.empty ()) {
delete av_nc.back ();
@@ -155,14 +162,21 @@ B::~B()
if (b_inst == this) {
b_inst = 0;
}
--b_count;
}
B::B (const B &d)
{
operator=(d);
++b_count;
}
B &B::operator=(const B &d)
int B::instance_count ()
{
return b_count;
}
B &B::operator=(const B &d)
{
if (&d == this) {
return *this;
@@ -213,6 +227,11 @@ bool B::has_inst ()
return b_inst != 0;
}
tl::Variant B::new_b_by_variant ()
{
return tl::Variant (B ());
}
std::string B::addr () const
{
char c[50];
@@ -790,6 +809,7 @@ static gsi::QFlagsClass<Enum> decl_qflags_enum ("", "Enums");
static gsi::Class<A> decl_a ("", "A",
gsi::constructor ("new_a|new", &a_ctor) +
gsi::method ("instance_count", &A::instance_count) +
gsi::method ("new_a_by_variant", &A::new_a_by_variant) +
gsi::method ("br", &A::br) +
gsi::method ("get_e", &A::get_e) +
gsi::method ("get_eptr", &A::get_eptr) +
@@ -918,17 +938,16 @@ static gsi::Class<B> decl_b ("", "B",
gsi::method ("has_inst", &B::has_inst) +
gsi::method ("set_inst", &B::set_inst) +
gsi::method ("del_inst", &B::del_inst) +
gsi::method ("instance_count", &B::instance_count) +
gsi::method ("new_b_by_variant", &B::new_b_by_variant) +
gsi::method ("addr", &B::addr) +
gsi::method ("b1|always_5", &B::b1) +
gsi::method ("b2|str", &B::str) +
gsi::method ("always_5", &B::always_5) +
gsi::method ("str", &B::str) +
gsi::method ("set_str", &B::set_str) +
gsi::method ("str_ccptr", &B::str_ccptr) +
gsi::method ("set_str_combine", &B::set_str_combine) +
gsi::method_ext ("b3|aptr_to_n", &b3_ext) +
gsi::method ("b4|aref_to_s", &B::b4) +
gsi::method ("b5", &B::b5) +
gsi::method ("b5a", &B::b5a) +
gsi::method ("b5b", &B::b5b) +
gsi::method ("make_a", &B::make_a) +
gsi::method ("set_an", &B::set_an) +
gsi::method ("an", &B::an) +
+13 -14
View File
@@ -93,6 +93,11 @@ struct A
*/
static const char *a_static ();
/**
* @brief Construction through tl::Variant
*/
static tl::Variant new_a_by_variant ();
/*
* @brief A dummy method providing a chance to set a breakpoint in the script
*/
@@ -445,9 +450,16 @@ struct B
static B *inst ();
static bool has_inst ();
static int instance_count ();
/**
* @brief Construction through tl::Variant
*/
static tl::Variant new_b_by_variant ();
std::string addr () const;
int b1 () const {
int always_5 () const {
return 5;
}
@@ -469,19 +481,6 @@ struct B
return tl::sprintf ("b4_result: %d", aref.n);
}
void b5 (const char *p) {
m = p;
}
void b5b (const char *p1, const char *p2) {
m = p1;
m += p2;
}
const char *b5a () const {
return m.c_str ();
}
A make_a (int n) {
return A(n);
}