summaryrefslogtreecommitdiff
path: root/Lib/ruby/rubyclasses.swg
blob: d8b97232e58c625e28e83004bef5103bfed7cb11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
237
238
239
240
241
#ifdef __cplusplus

/*
  GC_VALUE is used as a replacement of Ruby's VALUE.
  GC_VALUE automatically handles registering and unregistering
  of the underlying ruby object with the GC.

  It can be used if you want to create STL containers of VALUEs, such as:
  
     std::vector< GC_VALUE >;

  or as a member variable:
  
     struct A {
       GC_VALUE obj;
       A(VALUE o) : _obj(o) {
       }
     };

   or as a input/output value (not much use for this, as VALUE works as
   well here, thou):

     GC_VALUE func(GC_VALUE obj) { 
       GC_VALUE out = rb_obj_classname(obj);
       return out;
     }


   GC_VALUE is 'visible' at the wrapped side, so you can do:

      %template(RubyVector) std::vector<swig::GC_VALUE>;

   and all the proper typemaps will be used.
   
*/

namespace swig {

  %nodirector GC_VALUE;

  // We ignore the constructor so that user can never create a GC_VALUE manually
  %ignore GC_VALUE::GC_VALUE;

  struct GC_VALUE {
    VALUE inspect() const;
    VALUE to_s() const;
    GC_VALUE();
  protected:
    GC_VALUE( const GC_VALUE& );
    ~GC_VALUE();
  };

  %exception GC_VALUE {};


  %apply VALUE   {GC_VALUE};

  // Make sure this is the last typecheck done
  %typecheck(999999,noblock=1) GC_VALUE, GC_VALUE&, 
    const GC_VALUE& { $1 = 1; };

  /* For input */
  %typemap(in,noblock=1) GC_VALUE* (GC_VALUE r), GC_VALUE& (GC_VALUE r)  {
     r = $input; $1 = &r;
   }

  /* For output */
  %typemap(out,noblock=1)  GC_VALUE {
    $result = (VALUE)$1;
  }
  
  %typemap(out,noblock=1)  GC_VALUE*, GC_VALUE const & {
    $result = (VALUE)*$1;
  }

  %ignore LANGUAGE_OBJ;
  typedef GC_VALUE LANGUAGE_OBJ;
}

%{
namespace swig {
  class GC_VALUE {
  protected:
    VALUE  _obj;

  public:
    GC_VALUE() :_obj( Qnil )
    {
      GC_register();
    }

    GC_VALUE(const GC_VALUE& item) : _obj(item._obj)
    {
      GC_register();
    }
    
    GC_VALUE(VALUE obj) :_obj(obj)
    {
      GC_register();
    }
    
    ~GC_VALUE() 
    {
      GC_unregister();
    }
    
    GC_VALUE & operator=(const GC_VALUE& item) 
    {
      _obj = item._obj;
      return *this;      
    }

    void GC_register()
    {
      rb_gc_register_address( &_obj );
    }

    void GC_unregister()
    {
      rb_gc_unregister_address( &_obj );
    }
    
    operator VALUE() const
    {
      return _obj;
    }

    VALUE inspect() const
    {
      return rb_inspect(_obj);
    }

    VALUE to_s() const
    {
      return rb_inspect(_obj);
    }

  };

  typedef GC_VALUE LANGUAGE_OBJ;
}


/**
 * std::less< GC_VALUE > functor so that STL containers will accept
 * GC_VALUE.
 * 
 */

namespace std {
  template <>
  struct less< swig::GC_VALUE >: public binary_function< swig::GC_VALUE, 
							 swig::GC_VALUE, bool >
  {
    static ID  cmp_id;
    static ID hash_id;


    static VALUE swig_protect_funcall( VALUE p )
    {
      swig::GC_VALUE* args = (swig::GC_VALUE*) p;
      return rb_funcall(VALUE(args[0]), cmp_id, 1, VALUE(args[1]));
    }

    bool
    operator()( const swig::GC_VALUE& v, const swig::GC_VALUE& w ) const
    { 
      // SWIG_RUBY_THREAD_BEGIN_BLOCK;

      VALUE ret = Qnil;

      // First, try to compare using the <=> operator if present
      if ( rb_respond_to( v, cmp_id ) == Qtrue )
	{
	  int status;
	  swig::GC_VALUE args[] = { v, w };
	  ret = rb_protect( PROTECTFUNC(swig_protect_funcall), VALUE(args), 
			    &status );
	}

      bool res;
      // If that fails, try to use the two object's hash function to compare.
      if ( ret == Qnil ) {
	VALUE a = rb_funcall( VALUE(v), hash_id, 0 );
	VALUE b = rb_funcall( VALUE(w), hash_id, 0 );
	res = a < b;  // as shifted integers
      }
      else
	{
	  res = NUM2INT( ret ) < 0;
	}
      // SWIG_RUBY_THREAD_END_BLOCK;
      return res;
    }
  };

  ID less< swig::GC_VALUE >::cmp_id  = rb_intern("<=>");
  ID less< swig::GC_VALUE >::hash_id = rb_intern("hash");
}
%}


//
// Fragment that contains traits to properly deal with GC_VALUE.
// These functions may be invoked as a need of the from(), asval(),
// asptr() and as() template functors, usually used in %typemaps.
//
%fragment(SWIG_Traits_frag(swig::GC_VALUE),"header",fragment="StdTraits") {
namespace swig {
  template <>  struct traits<GC_VALUE > {
    typedef value_category category;
    static const char* type_name() { return "GC_VALUE"; }
  };
  
  template <>  struct traits_from<GC_VALUE> {
    typedef GC_VALUE value_type;
    static VALUE from(const value_type& val) {
      return static_cast<VALUE>(val);
    }
  };
  
  template <> 
  struct traits_check<GC_VALUE, value_category> {
    static bool check(GC_VALUE) {
      return true;
    }
  };
  
  template <>  struct traits_asval<GC_VALUE > {   
    typedef GC_VALUE value_type;
    static int asval(VALUE obj, value_type *val) {
      if (val) *val = obj;
      return SWIG_OK;
    }
  };
} // swig
} // %fragment(traits for swig::GC_VALUE)


#endif  // __cplusplus