Skip to content

Commit

Permalink
Merge pull request #223 from ruby-rice/to_ruby
Browse files Browse the repository at this point in the history
Update To_Ruby Type handling
  • Loading branch information
cfis authored Nov 19, 2024
2 parents 914adef + f16692f commit 22b9ae3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions rice/detail/NativeAttributeGet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace Rice
using NativeAttribute_T = NativeAttributeGet<Attribute_T>;

using T = typename attribute_traits<Attribute_T>::attr_type;
using T_Unqualified = remove_cv_recursive_t<T>;
using Receiver_T = typename attribute_traits<Attribute_T>::class_type;

using To_Ruby_T = remove_cv_recursive_t<T>;

public:
// Register attribute getter with Ruby
static void define(VALUE klass, std::string name, Attribute_T attribute);
Expand Down
4 changes: 2 additions & 2 deletions rice/detail/NativeAttributeGet.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ namespace Rice::detail
if constexpr (std::is_member_object_pointer_v<Attribute_T>)
{
Receiver_T* nativeSelf = From_Ruby<Receiver_T*>().convert(self);
return To_Ruby<T_Unqualified>().convert(nativeSelf->*attribute_);
return To_Ruby<To_Ruby_T>().convert(nativeSelf->*attribute_);
}
else
{
return To_Ruby<T_Unqualified>().convert(*attribute_);
return To_Ruby<To_Ruby_T>().convert(*attribute_);
}
}
} // Rice
8 changes: 5 additions & 3 deletions rice/detail/NativeFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "MethodInfo.hpp"
#include "../traits/function_traits.hpp"
#include "../traits/method_traits.hpp"
#include "../traits/rice_traits.hpp"
#include "from_ruby.hpp"

namespace Rice::detail
Expand Down Expand Up @@ -50,11 +51,12 @@ namespace Rice::detail

// We remove const to avoid an explosion of To_Ruby specializations and Ruby doesn't
// have the concept of constants anyways
using Return_T = remove_cv_recursive_t<typename function_traits<Function_T>::return_type>;
using Return_T = typename function_traits<Function_T>::return_type;
using Receiver_T = typename method_traits<Function_T, IsMethod>::Class_T;
using Arg_Ts = typename method_traits<Function_T, IsMethod>::Arg_Ts;
static constexpr std::size_t arity = method_traits<Function_T, IsMethod>::arity;
using From_Ruby_Args_Ts = typename tuple_map<From_Ruby, Arg_Ts>::type;
using To_Ruby_T = remove_cv_recursive_t<Return_T>;

// Register function with Ruby
static void define(VALUE klass, std::string method_name, Function_T function, MethodInfo* methodInfo);
Expand Down Expand Up @@ -82,7 +84,7 @@ namespace Rice::detail
From_Ruby_Args_Ts createFromRuby(std::index_sequence<I...>& indices);

// Convert C++ value to Ruby
To_Ruby<Return_T> createToRuby();
To_Ruby<To_Ruby_T> createToRuby();

// Convert Ruby argv pointer to Ruby values
std::vector<VALUE> getRubyValues(int argc, VALUE* argv);
Expand All @@ -109,7 +111,7 @@ namespace Rice::detail
std::string method_name_;
Function_T function_;
From_Ruby_Args_Ts fromRubys_;
To_Ruby<Return_T> toRuby_;
To_Ruby<To_Ruby_T> toRuby_;
std::unique_ptr<MethodInfo> methodInfo_;
};
}
Expand Down
10 changes: 5 additions & 5 deletions rice/detail/NativeFunction.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ namespace Rice::detail
}

