1#include <ruby.h>
2
3/* Remove global macros defined in Ruby's win32.h */
4#ifdef write
5# undef write
6#endif
7#ifdef read
8# undef read
9#endif
10#ifdef bind
11# undef bind
12#endif
13#ifdef close
14# undef close
15#endif
16#ifdef connect
17# undef connect
18#endif
19
20
21/* Ruby 1.7 defines NUM2LL(), LL2NUM() and ULL2NUM() macros */
22#ifndef NUM2LL
23#define NUM2LL(x) NUM2LONG((x))
24#endif
25#ifndef LL2NUM
26#define LL2NUM(x) INT2NUM((long) (x))
27#endif
28#ifndef ULL2NUM
29#define ULL2NUM(x) UINT2NUM((unsigned long) (x))
30#endif
31
32/* Ruby 1.7 doesn't (yet) define NUM2ULL() */
33#ifndef NUM2ULL
34#ifdef HAVE_LONG_LONG
35#define NUM2ULL(x) rb_num2ull((x))
36#else
37#define NUM2ULL(x) NUM2ULONG(x)
38#endif
39#endif
40
41/* RSTRING_LEN, etc are new in Ruby 1.9, but ->ptr and ->len no longer work */
42/* Define these for older versions so we can just write code the new way */
43#ifndef RSTRING_LEN
44# define RSTRING_LEN(x) RSTRING(x)->len
45#endif
46#ifndef RSTRING_PTR
47# define RSTRING_PTR(x) RSTRING(x)->ptr
48#endif
49#ifndef RSTRING_END
50# define RSTRING_END(x) (RSTRING_PTR(x) + RSTRING_LEN(x))
51#endif
52#ifndef RARRAY_LEN
53# define RARRAY_LEN(x) RARRAY(x)->len
54#endif
55#ifndef RARRAY_PTR
56# define RARRAY_PTR(x) RARRAY(x)->ptr
57#endif
58#ifndef RFLOAT_VALUE
59# define RFLOAT_VALUE(x) RFLOAT(x)->value
60#endif
61#ifndef DOUBLE2NUM
62# define DOUBLE2NUM(x) rb_float_new(x)
63#endif
64#ifndef RHASH_TBL
65# define RHASH_TBL(x) (RHASH(x)->tbl)
66#endif
67#ifndef RHASH_ITER_LEV
68# define RHASH_ITER_LEV(x) (RHASH(x)->iter_lev)
69#endif
70#ifndef RHASH_IFNONE
71# define RHASH_IFNONE(x) (RHASH(x)->ifnone)
72#endif
73#ifndef RHASH_SIZE
74# define RHASH_SIZE(x) (RHASH(x)->tbl->num_entries)
75#endif
76#ifndef RHASH_EMPTY_P
77# define RHASH_EMPTY_P(x) (RHASH_SIZE(x) == 0)
78#endif
79#ifndef RSTRUCT_LEN
80# define RSTRUCT_LEN(x) RSTRUCT(x)->len
81#endif
82#ifndef RSTRUCT_PTR
83# define RSTRUCT_PTR(x) RSTRUCT(x)->ptr
84#endif
85
86
87
88/*
89 * Need to be very careful about how these macros are defined, especially
90 * when compiling C++ code or C code with an ANSI C compiler.
91 *
92 * VALUEFUNC(f) is a macro used to typecast a C function that implements
93 * a Ruby method so that it can be passed as an argument to API functions
94 * like rb_define_method() and rb_define_singleton_method().
95 *
96 * VOIDFUNC(f) is a macro used to typecast a C function that implements
97 * either the "mark" or "free" stuff for a Ruby Data object, so that it
98 * can be passed as an argument to API functions like Data_Wrap_Struct()
99 * and Data_Make_Struct().
100 */
101 
102#ifdef __cplusplus
103#  ifndef RUBY_METHOD_FUNC /* These definitions should work for Ruby 1.4.6 */
104#    define PROTECTFUNC(f) ((VALUE (*)()) f)
105#    define VALUEFUNC(f) ((VALUE (*)()) f)
106#    define VOIDFUNC(f)  ((void (*)()) f)
107#  else
108#    ifndef ANYARGS /* These definitions should work for Ruby 1.6 */
109#      define PROTECTFUNC(f) ((VALUE (*)()) f)
110#      define VALUEFUNC(f) ((VALUE (*)()) f)
111#      define VOIDFUNC(f)  ((RUBY_DATA_FUNC) f)
112#    else /* These definitions should work for Ruby 1.7+ */
113#      define PROTECTFUNC(f) ((VALUE (*)(VALUE)) f)
114#      define VALUEFUNC(f) ((VALUE (*)(ANYARGS)) f)
115#      define VOIDFUNC(f)  ((RUBY_DATA_FUNC) f)
116#    endif
117#  endif
118#else
119#  define VALUEFUNC(f) (f)
120#  define VOIDFUNC(f) (f)
121#endif
122
123/* Don't use for expressions have side effect */
124#ifndef RB_STRING_VALUE
125#define RB_STRING_VALUE(s) (TYPE(s) == T_STRING ? (s) : (*(volatile VALUE *)&(s) = rb_str_to_str(s)))
126#endif
127#ifndef StringValue
128#define StringValue(s) RB_STRING_VALUE(s)
129#endif
130#ifndef StringValuePtr
131#define StringValuePtr(s) RSTRING_PTR(RB_STRING_VALUE(s))
132#endif
133#ifndef StringValueLen
134#define StringValueLen(s) RSTRING_LEN(RB_STRING_VALUE(s))
135#endif
136#ifndef SafeStringValue
137#define SafeStringValue(v) do {\
138    StringValue(v);\
139    rb_check_safe_str(v);\
140} while (0)
141#endif
142
143#ifndef HAVE_RB_DEFINE_ALLOC_FUNC
144#define rb_define_alloc_func(klass, func) rb_define_singleton_method((klass), "new", VALUEFUNC((func)), -1)
145#define rb_undef_alloc_func(klass) rb_undef_method(CLASS_OF((klass)), "new")
146#endif
147
148static VALUE _mSWIG = Qnil;
149