Class: Wrapture::StructSpec
- Inherits:
-
Object
- Object
- Wrapture::StructSpec
- Defined in:
- lib/wrapture/struct_spec.rb
Overview
A description of a struct.
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
A list of rules defined for this struct.
Class Method Summary collapse
-
.normalize_spec_hash(spec) ⇒ Object
Normalizes a hash specification of a struct.
Instance Method Summary collapse
-
#declaration(name) ⇒ Object
A declaration of the struct with the given variable name.
-
#includes ⇒ Object
A list of includes required for this struct.
-
#initialize(spec) ⇒ StructSpec
constructor
Creates a struct spec based on the provided spec hash.
-
#member_list ⇒ Object
A string containing the typed members of the struct, separated by commas.
-
#member_list_with_defaults ⇒ Object
A string containing the typed members of the struct, with their default values if provided, separated by commas.
-
#members ⇒ Object
The members of the struct.
-
#members? ⇒ Boolean
True if there are members included in the struct specification.
-
#name ⇒ Object
The name of this struct.
-
#pointer_declaration(name) ⇒ Object
A declaration of a pointer to the struct with the given variable name.
-
#rules_check(name) ⇒ Object
A string containing an expression that returns true if the struct with the given name meets all rules defined for this struct.
Constructor Details
#initialize(spec) ⇒ StructSpec
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)
53 54 55 56 57 |
# 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 |
Instance Attribute Details
#rules ⇒ Object (readonly)
A list of rules defined for this struct.
40 41 42 |
# File 'lib/wrapture/struct_spec.rb', line 40 def rules @rules end |
Class Method Details
.normalize_spec_hash(spec) ⇒ Object
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).
28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Method Details
#declaration(name) ⇒ Object
A declaration of the struct with the given variable name.
60 61 62 |
# File 'lib/wrapture/struct_spec.rb', line 60 def declaration(name) "struct #{@spec['name']} #{name}" end |
#includes ⇒ Object
A list of includes required for this struct.
65 66 67 |
# File 'lib/wrapture/struct_spec.rb', line 65 def includes @spec['includes'].dup end |
#member_list ⇒ Object
A string containing the typed members of the struct, separated by commas.
70 71 72 73 74 75 76 77 78 |
# 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 |
#member_list_with_defaults ⇒ Object
A string containing the typed members of the struct, with their default values if provided, separated by commas.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |
#members ⇒ Object
The members of the struct
104 105 106 |
# File 'lib/wrapture/struct_spec.rb', line 104 def members @spec['members'] end |
#members? ⇒ Boolean
True if there are members included in the struct specification.
109 110 111 |
# File 'lib/wrapture/struct_spec.rb', line 109 def members? !@spec['members'].empty? end |
#name ⇒ Object
The name of this struct
114 115 116 |
# File 'lib/wrapture/struct_spec.rb', line 114 def name @spec['name'] end |
#pointer_declaration(name) ⇒ Object
A declaration of a pointer to the struct with the given variable name.
119 120 121 |
# File 'lib/wrapture/struct_spec.rb', line 119 def pointer_declaration(name) "struct #{@spec['name']} *#{name}" end |
#rules_check(name) ⇒ Object
A string containing an expression that returns true if the struct with the given name meets all rules defined for this struct.
125 126 127 |
# File 'lib/wrapture/struct_spec.rb', line 125 def rules_check(name) @rules.map { |rule| rule.check(variable: name) }.join(' && ') end |