MRuby-Binding: Bind #initialize_copy for clonable classes

Port of b7a2ba830c.
This commit is contained in:
Jonas Kulla 2014-08-09 20:12:06 +02:00
parent 1cc1541377
commit 3b35fb219c
5 changed files with 40 additions and 17 deletions

View File

@ -22,6 +22,8 @@
#ifndef BINDINGUTIL_H #ifndef BINDINGUTIL_H
#define BINDINGUTIL_H #define BINDINGUTIL_H
#include "exception.h"
#include <mruby.h> #include <mruby.h>
#include <mruby/data.h> #include <mruby/data.h>
#include <mruby/variable.h> #include <mruby/variable.h>
@ -212,15 +214,16 @@ defineClass(mrb_state *mrb, const char *name)
#define DEF_PROP_B(Klass, PropName) \ #define DEF_PROP_B(Klass, PropName) \
DEF_PROP(Klass, mrb_bool, PropName, "b", bool) DEF_PROP(Klass, mrb_bool, PropName, "b", bool)
// FIXME: use initialize_copy #define INITCOPY_FUN(Klass) \
#define CLONE_FUN(Klass) \ MRB_METHOD(Klass##InitializeCopy) \
MRB_METHOD(Klass##Clone) \
{ \ { \
Klass *k = getPrivateData<Klass>(mrb, self); \ mrb_value origObj; \
mrb_value dupObj = mrb_obj_clone(mrb, self); \ mrb_get_args(mrb, "o", &origObj); \
Klass *dupK = new Klass(*k); \ Klass *orig = getPrivateData<Klass>(mrb, origObj); \
setPrivateData(dupObj, dupK, Klass##Type); \ Klass *k = 0; \
return dupObj; \ GUARD_EXC( k = new Klass(*orig); ) \
setPrivateData(self, k, Klass##Type); \
return self; \
} }
#define MARSH_LOAD_FUN(Klass) \ #define MARSH_LOAD_FUN(Klass) \

View File

@ -297,7 +297,7 @@ MRB_METHOD(bitmapSetFont)
return mrb_nil_value(); return mrb_nil_value();
} }
CLONE_FUN(Bitmap) INITCOPY_FUN(Bitmap)
void void
@ -308,6 +308,7 @@ bitmapBindingInit(mrb_state *mrb)
disposableBindingInit<Bitmap>(mrb, klass); disposableBindingInit<Bitmap>(mrb, klass);
mrb_define_method(mrb, klass, "initialize", bitmapInitialize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(1)); mrb_define_method(mrb, klass, "initialize", bitmapInitialize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(1));
mrb_define_method(mrb, klass, "initialize_copy", BitmapInitializeCopy, MRB_ARGS_REQ(1));
mrb_define_method(mrb, klass, "width", bitmapWidth, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "width", bitmapWidth, MRB_ARGS_NONE());
mrb_define_method(mrb, klass, "height", bitmapHeight, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "height", bitmapHeight, MRB_ARGS_NONE());
@ -326,5 +327,4 @@ bitmapBindingInit(mrb_state *mrb)
mrb_define_method(mrb, klass, "font=", bitmapSetFont, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "font=", bitmapSetFont, MRB_ARGS_REQ(1));
mrb_define_method(mrb, klass, "inspect", inspectObject, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "inspect", inspectObject, MRB_ARGS_NONE());
mrb_define_method(mrb, klass, "clone", BitmapClone, MRB_ARGS_NONE());
} }

View File

