Skip to content

Commit 90f20b9

Browse files
committed
Solve issues with ownership in cxx port.
1 parent 7f59fe8 commit 90f20b9

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

source/ports/cxx_port/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,19 @@ target_link_options(${target}
177177

178178
INTERFACE
179179
)
180+
181+
#
182+
# Deployment
183+
#
184+
185+
# Header files
186+
install(DIRECTORY
187+
${CMAKE_CURRENT_SOURCE_DIR}/include/${target} DESTINATION ${INSTALL_INCLUDE}
188+
COMPONENT dev
189+
)
190+
191+
# Inline files
192+
install(DIRECTORY
193+
${CMAKE_CURRENT_SOURCE_DIR}/inline/${target} DESTINATION ${INSTALL_INCLUDE}
194+
COMPONENT dev
195+
)

source/ports/cxx_port/include/metacall/metacall.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ class value_base
5353
protected:
5454
std::unique_ptr<void, void (*)(void *)> value_ptr;
5555

56-
explicit value_base(void *value_ptr, void (*destructor)(void *) = &noop_destructor) :
57-
// TODO: &metacall_value_destroy
58-
value_ptr(value_ptr, destructor)
59-
{
60-
}
56+
explicit value_base(void *value_ptr, void (*destructor)(void *) = &metacall_value_destroy) :
57+
value_ptr(value_ptr, destructor) {}
6158

6259
static void noop_destructor(void *) {}
6360
};
@@ -66,8 +63,8 @@ template <typename T>
6663
class METACALL_API value : public value_base
6764
{
6865
public:
69-
explicit value(const T &v) :
70-
value_base(create(v))
66+
explicit value(const T &v, void (*destructor)(void *) = &metacall_value_destroy) :
67+
value_base(create(v), destructor)
7168
{
7269
if (value_ptr == nullptr)
7370
{
@@ -365,7 +362,7 @@ class METACALL_API map : public value_base
365362
void **tuple_array = metacall_value_to_array(tuple);
366363

367364
// Create the pair
368-
auto value_pair = std::make_pair(value<K>(pair.first), value<V>(pair.second));
365+
auto value_pair = std::make_pair(value<K>(pair.first, &value_base::noop_destructor), value<V>(pair.second, &value_base::noop_destructor));
369366

370367
// Insert into metacall value map
371368
tuple_array[0] = value_pair.first.to_raw();

source/tests/metacall_cxx_port_test/source/metacall_cxx_port_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ void *cxx_map_test(size_t argc, void *args[], void *data)
4646
return metacall_value_create_null();
4747
}
4848

49+
void *cxx_recursive_map_test(size_t argc, void *args[], void *data)
50+
{
51+
map<std::string, float> m(args[0]);
52+
53+
(void)argc;
54+
(void)data;
55+
56+
EXPECT_EQ((float)m["hello"], (float)3.0f);
57+
EXPECT_EQ((float)m["world"], (float)4.0f);
58+
59+
printf("hello => %f\n", m["hello"]);
60+
printf("world => %f\n", m["world"]);
61+
fflush(stdout);
62+
63+
return metacall_value_create_null();
64+
}
65+
4966
TEST_F(metacall_cxx_port_test, DefaultConstructor)
5067
{
5168
ASSERT_EQ((int)0, (int)metacall_initialize());
@@ -71,6 +88,28 @@ TEST_F(metacall_cxx_port_test, DefaultConstructor)
7188
metacall_value_destroy(ret);
7289
}
7390

91+
/*
92+
{
93+
map<std::string, map<std::string, float>> m = {
94+
{ "hello", { "world", 4.0f } },
95+
};
96+
97+
void *args[] = {
98+
m.to_raw()
99+
};
100+
101+
metacall_register("cxx_recursive_map_test", cxx_recursive_map_test, NULL, METACALL_NULL, 1, METACALL_MAP);
102+
103+
void *ret = metacallv_s("cxx_recursive_map_test", args, 1);
104+
105+
EXPECT_NE((void *)NULL, (void *)ret);
106+
107+
EXPECT_EQ((enum metacall_value_id)metacall_value_id(ret), (enum metacall_value_id)METACALL_NULL);
108+
109+
metacall_value_destroy(ret);
110+
}
111+
*/
112+
74113
/* Print inspect information */
75114
{
76115
size_t size = 0;

0 commit comments

Comments
 (0)