rucy 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.doc/ext/rucy/class.cpp +48 -56
  3. data/.doc/ext/rucy/exception.cpp +1 -1
  4. data/.doc/ext/rucy/function.cpp +14 -2
  5. data/.doc/ext/rucy/struct.cpp +2 -20
  6. data/.doc/ext/rucy/tester.cpp +7 -19
  7. data/.doc/ext/rucy/value.cpp +23 -1
  8. data/{README → README.md} +0 -0
  9. data/Rakefile +7 -5
  10. data/VERSION +1 -1
  11. data/bin/rucy2rdoc +8 -5
  12. data/ext/rucy/class.cpp +78 -87
  13. data/ext/rucy/class.h +12 -6
  14. data/ext/rucy/exception.cpp +17 -17
  15. data/ext/rucy/extconf.rb +11 -52
  16. data/ext/rucy/function.cpp +25 -12
  17. data/ext/rucy/struct.cpp +8 -26
  18. data/ext/rucy/tester.cpp +11 -24
  19. data/ext/rucy/value.cpp +32 -9
  20. data/include/rucy/class.h +5 -3
  21. data/include/rucy/exception.h +17 -71
  22. data/include/rucy/extension.h.erb +489 -0
  23. data/include/rucy/function.h.erb +20 -34
  24. data/include/rucy/module.h.erb +20 -14
  25. data/include/rucy/ruby.h +23 -0
  26. data/include/rucy/rucy.h +7 -66
  27. data/include/rucy/symbol.h +11 -11
  28. data/include/rucy/value.h.erb +98 -176
  29. data/include/rucy.h +9 -1
  30. data/lib/rucy/module.rb +11 -7
  31. data/rucy.gemspec +3 -4
  32. data/src/class.cpp +34 -6
  33. data/src/exception.cpp +69 -54
  34. data/src/extension.cpp +59 -0
  35. data/src/function.cpp.erb +17 -25
  36. data/src/module.cpp.erb +25 -16
  37. data/src/rucy.cpp +15 -25
  38. data/src/symbol.cpp +18 -17
  39. data/src/value.cpp.erb +374 -175
  40. data/task/doc.rake +5 -0
  41. data/test/helper.rb +6 -2
  42. data/test/test_class.rb +27 -20
  43. data/test/test_function.rb +6 -0
  44. data/test/test_value.rb +4 -0
  45. metadata +29 -39
  46. data/.gitignore +0 -22
  47. data/ChangeLog +0 -13
  48. data/include/rucy/defs.h.erb +0 -76
  49. data/include/rucy/extension.h +0 -206
data/src/value.cpp.erb CHANGED
@@ -3,287 +3,303 @@
3
3
 
4
4
 
5
5
  #include <assert.h>
6
- #include <rucy/rucy.h>
7
- #include <rucy/function.h>
8
- #include <rucy/exception.h>
6
+ #include "rucy/function.h"
7
+ #include "rucy/exception.h"
9
8
 
10
9
 
11
10
  namespace Rucy
