Class: NiceFFI::Struct

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/net-snmp2.rb

Overview

XXX

I just monkeypatched this to take a nil first argument.  Seems to work
Should probably submit this as a patch

Instance Method Summary collapse

Constructor Details

#initialize(val = nil, options = {}) ⇒ Struct

Returns a new instance of Struct.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/net-snmp2.rb', line 47

def initialize( val = nil, options={} )
  # Stores certain kinds of member values so that we don't need
  # to create a new object every time they are read.
  @member_cache = {}

  options = {:autorelease => true}.merge!( options )

  case val

  when Hash
    super(FFI::Buffer.new(size))
    init_from_hash( val )         # Read the values from a Hash.

  # Note: plain "Array" would mean FFI::Struct::Array in this scope.
  when ::Array
    super(FFI::Buffer.new(size))
    init_from_array( val )        # Read the values from an Array.

  when String
    super(FFI::Buffer.new(size))
    init_from_bytes( val )        # Read the values from a bytestring.

  when self.class
    super(FFI::Buffer.new(size))
    init_from_bytes( val.to_bytes ) # Read the values from another instance.

  when FFI::Pointer, FFI::Buffer
    val = _make_autopointer( val, options[:autorelease] )

    # Normal FFI::Struct behavior to wrap the pointer.
    super( val )

  when nil
    super(val)
  else
    raise TypeError, "cannot create new #{self.class} from #{val.inspect}"
  end
end