@ -159,9 +159,9 @@ MARSH_LOAD_FUN(Color)
MARSH_LOAD_FUN(Tone) MARSH_LOAD_FUN(Tone)
MARSH_LOAD_FUN(Rect) MARSH_LOAD_FUN(Rect)
CLONE_FUN(Tone) INITCOPY_FUN(Tone)
CLONE_FUN(Color) INITCOPY_FUN(Color)
CLONE_FUN(Rect) INITCOPY_FUN(Rect)
#define MRB_ATTR_R(Class, Attr, sym) mrb_define_method(mrb, klass, sym, Class##Get##Attr, MRB_ARGS_NONE()) #define MRB_ATTR_R(Class, Attr, sym) mrb_define_method(mrb, klass, sym, Class##Get##Attr, MRB_ARGS_NONE())
#define MRB_ATTR_W(Class, Attr, sym) mrb_define_method(mrb, klass, sym "=", Class##Set##Attr, MRB_ARGS_REQ(1)) #define MRB_ATTR_W(Class, Attr, sym) mrb_define_method(mrb, klass, sym "=", Class##Set##Attr, MRB_ARGS_REQ(1))
@ -173,8 +173,8 @@ CLONE_FUN(Rect)
mrb_define_class_method(mrb, klass, "_load", Klass##Load, MRB_ARGS_REQ(1)); \ mrb_define_class_method(mrb, klass, "_load", Klass##Load, MRB_ARGS_REQ(1)); \
serializableBindingInit<Klass>(mrb, klass); \ serializableBindingInit<Klass>(mrb, klass); \
mrb_define_method(mrb, klass, "initialize", Klass##Initialize, MRB_ARGS_REQ(3) | MRB_ARGS_OPT(1)); \ mrb_define_method(mrb, klass, "initialize", Klass##Initialize, MRB_ARGS_REQ(3) | MRB_ARGS_OPT(1)); \
mrb_define_method(mrb, klass, "initialize_copy", Klass##InitializeCopy, MRB_ARGS_REQ(1)); \
mrb_define_method(mrb, klass, "set", Klass##Set, MRB_ARGS_REQ(3) | MRB_ARGS_OPT(1)); \ mrb_define_method(mrb, klass, "set", Klass##Set, MRB_ARGS_REQ(3) | MRB_ARGS_OPT(1)); \
mrb_define_method(mrb, klass, "clone", Klass##Clone, MRB_ARGS_NONE()); \
mrb_define_method(mrb, klass, "==", Klass##Equal, MRB_ARGS_REQ(1)); \ mrb_define_method(mrb, klass, "==", Klass##Equal, MRB_ARGS_REQ(1)); \
mrb_define_method(mrb, klass, "to_s", Klass##Stringify, MRB_ARGS_NONE()); \ mrb_define_method(mrb, klass, "to_s", Klass##Stringify, MRB_ARGS_NONE()); \
mrb_define_method(mrb, klass, "inspect", Klass##Stringify, MRB_ARGS_NONE()); \ mrb_define_method(mrb, klass, "inspect", Klass##Stringify, MRB_ARGS_NONE()); \

View File

@ -59,6 +59,22 @@ MRB_METHOD(fontInitialize)
return self; return self;
} }
MRB_METHOD(fontInitializeCopy)
{
mrb_value origObj;
mrb_get_args(mrb, "o", &origObj);
Font *orig = getPrivateData<Font>(mrb, origObj);
Font *f = new Font(*orig);
setPrivateData(self, f, FontType);
/* Wrap property objects */
f->setColor(new Color(*f->getColor()));
wrapProperty(mrb, self, f->getColor(), CScolor, ColorType);
return self;
}
MRB_METHOD(FontGetName) MRB_METHOD(FontGetName)
{ {
Font *f = getPrivateData<Font>(mrb, self); Font *f = getPrivateData<Font>(mrb, self);
@ -158,6 +174,7 @@ fontBindingInit(mrb_state *mrb)
INIT_KLASS_PROP_BIND(Font, DefaultColor, "default_color"); INIT_KLASS_PROP_BIND(Font, DefaultColor, "default_color");
mrb_define_method(mrb, klass, "initialize", fontInitialize, MRB_ARGS_OPT(2)); mrb_define_method(mrb, klass, "initialize", fontInitialize, MRB_ARGS_OPT(2));
mrb_define_method(mrb, klass, "initialize_copy", fontInitializeCopy, MRB_ARGS_REQ(1));
INIT_PROP_BIND(Font, Name, "name"); INIT_PROP_BIND(Font, Name, "name");
INIT_PROP_BIND(Font, Size, "size"); INIT_PROP_BIND(Font, Size, "size");

View File

@ -141,6 +141,7 @@ MRB_METHOD(tableSetAt)
} }
MARSH_LOAD_FUN(Table) MARSH_LOAD_FUN(Table)
INITCOPY_FUN(Table)
void void
tableBindingInit(mrb_state *mrb) tableBindingInit(mrb_state *mrb)
@ -151,6 +152,8 @@ tableBindingInit(mrb_state *mrb)
serializableBindingInit<Table>(mrb, klass); serializableBindingInit<Table>(mrb, klass);
mrb_define_method(mrb, klass, "initialize", tableInitialize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(2)); mrb_define_method(mrb, klass, "initialize", tableInitialize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(2));
mrb_define_method(mrb, klass, "initialize_copy", TableInitializeCopy, MRB_ARGS_REQ(1));
mrb_define_method(mrb, klass, "resize", tableResize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(2)); mrb_define_method(mrb, klass, "resize", tableResize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(2));
mrb_define_method(mrb, klass, "xsize", tableXSize, MRB_ARGS_NONE() ); mrb_define_method(mrb, klass, "xsize", tableXSize, MRB_ARGS_NONE() );
mrb_define_method(mrb, klass, "ysize", tableYSize, MRB_ARGS_NONE() ); mrb_define_method(mrb, klass, "ysize", tableYSize, MRB_ARGS_NONE() );