[前][次][番号順一覧][スレッド一覧]

ruby-changes:30646

From: ko1 <ko1@a...>
Date: Thu, 29 Aug 2013 18:12:32 +0900 (JST)
Subject: [ruby-changes:30646] ko1:r42725 (trunk): * struct.c (rb_struct_define_without_accessor_under): added.

ko1	2013-08-29 18:12:25 +0900 (Thu, 29 Aug 2013)

  New Revision: 42725

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42725

  Log:
    * struct.c (rb_struct_define_without_accessor_under): added.
      This function is similar to rb_define_class_under() against
      rb_define_class().
    * include/ruby/intern.h: add a declaration of this function.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/intern.h
    trunk/struct.c
Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 42724)
+++ include/ruby/intern.h	(revision 42725)
@@ -848,6 +848,8 @@ VALUE rb_struct_s_members(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L848
 VALUE rb_struct_members(VALUE);
 VALUE rb_struct_alloc_noinit(VALUE);
 VALUE rb_struct_define_without_accessor(const char *, VALUE, rb_alloc_func_t, ...);
+VALUE rb_struct_define_without_accessor_under(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, ...);
+
 /* thread.c */
 typedef void rb_unblock_function_t(void *);
 typedef VALUE rb_blocking_function_t(void *);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42724)
+++ ChangeLog	(revision 42725)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Aug 29 18:05:33 2013  Koichi Sasada  <ko1@a...>
+
+	* struct.c (rb_struct_define_without_accessor_under): added.
+	  This function is similar to rb_define_class_under() against
+	  rb_define_class().
+
+	* include/ruby/intern.h: add a declaration of this function.
+
 Thu Aug 29 17:03:10 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* vm_insnhelper.c (vm_call_method): a method entry refers the based
Index: struct.c
===================================================================
--- struct.c	(revision 42724)
+++ struct.c	(revision 42725)
@@ -229,10 +229,38 @@ rb_struct_alloc_noinit(VALUE klass) https://github.com/ruby/ruby/blob/trunk/struct.c#L229
     return struct_alloc(klass);
 }
 
-VALUE
-rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_func_t alloc, ...)
+static VALUE
+struct_define_without_accessor(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, VALUE members)
 {
     VALUE klass;
+
+    if (class_name) {
+	if (outer) {
+	    klass = rb_define_class_under(outer, class_name, super);
+	}
+	else {
+	    klass = rb_define_class(class_name, super);
+	}
+    }
+    else {
+	klass = anonymous_struct(super);
+    }
+
+    rb_ivar_set(klass, id_members, members);
+
+    if (alloc) {
+	rb_define_alloc_func(klass, alloc);
+    }
+    else {
+	rb_define_alloc_func(klass, struct_alloc);
+    }
+
+    return klass;
+}
+
+VALUE
+rb_struct_define_without_accessor_under(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, ...)
+{
     va_list ar;
     VALUE members;
     char *name;
@@ -245,21 +273,25 @@ rb_struct_define_without_accessor(const https://github.com/ruby/ruby/blob/trunk/struct.c#L273
     va_end(ar);
     OBJ_FREEZE(members);
 
-    if (class_name) {
-        klass = rb_define_class(class_name, super);
-    }
-    else {
-	klass = anonymous_struct(super);
-    }
+    return struct_define_without_accessor(outer, class_name, super, alloc, members);
+}
 
-    rb_ivar_set(klass, id_members, members);
+VALUE
+rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_func_t alloc, ...)
+{
+    va_list ar;
+    VALUE members;
+    char *name;
 
-    if (alloc)
-        rb_define_alloc_func(klass, alloc);
-    else
-        rb_define_alloc_func(klass, struct_alloc);
+    members = rb_ary_tmp_new(0);
+    va_start(ar, alloc);
+    while ((name = va_arg(ar, char*)) != NULL) {
+        rb_ary_push(members, ID2SYM(rb_intern(name)));
+    }
+    va_end(ar);
+    OBJ_FREEZE(members);
 
-    return klass;
+    return struct_define_without_accessor(0, class_name, super, alloc, members);
 }
 
 VALUE

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]