Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

conversions.c
Go to the documentation of this file.
1 #include <fiddle.h>
2
3 ffi_type *
4 int_to_ffi_type(int type)
5 {
6 int signed_p = 1;
7
8 if (type < 0) {
9 type = -1 * type;
10 signed_p = 0;
11 }
12
13 #define rb_ffi_type_of(t) (signed_p ? &ffi_type_s##t : &ffi_type_u##t)
14
15 switch (type) {
16 case TYPE_VOID:
17 return &ffi_type_void;
18 case TYPE_VOIDP:
19 return &ffi_type_pointer;
20 case TYPE_CHAR:
21 return rb_ffi_type_of(char);
22 case TYPE_SHORT:
23 return rb_ffi_type_of(short);
24 case TYPE_INT:
25 return rb_ffi_type_of(int);
26 case TYPE_LONG:
27 return rb_ffi_type_of(long);
28 #if HAVE_LONG_LONG
29 case TYPE_LONG_LONG:
30 return rb_ffi_type_of(long_long);
31 #endif
32 case TYPE_FLOAT:
33 return &ffi_type_float;
34 case TYPE_DOUBLE:
35 return &ffi_type_double;
36 default:
37 rb_raise(rb_eRuntimeError, "unknown type %d", type);
38 }
39 return &ffi_type_pointer;
40 }
41
42 void
43 value_to_generic(int type, VALUE src, fiddle_generic * dst)
44 {
45 switch (type) {
46 case TYPE_VOID:
47 break;
48 case TYPE_VOIDP:
49 dst->pointer = NUM2PTR(rb_Integer(src));
50 break;
51 case TYPE_CHAR:
52 dst->schar = (signed char)NUM2INT(src);
53 break;
54 case -TYPE_CHAR:
55 dst->uchar = (unsigned char)NUM2UINT(src);
56 break;
57 case TYPE_SHORT:
58 dst->sshort = (unsigned short)NUM2INT(src);
59 break;
60 case -TYPE_SHORT:
61 dst->sshort = (signed short)NUM2UINT(src);
62 break;
63 case TYPE_INT:
64 dst->sint = NUM2INT(src);
65 break;
66 case -TYPE_INT:
67 dst->uint = NUM2UINT(src);
68 break;
69 case TYPE_LONG:
70 dst->slong = NUM2LONG(src);
71 break;
72 case -TYPE_LONG:
73 dst->ulong = NUM2ULONG(src);
74 break;
75 #if HAVE_LONG_LONG
76 case TYPE_LONG_LONG:
77 dst->slong_long = NUM2LL(src);
78 break;
79 case -TYPE_LONG_LONG:
80 dst->ulong_long = NUM2ULL(src);
81 break;
82 #endif
83 case TYPE_FLOAT:
84 dst->ffloat = (float)NUM2DBL(src);
85 break;
86 case TYPE_DOUBLE:
87 dst->ddouble = NUM2DBL(src);
88 break;
89 default:
90 rb_raise(rb_eRuntimeError, "unknown type %d", type);
91 }
92 }
93
94 VALUE
95 generic_to_value(VALUE rettype, fiddle_generic retval)
96 {
97 int type = NUM2INT(rettype);
98 VALUE cPointer;
99
100 cPointer = rb_const_get(mFiddle, rb_intern("Pointer"));
101
102 switch (type) {
103 case TYPE_VOID:
104 return Qnil;
105 case TYPE_VOIDP:
106 return rb_funcall(cPointer, rb_intern("[]"), 1,
107 PTR2NUM((void *)retval.pointer));
108 case TYPE_CHAR:
109 return INT2NUM((signed char)retval.fffi_sarg);
110 case -TYPE_CHAR:
111 return INT2NUM((unsigned char)retval.fffi_arg);
112 case TYPE_SHORT:
113 return INT2NUM((signed short)retval.fffi_sarg);
114 case -TYPE_SHORT:
115 return INT2NUM((unsigned short)retval.fffi_arg);
116 case TYPE_INT:
117 return INT2NUM((signed int)retval.fffi_sarg);
118 case -TYPE_INT:
119 return UINT2NUM((unsigned int)retval.fffi_arg);
120 case TYPE_LONG:
121 return LONG2NUM(retval.slong);
122 case -TYPE_LONG:
123 return ULONG2NUM(retval.ulong);
124 #if HAVE_LONG_LONG
125 case TYPE_LONG_LONG:
126 return LL2NUM(retval.slong_long);
127 case -TYPE_LONG_LONG:
128 return ULL2NUM(retval.ulong_long);
129 #endif
130 case TYPE_FLOAT:
131 return rb_float_new(retval.ffloat);
132 case TYPE_DOUBLE:
133 return rb_float_new(retval.ddouble);
134 default:
135 rb_raise(rb_eRuntimeError, "unknown type %d", type);
136 }
137
138 UNREACHABLE;
139 }
140
141 /* vim: set noet sw=4 sts=4 */
fiddle_generic::ffloat
float ffloat
Definition: conversions.h:18
int_to_ffi_type
ffi_type * int_to_ffi_type(int type)
Definition: conversions.c:4
TYPE_VOIDP
#define TYPE_VOIDP
Definition: fiddle.h:113
fiddle.h
mFiddle
VALUE mFiddle
Definition: fiddle.c:3
fiddle_generic::schar
signed char schar
Definition: conversions.h:11
value_to_generic
void value_to_generic(int type, VALUE src, fiddle_generic *dst)
Definition: conversions.c:43
fiddle_generic::ulong
unsigned long ulong
Definition: conversions.h:16
fiddle_generic::ddouble
double ddouble
Definition: conversions.h:19
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1876
generic_to_value
VALUE generic_to_value(VALUE rettype, fiddle_generic retval)
Definition: conversions.c:95
UNREACHABLE
#define UNREACHABLE
Definition: ruby.h:40
fiddle_generic::sshort
signed short sshort
Definition: conversions.h:13
NUM2ULONG
#define NUM2ULONG(x)
NUM2PTR
#define NUM2PTR(x)
Definition: dl.h:169
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
TYPE_CHAR
#define TYPE_CHAR
Definition: fiddle.h:114
TYPE_SHORT
#define TYPE_SHORT
Definition: fiddle.h:115
LONG2NUM
#define LONG2NUM(x)
rb_float_new
static VALUE rb_float_new(double d)
Definition: ripper.y:790
TYPE_INT
#define TYPE_INT
Definition: fiddle.h:116
TYPE_DOUBLE
#define TYPE_DOUBLE
Definition: fiddle.h:122
TYPE_LONG
#define TYPE_LONG
Definition: fiddle.h:117
NUM2DBL
#define NUM2DBL(x)
Qnil
#define Qnil
Definition: tcltklib.c:1896
fiddle_generic::slong
signed long slong
Definition: conversions.h:17
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:510
PTR2NUM
#define PTR2NUM(x)
Definition: dl.h:168
fiddle_generic::uint
unsigned int uint
Definition: conversions.h:14
fiddle_generic::uchar
unsigned char uchar
Definition: conversions.h:10
fiddle_generic::fffi_arg
ffi_arg fffi_arg
Definition: conversions.h:8
fiddle_generic::pointer
void * pointer
Definition: conversions.h:24
NUM2UINT
#define NUM2UINT(x)
retval
VALUE retval
Definition: tcltklib.c:7830
type
int type
Definition: tcltklib.c:111
rb_ffi_type_of
#define rb_ffi_type_of(t)
fiddle_generic
Definition: conversions.h:6
TYPE_FLOAT
#define TYPE_FLOAT
Definition: fiddle.h:121
rb_Integer
VALUE rb_Integer(VALUE)
Definition: object.c:2488
src
VALUE src
Definition: tcltklib.c:7953
NUM2LONG
#define NUM2LONG(x)
TYPE_VOID
#define TYPE_VOID
Definition: fiddle.h:112
UINT2NUM
#define UINT2NUM(x)
INT2NUM
#define INT2NUM(x)
fiddle_generic::fffi_sarg
ffi_sarg fffi_sarg
Definition: conversions.h:9
NUM2INT
#define NUM2INT(x)
fiddle_generic::sint
signed int sint
Definition: conversions.h:15
VALUE
unsigned long VALUE
Definition: ripper.y:104
rb_intern
#define rb_intern(str)
ULONG2NUM
#define ULONG2NUM(x)
dst
char * dst
Definition: tcltklib.c:9868