Class: Rubber::C_Flags

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

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/flags.rb', line 5

def child_names
  @child_names
end

Instance Method Details

#code(io) ⇒ Object



14
15
# File 'lib/rubber/codegen/flags.rb', line 14

def code(io)
end

#declare(io) ⇒ Object



16
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
233
234
235
236
# File 'lib/rubber/codegen/flags.rb', line 16

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

static VALUE flagsBaseClass;

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

static VALUE make_flags_value(VALUE klass, int value, char *name, char *fullname)
{
	FlagsData *data = NULL;
	
	data = ALLOC(FlagsData);
	data->value = value;
	data->name = name;
	data->fullname = fullname;
	
	return Data_Wrap_Struct(klass, NULL, free, data);
}
static int flags_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, flagsBaseClass))
{
	FlagsData *data = NULL;
	
	if ((klass != Qnil) && (!rb_obj_is_kind_of(value, klass)))
	{
		rb_raise(rb_eTypeError, \"Wrong type of flags  %s (%s required)\", rb_obj_classname(value), rb_class2name(klass));
	}
	
	Data_Get_Struct(value, FlagsData, data);
	return data->value;
}
	break;
	}
	return 0;
	
}

static VALUE rubber_flags_inspect(VALUE value)
{
	FlagsData *data = NULL;
	volatile VALUE str = rb_str_new(\"#<\", 2);
	char number[16] = \"\"; 
	
	Data_Get_Struct(value, FlagsData, 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_flags_to_s(VALUE value)
{
	FlagsData *data = NULL;
	
	Data_Get_Struct(value, FlagsData, data);
	
	return rb_str_new2(data->fullname);
}
static VALUE rubber_flags_name(VALUE value)
{
	FlagsData *data = NULL;
	
	Data_Get_Struct(value, FlagsData, data);
	
	return rb_str_new2(data->name);
}

static VALUE rubber_flags_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_flags_to_i(VALUE value)
{
	FlagsData *data = NULL;
	
	Data_Get_Struct(value, FlagsData, data);
	
	return INT2FIX(data->value);
}

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

static VALUE rubber_flags_and(VALUE value, VALUE other)
{
	FlagsData *data = NULL;
	int original = 0;
	int other_num = 0;
	
	Data_Get_Struct(value, FlagsData, data);

	original = data->value;

	other_num = flags_value_to_int(value, CLASS_OF(value));
	
//	return INT2NUM(original & other_num);
	return make_flags_value(CLASS_OF(value), original & other_num, "", "");
}

static VALUE rubber_flags_or(VALUE value, VALUE other)
{
	FlagsData *data = NULL;
	int original = 0;
	int other_num = 0;
	
	Data_Get_Struct(value, FlagsData, data);

	original = data->value;

	other_num = flags_value_to_int(value, CLASS_OF(value));
	
	return make_flags_value(CLASS_OF(value), original | other_num, "", "");
}



EOHEADER
  end
  args.each do |arg|
    io.puts "static VALUE #{default_cname}_#{arg} = Qnil;"
  end
io.puts <<-EO_INSPECT
	static VALUE rubber_#{default_cname}_flags_inspect(VALUE value)
{
	FlagsData *data = NULL;
	volatile VALUE str = rb_str_new(\"#<\", 2);
	char number[16] = ""; 
	int c=0;
	
	Data_Get_Struct(value, FlagsData, data);
	
	rb_str_cat2(str, rb_obj_classname(value));
	rb_str_cat2(str, " - ");
EO_INSPECT
	args.each do |arg|
uniq = arg[@strip..-1]
io.puts <<-EOARG 
if ((data->value & #{arg})==#{arg}) {
	if (c>0)
		rb_str_cat2(str, ", ");
	rb_str_cat2(str, #{uniq.downcase.gsub(/_/,'-').inspect});
	c ++;
}
EOARG
	end
io.puts <<-EO_INSPECT
	rb_str_cat2(str, " (");
	sprintf(number, "%i", data->value);
	rb_str_cat2(str, number);
	rb_str_cat2(str, ")>");
	
	return str;
}
EO_INSPECT

  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 flags_#{name}_to_ruby(int value)
__attribute__ ((unused))
;
static int flags_ruby_to_#{name}(VALUE val)
__attribute__ ((unused))
;
#endif

"
  io.puts "static VALUE flags_#{name}_to_ruby(int value) { switch(value) {"
  args.each do |arg|
    io.puts "    case #{arg}: return #{default_cname}_#{arg};"
  end    
  io.puts <<-EOB
	}; return make_flags_value(#{cname}, value, "various", "Various"); }
	static int flags_ruby_to_#{name}(VALUE val) { return flags_value_to_int(val, #{cname}); }
	EOB
end

#default_cnameObject



238
239
240
# File 'lib/rubber/codegen/flags.rb', line 238

def default_cname
  "flags"+name
end

#doc_rd(io) ⇒ Object



241
242
243
244
# File 'lib/rubber/codegen/flags.rb', line 241

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

#fullnameObject



248
249
250
251
252
253
254
# File 'lib/rubber/codegen/flags.rb', line 248

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

#get_rootObject



245
# File 'lib/rubber/codegen/flags.rb', line 245

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

#initObject



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

def init()
  ($custom_maps[name] ||= {})["VALUE"] = "flags_#{name}_to_ruby((%%))"
  ($custom_maps["VALUE"] ||= {})[name] = "flags_ruby_to_#{name}((%%))"
  @splits = strip_prefixes(args)
  @strip = args.first.length - @splits.first.length
end

#is_root?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/rubber/codegen/flags.rb', line 245

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

#register(io, already_defined = false) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rubber/codegen/flags.rb', line 273

def register(io, already_defined=false)
  unless @@declared_register
  	@@declared_register = true
    io.puts "  flagsBaseClass = rb_define_class(\"Flags\", rb_cObject);"
    io.puts '    rb_define_method(flagsBaseClass, "inspect", rubber_flags_inspect, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "to_i", rubber_flags_to_i, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "coerce", rubber_flags_coerce, 1);'
    io.puts '    rb_define_method(flagsBaseClass, "to_s", rubber_flags_to_s, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "to_str", rubber_flags_to_s, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "fullname", rubber_flags_to_s, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "name", rubber_flags_name, 0);'
    io.puts '    rb_define_method(flagsBaseClass, "<=>", rubber_flags_cmp, 0);'
 io.puts '    rb_define_method(flagsBaseClass, "&", rubber_flags_and, 1);'
 io.puts '    rb_define_method(flagsBaseClass, "|", rubber_flags_or, 1);'
    io.puts '    '
  end
  if parent
    io.puts "  #{cname} = rb_define_class_under(#{parent.cname}, #{name.inspect}, flagsBaseClass);"
  else
    io.puts "  #{cname} = rb_define_class(#{name.inspect}, flagsBaseClass);"
  end
	io.puts '    rb_define_method('+cname+', "inspect", rubber_'+default_cname+'_flags_inspect, 0);'

  
  args.each do |arg|
    uniq = arg[@strip..-1]
    io.puts "    #{default_cname}_#{arg} = make_flags_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



255
256
257
258
259
260
# File 'lib/rubber/codegen/flags.rb', line 255

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

#strip_prefixes(arr) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/rubber/codegen/flags.rb', line 261

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