class Gogyou::Accessor::Array

Public Class Methods

aset(buffer, offset, value) click to toggle source
# File lib/gogyou/accessor.rb, line 495
def self.aset(buffer, offset, value)
  case value
  when ::String
    raise ArgumentError, "buffer size too small" unless value.bytesize <= self::BYTESIZE
    buffer.setbinary(offset, value, 0, self::BYTESIZE)
  when ::Array
    raise NotImplementedError
  when self::SUBTYPE
    raise NotImplementedError
  else
    raise ArgumentError
  end

  value
end
define(model) click to toggle source
# File lib/gogyou/accessor.rb, line 398
      def self.define(model)
        klass = ::Class.new(self)
        klass.class_eval do
          field = model.fields[0]
          const_set(:MODEL, model)
          const_set(:BYTESIZE, model.bytesize)
          const_set(:BYTEALIGN, model.bytealign)
          const_set(:EXTENSIBLE, model.bytesize == 0 || model.extensible?)
          const_set(:ELEMENTS, elements = field.name)

          vector = field.vector

          if vector
            type = define_subarray(field)
          else
            type = field.type
          end

          if type.kind_of?(::Module)
            # すでに名前が定義されてる場合はこれで固定される
            type.name
          end
          const_set(:SUBTYPE, type)
          bytesize = type.bytesize

          if model.bytesize == 0
            class_eval <<-EOS, __FILE__, __LINE__ + 1
              def check_index(index)
                index = index.to_i
                unless index >= 0 && index < elementsize
                  raise IndexError, "out of element size (index \#{index} for 0 ... \#{elementsize})", caller
                end
                index
              end

              def <<(value)
                raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__], caller if frozen?
                voff = (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(SUBTYPE.bytesize)
                expandsize = @offset__GOGYOU__ + voff + SUBTYPE.bytesize
                @buffer__GOGYOU__.resize(expandsize)
                SUBTYPE.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + voff, value)
                self
              end

              def elementsize
                (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).unit_floor(SUBTYPE.bytesize)
              end

              alias size elementsize

              def bytesize
                (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(SUBTYPE.bytesize)
              end
            EOS
          else
            eval <<-EOS
              def check_index(index)
                index = index.to_i
                unless index >= 0 && (#{elements.nil?} || index < #{elements})
                  raise IndexError, "out of element size (index \#{index} for 0 ... #{elements})", caller
                end
                index
              end

              def elementsize
                #{elements}
              end

              alias size elementsize

              def bytesize
                #{type.bytesize * elements}
              end
            EOS
          end

          class_eval <<-EOS, __FILE__, __LINE__ + 1
            def to_s
              @buffer__GOGYOU__.byteslice(@offset__GOGYOU__, bytesize)
            end

            def [](index)
              v = SUBTYPE.aref(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * #{bytesize})
              v.infect_from(self, @buffer__GOGYOU__) unless v.frozen?
              v.freeze if #{field.const?} || frozen? || @buffer__GOGYOU__.frozen?
              v
            end

            def []=(index, value)
              raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__, index], caller if #{field.const?} || frozen?
              SUBTYPE.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * #{bytesize}, value)
            end
          EOS
        end
        klass
      end
elements() click to toggle source
# File lib/gogyou/accessor.rb, line 394
def self.elements
  self::ELEMENTS
end

Public Instance Methods

bytesize() click to toggle source
Calls superclass method Gogyou::Accessor#bytesize
# File lib/gogyou/accessor.rb, line 511
def bytesize
  return super unless self.class.extensible?
  self.class::BYTESIZE * @buffer__GOGYOU__.bytesize.unit_floor(self.class::SUBTYPE)
end
each() { |self| ... } click to toggle source
# File lib/gogyou/accessor.rb, line 516
def each
  return to_enum unless block_given?
  elementsize.times { |i| yield self[i] }
  self
end
each_with_index() { |self, i| ... } click to toggle source
# File lib/gogyou/accessor.rb, line 522
def each_with_index
  return to_enum(:each_with_index) unless block_given?
  elementsize.times { |i| yield self[i], i }
  self
end
inspect() click to toggle source
# File lib/gogyou/accessor.rb, line 528
def inspect
  text = "["
  elementsize.times.with_index do |n, i|
    text << (i > 0 ? ", " : "") << __send__(:[], n).inspect
  end
  text << "]"
end
pretty_print(q) click to toggle source
# File lib/gogyou/accessor.rb, line 536
def pretty_print(q)
  q.group(1, "[") do
    elementsize.times.with_index do |n, i|
      if i > 0
        q.text ","
        q.breakable " "
      end
      q.pp __send__(:[], n)
    end
    q.text "]"
  end
end