-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feature request) reading a size_t
from a Pointer
#118
Comments
I’ve added a temporary solution on my personal project: module FFI
class Pointer
type = FFI.find_type(:size_t)
type, _ = FFI::TypeDefs.find do |(name, t)|
method_defined? "read_#{name}" if t == type
end
alias_method :read_size_t, "read_#{type}" if type
end
end I’m blissfully unaware about the implications of “size_t”, all I do remember is that it’s a peculiar little type. For now, my above code will do for me. |
I wonder if this could be generalised into a read_type() method. class Pointer
# FFI.find_type() will resolve :size_t down to e.g. Type::INT32, so map from that to the appropriate read/write type
TYPE_METHOD_NAMES = {
Type::INT32, :int,
Type::INT64, :long_long,
# etc, etc
}
def read_type(type)
method_name = TYPE_METHOD_NAMES[FFI.find_type(type)]
self.send(method_name)
end
end That will be a lot slower than an alias, but it avoids polluting the Pointer class with a gazillion read/write methods for each type alias. |
Aye; one just has to remember to pass the arguments along as well. |
Closing all feature-request issues |
I am binding a method that takes a pointer to a
:size_t
, but I cannot currently find anyread_size_t
method on FFI::Pointer. It’d be nice to have one.The text was updated successfully, but these errors were encountered: