[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

The new allocation scheme and extensions

Shu-yu Guo

11/1/2003 9:43:00 PM

In 1.6, when writing an extension which required Data_Wrap_Struct, the
programmer would define the singleton method "new" to handle that. In 1.8, the
scheme seems to be to write an allocate function (to pass to
rb_define_alloc_func) then do the rest of the code in the intialize function.
Indeed, that's what Class#new does now.

So my question is, when writing extensions, is the "old" way of defining the
singleton method "new" deprecated? Are we to use the new allocate/initialize
way now?

--
Shu-yu Guo

3 Answers

matz

11/1/2003 11:47:00 PM

0

Hi,

In message "The new allocation scheme and extensions"
on 03/11/02, Shu-yu Guo <shu@aria.rufuran.org> writes:

|So my question is, when writing extensions, is the "old" way of defining the
|singleton method "new" deprecated? Are we to use the new allocate/initialize
|way now?

If you have chance to update the extension, use new allocation scheme
now. But the old scheme would work during 1.8.x series.

matz.

Paul Brannan

11/6/2003 2:22:00 PM

0

On Sun, Nov 02, 2003 at 06:43:14AM +0900, Shu-yu Guo wrote:
> In 1.6, when writing an extension which required Data_Wrap_Struct, the
> programmer would define the singleton method "new" to handle that. In 1.8, the
> scheme seems to be to write an allocate function (to pass to
> rb_define_alloc_func) then do the rest of the code in the intialize function.
> Indeed, that's what Class#new does now.
>
> So my question is, when writing extensions, is the "old" way of defining the
> singleton method "new" deprecated? Are we to use the new allocate/initialize
> way now?

You can do this on 1.6:

inline VALUE ruby_16_new(int argc, VALUE * argv, VALUE klass)
{
VALUE obj = rb_funcall(klass, rb_intern("allocate"), 0);
rb_obj_call_init(obj, argc, argv);
return obj;
}

rb_define_singleton_method(klass, "allocate", allocate_func, -1);
rb_define_singleton_method(klass, "new", ruby_16_new, -1);
rb_define_method(klass.v, "initialize", initialize_func, -1);

This allows you to divide your code into allocate/initialize similar to
how you would on 1.8.

Paul


KUBO Takehiro

11/7/2003 3:31:00 AM

0

Paul Brannan <pbrannan@atdesk.com> writes:

> On Sun, Nov 02, 2003 at 06:43:14AM +0900, Shu-yu Guo wrote:
>> In 1.6, when writing an extension which required Data_Wrap_Struct, the
>> programmer would define the singleton method "new" to handle that. In 1.8, the
>> scheme seems to be to write an allocate function (to pass to
>> rb_define_alloc_func) then do the rest of the code in the intialize function.
>> Indeed, that's what Class#new does now.
>>
>> So my question is, when writing extensions, is the "old" way of defining the
>> singleton method "new" deprecated? Are we to use the new allocate/initialize
>> way now?
>
> You can do this on 1.6:
>
> inline VALUE ruby_16_new(int argc, VALUE * argv, VALUE klass)
> {
> VALUE obj = rb_funcall(klass, rb_intern("allocate"), 0);
> rb_obj_call_init(obj, argc, argv);
> return obj;
> }
>
> rb_define_singleton_method(klass, "allocate", allocate_func, -1);
> rb_define_singleton_method(klass, "new", ruby_16_new, -1);
> rb_define_method(klass.v, "initialize", initialize_func, -1);
>
> This allows you to divide your code into allocate/initialize similar to
> how you would on 1.8.

Or you can do this on 1.6 and 1.8:

static VALUE foo_s_allocate(VALUE klass)
{
......
}

static VALUE foo_initialize(int argc, VALUE *argv, VALUE self)
{
......
}

#ifndef HAVE_RB_DEFINE_ALLOC_FUNC
/* ruby 1.6 */
static VALUE foo_s_new(int argc, VALUE * argv, VALUE klass)
{
VALUE obj = foo_s_allocate(klass);
rb_obj_call_init(obj, argc, argv);
return obj;
}
#endif

void Init_foo()
{
....
#ifdef HAVE_RB_DEFINE_ALLOC_FUNC
/* ruby 1.8 */
rb_define_alloc_func(klass, foo_s_allocate);
#else
/* ruby 1.6 */
rb_define_singleton_method(klass, "new", foo_s_new, -1);
#endif
rb_define_method(klass, "initialize", foo_initialize, -1);
....
}

--
KUBO Takehiro
kubo@jiubao.org