12
11
  {
13
12
 
14
13
 
15
- Value::Value ()
16
- : v(Qnil)
14
+ static void
15
+ check_type (RubyValue value, RubyValueType type)
17
16
  {
17
+ return rb_check_type(value, type);
18
18
  }
19
19
 
20
- Value::Value (bool b)
21
- : v(b ? Qtrue : Qfalse)
20
+ static bool
21
+ test_value (RubyValue value)
22
22
  {
23
+ return RTEST(value); //!NIL_P(v) && v != Qfalse;
23
24
  }
24
25
 
25
- Value::Value (int n)
26
- : v(INT2FIX(n))
27
- {
28
- }
29
26
 
30
- Value::Value (float f)
31
- : v(rb_float_new(f))
27
+ Value::Value ()
28
+ : val(nil())
32
29
  {
33
30
  }
34
31
 
35
- Value::Value (double d)
36
- : v(rb_float_new(d))
32
+ Value::Value (bool b)
33
+ : val(b ? Qtrue : Qfalse)
37
34
  {
38
35
  }
39
36
 
40
- Value::Value (const char* s, bool tainted)
41
- : v(!s ? Qnil : tainted ? rb_tainted_str_new2(s) : rb_str_new2(s))
37
+ Value::Value (int n)
38
+ : val(INT2NUM(n))
42
39
  {
43
40
  }
44
41
 
45
- Value::Value (const char* s, size_t len, bool tainted)
46
- : v(!s ? Qnil : tainted ? rb_tainted_str_new(s, len) : rb_str_new(s, len))
42
+ Value::Value (unsigned int n)
43
+ : val(UINT2NUM(n))
47
44
  {
48
45
  }
49
46
 
50
- Value::Value (size_t size, const Value* values)
51
- : v(size > 0 ? rb_ary_new2(size) : rb_ary_new())
47
+ Value::Value (float n)
48
+ : val(rb_float_new(n))
52
49
  {
53
- if (!values) return;
54
- for (size_t i = 0; i < size; ++i)
55
- push(values[i]);
56
50
  }
57
51
 
58
- Value::Value (VALUE v)
59
- : v(v)
52
+ Value::Value (double n)
53
+ : val(rb_float_new(n))
60
54
  {
61
55
  }
62
56
 
63
- VALUE
64
- Value::value () const
57
+ Value::Value (const char* s)
58
+ : val(s ? rb_str_new2(s) : Qnil)
65
59
  {
66
- return v;
60
+ if (!s)
61
+ argument_error(__FILE__, __LINE__);
67
62
  }
68
63
 
69
- void
70
- Value::mark () const
64
+ Value::Value (const char* s, size_t len)
65
+ : val(s ? rb_str_new(s, len) : Qnil)
71
66
  {
72
- rb_gc_mark(v);
67
+ if (!s)
68
+ argument_error(__FILE__, __LINE__);
73
69
  }
74
70
 
75
- int
76
- Value::type () const
71
+ Value::Value (size_t size, const Value* array)
77
72
  {
78
- return TYPE(v);
73
+ if (size == 0)
74
+ {
75
+ val = rb_ary_new();
76
+ }
77
+ else if (array)
78
+ {
79
+ val = rb_ary_new4(size, (const RubyValue*) array);
80
+ }
81
+ else
82
+ {
83
+ val = rb_ary_new2(size);
84
+ }
79
85
  }
80
86
 
81
- Value
82
- Value::klass () const
87
+ Value::Value (RubyValue v)
88
+ : val(v)
83
89
  {
84
- return RBASIC(v)->klass;
85
90
  }
86
91
 
87
92
  bool
88
- Value::is_kind_of (Value klass) const
89
- {
90
- SYM_Q(kind_of);
91
- return call(kind_of, klass);
92
- }
93
-
94
- static int
95
- get_object_size (Value obj, Symbol method)
93
+ Value::is_i () const
96
94
  {
97
- if (obj.type() == T_STRING)
98
- return (int) RSTRING_LEN(obj.value());
99
- else if (obj.type() == T_ARRAY)
100
- return (int) RARRAY_LEN(obj.value());
101
- else
102
- return obj.call(method);
95
+ return FIXNUM_P(val) || type() == RUBY_T_BIGNUM || is_kind_of(rb_cInteger);
103
96
  }
104
97
 
105
- int
106
- Value::size () const
98
+ bool
99
+ Value::is_f () const
107
100
  {
108
- SYM(size);
109
- return get_object_size(v, size);
101
+ return RB_FLOAT_TYPE_P(val) || is_kind_of(rb_cFloat);
110
102
  }
111
103
 
112
- int
113
- Value::length () const
104
+ bool
105
+ Value::is_s () const
114
106
  {
115
- SYM(length);
116
- return get_object_size(v, length);
107
+ return type() == RUBY_T_STRING || is_kind_of(rb_cString);
117
108
  }
118
109
 
119
- Value
120
- Value::to_i () const
110
+ bool
111
+ Value::is_sym () const
121
112
  {
122
- if (FIXNUM_P(v) || TYPE(v) == T_BIGNUM) return *this;
123
- SYM(to_i);
124
- return call(to_i);
113
+ return SYMBOL_P(val) || is_kind_of(rb_cSymbol);
125
114
  }
126
115
 
127
- Value
128
- Value::to_f () const
116
+ bool
117
+ Value::is_array () const
129
118
  {
130
- if (TYPE(v) == T_FLOAT) return *this;
131
- SYM(to_f);
132
- return call(to_f);
119
+ return type() == RUBY_T_ARRAY || is_kind_of(rb_cArray);
133
120
  }
134
121
 
135
- Value
136
- Value::to_s () const
122
+ bool
123
+ Value::is_hash () const
137
124
  {
138
- if (TYPE(v) == T_STRING) return *this;
139
- SYM(to_s);
140
- return call(to_s);
125
+ return type() == RUBY_T_HASH || is_kind_of(rb_cHash);
141
126
  }
142
127
 
143
- Value
144
- Value::to_sym () const
128
+ bool
129
+ Value::is_nil () const
145
130
  {
146
- if (TYPE(v) == T_SYMBOL) return *this;
147
- SYM(to_sym);
148
- return call(to_sym);
131
+ return NIL_P(val);
149
132
  }
150
133
 
151
134
  int
152
135
  Value::as_i (bool convert) const
153
136
  {
154
- return to<int>(*this, convert);
137
+ return as<int>(convert);
155
138
  }
156
139
 
157
140
  double
158
141
  Value::as_f (bool convert) const
159
142
  {
160
- return to<double>(*this, convert);
143
+ return as<double>(convert);
161
144
  }
162
145
 
163
146
  const char*
164
147
  Value::as_s (bool convert) const
165
148
  {
166
- return to<const char*>(*this, convert);
149
+ return as<const char*>(convert);
167
150
  }
168
151
 
169
152
  Symbol
170
153
  Value::as_sym (bool convert) const
171
154
  {
172
- return to<Symbol>(*this, convert);
155
+ return as<Symbol>(convert);
173
156
  }
174
157
 
175
- bool
176
- Value::is_i () const
158
+ Value*
159
+ Value::as_array (bool convert)
177
160
  {
178
- return is_kind_of(rb_cFixnum);
161
+ return as<Value*>(convert);
179
162
  }
180
163
 
181
- bool
182
- Value::is_f () const
164
+ const Value*
165
+ Value::as_array (bool convert) const
183
166
  {
184
- return is_kind_of(rb_cFloat);
167
+ return as<const Value*>(convert);
185
168
  }
186
169
 
187
- bool
188
- Value::is_s () const
170
+ Value
171
+ Value::to_i () const
189
172
  {
190
- return is_kind_of(rb_cString);
173
+ if (is_i()) return *this;
174
+ RUCY_SYM(to_i);
175
+ return call(to_i);
191
176
  }
192
177
 
193
- bool
194
- Value::is_sym () const
178
+ Value
179
+ Value::to_f () const
195
180
  {
196
- return is_kind_of(rb_cSymbol);
181
+ if (is_f()) return *this;
182
+ RUCY_SYM(to_f);
183
+ return call(to_f);
197
184
  }
198
185
 
199
- bool
200
- Value::is_a () const
186
+ Value
187
+ Value::to_s () const
201
188
  {
202
- return is_kind_of(rb_cArray);
189
+ if (is_s()) return *this;
190
+ RUCY_SYM(to_s);
191
+ return call(to_s);
203
192
  }
204
193
 
205
- bool
206
- Value::is_nil () const
194
+ Value
195
+ Value::to_sym () const
196
+ {
197
+ if (is_sym()) return *this;
198
+ RUCY_SYM(to_sym);
199
+ return call(to_sym);
200
+ }
201
+
202
+ Value
203
+ Value::to_array () const
207
204
  {
208
- return NIL_P(v);
205
+ if (is_array()) return *this;
206
+ RUCY_SYM(to_a);
207
+ return call(to_a);
209
208
  }
210
209
 
211
210
  % ["call", "operator ()"].each do |op|
212
211
  Value
213
212
  Value::<%= op %> (Symbol name, int argc, const Value* argv) const
214
213
  {
215
- return protect(rb_funcall2, value(), name.id(), argc, (const VALUE*) argv);
214
+ return protect(rb_funcall2, val, name.symbol(), argc, (const RubyValue*) argv);
216
215
  }
217
216
  % NTIMES.each do |n|
218
217
  Value
219
218
  Value::<%= op %> (Symbol name<%= params(n) {|i| ", Value v#{i}"} %>) const
220
219
  {
221
- const VALUE args[] = {<%= params(n, ", ") {|i| "v#{i}"} %>};
222
- return protect(rb_funcall2, value(), name.id(), <%= n %>, args);
220
+ const RubyValue args[] = {<%= params(n, ', ') {|i| "v#{i}"} %>};
221
+ return protect(rb_funcall2, val, name.symbol(), <%= n %>, args);
223
222
  }
224
223
  % end
225
224
  % end
226
225
 
227
- Value
228
- Value::inspect () const
226
+ void
227
+ Value::mark () const
229
228
  {
230
- SYM(inspect);
231
- return call(inspect);
229
+ rb_gc_mark(val);
232
230
  }
233
231
 
234
- const char*
235
- Value::c_str () const
232
+ RubyValue
233
+ Value::value () const
236
234
  {
237
- return value_to<const char*>(*this);
235
+ return val;
238
236
  }
239
237
 
240
- Value&
241
- Value::operator [] (int i)
238
+ RubyValueType
239
+ Value::type () const
242
240
  {
243
- if (type() != T_ARRAY) type_error();
244
- return *(Value*) &RARRAY_PTR(v)[i];
241
+ return TYPE(val);
245
242
  }
246
243
 
247
- const Value&
248
- Value::operator [] (int i) const
244
+ Value::operator RubyValue () const
249
245
  {
250
- return const_cast<Value*>(this)->operator[](i);
246
+ return val;
251
247
  }
252
248
 
253
- Value::operator VALUE () const
249
+ Value::operator bool () const
254
250
  {
255
- return value();
251
+ return test_value(val);
256
252
  }
257
253
 
258
- static bool
259
- test_value (VALUE v)
254
+ bool
255
+ Value::operator ! () const
260
256
  {
261
- return !NIL_P(v) && v != Qfalse;// RTEST(v);
257
+ return !operator bool();
262
258
  }
263
259
 
264
- Value::operator bool () const
260
+ bool
261
+ Value::operator == (const Value& rhs) const
265
262
  {
266
- return test_value(v);
263
+ return operator==(rhs.val);
267
264
  }
268
265
 
269
266
  bool
270
- Value::operator ! () const
267
+ Value::operator == (RubyValue rhs) const
271
268
  {
272
- return !operator bool();
269
+ return val == rhs;// test_value(rb_obj_equal(val, rhs);
273
270
  }
274
271
 
275
272
  bool
276
- Value::operator == (const Value& rhs) const
273
+ Value::operator != (const Value& rhs) const
277
274
  {
278
- return v == rhs.v;// test_value(rb_obj_equal(v, rhs.v);
275
+ return !operator==(rhs);
279
276
  }
280
277
 
281
278
  bool
282
- Value::operator != (const Value& rhs) const
279
+ Value::operator != (RubyValue rhs) const
283
280
  {
284
281
  return !operator==(rhs);
285
282
  }
286
283
 
284
+ Value
285
+ Value::klass () const
286
+ {
287
+ return RBASIC(val)->klass;
288
+ }
289
+
290
+ bool
291
+ Value::is_kind_of (Value klass) const
292
+ {
293
+ return rb_obj_is_kind_of(val, klass);
294
+ }
295
+
296
+ Value
297
+ Value::inspect () const
298
+ {
299
+ RUCY_SYM(inspect);
300
+ return call(inspect);
301
+ }
302
+
287
303
  Value
288
304
  Value::push (Value obj)
289
305
  {
@@ -308,6 +324,49 @@ namespace Rucy
308
324
  return rb_ary_unshift(value(), obj.value());
309
325
  }
310
326
 
327
+ Value&
328
+ Value::operator [] (int i)
329
+ {
330
+ return as_array()[i];
331
+ }
332
+
333
+ const Value&
334
+ Value::operator [] (int i) const
335
+ {
336
+ return const_cast<Value*>(this)->operator[](i);
337
+ }
338
+
339
+ const char*
340
+ Value::c_str () const
341
+ {
342
+ return as<const char*>(true);
343
+ }
344
+
345
+ static int
346
+ get_length (const Value& value, RubySymbol symbol)
347
+ {
348
+ if (value.type() == RUBY_T_ARRAY)
349
+ return (int) RARRAY_LEN(value.value());
350
+ else if (value.type() == RUBY_T_STRING)
351
+ return (int) RSTRING_LEN(value.value());
352
+ else
353
+ return value.call(symbol);
354
+ }
355
+
356
+ int
357
+ Value::length () const
358
+ {
359
+ RUCY_SYM(length);
360
+ return get_length(*this, length);
361
+ }
362
+
363
+ int
364
+ Value::size () const
365
+ {
366
+ RUCY_SYM(size);
367
+ return get_length(*this, size);
368
+ }
369
+
311
370
 
312
371
  GlobalValue::GlobalValue ()
313
372
  {
@@ -320,50 +379,56 @@ namespace Rucy
320
379
  init(gc_);
321
380
  }
322
381
 
323
- GlobalValue::GlobalValue (int i, bool gc_)
324
- : Super(i)
382
+ GlobalValue::GlobalValue (int n, bool gc_)
383
+ : Super(n)
325
384
  {
326
385
  init(gc_);
327
386
  }
328
387
 
329
- GlobalValue::GlobalValue (float f, bool gc_)
330
- : Super(f)
388
+ GlobalValue::GlobalValue (unsigned int n, bool gc_)
389
+ : Super(n)
331
390
  {
332
391
  init(gc_);
333
392
  }
334
393
 
335
- GlobalValue::GlobalValue (double d, bool gc_)
336
- : Super(d)
394
+ GlobalValue::GlobalValue (float n, bool gc_)
395
+ : Super(n)
337
396
  {
338
397
  init(gc_);
339
398
  }
340
399
 
341
- GlobalValue::GlobalValue (const char* s, bool tainted, bool gc_)
342
- : Super(s, tainted)
400
+ GlobalValue::GlobalValue (double n, bool gc_)
401
+ : Super(n)
343
402
  {
344
403
  init(gc_);
345
404
  }
346
405
 
347
- GlobalValue::GlobalValue (const char* s, size_t len, bool tainted, bool gc_)
348
- : Super(s, len, tainted)
406
+ GlobalValue::GlobalValue (const char* s, bool gc_)
407
+ : Super(s)
349
408
  {
350
409
  init(gc_);
351
410
  }
352
411
 
353
- GlobalValue::GlobalValue (size_t size, const Value* values, bool gc_)
354
- : Super(size, values)
412
+ GlobalValue::GlobalValue (const char* s, size_t len, bool gc_)
413
+ : Super(s, len)
355
414
  {
356
415
  init(gc_);
357
416
  }
358
417
 
359
- GlobalValue::GlobalValue (VALUE v, bool gc_)
418
+ GlobalValue::GlobalValue (size_t size, const Value* array, bool gc_)
419
+ : Super(size, array)
420
+ {
421
+ init(gc_);
422
+ }
423
+
424
+ GlobalValue::GlobalValue (RubyValue v, bool gc_)
360
425
  : Super(v)
361
426
  {
362
427
  init(gc_);
363
428
  }
364
429
 
365
- GlobalValue::GlobalValue (const Value& obj, bool gc_)
366
- : Super(obj)
430
+ GlobalValue::GlobalValue (const Value& v, bool gc_)
431
+ : Super(v)
367
432
  {
368
433
  init(gc_);
369
434
  }
@@ -375,9 +440,9 @@ namespace Rucy
375
440
  }
376
441
 
377
442
  GlobalValue&
378
- GlobalValue::operator = (const Value& obj)
443
+ GlobalValue::operator = (const Value& v)
379
444
  {
380
- Super::operator=(obj);
445
+ Super::operator=(v);
381
446
  update_guard();
382
447
  return *this;
383
448
  }
@@ -388,7 +453,7 @@ namespace Rucy
388
453
  }
389
454
 
390
455
  void
391
- GlobalValue::gc (bool enable)
456
+ GlobalValue::gc (bool enable) const
392
457
  {
393
458
  gc_disable_count += enable ? -1 : +1;
394
459
  update_guard();
@@ -403,28 +468,34 @@ namespace Rucy
403
468
  }
404
469
 
405
470
  void
406
- GlobalValue::update_guard ()
471
+ GlobalValue::update_guard () const
407
472
  {
408
473
  assert(gc_disable_count >= 0);
409
474
 
410
- if (IMMEDIATE_P(v))
475
+ if (IMMEDIATE_P(val))
411
476
  {
412
- if (gc_guarded) rb_gc_unregister_address(&v);
477
+ if (gc_guarded) rb_gc_unregister_address((RubyValue*) &val);
413
478
  gc_guarded = false;
414
479
  }
415
480
  else if (gc_disable_count > 0)
416
481
  {
417
- if (!gc_guarded) rb_gc_register_address(&v);
482
+ if (!gc_guarded) rb_gc_register_address((RubyValue*) &val);
418
483
  gc_guarded = true;
419
484
  }
420
485
  else
421
486
  {
422
- if (gc_guarded) rb_gc_unregister_address(&v);
487
+ if (gc_guarded) rb_gc_unregister_address((RubyValue*) &val);
423
488
  gc_guarded = false;
424
489
  }
425
490
  }
