Skip to content

Commit 9d66524

Browse files
authored
Merge pull request #279 from ruby-rice/dev
Dev
2 parents a6824f6 + baa3455 commit 9d66524

File tree

3 files changed

+84
-11
lines changed

3 files changed

+84
-11
lines changed

include/rice/stl.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3990,8 +3990,9 @@ namespace Rice
39903990
return element;
39913991
});
39923992

3993-
rb_define_alias(klass_, "<<", "push");
3994-
rb_define_alias(klass_, "append", "push");
3993+
rb_define_alias(klass_, "push_back", "push");
3994+
rb_define_alias(klass_, "<<", "push");
3995+
rb_define_alias(klass_, "append", "push");
39953996
}
39963997

39973998
void define_enumerable()
@@ -4119,7 +4120,10 @@ namespace Rice
41194120
return Data_Type<std::vector<T>>::is_descendant(value) ? Convertible::Exact : Convertible::None;
41204121
break;
41214122
case RUBY_T_ARRAY:
4122-
return Convertible::Cast;
4123+
if constexpr (std::is_default_constructible_v<T>)
4124+
{
4125+
return Convertible::Cast;
4126+
}
41234127
break;
41244128
default:
41254129
return Convertible::None;
@@ -4180,7 +4184,10 @@ namespace Rice
41804184
return Data_Type<std::vector<T>>::is_descendant(value) ? Convertible::Exact : Convertible::None;
41814185
break;
41824186
case RUBY_T_ARRAY:
4183-
return Convertible::Cast;
4187+
if constexpr (std::is_default_constructible_v<T>)
4188+
{
4189+
return Convertible::Cast;
4190+
}
41844191
break;
41854192
default:
41864193
return Convertible::None;
@@ -4240,7 +4247,10 @@ namespace Rice
42404247
return Convertible::Exact;
42414248
break;
42424249
case RUBY_T_ARRAY:
4243-
return Convertible::Cast;
4250+
if constexpr (std::is_default_constructible_v<T>)
4251+
{
4252+
return Convertible::Cast;
4253+
}
42444254
break;
42454255
default:
42464256
return Convertible::None;

rice/stl/vector.ipp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ namespace Rice
280280
return element;
281281
});
282282

283-
rb_define_alias(klass_, "<<", "push");
284-
rb_define_alias(klass_, "append", "push");
283+
rb_define_alias(klass_, "push_back", "push");
284+
rb_define_alias(klass_, "<<", "push");
285+
rb_define_alias(klass_, "append", "push");
285286
}
286287

287288
void define_enumerable()
@@ -409,7 +410,10 @@ namespace Rice
409410
return Data_Type<std::vector<T>>::is_descendant(value) ? Convertible::Exact : Convertible::None;
410411
break;
411412
case RUBY_T_ARRAY:
412-
return Convertible::Cast;
413+
if constexpr (std::is_default_constructible_v<T>)
414+
{
415+
return Convertible::Cast;
416+
}
413417
break;
414418
default:
415419
return Convertible::None;
@@ -470,7 +474,10 @@ namespace Rice
470474
return Data_Type<std::vector<T>>::is_descendant(value) ? Convertible::Exact : Convertible::None;
471475
break;
472476
case RUBY_T_ARRAY:
473-
return Convertible::Cast;
477+
if constexpr (std::is_default_constructible_v<T>)
478+
{
479+
return Convertible::Cast;
480+
}
474481
break;
475482
default:
476483
return Convertible::None;
@@ -530,7 +537,10 @@ namespace Rice
530537
return Convertible::Exact;
531538
break;
532539
case RUBY_T_ARRAY:
533-
return Convertible::Cast;
540+
if constexpr (std::is_default_constructible_v<T>)
541+
{
542+
return Convertible::Cast;
543+
}
534544
break;
535545
default:
536546
return Convertible::None;

test/test_Stl_Variant.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "unittest.hpp"
1+
#include "unittest.hpp"
22
#include "embed_ruby.hpp"
33
#include <rice/rice.hpp>
44
#include <rice/stl.hpp>
@@ -349,3 +349,56 @@ TESTCASE(ClassRoundtripRef)
349349
ASSERT_EQUAL("Hi from MyClass2", detail::From_Ruby<std::string>().convert(hello));
350350
}
351351
#endif
352+
353+
354+
namespace
355+
{
356+
class MyClass4
357+
{
358+
public:
359+
size_t variantIndex(std::variant<std::vector<std::string>, std::vector<int>> variant)
360+
{
361+
return variant.index();
362+
}
363+
};
364+
}
365+
366+
TESTCASE(VariantWithTwoVectors)
367+
{
368+
using namespace std::complex_literals;
369+
370+
define_class<MyClass4>("MyClass4").
371+
define_constructor(Constructor<MyClass4>()).
372+
define_method("variant_index", &MyClass4::variantIndex);
373+
374+
Module m = define_module("Testing");
375+
376+
std::string code = u8R"(vector = Std::Vector≺string≻.new
377+
vector << "a" << "b" << "c"
378+
my_class = MyClass4.new
379+
my_class.variant_index(vector))";
380+
381+
Object result = m.module_eval(code);
382+
ASSERT_EQUAL(0, detail::From_Ruby<size_t>().convert(result));
383+
384+
code = u8R"(vector = Std::Vector≺int≻.new
385+
vector.push_back(4)
386+
my_class = MyClass4.new
387+
my_class.variant_index(vector))";
388+
result = m.module_eval(code);
389+
ASSERT_EQUAL(1, detail::From_Ruby<size_t>().convert(result));
390+
391+
code = u8R"(my_class = MyClass4.new
392+
my_class.variant_index(["x", "y", "z"]))";
393+
result = m.module_eval(code);
394+
ASSERT_EQUAL(0, detail::From_Ruby<size_t>().convert(result));
395+
396+
code = u8R"(my_class = MyClass4.new
397+
my_class.variant_index([5, 6]))";
398+
399+
ASSERT_EXCEPTION_CHECK(
400+
Exception,
401+
m.module_eval(code),
402+
ASSERT_EQUAL("wrong argument type Integer (expected String)", ex.what())
403+
);
404+
}

0 commit comments

Comments
 (0)