Class: XS::PollItems

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ffi-rxs/poll_items.rb

Instance Method Summary collapse

Constructor Details

#initializePollItems

Returns a new instance of PollItems.



7
8
9
10
11
# File 'lib/ffi-rxs/poll_items.rb', line 7

def initialize
  @element_size = LibXS::PollItem.size
  @store = nil
  @items = []
end

Instance Method Details

#<<(obj) ⇒ Object Also known as: push



35
36
37
38
# File 'lib/ffi-rxs/poll_items.rb', line 35

def <<(obj)
  @dirty = true
  @items << obj
end

#addressObject



17
18
19
20
# File 'lib/ffi-rxs/poll_items.rb', line 17

def address
  clean
  @store
end

#delete(sock) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ffi-rxs/poll_items.rb', line 41

def delete sock
  address = sock.socket.address
  found = false
  
  each_with_index do |item, index|
    if address == item[:socket].address
      @items.delete_at index
      found = true
      @dirty = true
      clean
      break
    end
  end
  
  # these semantics are different from the usual Array#delete; returns a
  # boolean instead of the actual item or nil
  found
end

#delete_at(index) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ffi-rxs/poll_items.rb', line 60

def delete_at index
  value = nil
  unless @items.empty?
    value = @items.delete_at index
    @dirty = true
    clean
  end
  
  value
end

#each(&blk) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/ffi-rxs/poll_items.rb', line 71

def each &blk
  clean
  index = 0
  until index >= @items.size do
    struct = get index
    yield struct
    index += 1
  end
end

#each_with_index(&blk) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/ffi-rxs/poll_items.rb', line 81

def each_with_index &blk
  clean
  index = 0
  until index >= @items.size do
    struct = get index
    yield struct, index
    index += 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


15
# File 'lib/ffi-rxs/poll_items.rb', line 15

def empty?; @items.empty?; end

#get(index) ⇒ Object Also known as: []



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ffi-rxs/poll_items.rb', line 22

def get index
  unless @items.empty? || index.nil?
    clean

    # pointer arithmetic in ruby
    pointer = @store + (@element_size * index)

    # cast the memory to a PollItem
    LibXS::PollItem.new pointer
  end
end

#inspectObject



91
92
93
94
95
96
# File 'lib/ffi-rxs/poll_items.rb', line 91

def inspect
  clean
  str = ""
  each { |item| str << "ptr [#{item[:socket]}], events [#{item[:events]}], revents [#{item[:revents]}], " }
  str.chop.chop
end

#sizeObject



13
# File 'lib/ffi-rxs/poll_items.rb', line 13

def size; @items.size; end

#to_sObject



98
# File 'lib/ffi-rxs/poll_items.rb', line 98

def to_s(); inspect; end