Class: CType::StructUnion

Inherits:
Object
  • Object
show all
Defined in:
lib/dbc/ctype.rb

Direct Known Subclasses

Struct, Union

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ StructUnion

Returns a new instance of StructUnion.



99
100
101
102
# File 'lib/dbc/ctype.rb', line 99

def initialize(name = nil)
	@name = name
	@members = nil
end

Class Method Details

.get(type, name, members = nil) ⇒ Object

want only one instance of each unique Struct or Union



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dbc/ctype.rb', line 83

def StructUnion.get(type, name, members = nil)
	case type
		when 'struct'
			if members
				Struct[name].add(members)
			else
				Struct[name]
			end
		when 'union'
			if members
				Union[name].add(members)
			else
				Union[name]
			end
	end
end

Instance Method Details

#add(items) ⇒ Object



103
104
105
106
107
# File 'lib/dbc/ctype.rb', line 103

def add(items)
	#warn "structure or union #{@name} already defined" if @members
	@members = items.flatten
	self
end

#evaluate(identifier) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dbc/ctype.rb', line 118

def evaluate(identifier)
	return self if identifier.empty?
	CType.evaluation_error(identifier) unless identifier =~ /\A\.([A-Za-z_]\w*)/
	post_match = $'
	p_type = lookup($1)
	if p_type
		p_type.evaluate(post_match)
	else
		CType.evaluation_error(identifier)
	end
end

#get_membersObject



114
115
116
# File 'lib/dbc/ctype.rb', line 114

def get_members
	@members.dup
end

#keywordObject



130
131
132
133
134
135
136
# File 'lib/dbc/ctype.rb', line 130

def keyword
	if self.class == Struct
		'struct'
	elsif self.class == Union
		'union'
	end
end

#lookup(identifier) ⇒ Object



108
109
110
111
112
# File 'lib/dbc/ctype.rb', line 108

def lookup(identifier)
	@members.find do |m|
		identifier == m.identifier
	end
end

#to_def(indent = '') ⇒ Object

to_def is pretty much depreciated - might be useful for debugging in the future to_def methods would have to be added to all classes in this module though.



150
151
152
153
154
155
156
157
# File 'lib/dbc/ctype.rb', line 150

def to_def(indent='')
	str =	to_s
	str << " {\n"
	@members.each do |m|
		str << indent << "\t" << m.to_def(indent + "\t") << ";\n"
	end
	str << indent << '}'
end

#to_init_s(ident = nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/dbc/ctype.rb', line 140

def to_init_s(ident = nil)
	str = ''
	str << keyword
	str << ' ' << @name if @name
	str << ' ' << ident if ident
	str # make sure we don't return nil
end

#to_sObject



137
138
139
# File 'lib/dbc/ctype.rb', line 137

def to_s
	to_init_s()
end