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.



94
95
96
97
# File 'lib/dbc/ctype.rb', line 94

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dbc/ctype.rb', line 78

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



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

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

#evaluate(identifier) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dbc/ctype.rb', line 113

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



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

def get_members
	@members.dup
end

#keywordObject



125
126
127
128
129
130
131
# File 'lib/dbc/ctype.rb', line 125

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

#lookup(identifier) ⇒ Object



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

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.



145
146
147
148
149
150
151
152
# File 'lib/dbc/ctype.rb', line 145

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



135
136
137
138
139
140
141
# File 'lib/dbc/ctype.rb', line 135

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



132
133
134
# File 'lib/dbc/ctype.rb', line 132

def to_s
	to_init_s()
end