Skip to content

Commit

Permalink
Fix std::vector to be supported in std::variant wrapping
Browse files Browse the repository at this point in the history
Related to #190
  • Loading branch information
jasonroelofs committed Jan 2, 2024
1 parent 6242c34 commit 37f5850
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
16 changes: 16 additions & 0 deletions include/rice/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,11 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
Arg* arg_ = nullptr;
};
Expand Down Expand Up @@ -2416,6 +2421,11 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
Arg* arg_ = nullptr;
std::vector<T> converted_;
Expand Down Expand Up @@ -2451,10 +2461,16 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
std::vector<T> converted_;
};
}
}


#endif // Rice__stl__hpp_
17 changes: 16 additions & 1 deletion rice/stl/vector.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
Arg* arg_ = nullptr;
};
Expand Down Expand Up @@ -480,6 +485,11 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
Arg* arg_ = nullptr;
std::vector<T> converted_;
Expand Down Expand Up @@ -515,8 +525,13 @@ namespace Rice
}
}

bool is_convertible(VALUE value)
{
return rb_type(value) == RUBY_T_ARRAY;
}

private:
std::vector<T> converted_;
};
}
}
}
18 changes: 18 additions & 0 deletions test/test_Stl_Variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace
using Intrinsic_Variant_T = std::variant<
std::string,
std::complex<double>,
std::vector<int>,
double,
bool,
int
Expand Down Expand Up @@ -44,6 +45,12 @@ namespace
return result;
}

Intrinsic_Variant_T variantVector()
{
Intrinsic_Variant_T result { std::vector<int>{1, 2, 3} };
return result;
}

Intrinsic_Variant_T variantDouble()
{
Intrinsic_Variant_T result { 3.3 };
Expand Down Expand Up @@ -88,6 +95,7 @@ void makeIntrinsicVariant()
define_constructor(Constructor<MyClass>()).
define_method("variant_string", &MyClass::variantString).
define_method("variant_complex", &MyClass::variantComplex).
define_method("variant_vector", &MyClass::variantVector).
define_method("variant_double", &MyClass::variantDouble).
define_method("variant_bool_true", &MyClass::variantBoolTrue).
define_method("variant_bool_false", &MyClass::variantBoolFalse).
Expand All @@ -109,6 +117,10 @@ TESTCASE(IntrinsicReturns)
result = myClass.call("variant_complex");
ASSERT_EQUAL(1i, detail::From_Ruby<std::complex<double>>().convert(result));

result = myClass.call("variant_vector");
std::vector<int> converted = detail::From_Ruby<std::vector<int>>().convert(result);
ASSERT_EQUAL(3, converted.size());

result = myClass.call("variant_double");
ASSERT_EQUAL(3.3, detail::From_Ruby<double>().convert(result));

Expand Down Expand Up @@ -139,6 +151,12 @@ TESTCASE(IntrinsicRoundtrip)
result = m.module_eval(code);
ASSERT_EQUAL((2.0 + 3i), detail::From_Ruby<std::complex<double>>().convert(result));

code = R"(my_class = MyClass.new
my_class.roundtrip([1, 2, 3]))";
result = m.module_eval(code);
std::vector<int> expected = {1, 2, 3};
ASSERT_EQUAL(expected, detail::From_Ruby<std::vector<int>>().convert(result));

code = R"(my_class = MyClass.new
my_class.roundtrip(44.4))";
result = m.module_eval(code);
Expand Down

0 comments on commit 37f5850

Please sign in to comment.