class ZMQ::PollItems

Public Class Methods

new() click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 11
def initialize
  @pollables  = {}
  @item_size  = LibZMQ::PollItem.size
  @item_store = nil
end

Public Instance Methods

<<(poll_item) click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 32
def <<(poll_item)
  @dirty = true
  @pollables[poll_item.pollable] = OpenStruct.new(:index => size, :data => poll_item)
end
Also aliased as: push
[](pollable)
Alias for: get
address() click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 17
def address
  clean
  @item_store
end
delete(pollable) click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 38
def delete pollable
  if @pollables.delete(pollable)
    @dirty = true
    clean
    true
  else
    false
  end
end
each() { |get(pollable)| ... } click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 48
def each &blk
  clean
  @pollables.each_key do |pollable|
    yield get(pollable)
  end
end
get(pollable) click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 22
def get pollable
  return unless entry = @pollables[pollable]
  clean
  pointer = @item_store + (@item_size * entry.index)
  item = ZMQ::PollItem.from_pointer(pointer)
  item.pollable = pollable
  item
end
Also aliased as: []
inspect() click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 55
def inspect
  clean
  str = ""
  each { |item| str << "ptr [#{item[:socket]}], events [#{item[:events]}], revents [#{item[:revents]}], " }
  str.chop.chop
end
push(poll_item)
Alias for: <<
to_s() click to toggle source
# File lib/ffi-rzmq/poll_items.rb, line 62
def to_s; inspect; end

Private Instance Methods

clean() click to toggle source

Allocate a contiguous chunk of memory and copy over the PollItem structs to this block. Note that the old +@store+ value goes out of scope so when it is garbage collected that native memory should be automatically freed.

# File lib/ffi-rzmq/poll_items.rb, line 69
def clean
  if @dirty
    @item_store = FFI::MemoryPointer.new @item_size, size, true

    offset = 0
    @pollables.each_with_index do |(pollable, entry), index|
      entry.index = index
      LibC.memcpy(@item_store + offset, entry.data.pointer, @item_size)
      offset += @item_size
    end

    @dirty = false
  end
end