class Wrapture::StructSpec
A description of a struct.
Attributes
A list of rules defined for this struct.
Public Class Methods
Creates a struct spec based on the provided spec hash.
The hash must have the following keys:
- name
-
the name of the struct
The following keys are optional:
- includes
-
a list of includes required for the struct
- members
-
a list of the members of the struct, each with a type and name
field
- rules
-
a list of conditions this struct and its members must meet (refer
to the RuleSpec
class for more details)
# File lib/wrapture/struct_spec.rb, line 53 def initialize(spec) @spec = StructSpec.normalize_spec_hash(spec) @rules = @spec['rules'].map { |rule_spec| RuleSpec.new(rule_spec) } end
Normalizes a hash specification of a struct. Normalization will check for things like invalid keys, duplicate entries in include lists, and will set missing keys to their default value (for example, an empty list if no includes are given).
# File lib/wrapture/struct_spec.rb, line 28 def self.normalize_spec_hash(spec) normalized = spec.dup normalized.default = [] normalized['includes'] = Wrapture.normalize_includes spec['includes'] normalized['members'] ||= [] normalized end
Public Instance Methods
A declaration of the struct with the given variable name.
# File lib/wrapture/struct_spec.rb, line 60 def declaration(name) "struct #{@spec['name']} #{name}" end
A list of includes required for this struct.
# File lib/wrapture/struct_spec.rb, line 65 def includes @spec['includes'].dup end
A string containing the typed members of the struct, separated by commas.
# File lib/wrapture/struct_spec.rb, line 70 def member_list members = [] @spec['members'].each do |member| members << TypeSpec.new(member['type']).variable(member['name']) end members.join ', ' end
A string containing the typed members of the struct, with their default values if provided, separated by commas.
# File lib/wrapture/struct_spec.rb, line 82 def member_list_with_defaults @spec['members'].map do |member| member_str = TypeSpec.new(member['type']).variable(member['name']) if member.key?('default-value') default_value = member['default-value'] member_str += ' = ' member_str += if member['type'] == 'const char *' '"' + default_value + '"' elsif member['type'].end_with?('char') "'#{default_value}'" else default_value.to_s end end member_str end.join(', ') end
The members of the struct
# File lib/wrapture/struct_spec.rb, line 104 def members @spec['members'] end
True if there are members included in the struct specification.
# File lib/wrapture/struct_spec.rb, line 109 def members? !@spec['members'].empty? end
The name of this struct
# File lib/wrapture/struct_spec.rb, line 114 def name @spec['name'] end
A declaration of a pointer to the struct with the given variable name.
# File lib/wrapture/struct_spec.rb, line 119 def pointer_declaration(name) "struct #{@spec['name']} *#{name}" end
A string containing an expression that returns true if the struct with the given name meets all rules defined for this struct.
# File lib/wrapture/struct_spec.rb, line 125 def rules_check(name) @rules.map { |rule| rule.check(variable: name) }.join(' && ') end