Class: Gogyou::Model::BasicCreator::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/gogyou/model.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(creator, packexp = Field::PACKSIZE_NOTDEFINE) ⇒ Proxy

class Proxy < BasicObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/gogyou/model.rb', line 242

def initialize(creator, packexp = Field::PACKSIZE_NOTDEFINE)
  #singleton_class = (class << proxy; self; end)
  singleton_class.class_eval do
    latest_fields = nil
    #define_method(:method_missing, ->(type, *args) { latest_fields = creator.addfield(type, args); nil })
    creator.typemap.each_key do |t|
      define_method(t, ->(*args) { latest_fields = creator.addfield(t, packexp, args); nil })
    end
    define_method(:struct, ->(*args, &block) { latest_fields = creator.struct(args, packexp, &block); nil })
    define_method(:union, ->(*args, &block) { latest_fields = creator.union(args, packexp, &block); nil })
    define_method(:const, ->(dummy_fields) { creator.const(latest_fields); latest_fields = nil; nil })
    define_method(:typedef, ->(*args, &block) { creator.typedef(args, &block) })
    packexp0 = nil
    define_method(:packed, ->(bytealign = 1, &block) {
      raise "wrong nested ``packed''" if packexp0
      exp = Math.log(bytealign, 2)
      # exp が Nan Infinity -Infinity の場合は例外が発生するので、それに対する処置も行う
      unless ((exp = exp.to_i) rescue nil) && (1 << exp) == bytealign
        raise ArgumentError, "shall be given power of two (but #{bytealign})"
      end

      begin
        packexp0 = packexp
        packexp = exp
        self.instance_exec(&block)
      ensure
        (packexp, packexp0) = packexp0, nil
      end

      nil
    })
    if creator.respond_to?(:bytealign)
      define_method(:bytealign, ->(bytesize, &block) { creator.bytealign(bytesize, &block); nil })
    end
    if creator.respond_to?(:padding)
      define_method(:padding, ->(bytesize, &block) { creator.padding(bytesize, &block); nil })
    end
  end
end