Skip to content

Commit

Permalink
More fixes for std containers
Browse files Browse the repository at this point in the history
  • Loading branch information
cfis committed Nov 19, 2024
1 parent 22b9ae3 commit 961e58e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion rice/stl/map.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace Rice
return std::nullopt;
}
})
.define_method("[]=", [](T& map, Key_T key, Mapped_T value) -> Mapped_T
.define_method("[]=", [](T& map, Key_T key, Mapped_T& value) -> Mapped_T
{
map[key] = value;
return value;
Expand Down
2 changes: 1 addition & 1 deletion rice/stl/unordered_map.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace Rice
return std::nullopt;
}
})
.define_method("[]=", [](T& unordered_map, Key_T key, Mapped_T value) -> Mapped_T
.define_method("[]=", [](T& unordered_map, Key_T key, Mapped_T& value) -> Mapped_T
{
unordered_map[key] = value;
return value;
Expand Down
26 changes: 14 additions & 12 deletions rice/stl/vector.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ namespace Rice
template<typename T>
class VectorHelper
{
// We do NOT use Reference_T and instead use Value_T& to avoid the weirdness
// We do NOT use Reference_T and instead use Parameter_T to avoid the weirdness
// of std::vector<bool>. Reference_T is actually a proxy class that we do not
// want to have to register with Rice nor do we want to pass it around.
using Value_T = typename T::value_type;
using Size_T = typename T::size_type;
using Difference_T = typename T::difference_type;
// For To_Ruby_T however we do need to use reference type because this is what
// will be passed by an interator to To_Ruby#convert
using Reference_T = typename T::reference;
using Parameter_T = std::conditional_t<std::is_pointer_v<Value_T>, Value_T, Value_T&>;
using To_Ruby_T = detail::remove_cv_recursive_t<typename T::reference>;

public:
Expand Down Expand Up @@ -166,7 +168,7 @@ namespace Rice
{
if constexpr (detail::is_comparable_v<Value_T>)
{
klass_.define_method("delete", [](T& vector, Value_T& element) -> std::optional<Value_T>
klass_.define_method("delete", [](T& vector, Parameter_T element) -> std::optional<Value_T>
{
auto iter = std::find(vector.begin(), vector.end(), element);
if (iter == vector.end())
Expand All @@ -180,11 +182,11 @@ namespace Rice
return result;
}
})
.define_method("include?", [](T& vector, Value_T& element)
.define_method("include?", [](T& vector, Parameter_T element)
{
return std::find(vector.begin(), vector.end(), element) != vector.end();
})
.define_method("index", [](T& vector, Value_T& element) -> std::optional<Difference_T>
.define_method("index", [](T& vector, Parameter_T element) -> std::optional<Difference_T>
{
auto iter = std::find(vector.begin(), vector.end(), element);
if (iter == vector.end())
Expand All @@ -199,15 +201,15 @@ namespace Rice
}
else
{
klass_.define_method("delete", [](T& vector, Value_T& element) -> std::optional<Value_T>
klass_.define_method("delete", [](T& vector, Parameter_T element) -> std::optional<Value_T>
{
return std::nullopt;
})
.define_method("include?", [](const T& vector, Value_T& element)
.define_method("include?", [](const T& vector, Parameter_T element)
{
return false;
})
.define_method("index", [](const T& vector, Value_T& element) -> std::optional<Difference_T>
.define_method("index", [](const T& vector, Parameter_T element) -> std::optional<Difference_T>
{
return std::nullopt;
});
Expand All @@ -224,7 +226,7 @@ namespace Rice
vector.erase(iter);
return result;
})
.define_method("insert", [this](T& vector, Difference_T index, Value_T& element) -> T&
.define_method("insert", [this](T& vector, Difference_T index, Parameter_T element) -> T&
{
index = normalizeIndex(vector.size(), index, true);
auto iter = vector.begin() + index;
Expand All @@ -244,13 +246,13 @@ namespace Rice
return std::nullopt;
}
})
.define_method("push", [](T& vector, Value_T& element) -> T&
.define_method("push", [](T& vector, Parameter_T element) -> T&
{
vector.push_back(element);
return vector;
})
.define_method("shrink_to_fit", &T::shrink_to_fit)
.define_method("[]=", [this](T& vector, Difference_T index, Value_T& element) -> Value_T&
.define_method("[]=", [this](T& vector, Difference_T index, Parameter_T element) -> Parameter_T
{
index = normalizeIndex(vector.size(), index, true);
vector[index] = element;
Expand All @@ -273,9 +275,9 @@ namespace Rice
klass_.define_method("to_a", [](T& vector)
{
VALUE result = rb_ary_new();
std::for_each(vector.begin(), vector.end(), [&result](const Value_T& element)
std::for_each(vector.begin(), vector.end(), [&result](const Reference_T element)
{
VALUE value = detail::To_Ruby<Value_T&>().convert(element);
VALUE value = detail::To_Ruby<Parameter_T>().convert(element);
rb_ary_push(result, value);
});

Expand Down

0 comments on commit 961e58e

Please sign in to comment.