add a test for IA64 Debian GNU/Linux Etch.
[ruby-svn.git] / struct.c
blob74d20a426c28d3bea0e87d3ada6edaa23a26eeb0
1 /**********************************************************************
3 struct.c -
5 $Author$
6 created at: Tue Mar 22 18:44:30 JST 1995
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
14 VALUE rb_cStruct;
16 static VALUE struct_alloc(VALUE);
18 VALUE
19 rb_struct_iv_get(VALUE c, const char *name)
21 ID id;
23 id = rb_intern(name);
24 for (;;) {
25 if (rb_ivar_defined(c, id))
26 return rb_ivar_get(c, id);
27 c = RCLASS_SUPER(c);
28 if (c == 0 || c == rb_cStruct)
29 return Qnil;
33 VALUE
34 rb_struct_s_members(VALUE klass)
36 VALUE members = rb_struct_iv_get(klass, "__members__");
38 if (NIL_P(members)) {
39 rb_raise(rb_eTypeError, "uninitialized struct");
41 if (TYPE(members) != T_ARRAY) {
42 rb_raise(rb_eTypeError, "corrupted struct");
44 return members;
47 VALUE
48 rb_struct_members(VALUE s)
50 VALUE members = rb_struct_s_members(rb_obj_class(s));
52 if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
53 rb_raise(rb_eTypeError, "struct size differs (%ld required %ld given)",
54 RARRAY_LEN(members), RSTRUCT_LEN(s));
56 return members;
59 static VALUE
60 rb_struct_s_members_m(VALUE klass)
62 VALUE members, ary;
63 VALUE *p, *pend;
65 members = rb_struct_s_members(klass);
66 ary = rb_ary_new2(RARRAY_LEN(members));
67 p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members);
68 while (p < pend) {
69 rb_ary_push(ary, *p);
70 p++;
73 return ary;
77 * call-seq:
78 * struct.members => array
80 * Returns an array of strings representing the names of the instance
81 * variables.
83 * Customer = Struct.new(:name, :address, :zip)
84 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
85 * joe.members #=> [:name, :address, :zip]
88 static VALUE
89 rb_struct_members_m(VALUE obj)
91 return rb_struct_s_members_m(rb_obj_class(obj));
94 VALUE
95 rb_struct_getmember(VALUE obj, ID id)
97 VALUE members, slot;
98 long i;
100 members = rb_struct_members(obj);
101 slot = ID2SYM(id);
102 for (i=0; i<RARRAY_LEN(members); i++) {
103 if (RARRAY_PTR(members)[i] == slot) {
104 return RSTRUCT_PTR(obj)[i];
107 rb_name_error(id, "%s is not struct member", rb_id2name(id));
108 return Qnil; /* not reached */
111 static VALUE
112 rb_struct_ref(VALUE obj)
114 return rb_struct_getmember(obj, rb_frame_this_func());
117 static VALUE rb_struct_ref0(VALUE obj) {return RSTRUCT_PTR(obj)[0];}
118 static VALUE rb_struct_ref1(VALUE obj) {return RSTRUCT_PTR(obj)[1];}
119 static VALUE rb_struct_ref2(VALUE obj) {return RSTRUCT_PTR(obj)[2];}
120 static VALUE rb_struct_ref3(VALUE obj) {return RSTRUCT_PTR(obj)[3];}
121 static VALUE rb_struct_ref4(VALUE obj) {return RSTRUCT_PTR(obj)[4];}
122 static VALUE rb_struct_ref5(VALUE obj) {return RSTRUCT_PTR(obj)[5];}
123 static VALUE rb_struct_ref6(VALUE obj) {return RSTRUCT_PTR(obj)[6];}
124 static VALUE rb_struct_ref7(VALUE obj) {return RSTRUCT_PTR(obj)[7];}
125 static VALUE rb_struct_ref8(VALUE obj) {return RSTRUCT_PTR(obj)[8];}
126 static VALUE rb_struct_ref9(VALUE obj) {return RSTRUCT_PTR(obj)[9];}
128 #define N_REF_FUNC (sizeof(ref_func) / sizeof(ref_func[0]))
130 static VALUE (*const ref_func[])(VALUE) = {
131 rb_struct_ref0,
132 rb_struct_ref1,
133 rb_struct_ref2,
134 rb_struct_ref3,
135 rb_struct_ref4,
136 rb_struct_ref5,
137 rb_struct_ref6,
138 rb_struct_ref7,
139 rb_struct_ref8,
140 rb_struct_ref9,
143 static void
144 rb_struct_modify(VALUE s)
146 if (OBJ_FROZEN(s)) rb_error_frozen("Struct");
147 if (!OBJ_TAINTED(s) && rb_safe_level() >= 4)
148 rb_raise(rb_eSecurityError, "Insecure: can't modify Struct");
151 static VALUE
152 rb_struct_set(VALUE obj, VALUE val)
154 VALUE members, slot;
155 long i;
157 members = rb_struct_members(obj);
158 rb_struct_modify(obj);
159 for (i=0; i<RARRAY_LEN(members); i++) {
160 slot = RARRAY_PTR(members)[i];
161 if (rb_id_attrset(SYM2ID(slot)) == rb_frame_this_func()) {
162 return RSTRUCT_PTR(obj)[i] = val;
165 rb_name_error(rb_frame_this_func(), "`%s' is not a struct member",
166 rb_id2name(rb_frame_this_func()));
167 return Qnil; /* not reached */
170 static VALUE
171 make_struct(VALUE name, VALUE members, VALUE klass)
173 VALUE nstr;
174 ID id;
175 long i;
177 OBJ_FREEZE(members);
178 if (NIL_P(name)) {
179 nstr = rb_class_new(klass);
180 rb_make_metaclass(nstr, RBASIC(klass)->klass);
181 rb_class_inherited(klass, nstr);
183 else {
184 id = SYM2ID(rb_str_intern(name));
185 if (!rb_is_const_id(id)) {
186 rb_name_error(id, "identifier %s needs to be constant", StringValuePtr(name));
188 if (rb_const_defined_at(klass, id)) {
189 rb_warn("redefining constant Struct::%s", StringValuePtr(name));
190 rb_mod_remove_const(klass, ID2SYM(id));
192 nstr = rb_define_class_under(klass, rb_id2name(id), klass);
194 rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY_LEN(members)));
195 rb_iv_set(nstr, "__members__", members);
197 rb_define_alloc_func(nstr, struct_alloc);
198 rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
199 rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
200 rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
201 for (i=0; i< RARRAY_LEN(members); i++) {
202 ID id = SYM2ID(RARRAY_PTR(members)[i]);
203 if (rb_is_local_id(id) || rb_is_const_id(id)) {
204 if (i < N_REF_FUNC) {
205 rb_define_method_id(nstr, id, ref_func[i], 0);
207 else {
208 rb_define_method_id(nstr, id, rb_struct_ref, 0);
210 rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
214 return nstr;
217 VALUE
218 rb_struct_alloc_noinit(VALUE klass)
220 return struct_alloc(klass);
223 VALUE
224 rb_struct_define_without_accessor(char *class_name, VALUE super, rb_alloc_func_t alloc, ...)
226 VALUE klass;
227 va_list ar;
228 VALUE members;
229 long i;
230 char *name;
232 members = rb_ary_new2(0);
233 va_start(ar, alloc);
234 i = 0;
235 while ((name = va_arg(ar, char*)) != NULL) {
236 rb_ary_push(members, ID2SYM(rb_intern(name)));
238 va_end(ar);
239 OBJ_FREEZE(members);
241 if (class_name) {
242 klass = rb_define_class(class_name, super);
244 else {
245 klass = rb_class_new(super);
246 rb_make_metaclass(klass, RBASIC(super)->klass);
247 rb_class_inherited(super, klass);
250 rb_iv_set(klass, "__size__", LONG2NUM(RARRAY_LEN(members)));
251 rb_iv_set(klass, "__members__", members);
253 if (alloc)
254 rb_define_alloc_func(klass, alloc);
255 else
256 rb_define_alloc_func(klass, struct_alloc);
258 return klass;
261 VALUE
262 rb_struct_define(const char *name, ...)
264 va_list ar;
265 VALUE nm, ary;
266 char *mem;
268 if (!name) nm = Qnil;
269 else nm = rb_str_new2(name);
270 ary = rb_ary_new();
272 va_start(ar, name);
273 while ((mem = va_arg(ar, char*)) != 0) {
274 ID slot = rb_intern(mem);
275 rb_ary_push(ary, ID2SYM(slot));
277 va_end(ar);
279 return make_struct(nm, ary, rb_cStruct);
283 * call-seq:
284 * Struct.new( [aString] [, aSym]+> ) => StructClass
285 * StructClass.new(arg, ...) => obj
286 * StructClass[arg, ...] => obj
288 * Creates a new class, named by <i>aString</i>, containing accessor
289 * methods for the given symbols. If the name <i>aString</i> is
290 * omitted, an anonymous structure class will be created. Otherwise,
291 * the name of this struct will appear as a constant in class
292 * <code>Struct</code>, so it must be unique for all
293 * <code>Struct</code>s in the system and should start with a capital
294 * letter. Assigning a structure class to a constant effectively gives
295 * the class the name of the constant.
297 * <code>Struct::new</code> returns a new <code>Class</code> object,
298 * which can then be used to create specific instances of the new
299 * structure. The number of actual parameters must be
300 * less than or equal to the number of attributes defined for this
301 * class; unset parameters default to \nil{}. Passing too many
302 * parameters will raise an \E{ArgumentError}.
304 * The remaining methods listed in this section (class and instance)
305 * are defined for this generated class.
307 * # Create a structure with a name in Struct
308 * Struct.new("Customer", :name, :address) #=> Struct::Customer
309 * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
311 * # Create a structure named by its constant
312 * Customer = Struct.new(:name, :address) #=> Customer
313 * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
316 static VALUE
317 rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
319 VALUE name, rest;
320 long i;
321 VALUE st;
322 ID id;
324 rb_scan_args(argc, argv, "1*", &name, &rest);
325 for (i=0; i<RARRAY_LEN(rest); i++) {
326 id = rb_to_id(RARRAY_PTR(rest)[i]);
327 RARRAY_PTR(rest)[i] = ID2SYM(id);
329 if (!NIL_P(name) && SYMBOL_P(name)) {
330 rb_ary_unshift(rest, name);
331 name = Qnil;
333 st = make_struct(name, rest, klass);
334 if (rb_block_given_p()) {
335 rb_mod_module_eval(0, 0, st);
338 return st;
344 VALUE
345 rb_struct_initialize(VALUE self, VALUE values)
347 VALUE klass = rb_obj_class(self);
348 VALUE size;
349 long n;
351 rb_struct_modify(self);
352 size = rb_struct_iv_get(klass, "__size__");
353 n = FIX2LONG(size);
354 if (n < RARRAY_LEN(values)) {
355 rb_raise(rb_eArgError, "struct size differs");
357 MEMCPY(RSTRUCT_PTR(self), RARRAY_PTR(values), VALUE, RARRAY_LEN(values));
358 if (n > RARRAY_LEN(values)) {
359 rb_mem_clear(RSTRUCT_PTR(self)+RARRAY_LEN(values),
360 n-RARRAY_LEN(values));
362 return Qnil;
365 static VALUE
366 struct_alloc(VALUE klass)
368 VALUE size;
369 long n;
370 NEWOBJ(st, struct RStruct);
371 OBJSETUP(st, klass, T_STRUCT);
373 size = rb_struct_iv_get(klass, "__size__");
374 n = FIX2LONG(size);
376 if (0 < n && n <= RSTRUCT_EMBED_LEN_MAX) {
377 RBASIC(st)->flags &= ~RSTRUCT_EMBED_LEN_MASK;
378 RBASIC(st)->flags |= n << RSTRUCT_EMBED_LEN_SHIFT;
379 rb_mem_clear(st->as.ary, n);
381 else {
382 st->as.heap.ptr = ALLOC_N(VALUE, n);
383 rb_mem_clear(st->as.heap.ptr, n);
384 st->as.heap.len = n;
387 return (VALUE)st;
390 VALUE
391 rb_struct_alloc(VALUE klass, VALUE values)
393 return rb_class_new_instance(RARRAY_LEN(values), RARRAY_PTR(values), klass);
396 VALUE
397 rb_struct_new(VALUE klass, ...)
399 VALUE sz, *mem;
400 long size, i;
401 va_list args;
403 sz = rb_struct_iv_get(klass, "__size__");
404 size = FIX2LONG(sz);
405 mem = ALLOCA_N(VALUE, size);
406 va_start(args, klass);
407 for (i=0; i<size; i++) {
408 mem[i] = va_arg(args, VALUE);
410 va_end(args);
412 return rb_class_new_instance(size, mem, klass);
416 * call-seq:
417 * struct.each {|obj| block } => struct
419 * Calls <i>block</i> once for each instance variable, passing the
420 * value as a parameter.
422 * Customer = Struct.new(:name, :address, :zip)
423 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
424 * joe.each {|x| puts(x) }
426 * <em>produces:</em>
428 * Joe Smith
429 * 123 Maple, Anytown NC
430 * 12345
433 static VALUE
434 rb_struct_each(VALUE s)
436 long i;
438 RETURN_ENUMERATOR(s, 0, 0);
439 for (i=0; i<RSTRUCT_LEN(s); i++) {
440 rb_yield(RSTRUCT_PTR(s)[i]);
442 return s;
446 * call-seq:
447 * struct.each_pair {|sym, obj| block } => struct
449 * Calls <i>block</i> once for each instance variable, passing the name
450 * (as a symbol) and the value as parameters.
452 * Customer = Struct.new(:name, :address, :zip)
453 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
454 * joe.each_pair {|name, value| puts("#{name} => #{value}") }
456 * <em>produces:</em>
458 * name => Joe Smith
459 * address => 123 Maple, Anytown NC
460 * zip => 12345
463 static VALUE
464 rb_struct_each_pair(VALUE s)
466 VALUE members;
467 long i;
469 RETURN_ENUMERATOR(s, 0, 0);
470 members = rb_struct_members(s);
471 for (i=0; i<RSTRUCT_LEN(s); i++) {
472 rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT_PTR(s)[i]);
474 return s;
477 static VALUE
478 inspect_struct(VALUE s, VALUE dummy, int recur)
480 char *cname = rb_class2name(rb_obj_class(s));
481 VALUE str, members;
482 long i;
484 if (recur) {
485 return rb_sprintf("#<struct %s:...>", cname);
488 members = rb_struct_members(s);
489 if (cname[0] == '#') {
490 str = rb_str_new2("#<struct ");
492 else {
493 str = rb_sprintf("#<struct %s ", cname);
495 for (i=0; i<RSTRUCT_LEN(s); i++) {
496 VALUE slot;
497 ID id;
499 if (i > 0) {
500 rb_str_cat2(str, ", ");
502 slot = RARRAY_PTR(members)[i];
503 id = SYM2ID(slot);
504 if (rb_is_local_id(id) || rb_is_const_id(id)) {
505 rb_str_append(str, rb_id2str(id));
507 else {
508 rb_str_append(str, rb_inspect(slot));
510 rb_str_cat2(str, "=");
511 rb_str_append(str, rb_inspect(RSTRUCT_PTR(s)[i]));
513 rb_str_cat2(str, ">");
514 OBJ_INFECT(str, s);
516 return str;
520 * call-seq:
521 * struct.to_s => string
522 * struct.inspect => string
524 * Describe the contents of this struct in a string.
527 static VALUE
528 rb_struct_inspect(VALUE s)
530 return rb_exec_recursive(inspect_struct, s, 0);
534 * call-seq:
535 * struct.to_a => array
536 * struct.values => array
538 * Returns the values for this instance as an array.
540 * Customer = Struct.new(:name, :address, :zip)
541 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
542 * joe.to_a[1] #=> "123 Maple, Anytown NC"
545 static VALUE
546 rb_struct_to_a(VALUE s)
548 return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_PTR(s));
551 /* :nodoc: */
552 static VALUE
553 rb_struct_init_copy(VALUE copy, VALUE s)
555 if (copy == s) return copy;
556 rb_check_frozen(copy);
557 if (!rb_obj_is_instance_of(s, rb_obj_class(copy))) {
558 rb_raise(rb_eTypeError, "wrong argument class");
560 if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
561 rb_raise(rb_eTypeError, "struct size mismatch");
563 MEMCPY(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), VALUE, RSTRUCT_LEN(copy));
565 return copy;
568 static VALUE
569 rb_struct_aref_id(VALUE s, ID id)
571 VALUE members;
572 long i, len;
574 members = rb_struct_members(s);
575 len = RARRAY_LEN(members);
576 for (i=0; i<len; i++) {
577 if (SYM2ID(RARRAY_PTR(members)[i]) == id) {
578 return RSTRUCT_PTR(s)[i];
581 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
582 return Qnil; /* not reached */
586 * call-seq:
587 * struct[symbol] => anObject
588 * struct[fixnum] => anObject
590 * Attribute Reference---Returns the value of the instance variable
591 * named by <i>symbol</i>, or indexed (0..length-1) by
592 * <i>fixnum</i>. Will raise <code>NameError</code> if the named
593 * variable does not exist, or <code>IndexError</code> if the index is
594 * out of range.
596 * Customer = Struct.new(:name, :address, :zip)
597 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
599 * joe["name"] #=> "Joe Smith"
600 * joe[:name] #=> "Joe Smith"
601 * joe[0] #=> "Joe Smith"
604 VALUE
605 rb_struct_aref(VALUE s, VALUE idx)
607 long i;
609 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
610 return rb_struct_aref_id(s, rb_to_id(idx));
613 i = NUM2LONG(idx);
614 if (i < 0) i = RSTRUCT_LEN(s) + i;
615 if (i < 0)
616 rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
617 i, RSTRUCT_LEN(s));
618 if (RSTRUCT_LEN(s) <= i)
619 rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
620 i, RSTRUCT_LEN(s));
621 return RSTRUCT_PTR(s)[i];
624 static VALUE
625 rb_struct_aset_id(VALUE s, ID id, VALUE val)
627 VALUE members;
628 long i, len;
630 members = rb_struct_members(s);
631 rb_struct_modify(s);
632 len = RARRAY_LEN(members);
633 if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
634 rb_raise(rb_eTypeError, "struct size differs (%ld required %ld given)",
635 RARRAY_LEN(members), RSTRUCT_LEN(s));
637 for (i=0; i<len; i++) {
638 if (SYM2ID(RARRAY_PTR(members)[i]) == id) {
639 RSTRUCT_PTR(s)[i] = val;
640 return val;
643 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
647 * call-seq:
648 * struct[symbol] = obj => obj
649 * struct[fixnum] = obj => obj
651 * Attribute Assignment---Assigns to the instance variable named by
652 * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
653 * returns it. Will raise a <code>NameError</code> if the named
654 * variable does not exist, or an <code>IndexError</code> if the index
655 * is out of range.
657 * Customer = Struct.new(:name, :address, :zip)
658 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
660 * joe["name"] = "Luke"
661 * joe[:zip] = "90210"
663 * joe.name #=> "Luke"
664 * joe.zip #=> "90210"
667 VALUE
668 rb_struct_aset(VALUE s, VALUE idx, VALUE val)
670 long i;
672 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
673 return rb_struct_aset_id(s, rb_to_id(idx), val);
676 i = NUM2LONG(idx);
677 if (i < 0) i = RSTRUCT_LEN(s) + i;
678 if (i < 0) {
679 rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
680 i, RSTRUCT_LEN(s));
682 if (RSTRUCT_LEN(s) <= i) {
683 rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
684 i, RSTRUCT_LEN(s));
686 rb_struct_modify(s);
687 return RSTRUCT_PTR(s)[i] = val;
690 static VALUE
691 struct_entry(VALUE s, long n)
693 return rb_struct_aref(s, LONG2NUM(n));
697 * call-seq:
698 * struct.values_at(selector,... ) => an_array
700 * Returns an array containing the elements in
701 * _self_ corresponding to the given selector(s). The selectors
702 * may be either integer indices or ranges.
703 * See also </code>.select<code>.
705 * a = %w{ a b c d e f }
706 * a.values_at(1, 3, 5)
707 * a.values_at(1, 3, 5, 7)
708 * a.values_at(-1, -3, -5, -7)
709 * a.values_at(1..3, 2...5)
712 static VALUE
713 rb_struct_values_at(int argc, VALUE *argv, VALUE s)
715 return rb_get_values_at(s, RSTRUCT_LEN(s), argc, argv, struct_entry);
719 * call-seq:
720 * struct.select {|i| block } => array
722 * Invokes the block passing in successive elements from
723 * <i>struct</i>, returning an array containing those elements
724 * for which the block returns a true value (equivalent to
725 * <code>Enumerable#select</code>).
727 * Lots = Struct.new(:a, :b, :c, :d, :e, :f)
728 * l = Lots.new(11, 22, 33, 44, 55, 66)
729 * l.select {|v| (v % 2).zero? } #=> [22, 44, 66]
732 static VALUE
733 rb_struct_select(int argc, VALUE *argv, VALUE s)
735 VALUE result;
736 long i;
738 if (argc > 0) {
739 rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
741 result = rb_ary_new();
742 for (i = 0; i < RSTRUCT_LEN(s); i++) {
743 if (RTEST(rb_yield(RSTRUCT_PTR(s)[i]))) {
744 rb_ary_push(result, RSTRUCT_PTR(s)[i]);
748 return result;
752 * call-seq:
753 * struct == other_struct => true or false
755 * Equality---Returns <code>true</code> if <i>other_struct</i> is
756 * equal to this one: they must be of the same class as generated by
757 * <code>Struct::new</code>, and the values of all instance variables
758 * must be equal (according to <code>Object#==</code>).
760 * Customer = Struct.new(:name, :address, :zip)
761 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
762 * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
763 * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
764 * joe == joejr #=> true
765 * joe == jane #=> false
768 static VALUE
769 rb_struct_equal(VALUE s, VALUE s2)
771 long i;
773 if (s == s2) return Qtrue;
774 if (TYPE(s2) != T_STRUCT) return Qfalse;
775 if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
776 if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
777 rb_bug("inconsistent struct"); /* should never happen */
780 for (i=0; i<RSTRUCT_LEN(s); i++) {
781 if (!rb_equal(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
783 return Qtrue;
787 * call-seq:
788 * struct.hash => fixnum
790 * Return a hash value based on this struct's contents.
793 static VALUE
794 rb_struct_hash(VALUE s)
796 long i, h;
797 VALUE n;
799 h = rb_hash(rb_obj_class(s));
800 for (i = 0; i < RSTRUCT_LEN(s); i++) {
801 h = (h << 1) | (h<0 ? 1 : 0);
802 n = rb_hash(RSTRUCT_PTR(s)[i]);
803 h ^= NUM2LONG(n);
805 return LONG2FIX(h);
809 * code-seq:
810 * struct.eql?(other) => true or false
812 * Two structures are equal if they are the same object, or if all their
813 * fields are equal (using <code>eql?</code>).
816 static VALUE
817 rb_struct_eql(VALUE s, VALUE s2)
819 long i;
821 if (s == s2) return Qtrue;
822 if (TYPE(s2) != T_STRUCT) return Qfalse;
823 if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
824 if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
825 rb_bug("inconsistent struct"); /* should never happen */
828 for (i=0; i<RSTRUCT_LEN(s); i++) {
829 if (!rb_eql(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
831 return Qtrue;
835 * call-seq:
836 * struct.length => fixnum
837 * struct.size => fixnum
839 * Returns the number of instance variables.
841 * Customer = Struct.new(:name, :address, :zip)
842 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
843 * joe.length #=> 3
846 static VALUE
847 rb_struct_size(VALUE s)
849 return LONG2FIX(RSTRUCT_LEN(s));
853 * A <code>Struct</code> is a convenient way to bundle a number of
854 * attributes together, using accessor methods, without having to write
855 * an explicit class.
857 * The <code>Struct</code> class is a generator of specific classes,
858 * each one of which is defined to hold a set of variables and their
859 * accessors. In these examples, we'll call the generated class
860 * ``<i>Customer</i>Class,'' and we'll show an example instance of that
861 * class as ``<i>Customer</i>Inst.''
863 * In the descriptions that follow, the parameter <i>symbol</i> refers
864 * to a symbol, which is either a quoted string or a
865 * <code>Symbol</code> (such as <code>:name</code>).
867 void
868 Init_Struct(void)
870 rb_cStruct = rb_define_class("Struct", rb_cObject);
871 rb_include_module(rb_cStruct, rb_mEnumerable);
873 rb_undef_alloc_func(rb_cStruct);
874 rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1);
876 rb_define_method(rb_cStruct, "initialize", rb_struct_initialize, -2);
877 rb_define_method(rb_cStruct, "initialize_copy", rb_struct_init_copy, 1);
879 rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
880 rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
881 rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
883 rb_define_method(rb_cStruct, "to_s", rb_struct_inspect, 0);
884 rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
885 rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
886 rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);
887 rb_define_method(rb_cStruct, "size", rb_struct_size, 0);
888 rb_define_method(rb_cStruct, "length", rb_struct_size, 0);
890 rb_define_method(rb_cStruct, "each", rb_struct_each, 0);
891 rb_define_method(rb_cStruct, "each_pair", rb_struct_each_pair, 0);
892 rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
893 rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
894 rb_define_method(rb_cStruct, "select", rb_struct_select, -1);
895 rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
897 rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);