Skip to content
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

Closed
Burgestrand opened this issue Jun 19, 2011 · 4 comments
Closed

(feature request) reading a size_t from a Pointer #118

Burgestrand opened this issue Jun 19, 2011 · 4 comments

Comments

@Burgestrand
Copy link
Contributor

I am binding a method that takes a pointer to a :size_t, but I cannot currently find any read_size_t method on FFI::Pointer. It’d be nice to have one.

@Burgestrand
Copy link
Contributor Author

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.

@ghost
Copy link

ghost commented Jun 20, 2011

I wonder if this could be generalised into a read_type() method.
e.g.

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.

@Burgestrand
Copy link
Contributor Author

Aye; one just has to remember to pass the arguments along as well.

@ghost
Copy link

ghost commented Sep 22, 2012

Closing all feature-request issues

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant