Skip to content

Commit 922cf7a

Browse files
committed
Adding array to cxx port calls.
1 parent e552a07 commit 922cf7a

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

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

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class value : public value_base
8787
throw std::runtime_error("Unsupported MetaCall value");
8888
}
8989

90+
T to_value()
91+
{
92+
throw std::runtime_error("Unsupported MetaCall value");
93+
}
94+
9095
// Type-specific creation (calls specialized version below)
9196
static void *create(const T &v);
9297

@@ -109,7 +114,7 @@ inline enum metacall_value_id value<bool>::id()
109114
template <>
110115
inline bool value<bool>::to_value() const
111116
{
112-
return metacall_value_to_bool(value_ptr.get());
117+
return metacall_value_to_bool(to_raw());
113118
}
114119

115120
template <>
@@ -127,7 +132,7 @@ inline enum metacall_value_id value<char>::id()
127132
template <>
128133
inline char value<char>::to_value() const
129134
{
130-
return metacall_value_to_char(value_ptr.get());
135+
return metacall_value_to_char(to_raw());
131136
}
132137

133138
template <>
@@ -145,7 +150,7 @@ inline enum metacall_value_id value<short>::id()
145150
template <>
146151
inline short value<short>::to_value() const
147152
{
148-
return metacall_value_to_short(value_ptr.get());
153+
return metacall_value_to_short(to_raw());
149154
}
150155

151156
template <>
@@ -163,7 +168,7 @@ inline enum metacall_value_id value<int>::id()
163168
template <>
164169
inline int value<int>::to_value() const
165170
{
166-
return metacall_value_to_int(value_ptr.get());
171+
return metacall_value_to_int(to_raw());
167172
}
168173

169174
template <>
@@ -181,7 +186,7 @@ inline enum metacall_value_id value<long>::id()
181186
template <>
182187
inline long value<long>::to_value() const
183188
{
184-
return metacall_value_to_long(value_ptr.get());
189+
return metacall_value_to_long(to_raw());
185190
}
186191

187192
template <>
@@ -199,7 +204,7 @@ inline enum metacall_value_id value<float>::id()
199204
template <>
200205
inline float value<float>::to_value() const
201206
{
202-
return metacall_value_to_float(value_ptr.get());
207+
return metacall_value_to_float(to_raw());
203208
}
204209

205210
template <>
@@ -217,7 +222,7 @@ inline enum metacall_value_id value<double>::id()
217222
template <>
218223
inline double value<double>::to_value() const
219224
{
220-
return metacall_value_to_double(value_ptr.get());
225+
return metacall_value_to_double(to_raw());
221226
}
222227

223228
template <>
@@ -235,7 +240,7 @@ inline enum metacall_value_id value<std::string>::id()
235240
template <>
236241
inline std::string value<std::string>::to_value() const
237242
{
238-
return metacall_value_to_string(value_ptr.get());
243+
return metacall_value_to_string(to_raw());
239244
}
240245

241246
template <>
@@ -253,7 +258,7 @@ inline enum metacall_value_id value<const char *>::id()
253258
template <>
254259
inline const char *value<const char *>::to_value() const
255260
{
256-
return metacall_value_to_string(value_ptr.get());
261+
return metacall_value_to_string(to_raw());
257262
}
258263

259264
template <>
@@ -271,7 +276,7 @@ inline enum metacall_value_id value<std::vector<char>>::id()
271276
template <>
272277
inline std::vector<char> value<std::vector<char>>::to_value() const
273278
{
274-
void *ptr = value_ptr.get();
279+
void *ptr = to_raw();
275280
char *buffer = static_cast<char *>(metacall_value_to_buffer(ptr));
276281
std::vector<char> buffer_vector(buffer, buffer + metacall_value_count(ptr));
277282

@@ -293,7 +298,7 @@ inline enum metacall_value_id value<std::vector<unsigned char>>::id()
293298
template <>
294299
inline std::vector<unsigned char> value<std::vector<unsigned char>>::to_value() const
295300
{
296-
void *ptr = value_ptr.get();
301+
void *ptr = to_raw();
297302
unsigned char *buffer = static_cast<unsigned char *>(metacall_value_to_buffer(ptr));
298303
std::vector<unsigned char> buffer_vector(buffer, buffer + metacall_value_count(ptr));
299304

@@ -315,7 +320,7 @@ inline enum metacall_value_id value<void *>::id()
315320
template <>
316321
inline void *value<void *>::to_value() const
317322
{
318-
return metacall_value_to_ptr(value_ptr.get());
323+
return metacall_value_to_ptr(to_raw());
319324
}
320325

321326
template <>
@@ -370,29 +375,25 @@ class array : public value_base
370375
explicit array(void *array_value) :
371376
value_base(array_value, &value_base::noop_destructor) {}
372377

373-
void **to_value() const
374-
{
375-
void **array_ptr = metacall_value_to_array(value_ptr.get());
376-
377-
if (array_ptr == NULL)
378-
{
379-
throw std::runtime_error("Invalid MetaCall array");
380-
}
378+
array(array &arr) :
379+
value_base(arr.to_raw(), &value_base::noop_destructor) {}
381380

382-
return array_ptr;
381+
array &to_value()
382+
{
383+
return *this;
383384
}
384385

385386
template <typename T>
386387
T get(std::size_t index) const
387388
{
388-
void **array_ptr = to_value();
389+
void **array_ptr = to_array();
389390

390391
return value<T>(array_ptr[index]).to_value();
391392
}
392393

393394
value_ref operator[](std::size_t index) const
394395
{
395-
void **array_ptr = to_value();
396+
void **array_ptr = to_array();
396397

397398
return value_ref(array_ptr[index]);
398399
}
@@ -403,6 +404,18 @@ class array : public value_base
403404
}
404405

405406
private:
407+
void **to_array() const
408+
{
409+
void **array_ptr = metacall_value_to_array(to_raw());
410+
411+
if (array_ptr == NULL)
412+
{
413+
throw std::runtime_error("Invalid MetaCall array");
414+
}
415+
416+
return array_ptr;
417+
}
418+
406419
// Recursive function to create and fill the MetaCall array
407420
template <typename... Args>
408421
static void *create(Args &&...args)
@@ -449,6 +462,12 @@ inline enum metacall_value_id value<array>::id()
449462
return METACALL_ARRAY;
450463
}
451464

465+
template <>
466+
inline enum metacall_value_id value<array &>::id()
467+
{
468+
return METACALL_ARRAY;
469+
}
470+
452471
template <>
453472
inline array value<array>::to_value() const
454473
{
@@ -470,7 +489,7 @@ class map : public value_base
470489
throw std::runtime_error("Failed to create MetaCall map value");
471490
}
472491

473-
void **map_array = metacall_value_to_map(value_ptr.get());
492+
void **map_array = metacall_value_to_map(to_raw());
474493
size_t index = 0;
475494

476495
for (const auto &pair : list)
@@ -516,7 +535,7 @@ class map : public value_base
516535
protected:
517536
void rehash()
518537
{
519-
void *ptr = value_ptr.get();
538+
void *ptr = to_raw();
520539
void **map_array = metacall_value_to_map(ptr);
521540
const size_t size = metacall_value_count(ptr);
522541

source/tests/metacall_cxx_port_test/source/metacall_cxx_port_test.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ void *cxx_map_test(size_t argc, void *args[], void *data)
4444
return metacall::metacall_value_create_null();
4545
}
4646

47-
void *cxx_array_test(size_t argc, void *args[], void *data)
47+
std::nullptr_t cxx_array_test(metacall::array &a)
4848
{
49-
metacall::array a(args[0]);
50-
51-
(void)argc;
52-
(void)data;
53-
5449
EXPECT_EQ((float)a[0].as<int>(), (int)3);
5550
EXPECT_EQ((float)a[1].as<float>(), (float)4.0f);
5651

@@ -61,7 +56,7 @@ void *cxx_array_test(size_t argc, void *args[], void *data)
6156
printf("a[1] => %f\n", a[1].as<float>());
6257
fflush(stdout);
6358

64-
return metacall::metacall_value_create_null();
59+
return nullptr;
6560
}
6661

6762
void *cxx_map_array_test(size_t argc, void *args[], void *data)
@@ -127,15 +122,15 @@ TEST_F(metacall_cxx_port_test, DefaultConstructor)
127122

128123
EXPECT_EQ(nullptr, metacall::metacall<std::nullptr_t>("cxx_map_test", m));
129124
}
130-
125+
#endif
131126
{
132127
metacall::array a(3, 4.0f);
133128

134-
metacall::metacall_register("cxx_array_test", cxx_array_test, NULL, metacall::METACALL_NULL, 1, metacall::METACALL_ARRAY);
129+
auto fn = metacall::register_function(cxx_array_test);
135130

136-
EXPECT_EQ(nullptr, metacall::metacall<std::nullptr_t>("cxx_array_test", a));
131+
EXPECT_EQ(nullptr, fn(a));
137132
}
138-
133+
#if 0
139134
{
140135
metacall::map<std::string, metacall::array> m = {
141136
{ "includes", metacall::array("/a/path", "/another/path") },

0 commit comments

Comments
 (0)