Class: Ragweed::Ptr

Inherits:
Object show all
Defined in:
lib/ragweed/ptr.rb

Overview

TODO: make read/write work for other oses

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i) ⇒ Ptr

initialize with a number or another pointer (implements copy-ctor)



16
17
18
19
20
21
22
23
24
25
# File 'lib/ragweed/ptr.rb', line 16

def initialize(i)
  if i.kind_of? self.class
    @val = i.val
    @p = i.p
  elsif not i
    @val = 0
  else
    @val = i
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

everything else: work like an integer — also, where these calls return numbers, turn them back into pointers, so pointer math doesn’t shed the class wrapper



40
41
42
43
44
45
46
47
# File 'lib/ragweed/ptr.rb', line 40

def method_missing(meth, *args) 
  ret = @val.send meth, *args
  if ret.kind_of? Numeric
    ret = Ragweed::Ptr.new(ret)
    ret.p = self.p
  end
  ret
end

Instance Attribute Details

#pObject

A dubious achievement. Wrap Integers in a pointer class, which, when you call to_s, returns the marshalled type, and which exports read/write methods.



7
8
9
# File 'lib/ragweed/ptr.rb', line 7

def p
  @p
end

#valObject (readonly)

Returns the value of attribute val.



8
9
10
# File 'lib/ragweed/ptr.rb', line 8

def val
  @val
end

Instance Method Details

#null?Boolean

ptr-to-zero?

Returns:

  • (Boolean)


11
12
13
# File 'lib/ragweed/ptr.rb', line 11

def null?
  @val == 0
end

#read(sz) ⇒ Object



35
# File 'lib/ragweed/ptr.rb', line 35

def read(sz); p.read(self, sz); end

#to_iObject

return the underlying number



31
# File 'lib/ragweed/ptr.rb', line 31

def to_i; @val; end

#to_sObject

return the raw pointer bits



28
# File 'lib/ragweed/ptr.rb', line 28

def to_s; @val.to_l32; end

#write(arg) ⇒ Object

only works if you attach a process



34
# File 'lib/ragweed/ptr.rb', line 34

def write(arg); p.write(self, arg); end