static VALUE rb_struct_initialize_m(int argc, VALUE *argv, VALUE self) { VALUE klass = rb_obj_class(self); long n; rb_struct_modify(self); n = num_members(klass); if (n < argc) { rb_raise(rb_eArgError, "struct size differs"); } MEMCPY(RSTRUCT_PTR(self), argv, VALUE, argc); if (n > argc) { rb_mem_clear(RSTRUCT_PTR(self)+argc, n-argc); } return Qnil; }
VALUE rb_struct_new(VALUE klass, ...) { VALUE *mem; long size, i; va_list args; size = num_members(klass); mem = ALLOCA_N(VALUE, size); va_start(args, klass); for (i=0; i<size; i++) { mem[i] = va_arg(args, VALUE); } va_end(args); return rb_class_new_instance(size, mem, klass); }
static mrb_value mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_value self) { struct RClass *klass = mrb_obj_class(mrb, self); int i, n; n = num_members(mrb, klass); if (n < argc) { mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs"); } for (i = 0; i < argc; i++) { mrb_ary_set(mrb, self, i, argv[i]); } for (i = argc; i < n; i++) { mrb_ary_set(mrb, self, i, mrb_nil_value()); } return self; }
static mrb_value mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_value self) { struct RClass *klass = mrb_obj_class(mrb, self); int n; struct RStruct *st; mrb_struct_modify(self); n = num_members(mrb, klass); if (n < argc) { mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs"); } st = RSTRUCT(self); st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); st->len = n; memcpy(st->ptr, argv, sizeof(mrb_value)*argc); return self; }
static VALUE rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self) { VALUE klass = rb_obj_class(self); long i, n; rb_struct_modify(self); n = num_members(klass); if (n < argc) { rb_raise(rb_eArgError, "struct size differs"); } for (i=0; i<argc; i++) { RSTRUCT_SET(self, i, argv[i]); } if (n > argc) { rb_mem_clear((VALUE *)RSTRUCT_CONST_PTR(self)+argc, n-argc); } return Qnil; }
VALUE rb_struct_new(VALUE klass, ...) { VALUE tmpargs[N_REF_FUNC], *mem = tmpargs; int size, i; va_list args; size = rb_long2int(num_members(klass)); if (size > numberof(tmpargs)) { tmpargs[0] = rb_ary_tmp_new(size); mem = RARRAY_PTR(tmpargs[0]); } va_start(args, klass); for (i=0; i<size; i++) { mem[i] = va_arg(args, VALUE); } va_end(args); return rb_class_new_instance(size, mem, klass); }
mrb_value mrb_struct_new(mrb_state *mrb, struct RClass *klass, ...) { mrb_value tmpargs[N_REF_FUNC], *mem = tmpargs; int size, i; va_list args; size = mrb_long2int(num_members(mrb, klass)); if (size > numberof(tmpargs)) { tmpargs[0] = mrb_ary_tmp_new(mrb, size); mem = RARRAY_PTR(tmpargs[0]); } va_start(args, klass); for (i=0; i<size; i++) { mem[i] = va_arg(args, mrb_value); } va_end(args); return mrb_class_new_instance(mrb, size, mem, klass); }
static VALUE struct_alloc(VALUE klass) { long n; NEWOBJ_OF(st, struct RStruct, klass, T_STRUCT); n = num_members(klass); if (0 < n && n <= RSTRUCT_EMBED_LEN_MAX) { RBASIC(st)->flags &= ~RSTRUCT_EMBED_LEN_MASK; RBASIC(st)->flags |= n << RSTRUCT_EMBED_LEN_SHIFT; rb_mem_clear(st->as.ary, n); } else { st->as.heap.ptr = ALLOC_N(VALUE, n); rb_mem_clear(st->as.heap.ptr, n); st->as.heap.len = n; } return (VALUE)st; }
static mrb_value mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_value self) { struct RClass *klass = mrb_obj_class(mrb, self); long n; struct RStruct *st; mrb_struct_modify(self); n = num_members(mrb, klass); if (n < argc) { mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs"); } st = RSTRUCT(self); st->ptr = malloc(sizeof(mrb_value)*argc); st->len = n; memcpy(st->ptr, argv, sizeof(mrb_value)*argc); //if (n > argc) { // mrb_mem_clear(RSTRUCT_PTR(self)+argc, n-argc); //} return self; }