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
|