swap.cc
Go to the documentation of this file.
00001 #ifdef RUBY_IS_19
00002 #include "ruby_internals-1.9.h"
00003 #else
00004 #include "ruby_internals-1.8.h"
00005 #endif
00006 
00007 /*
00008  * Kernel.swap!(obj1, obj2, *args)
00009  *
00010  * Swaps the object which are being hold by obj1 and obj2.
00011  *
00012  * WARNING: I don't know if this can be called in a method of +obj1+ or +obj2+
00013  */
00014 static VALUE kernel_swap_bang(VALUE self, VALUE obj1, VALUE obj2)
00015 {
00016     // Save the definition of the old object
00017     RVALUE old_obj;
00018     memcpy(&old_obj, reinterpret_cast<void*>(obj1), SLOT_SIZE);
00019     // Place the definition of the new object in the slot of the old one
00020     memcpy(reinterpret_cast<void*>(obj1), reinterpret_cast<void*>(obj2), SLOT_SIZE);
00021     // Place the definition of the old object in the slot of the new one
00022     memcpy(reinterpret_cast<void*>(obj2), &old_obj, SLOT_SIZE);
00023 
00024     return Qnil;
00025 }
00026 
00027 extern "C" void Init_swap()
00028 {
00029     rb_define_singleton_method(rb_mKernel, "swap!", RUBY_METHOD_FUNC(kernel_swap_bang), 2);
00030 }
00031 


utilrb
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Thu Jan 2 2014 11:38:28