template<typename Class_T, typename Function_T, bool IsMethod>
To_Ruby<typename NativeFunction<Class_T, Function_T, IsMethod>::Return_T> NativeFunction<Class_T, Function_T, IsMethod>::createToRuby()
To_Ruby<typename NativeFunction<Class_T, Function_T, IsMethod>::To_Ruby_T> NativeFunction<Class_T, Function_T, IsMethod>::createToRuby()
{
// Does the From_Ruby instantiation work with ReturnInfo?
if constexpr (std::is_constructible_v<To_Ruby<Return_T>, Return*>)
if constexpr (std::is_constructible_v<To_Ruby<To_Ruby_T>, Return*>)
{
return To_Ruby<Return_T>(&this->methodInfo_->returnInfo);
return To_Ruby<To_Ruby_T>(&this->methodInfo_->returnInfo);
}
else
{
return To_Ruby<Return_T>();
return To_Ruby<To_Ruby_T>();
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ namespace Rice::detail
}
else
{
Return_T nativeResult = (Return_T)std::apply(this->function_, std::forward<decltype(selfAndNativeArgs)>(selfAndNativeArgs));
Return_T nativeResult = std::apply(this->function_, std::forward<decltype(selfAndNativeArgs)>(selfAndNativeArgs));

// Special handling if the method returns self. If so we do not want
// to create a new Ruby wrapper object and instead return self.
Expand Down
2 changes: 2 additions & 0 deletions rice/detail/NativeIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace Rice::detail
using NativeIterator_T = NativeIterator<T, Iterator_Func_T>;
using Iterator_T = typename function_traits<Iterator_Func_T>::return_type;
using Value_T = typename std::iterator_traits<Iterator_T>::value_type;
using Reference_T = typename std::iterator_traits<Iterator_T>::reference;
using Difference_T = typename std::iterator_traits<Iterator_T>::difference_type;
using To_Ruby_T = remove_cv_recursive_t<Reference_T>;

public:
// Register function with Ruby
Expand Down
2 changes: 1 addition & 1 deletion rice/detail/NativeIterator.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace Rice::detail

for (; it != end; ++it)
{
protect(rb_yield, detail::To_Ruby<Value_T&>().convert(*it));
protect(rb_yield, detail::To_Ruby<To_Ruby_T>().convert(*it));
}

return self;
Expand Down
7 changes: 5 additions & 2 deletions rice/stl/map.ipp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../traits/function_traits.hpp"
#include "../traits/rice_traits.hpp"
#include "../detail/from_ruby.hpp"
#include "../detail/to_ruby.hpp"
#include "../detail/RubyFunction.hpp"
Expand All @@ -23,8 +24,10 @@ namespace Rice
using Key_T = typename T::key_type;
using Mapped_T = typename T::mapped_type;
using Value_T = typename T::value_type;
using Reference_T = typename T::reference;
using Size_T = typename T::size_type;
using Difference_T = typename T::difference_type;
using To_Ruby_T = typename detail::remove_cv_recursive_t<Mapped_T>;

public:
MapHelper(Data_Type<T> klass) : klass_(klass)
Expand Down Expand Up @@ -194,10 +197,10 @@ namespace Rice
klass_.define_method("to_h", [](T& map)
{
VALUE result = rb_hash_new();
std::for_each(map.begin(), map.end(), [&result](const typename T::reference pair)
std::for_each(map.begin(), map.end(), [&result](const Reference_T pair)
{
VALUE key = detail::To_Ruby<Key_T&>().convert(pair.first);
VALUE value = detail::To_Ruby<Mapped_T&>().convert(pair.second);
VALUE value = detail::To_Ruby<To_Ruby_T&>().convert(pair.second);
rb_hash_aset(result, key, value);
});

Expand Down
9 changes: 6 additions & 3 deletions rice/stl/unordered_map.ipp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../traits/function_traits.hpp"
#include "../traits/rice_traits.hpp"
#include "../detail/from_ruby.hpp"
#include "../detail/to_ruby.hpp"
#include "../detail/RubyFunction.hpp"
Expand All @@ -20,8 +21,10 @@ namespace Rice
using Key_T = typename T::key_type;
using Mapped_T = typename T::mapped_type;
using Value_T = typename T::value_type;
using Reference_T = typename T::reference;
using Size_T = typename T::size_type;
using Difference_T = typename T::difference_type;
using To_Ruby_T = typename detail::remove_cv_recursive_t<Mapped_T>;

public:
UnorderedMapHelper(Data_Type<T> klass) : klass_(klass)
Expand Down Expand Up @@ -191,10 +194,10 @@ namespace Rice
klass_.define_method("to_h", [](T& unordered_map)
{
VALUE result = rb_hash_new();
std::for_each(unordered_map.begin(), unordered_map.end(), [&result](const auto& pair)
std::for_each(unordered_map.begin(), unordered_map.end(), [&result](const Reference_T& pair)
{
VALUE key = detail::To_Ruby<Key_T>().convert(pair.first);
VALUE value = detail::To_Ruby<Mapped_T>().convert(pair.second);
VALUE key = detail::To_Ruby<Key_T&>().convert(pair.first);
VALUE value = detail::To_Ruby<To_Ruby_T&>().convert(pair.second);
rb_hash_aset(result, key, value);
});

Expand Down

0 comments on commit 22b9ae3

Please sign in to comment.