Class: Oinky::Internal::DB_string

Inherits:
FFI::Struct
  • Object
show all
Includes:
NativeBuffer
Defined in:
lib/oinky.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NativeBuffer

#<=>, #clone, #each_byte, #each_char, #inspect, #rb_str

Instance Attribute Details

#refsObject

Returns the value of attribute refs.



71
72
73
# File 'lib/oinky.rb', line 71

def refs
  @refs
end

Class Method Details

.make(val) ⇒ Object



73
74
75
76
77
# File 'lib/oinky.rb', line 73

def self.make(val)
  nv = DB_string.new
  nv.refs = make_in_place(nv, val)
  nv
end

.make_in_place(nv, val) ⇒ Object

Have to separate like this because FFI Doesn’t allow assigning to structs by value.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oinky.rb', line 81

def self.make_in_place(nv, val)
  if val.is_a? String
    val = NativeMallocBuffer.new(val)
    nv[:bytes] = val.ptr
    nv[:length] = val.length
  elsif val.is_a? NativeBuffer
    nv[:bytes] = val.ptr
    nv[:length] = val.length
  else
    raise OinkyException.new("Argument is not a recognized string")
  end

  # Caller keeps the source object in memory as long as the variant.
  # This is important whatever the source, ruby strings and 
  # NativeBuffers equally.
  return val
end

Instance Method Details

#lengthObject



103
104
105
# File 'lib/oinky.rb', line 103

def length
  self[:length]
end

#ptrObject

These are required by NativeBuffer



100
101
102
# File 'lib/oinky.rb', line 100

def ptr
  self[:bytes]
end