Class: LibBin::Structure::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/libbin/data_types.rb,
ext/libbin/libbin_c.c

Overview

Field class that is instantiated to describe a structure field. Instances are immutable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, length, count, offset, sequence, condition, relative_offset, align, expect) ⇒ Field

Returns new Field.

Parameters:

  • name (Symbol, String)

    the name of the field.

  • type (Class, String, Proc)

    the type of the field, as a Class, or as a String or Proc that will be evaluated in the context of the LibBin::Structure instance.

  • length (nil, Integer, String, Proc)

    if given, consider the field a vector of the type. The length is either a constant Integer of a String or Proc that will be evaluated in the context of the LibBin::Structure instance.

  • count (nil, Integer, String, Proc)

    if given, consider the field is repeated count times. The count is either a constant Integer of a String or Proc that will be evaluated in the context of the LibBin::Structure instance.

  • offset (nil, integer, String, Proc)

    if given, the absolute offset in the file, or the offset from the parent position, where the field can be found. See relative offset. The offset is either a constant Integer of a String or Proc that will be evaluated in the context of the LibBin::Structure instance.

  • sequence (Boolean)

    if true, type, length, offset, and condition are evaluated for each repetition.

  • condition (nil, String, Proc)

    if given, the field, or repetition of the field can be conditionally present. The condition will be evaluated in the context of the LibBin::Structure instance.

  • relative_offset (Boolean)

    consider the offset relative to the field parent.

  • align (nil, Integer)

    if given, align the field. If given as an Integer it must be a power of 2. Else the field is aligned to the field's type preferred alignment

  • expect (nil, Proc, Object)

    if given as a Proc the proc must evaluate to a truthy value or an exception will be raised. Else, the object will be tested for equality, and an exception will be raised if objects were not equal. Proc is evaluated in the context of the LibBin::Structure instance and #__value will be set. Each repetition is validated.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/libbin/libbin_c.c', line 68

static VALUE cField_initialize(
    VALUE self,
    VALUE name,
    VALUE type,
    VALUE length,
    VALUE count,
    VALUE offset,
    VALUE sequence,
    VALUE condition,
    VALUE relative_offset,
    VALUE align,
    VALUE expect) {
  VALUE tmp;
  struct cField_data *data;
  TypedData_Get_Struct(self, struct cField_data, &cField_type, data);
  if (RTEST(align)) {
    size_t a = NUM2LL(align);
    if (a & (a-1))
      rb_raise(rb_eRuntimeError, "alignment is not a power of 2: %zu", a);
  }
  data->name = name;
  tmp = rb_str_dup(rb_obj_as_string(name));
  data->getter = rb_intern_str(tmp);
  data->setter = rb_intern_str(rb_str_cat(tmp, "=", 1));
  data->type = cField_preprocess_expression(self, type);
  data->length = cField_preprocess_expression(self, length);
  data->count = cField_preprocess_expression(self, count);
  data->offset = cField_preprocess_expression(self, offset);
  data->sequence = sequence;
  data->condition = cField_preprocess_expression(self, condition);
  data->relative_offset = relative_offset;
  data->align = align;
  data->expect = expect;
  return self;
}

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



10
11
12
# File 'lib/libbin/data_types.rb', line 10

def align
  @align
end

#conditionObject (readonly)

Returns the value of attribute condition.



9
10
11
# File 'lib/libbin/data_types.rb', line 9

def condition
  @condition
end

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/libbin/data_types.rb', line 7

def count
  @count
end

#expectObject (readonly)

Returns the value of attribute expect.



11
12
13
# File 'lib/libbin/data_types.rb', line 11

def expect
  @expect
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/libbin/data_types.rb', line 6

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/libbin/data_types.rb', line 4

def name
  @name
end

#offsetObject (readonly)

Returns the value of attribute offset.



8
9
10
# File 'lib/libbin/data_types.rb', line 8

def offset
  @offset
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/libbin/data_types.rb', line 5

def type
  @type
end

Instance Method Details

#relative_offset?Boolean

Returns true if the field offset should be relative to its parent.

Returns:

  • (Boolean)

    field offset is relative



# File 'lib/libbin/data_types.rb', line 13

#sequence?Boolean

Returns true if the field is a sequence

Returns:

  • (Boolean)

    field is a sequence



# File 'lib/libbin/data_types.rb', line 17