Class: Rubber::C_Enum

Inherits:
Object
  • Object
show all
Includes:
RegisterChildren
Defined in:
lib/rubber/codegen/enum.rb

Direct Known Subclasses

C_GEnum, C_GFlags

Constant Summary collapse

@@declared_base =
false
@@declared_register =
false

Instance Attribute Summary collapse

Attributes included from RegisterChildren

#source_file, #source_line

Instance Method Summary collapse

Methods included from RegisterChildren

#cname, #register_children

Instance Attribute Details

#child_namesObject (readonly)

Returns the value of attribute child_names.



5
6
7
# File 'lib/rubber/codegen/enum.rb', line 5

def child_names
  @child_names
end

Instance Method Details

#code(io) ⇒ Object



15
16
# File 'lib/rubber/codegen/enum.rb', line 15

def code(io)
end

#declare(io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubber/codegen/enum.rb', line 17

def declare(io)
  io.puts "static VALUE #{cname};"
  unless @@declared_base
  	@@declared_base = true
  	io.puts "

static VALUE enumBaseClass;

typedef struct {
	int value;
	char *name;
	char *fullname;
} EnumData;

static VALUE make_enum_value(VALUE klass, int value, char *name, char *fullname)
{
	EnumData *data = NULL;
	
	data = ALLOC(EnumData);
	data->value = value;
	data->name = name;
	data->fullname = fullname;
	
	return Data_Wrap_Struct(klass, NULL, free, data);
}
static int enum_value_to_int(VALUE value, VALUE klass)
{
	switch (TYPE(value))
	{
	case T_FIXNUM:
	case T_FLOAT:
return NUM2INT(value);
	break;
	case T_DATA:
if (rb_obj_is_kind_of(value, enumBaseClass))
{
	EnumData *data = NULL;
	
	if ((klass != Qnil) && (!rb_obj_is_kind_of(value, klass)))
	{
		rb_raise(rb_eTypeError, \"Wrong type of enum  %s (%s required)\", rb_obj_classname(value), rb_class2name(klass));
	}
	
	Data_Get_Struct(value, EnumData, data);
	return data->value;
}
	break;
	}
	return 0;
	
}

static VALUE rubber_enum_inspect(VALUE value)
{
	EnumData *data = NULL;
	volatile VALUE str = rb_str_new(\"#<\", 2);
	char number[16] = \"\"; 
	
	Data_Get_Struct(value, EnumData, data);
	
	rb_str_cat2(str, rb_obj_classname(value));
	rb_str_cat2(str, \" - \");
	rb_str_cat2(str, data->name);
	rb_str_cat2(str, \"(\");
	sprintf(number, \"%i\", data->value);
	rb_str_cat2(str, number);
	rb_str_cat2(str, \")>\");
	
	return str;
}

static VALUE rubber_enum_to_s(VALUE value)
{
	EnumData *data = NULL;
	
	Data_Get_Struct(value, EnumData, data);
	
	return rb_str_new2(data->fullname);
}
static VALUE rubber_enum_name(VALUE value)
{
	EnumData *data = NULL;
	
	Data_Get_Struct(value, EnumData, data);
	
	return rb_str_new2(data->name);
}

static VALUE rubber_enum_cmp(VALUE value, VALUE other)
{
	VALUE a,b;
	a = rb_funcall(value, rb_intern(\"to_i\"), 0);
	b = rb_funcall(other, rb_intern(\"to_i\"), 0);
#ifdef RB_NUM_COERCE_FUNCS_NEED_OPID
	return rb_num_coerce_cmp(a, b, rb_intern(\"==\"));
#else
	return rb_num_coerce_cmp(a, b);
#endif
}

static VALUE rubber_enum_to_i(VALUE value)
{
	EnumData *data = NULL;
	
	Data_Get_Struct(value, EnumData, data);
	
	return INT2FIX(data->value);
}

static VALUE rubber_enum_coerce(VALUE value, VALUE other)
{
	EnumData *data = NULL;
	
	Data_Get_Struct(value, EnumData, data);
	
	switch(TYPE(other))
	{
	case T_FIXNUM:
	case T_BIGNUM:
return INT2FIX(data->value);
	case T_FLOAT:
return rb_float_new(data->value);
	default:
return Qnil;
	}
}

"
  end
  args.each do |arg|
    io.puts "static VALUE #{default_cname}_#{arg} = Qnil;"
  end    
  io.puts "typedef int #{name};
#ifdef __GNUC__
// No point in declaring these unless we're using GCC
// They're ahead of any code that uses them anyway.
static VALUE enum_#{name}_to_ruby(int value)
__attribute__ ((unused))
;
static int enum_ruby_to_#{name}(VALUE val)
__attribute__ ((unused))
;
#endif

"
  io.puts "static VALUE enum_#{name}_to_ruby(int value) { switch(value) {"
  args.each do |arg|
    io.puts "    case #{arg}: return #{default_cname}_#{arg};"
  end    
  io.puts "}; return Qnil; }"
  io.puts "static int enum_ruby_to_#{name}(VALUE val) { return enum_value_to_int(val, #{cname}); }"
end

#default_cnameObject



170
171
172
# File 'lib/rubber/codegen/enum.rb', line 170

def default_cname
  "enum"+name
end

#doc_rd(io) ⇒ Object



173
174
175
176
# File 'lib/rubber/codegen/enum.rb', line 173

def doc_rd(io)
  depth = (fullname.gsub(/[^:]/,'').size >> 1)
  io.puts "=#{'=' * depth} enum #{fullname}"
end

#fullnameObject



180
181
182
183
184
185
186
# File 'lib/rubber/codegen/enum.rb', line 180

def fullname()
  if parent and parent.respond_to?(:fullname)
    "#{parent.fullname}::#{name}"
  else
    name
  end
end

#get_rootObject



177
# File 'lib/rubber/codegen/enum.rb', line 177

def get_root(); is_root? ? self : parent.get_root; end

#initObject



8
9
10
11
12
13
14
# File 'lib/rubber/codegen/enum.rb', line 8

def init()
  ($custom_maps[name] ||= {})["VALUE"] = "enum_#{name}_to_ruby((%%))"
  ($custom_maps["VALUE"] ||= {})[name] = "enum_ruby_to_#{name}((%%))"
  STDERR.puts "Auto-adding custom map for #{name}"
  @splits = strip_prefixes(args)
  @strip = args.first.length - @splits.first.length
end

#is_root?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/rubber/codegen/enum.rb', line 177

def is_root?()
  not parent.respond_to?(:fullname)
end

#register(io, already_defined = false) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/rubber/codegen/enum.rb', line 205

def register(io, already_defined=false)
  unless @@declared_register
  	@@declared_register = true
    io.puts "  enumBaseClass = rb_define_class(\"Enum\", rb_cObject);"
    io.puts '    rb_define_method(enumBaseClass, "inspect", rubber_enum_inspect, 0);'
    io.puts '    rb_define_method(enumBaseClass, "to_i", rubber_enum_to_i, 0);'
    io.puts '    rb_define_method(enumBaseClass, "coerce", rubber_enum_coerce, 1);'
    io.puts '    rb_define_method(enumBaseClass, "to_s", rubber_enum_to_s, 0);'
    io.puts '    rb_define_method(enumBaseClass, "to_str", rubber_enum_to_s, 0);'
    io.puts '    rb_define_method(enumBaseClass, "fullname", rubber_enum_to_s, 0);'
    io.puts '    rb_define_method(enumBaseClass, "name", rubber_enum_name, 0);'
    io.puts '    rb_define_method(enumBaseClass, "<=>", rubber_enum_cmp, 0);'
    io.puts '    '
  end
  if parent
    io.puts "  #{cname} = rb_define_class_under(#{parent.cname}, #{name.inspect}, enumBaseClass);"
  else
    io.puts "  #{cname} = rb_define_class(#{name.inspect}, enumBaseClass);"
  end
  
  args.each do |arg|
    uniq = arg[@strip..-1]
    uniq.sub!(/\A[^a-zA-Z]/,'')
    io.puts "    #{default_cname}_#{arg} = make_enum_value(#{cname}, #{arg}, #{uniq.downcase.gsub(/_/,'-').inspect}, #{arg.inspect});"
    io.puts "    rb_obj_freeze(#{default_cname}_#{arg});"
    io.puts "    rb_define_const(#{cname}, #{uniq.upcase.inspect}, #{default_cname}_#{arg});"
  end
end

#same_prefix(arr) ⇒ Object



187
188
189
190
191
192
# File 'lib/rubber/codegen/enum.rb', line 187

def same_prefix(arr)
  for i in arr
    return false if arr.first.first != i.first
  end
  return true
end

#strip_prefixes(arr) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/rubber/codegen/enum.rb', line 193

def strip_prefixes(arr)
  splits = arr.collect { |i| i.strip.split(/_/) }
  while (same_prefix(splits))
    splits.each { |i| i.shift }
  end
  splits.collect!{|i| i.join('_') }
  splits
end