Skip to content

Commit

Permalink
Merge pull request #262 from ruby-rice/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
cfis authored Feb 26, 2025
2 parents 89b5aff + 253bd8e commit f85e8f4
Show file tree
Hide file tree
Showing 8 changed files with 1,355 additions and 111 deletions.
42 changes: 1 addition & 41 deletions rice/MemoryView.ipp
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
namespace Rice
{
}

namespace Rice::detail
{
template<>
class To_Ruby<unsigned char**>
{
public:
VALUE convert(unsigned char** x)
{
std::runtime_error("To_Ruby unsigned char** is not implemented yet");
return Qnil;
}
};

template<>
class From_Ruby<unsigned char**>
{
public:
From_Ruby() = default;

explicit From_Ruby(Arg* arg) : arg_(arg)
{
}

Convertible is_convertible(VALUE value)
{
std::runtime_error("From_Ruby unsigned char** is not implemented yet");
return Convertible::None;
}

unsigned char** convert(VALUE value)
{
std::runtime_error("From_Ruby unsigned char** is not implemented yet");
return nullptr;
}

private:
Arg* arg_ = nullptr;
};
}
}
4 changes: 2 additions & 2 deletions rice/PointerView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Rice
PointerView(T* pointer, size_t size);
PointerView(const PointerView& other);

VALUE buffer(size_t offset, size_t count);
VALUE read(size_t offset, size_t count);
Array toArray(size_t offset, size_t count);

VALUE buffer();
VALUE read();
Array toArray();

T* pointer = nullptr;
Expand Down
33 changes: 22 additions & 11 deletions rice/PointerView.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Rice
}

template<typename T>
inline VALUE PointerView<T>::buffer(size_t offset, size_t count)
inline VALUE PointerView<T>::read(size_t offset, size_t count)
{
if (!this->pointer)
{
Expand All @@ -33,9 +33,9 @@ namespace Rice
}

template<typename T>
inline VALUE PointerView<T>::buffer()
inline VALUE PointerView<T>::read()
{
return this->buffer(0, this->size);
return this->read(0, this->size);
}

template<typename T>
Expand All @@ -47,21 +47,32 @@ namespace Rice
}
else if constexpr (std::is_fundamental_v<T>)
{
VALUE string = this->buffer(offset, count);
VALUE string = this->read(offset, count);
return String(string).unpack<T>();
}
else
{
// Is this a pointer to a pointer, so T**?
constexpr bool isPointerToPointer = std::is_pointer_v<T>;

Array result;
detail::To_Ruby<T&> toRuby;

T* begin = this->pointer + offset;
T* end = this->pointer + offset + count;

for (T* ptr = begin; ptr < end; ptr++)
{
T& object = *ptr;
result.push(object);

if constexpr (isPointerToPointer)
{
PointerView<std::remove_pointer_t<T>> pointerView(object);
result.push(pointerView);
}
else
{
result.push(object);
}
}
return result;
}
Expand All @@ -75,7 +86,7 @@ namespace Rice

// Specializations to handle void
template<>
inline VALUE PointerView<void>::buffer(size_t offset, size_t count)
inline VALUE PointerView<void>::read(size_t offset, size_t count)
{
return Qnil;
}
Expand All @@ -98,10 +109,10 @@ namespace Rice::detail

Data_Type<T> result = define_class_under<T>(rb_mRice, klassName).
define_attr("size", &T::size).
template define_method<VALUE(T::*)(size_t, size_t)>("buffer", &T::buffer, Return().setValue()).
template define_method<VALUE(T::*)()>("buffer", &T::buffer, Return().setValue()).
template define_method<Array(T::*)(size_t, size_t)>("to_array", &T::toArray, Return().setValue()).
template define_method<Array(T::*)()>("to_array", &T::toArray, Return().setValue());
template define_method<VALUE(T::*)(size_t, size_t)>("read", &T::read, Return().setValue()).
template define_method<VALUE(T::*)()>("read", &T::read, Return().setValue()).
template define_method<Array(T::*)(size_t, size_t)>("to_a", &T::toArray, Return().setValue()).
template define_method<Array(T::*)()>("to_a", &T::toArray, Return().setValue());

return result;
}
Expand Down
Loading

0 comments on commit f85e8f4

Please sign in to comment.