Class: PackedStruct::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/packed_struct/directive.rb

Overview

Contains information about a directive. A directive can be a name of the type, the endian(ness) of the type, the type of the type (short, int, long, etc.), and the signed(ness) of the type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *arguments) ⇒ Directive

Initialize the directive.

Parameters:

  • name (Symbol)

    the name of the directive.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/packed_struct/directive.rb', line 52

def initialize(name, *arguments)
  @name = name

  @options = arguments
  @tags    = {}
  @subs    = []
  @parent  = nil
  @value   = nil

  if arguments.first.is_a? Directive
    arguments.first.add_child(self)
    @subs = nil
  end
end

Instance Attribute Details

#nameSymbol (readonly)

The name of the directive. This is passed as the first value of the directive; from Package, it is the name of the method call.

Returns:

  • (Symbol)


12
13
14
# File 'lib/packed_struct/directive.rb', line 12

def name
  @name
end

#optionsArray<Object> (readonly)

The arguments passed to the directive.

Returns:

  • (Array<Object>)


17
18
19
# File 'lib/packed_struct/directive.rb', line 17

def options
  @options
end

#parentnil, Directive

The parent of this directive. The relationship is such that parent.subs.include?(self) is true. If this has a parent, it is nil.

Returns:



36
37
38
# File 'lib/packed_struct/directive.rb', line 36

def parent
  @parent
end

#subsnil, Array<Directive> (readonly)

The children of this directive. If this directive has a parent, this is nil.

Returns:



29
30
31
# File 'lib/packed_struct/directive.rb', line 29

def subs
  @subs
end

#tagsHash

The tags the directive has, such as type, signed(ness), endian(ness), and size. Not filled until #to_s is called.

Returns:

  • (Hash)


23
24
25
# File 'lib/packed_struct/directive.rb', line 23

def tags
  @tags
end

#valueObject



45
46
47
# File 'lib/packed_struct/directive.rb', line 45

def value
  @value || (@tags[:original].value if @tags[:original])
end

Instance Method Details

#+(other) ⇒ Directive

To show the size of something else, relative to this directive.

Returns:



128
129
130
131
132
133
134
# File 'lib/packed_struct/directive.rb', line 128

def +(other)
  dir = dup
  dir.tags = tags.dup
  dir.tags[:original   ] = self
  dir.tags[:size_modify] = +other
  dir
end

#-(other) ⇒ Directive

To show the size of something else, relative to this directive.

Returns:



117
118
119
120
121
122
123
# File 'lib/packed_struct/directive.rb', line 117

def -(other)
  dir = dup
  dir.tags = tags.dup
  dir.tags[:original   ] = self
  dir.tags[:size_modify] = -other
  dir
end

#[](size) ⇒ self

Set the size of this directive.

Returns:

  • (self)


88
89
90
91
# File 'lib/packed_struct/directive.rb', line 88

def [](size)
  @tags[:size] = size
  self
end

#add_child(child) ⇒ Directive

Add a child to this (or its parent’s) directive. If this is a child itself, it adds it to the parent of this. Invalidates the caches for #sub_names and #to_s

Parameters:

Returns:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/packed_struct/directive.rb', line 73

def add_child(child)
  if @parent
    @parent.add_child(child)
  else
    @_str = nil
    @_sub_types = nil
    @subs << child
    child.parent = self
    child
  end
end

#bytesizeNumeric

The number of bytes this takes up in the resulting packed string.

Returns:

  • (Numeric)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/packed_struct/directive.rb', line 139

def bytesize
  case @tags[:type]
  when nil
    size / 8
  when :short
    2
  when :int
    4
  when :long
    4
  when :float
    if sub_names.include?(:double)
      8
    else
      4
    end
  when :null
    size || 1
  when :string
    size
  else
    0
  end
end

#inspectString

Inspects the directive.

Returns:

  • (String)


110
111
112
# File 'lib/packed_struct/directive.rb', line 110

def inspect
  "#<#{self.class.name}:#{name}>"
end

#to_sString

Turn the directive into a string. Analyzes the subs before determining information, then outputs that. Caches the value until #add_child is next called.

Returns:

  • (String)


98
99
100
101
102
103
104
105
# File 'lib/packed_struct/directive.rb', line 98

def to_s
  return "" unless @subs
  @subs.compact!
  @tags[:signed]   = determine_signed
  @tags[:type]     = determine_type
  @tags[:endian]   = determine_endian
  "#{make_directive}#{make_length}"
end