426
491
 
427
492
 
493
+ Value
494
+ nil ()
495
+ {
496
+ return Qnil;
497
+ }
498
+
428
499
  Value
429
500
  value (bool b)
430
501
  {
@@ -440,7 +511,7 @@ namespace Rucy
440
511
  Value
441
512
  value (unsigned char n)
442
513
  {
443
- return (int) n;
514
+ return (unsigned int) n;
444
515
  }
445
516
 
446
517
  Value
@@ -452,7 +523,7 @@ namespace Rucy
452
523
  Value
453
524
  value (unsigned short n)
454
525
  {
455
- return (int) n;
526
+ return (unsigned int) n;
456
527
  }
457
528
 
458
529
  Value
@@ -464,7 +535,7 @@ namespace Rucy
464
535
  Value
465
536
  value (unsigned int n)
466
537
  {
467
- return UINT2NUM(n);
538
+ return n;
468
539
  }
469
540
 
470
541
  Value
@@ -492,52 +563,180 @@ namespace Rucy
492
563
  }
493
564
 
494
565
  Value
495
- value (float f)
566
+ value (float n)
496
567
  {
497
- return f;
568
+ return n;
498
569
  }
499
570
 
500
571
  Value
501
- value (double d)
572
+ value (double n)
502
573
  {
503
- return d;
574
+ return n;
504
575
  }
