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

ruby-changes:30296

From: nobu <ko1@a...>
Date: Sat, 3 Aug 2013 09:31:13 +0900 (JST)
Subject: [ruby-changes:30296] nobu:r42348 (trunk): struct.c: rb_struct_define_under

nobu	2013-08-03 09:31:02 +0900 (Sat, 03 Aug 2013)

  New Revision: 42348

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

  Log:
    struct.c: rb_struct_define_under
    
    * struct.c (rb_struct_define_under): new function to define Struct
      under the given namespace, not under Struct.  [Feature #8264]
    * ext/etc/etc.c: use rb_struct_define_under.

  Modified files:
    trunk/ChangeLog
    trunk/ext/etc/etc.c
    trunk/include/ruby/intern.h
    trunk/struct.c

Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 42347)
+++ include/ruby/intern.h	(revision 42348)
@@ -837,6 +837,7 @@ VALUE rb_str_ellipsize(VALUE, long); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L837
 /* struct.c */
 VALUE rb_struct_new(VALUE, ...);
 VALUE rb_struct_define(const char*, ...);
+VALUE rb_struct_define_under(VALUE, const char*, ...);
 VALUE rb_struct_alloc(VALUE, VALUE);
 VALUE rb_struct_initialize(VALUE, VALUE);
 VALUE rb_struct_aref(VALUE, VALUE);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42347)
+++ ChangeLog	(revision 42348)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Aug  3 09:30:57 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* struct.c (rb_struct_define_under): new function to define Struct
+	  under the given namespace, not under Struct.  [Feature #8264]
+
+	* ext/etc/etc.c: use rb_struct_define_under.
+
 Sat Aug  3 06:55:29 2013  NAKAMURA Usaku  <usa@r...>
 
 	* parse.y (value_expr_gen): now NODE_DEFN and NODE_DEFS are not void
Index: struct.c
===================================================================
--- struct.c	(revision 42347)
+++ struct.c	(revision 42348)
@@ -283,6 +283,25 @@ rb_struct_define(const char *name, ...) https://github.com/ruby/ruby/blob/trunk/struct.c#L283
     return setup_struct(st, ary);
 }
 
+VALUE
+rb_struct_define_under(VALUE outer, const char *name, ...)
+{
+    va_list ar;
+    VALUE ary;
+    char *mem;
+
+    ary = rb_ary_tmp_new(0);
+
+    va_start(ar, name);
+    while ((mem = va_arg(ar, char*)) != 0) {
+	ID slot = rb_intern(mem);
+	rb_ary_push(ary, ID2SYM(slot));
+    }
+    va_end(ar);
+
+    return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
+}
+
 /*
  *  call-seq:
  *    Struct.new([class_name] [, member_name]+>)                        -> StructClass
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 42347)
+++ ext/etc/etc.c	(revision 42348)
@@ -672,37 +672,38 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L672
     rb_define_module_function(mEtc, "sysconfdir", etc_sysconfdir, 0);
     rb_define_module_function(mEtc, "systmpdir", etc_systmpdir, 0);
 
-    sPasswd =  rb_struct_define(NULL,
-				"name",
+    sPasswd =  rb_struct_define_under(mEtc, "Passwd",
+				      "name",
 #ifdef HAVE_STRUCT_PASSWD_PW_PASSWD
-				"passwd",
+				      "passwd",
 #endif
-				"uid",
-				"gid",
+				      "uid",
+				      "gid",
 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
-				"gecos",
+				      "gecos",
 #endif
-				"dir",
-				"shell",
+				      "dir",
+				      "shell",
 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
-				"change",
+				      "change",
 #endif
 #ifdef HAVE_STRUCT_PASSWD_PW_QUOTA
-				"quota",
+				      "quota",
 #endif
 #ifdef HAVE_STRUCT_PASSWD_PW_AGE
-				"age",
+				      "age",
 #endif
 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
-				"uclass",
+				      "uclass",
 #endif
 #ifdef HAVE_STRUCT_PASSWD_PW_COMMENT
-				"comment",
+				      "comment",
 #endif
 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
-				"expire",
+				      "expire",
 #endif
-				NULL);
+				      NULL);
+#if 0
     /* Define-const: Passwd
      *
      * Passwd is a Struct that contains the following members:
@@ -743,18 +744,19 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L744
      *	    account expiration time(integer) must be compiled with +HAVE_STRUCT_PASSWD_PW_EXPIRE+
      */
     rb_define_const(mEtc, "Passwd", sPasswd);
-    rb_set_class_path(sPasswd, mEtc, "Passwd");
+#endif
     rb_define_const(rb_cStruct, "Passwd", sPasswd); /* deprecated name */
     rb_extend_object(sPasswd, rb_mEnumerable);
     rb_define_singleton_method(sPasswd, "each", etc_each_passwd, 0);
 
 #ifdef HAVE_GETGRENT
-    sGroup = rb_struct_define(NULL, "name",
+    sGroup = rb_struct_define_under(mEtc, "Group", "name",
 #ifdef HAVE_STRUCT_GROUP_GR_PASSWD
-			      "passwd",
+				    "passwd",
 #endif
-			      "gid", "mem", NULL);
+				    "gid", "mem", NULL);
 
+#if 0
     /* Define-const: Group
      *
      * Group is a Struct that is only available when compiled with +HAVE_GETGRENT+.
@@ -777,7 +779,7 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L779
      *	    members of the group.
      */
     rb_define_const(mEtc, "Group", sGroup);
-    rb_set_class_path(sGroup, mEtc, "Group");
+#endif
     rb_define_const(rb_cStruct, "Group", sGroup); /* deprecated name */
     rb_extend_object(sGroup, rb_mEnumerable);
     rb_define_singleton_method(sGroup, "each", etc_each_group, 0);

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

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