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
|
# File 'lib/rubber/codegen/flags.rb', line 16
def declare(io)
io.puts "static VALUE #{cname};"
unless @@declared_base
@@declared_base = true
io.puts "\nstatic VALUE flagsBaseClass;\n\ntypedef struct {\n int value;\n char *name;\n char *fullname;\n} FlagsData;\n\nstatic VALUE make_flags_value(VALUE klass, int value, char *name, char *fullname)\n{\n FlagsData *data = NULL;\n \n data = ALLOC(FlagsData);\n data->value = value;\n data->name = name;\n data->fullname = fullname;\n \n return Data_Wrap_Struct(klass, NULL, free, data);\n}\nstatic int flags_value_to_int(VALUE value, VALUE klass)\n{\n switch (TYPE(value))\n {\n case T_FIXNUM:\n case T_FLOAT:\nreturn NUM2INT(value);\n break;\n case T_DATA:\nif (rb_obj_is_kind_of(value, flagsBaseClass))\n{\n FlagsData *data = NULL;\n \n if ((klass != Qnil) && (!rb_obj_is_kind_of(value, klass)))\n {\n rb_raise(rb_eTypeError, \\\"Wrong type of flags %s (%s required)\\\", rb_obj_classname(value), rb_class2name(klass));\n }\n \n Data_Get_Struct(value, FlagsData, data);\n return data->value;\n}\n break;\n }\n return 0;\n \n}\n\nstatic VALUE rubber_flags_inspect(VALUE value)\n{\n FlagsData *data = NULL;\n volatile VALUE str = rb_str_new(\\\"#<\\\", 2);\n char number[16] = \\\"\\\"; \n \n Data_Get_Struct(value, FlagsData, data);\n \n rb_str_cat2(str, rb_obj_classname(value));\n rb_str_cat2(str, \\\" - \\\");\n rb_str_cat2(str, data->name);\n rb_str_cat2(str, \\\"(\\\");\n sprintf(number, \\\"%i\\\", data->value);\n rb_str_cat2(str, number);\n rb_str_cat2(str, \\\")>\\\");\n \n return str;\n}\n\nstatic VALUE rubber_flags_to_s(VALUE value)\n{\n FlagsData *data = NULL;\n \n Data_Get_Struct(value, FlagsData, data);\n \n return rb_str_new2(data->fullname);\n}\nstatic VALUE rubber_flags_name(VALUE value)\n{\n FlagsData *data = NULL;\n \n Data_Get_Struct(value, FlagsData, data);\n \n return rb_str_new2(data->name);\n}\n\nstatic VALUE rubber_flags_cmp(VALUE value, VALUE other)\n{\n VALUE a,b;\n a = rb_funcall(value, rb_intern(\\\"to_i\\\"), 0);\n b = rb_funcall(other, rb_intern(\\\"to_i\\\"), 0);\n return rb_num_coerce_cmp(a, b);\n}\n\nstatic VALUE rubber_flags_to_i(VALUE value)\n{\n FlagsData *data = NULL;\n \n Data_Get_Struct(value, FlagsData, data);\n \n return INT2FIX(data->value);\n}\n\nstatic VALUE rubber_flags_coerce(VALUE value, VALUE other)\n{\n FlagsData *data = NULL;\n \n Data_Get_Struct(value, FlagsData, data);\n \n switch(TYPE(other))\n {\n case T_FIXNUM:\n case T_BIGNUM:\nreturn INT2FIX(data->value);\n case T_FLOAT:\nreturn Qnil;\n default:\nreturn Qnil;\n }\n}\n\nstatic VALUE rubber_flags_and(VALUE value, VALUE other)\n{\n FlagsData *data = NULL;\n int original = 0;\n int other_num = 0;\n \n Data_Get_Struct(value, FlagsData, data);\n\n original = data->value;\n\n other_num = flags_value_to_int(value, CLASS_OF(value));\n \n// return INT2NUM(original & other_num);\n return make_flags_value(CLASS_OF(value), original & other_num, \"\", \"\");\n}\n\nstatic VALUE rubber_flags_or(VALUE value, VALUE other)\n{\n FlagsData *data = NULL;\n int original = 0;\n int other_num = 0;\n \n Data_Get_Struct(value, FlagsData, data);\n\n original = data->value;\n\n other_num = flags_value_to_int(value, CLASS_OF(value));\n \n return make_flags_value(CLASS_OF(value), original | other_num, \"\", \"\");\n}\n\n\n\n"
end
args.each do |arg|
io.puts "static VALUE #{default_cname}_#{arg} = Qnil;"
end
io.puts " static VALUE rubber_\#{default_cname}_flags_inspect(VALUE value)\n{\n FlagsData *data = NULL;\n volatile VALUE str = rb_str_new(\\\"#<\\\", 2);\n char number[16] = \"\"; \n int c=0;\n \n Data_Get_Struct(value, FlagsData, data);\n \n rb_str_cat2(str, rb_obj_classname(value));\n rb_str_cat2(str, \" - \");\n"
args.each do |arg|
uniq = arg[@strip..-1]
io.puts "if ((data->value & \#{arg})==\#{arg}) {\n if (c>0)\n rb_str_cat2(str, \", \");\n rb_str_cat2(str, \#{uniq.downcase.gsub(/_/,'-').inspect});\n c ++;\n}\n"
end
io.puts " rb_str_cat2(str, \" (\");\n sprintf(number, \"%i\", data->value);\n rb_str_cat2(str, number);\n rb_str_cat2(str, \")>\");\n \n return str;\n}\n"
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 " }; return make_flags_value(\#{cname}, value, \"various\", \"Various\"); }\n static int flags_ruby_to_\#{name}(VALUE val) { return flags_value_to_int(val, \#{cname}); }\n EOB\nend\n"
|