505
576
 
506
577
  Value
507
- value (const char* s, bool tainted)
578
+ value (const char* s)
508
579
  {
509
- return Value(s, tainted);
580
+ return Value(s);
510
581
  }
511
582
 
512
583
  Value
513
- value (const char* s, size_t len, bool tainted)
584
+ value (const char* s, size_t len)
514
585
  {
515
- return Value(s, len, tainted);
586
+ return Value(s, len);
516
587
  }
517
588
 
518
589
  Value
519
- value (size_t size, const Value* values)
590
+ value (size_t size, const Value* array)
520
591
  {
521
- return Value(size, values);
592
+ return Value(size, array);
522
593
  }
523
594
 
524
595
  Value
525
- value (Symbol sym)
596
+ value (void* ptr)
526
597
  {
527
- return ID2SYM(sym.id());
598
+ if (ptr)
599
+ argument_error(__FILE__, __LINE__, "Rucy::value(void*) can take only (void*) NULL.");
600
+
601
+ return nil();
528
602
  }
603
+ % (1..10).each do |n|
529
604
 
530
605
  Value
531
- value (void* ptr)
606
+ array (<%= params(n, ', ') {|i| "Value v#{i}"} %>)
532
607
  {
533
- if (ptr) argument_error("Rucy::value(void*) can take only (void*) NULL.");
534
- return Qnil;
608
+ Value tmp[] = {<%= params(n, ', ') {|i| "v#{i}"} %>};
609
+ return value(<%= n %>, tmp);
535
610
  }
611
+ % end
536
612
 
537
- Value
538
- nil ()
613
+
614
+ template <> bool
615
+ value_to<bool> (Value obj, bool convert)
539
616
  {
540
- return Qnil;
617
+ return (bool) obj;
618
+ }
619
+
620
+ template <> int
621
+ value_to<int> (Value obj, bool convert)
622
+ {
623
+ if (convert) obj = obj.to_i();
624
+ return NUM2INT(obj.value());
625
+ }
626
+
627
+ template <> unsigned int
628
+ value_to<unsigned int> (Value obj, bool convert)
629
+ {
630
+ if (convert) obj = obj.to_i();
631
+ return NUM2UINT(obj.value());
632
+ }
633
+
634
+ template <> char
635
+ value_to<char> (Value obj, bool convert)
636
+ {
637
+ return (char) value_to<int>(obj, convert);
638
+ }
639
+
640
+ template <> unsigned char
641
+ value_to<unsigned char> (Value obj, bool convert)
642
+ {
643
+ return (unsigned char) value_to<unsigned int>(obj, convert);
644
+ }
645
+
646
+ template <> short
647
+ value_to<short> (Value obj, bool convert)
648
+ {
649
+ return (short) value_to<int>(obj, convert);
650
+ }
651
+
652
+ template <> unsigned short
653
+ value_to<unsigned short> (Value obj, bool convert)
654
+ {
655
+ return (unsigned short) value_to<unsigned int>(obj, convert);
656
+ }
657
+
658
+ template <> long
659
+ value_to<long> (Value obj, bool convert)
660
+ {
661
+ if (convert) obj = obj.to_i();
662
+ return NUM2LONG(obj.value());
663
+ }
664
+
665
+ template <> unsigned long
666
+ value_to<unsigned long> (Value obj, bool convert)
667
+ {
668
+ if (convert) obj = obj.to_i();
669
+ return NUM2ULONG(obj.value());
670
+ }
671
+
672
+ template <> long long
673
+ value_to<long long> (Value obj, bool convert)
674
+ {
675
+ if (convert) obj = obj.to_i();
676
+ return NUM2LL(obj.value());
677
+ }
678
+
679
+ template <> unsigned long long
680
+ value_to<unsigned long long> (Value obj, bool convert)
681
+ {
682
+ if (convert) obj = obj.to_i();
683
+ return NUM2ULL(obj.value());
684
+ }
685
+
686
+ template <> double
687
+ value_to<double> (Value obj, bool convert)
688
+ {
689
+ if (convert) obj = obj.to_f();
690
+ check_type(obj, RUBY_T_FLOAT);
691
+
692
+ return RFLOAT_VALUE(obj.value());
693
+ }
694
+
695
+ template <> float
696
+ value_to<float> (Value obj, bool convert)
697
+ {
698
+ return (float) value_to<double>(obj, convert);
699
+ }
700
+
701
+ template <> char*
702
+ value_to<char*> (Value obj, bool convert)
703
+ {
704
+ if (convert) obj = obj.to_s();
705
+ RubyValue s = obj.value();
706
+ return StringValueCStr(s);
707
+ }
708
+
709
+ template <> const char*
710
+ value_to<const char*> (Value obj, bool convert)
711
+ {
712
+ return value_to<char*>(obj, convert);
713
+ }
714
+
715
+ template <> Symbol
716
+ value_to<Symbol> (Value obj, bool convert)
717
+ {
718
+ if (convert) obj = obj.to_sym();
719
+ check_type(obj, RUBY_T_SYMBOL);
720
+
721
+ return SYM2ID(obj.value());
722
+ }
723
+
724
+ template <> Value*
725
+ value_to<Value*> (Value obj, bool convert)
726
+ {
727
+ if (convert) obj = obj.to_array();
728
+ check_type(obj, RUBY_T_ARRAY);
729
+
730
+ return (Value*) RARRAY_PTR(obj.value());
731
+ }
732
+
733
+ template <> const Value*
734
+ value_to<const Value*> (Value obj, bool convert)
735
+ {
736
+ if (convert) obj = obj.to_array();
737
+ check_type(obj, RUBY_T_ARRAY);
738
+
739
+ return (const Value*) RARRAY_PTR(obj.value());
541
740
  }
542
741
 
543
742