repeated_field.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. // -----------------------------------------------------------------------------
  32. // Repeated field container type.
  33. // -----------------------------------------------------------------------------
  34. const rb_data_type_t RepeatedField_type = {
  35. "Google::Protobuf::RepeatedField",
  36. { RepeatedField_mark, RepeatedField_free, NULL },
  37. };
  38. VALUE cRepeatedField;
  39. RepeatedField* ruby_to_RepeatedField(VALUE _self) {
  40. RepeatedField* self;
  41. TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
  42. return self;
  43. }
  44. /*
  45. * call-seq:
  46. * RepeatedField.each(&block)
  47. *
  48. * Invokes the block once for each element of the repeated field. RepeatedField
  49. * also includes Enumerable; combined with this method, the repeated field thus
  50. * acts like an ordinary Ruby sequence.
  51. */
  52. VALUE RepeatedField_each(VALUE _self) {
  53. RepeatedField* self = ruby_to_RepeatedField(_self);
  54. upb_fieldtype_t field_type = self->field_type;
  55. VALUE field_type_class = self->field_type_class;
  56. int element_size = native_slot_size(field_type);
  57. size_t off = 0;
  58. for (int i = 0; i < self->size; i++, off += element_size) {
  59. void* memory = (void *) (((uint8_t *)self->elements) + off);
  60. VALUE val = native_slot_get(field_type, field_type_class, memory);
  61. rb_yield(val);
  62. }
  63. return Qnil;
  64. }
  65. /*
  66. * call-seq:
  67. * RepeatedField.[](index) => value
  68. *
  69. * Accesses the element at the given index. Throws an exception on out-of-bounds
  70. * errors.
  71. */
  72. VALUE RepeatedField_index(VALUE _self, VALUE _index) {
  73. RepeatedField* self = ruby_to_RepeatedField(_self);
  74. int element_size = native_slot_size(self->field_type);
  75. upb_fieldtype_t field_type = self->field_type;
  76. VALUE field_type_class = self->field_type_class;
  77. int index = NUM2INT(_index);
  78. if (index < 0 || index >= self->size) {
  79. rb_raise(rb_eRangeError, "Index out of range");
  80. }
  81. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  82. return native_slot_get(field_type, field_type_class, memory);
  83. }
  84. /*
  85. * call-seq:
  86. * RepeatedField.[]=(index, value)
  87. *
  88. * Sets the element at the given index. On out-of-bounds assignments, extends
  89. * the array and fills the hole (if any) with default values.
  90. */
  91. VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
  92. RepeatedField* self = ruby_to_RepeatedField(_self);
  93. upb_fieldtype_t field_type = self->field_type;
  94. VALUE field_type_class = self->field_type_class;
  95. int element_size = native_slot_size(field_type);
  96. int index = NUM2INT(_index);
  97. if (index < 0 || index >= (INT_MAX - 1)) {
  98. rb_raise(rb_eRangeError, "Index out of range");
  99. }
  100. if (index >= self->size) {
  101. RepeatedField_reserve(self, index + 1);
  102. upb_fieldtype_t field_type = self->field_type;
  103. int element_size = native_slot_size(field_type);
  104. for (int i = self->size; i <= index; i++) {
  105. void* elem = (void *)(((uint8_t *)self->elements) + i * element_size);
  106. native_slot_init(field_type, elem);
  107. }
  108. self->size = index + 1;
  109. }
  110. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  111. native_slot_set(field_type, field_type_class, memory, val);
  112. return Qnil;
  113. }
  114. static int kInitialSize = 8;
  115. void RepeatedField_reserve(RepeatedField* self, int new_size) {
  116. if (new_size <= self->capacity) {
  117. return;
  118. }
  119. if (self->capacity == 0) {
  120. self->capacity = kInitialSize;
  121. }
  122. while (self->capacity < new_size) {
  123. self->capacity *= 2;
  124. }
  125. void* old_elems = self->elements;
  126. int elem_size = native_slot_size(self->field_type);
  127. self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
  128. if (old_elems != NULL) {
  129. memcpy(self->elements, old_elems, self->size * elem_size);
  130. xfree(old_elems);
  131. }
  132. }
  133. /*
  134. * call-seq:
  135. * RepeatedField.push(value)
  136. *
  137. * Adds a new element to the repeated field.
  138. */
  139. VALUE RepeatedField_push(VALUE _self, VALUE val) {
  140. RepeatedField* self = ruby_to_RepeatedField(_self);
  141. upb_fieldtype_t field_type = self->field_type;
  142. int element_size = native_slot_size(field_type);
  143. RepeatedField_reserve(self, self->size + 1);
  144. int index = self->size;
  145. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  146. native_slot_set(field_type, self->field_type_class, memory, val);
  147. // native_slot_set may raise an error; bump index only after set.
  148. self->size++;
  149. return _self;
  150. }
  151. // Used by parsing handlers.
  152. void RepeatedField_push_native(VALUE _self, void* data) {
  153. RepeatedField* self = ruby_to_RepeatedField(_self);
  154. upb_fieldtype_t field_type = self->field_type;
  155. int element_size = native_slot_size(field_type);
  156. RepeatedField_reserve(self, self->size + 1);
  157. int index = self->size;
  158. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  159. memcpy(memory, data, element_size);
  160. self->size++;
  161. }
  162. void* RepeatedField_index_native(VALUE _self, int index) {
  163. RepeatedField* self = ruby_to_RepeatedField(_self);
  164. upb_fieldtype_t field_type = self->field_type;
  165. int element_size = native_slot_size(field_type);
  166. return ((uint8_t *)self->elements) + index * element_size;
  167. }
  168. /*
  169. * call-seq:
  170. * RepeatedField.pop => value
  171. *
  172. * Removes the last element and returns it. Throws an exception if the repeated
  173. * field is empty.
  174. */
  175. VALUE RepeatedField_pop(VALUE _self) {
  176. RepeatedField* self = ruby_to_RepeatedField(_self);
  177. upb_fieldtype_t field_type = self->field_type;
  178. VALUE field_type_class = self->field_type_class;
  179. int element_size = native_slot_size(field_type);
  180. if (self->size == 0) {
  181. rb_raise(rb_eRangeError, "Pop from empty repeated field is not allowed.");
  182. }
  183. int index = self->size - 1;
  184. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  185. VALUE ret = native_slot_get(field_type, field_type_class, memory);
  186. self->size--;
  187. return ret;
  188. }
  189. /*
  190. * call-seq:
  191. * RepeatedField.insert(*args)
  192. *
  193. * Pushes each arg in turn onto the end of the repeated field.
  194. */
  195. VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self) {
  196. for (int i = 0; i < argc; i++) {
  197. RepeatedField_push(_self, argv[i]);
  198. }
  199. return Qnil;
  200. }
  201. /*
  202. * call-seq:
  203. * RepeatedField.replace(list)
  204. *
  205. * Replaces the contents of the repeated field with the given list of elements.
  206. */
  207. VALUE RepeatedField_replace(VALUE _self, VALUE list) {
  208. RepeatedField* self = ruby_to_RepeatedField(_self);
  209. Check_Type(list, T_ARRAY);
  210. self->size = 0;
  211. for (int i = 0; i < RARRAY_LEN(list); i++) {
  212. RepeatedField_push(_self, rb_ary_entry(list, i));
  213. }
  214. return Qnil;
  215. }
  216. /*
  217. * call-seq:
  218. * RepeatedField.clear
  219. *
  220. * Clears (removes all elements from) this repeated field.
  221. */
  222. VALUE RepeatedField_clear(VALUE _self) {
  223. RepeatedField* self = ruby_to_RepeatedField(_self);
  224. self->size = 0;
  225. return Qnil;
  226. }
  227. /*
  228. * call-seq:
  229. * RepeatedField.length
  230. *
  231. * Returns the length of this repeated field.
  232. */
  233. VALUE RepeatedField_length(VALUE _self) {
  234. RepeatedField* self = ruby_to_RepeatedField(_self);
  235. return INT2NUM(self->size);
  236. }
  237. static VALUE RepeatedField_new_this_type(VALUE _self) {
  238. RepeatedField* self = ruby_to_RepeatedField(_self);
  239. VALUE new_rptfield = Qnil;
  240. VALUE element_type = fieldtype_to_ruby(self->field_type);
  241. if (self->field_type_class != Qnil) {
  242. new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
  243. element_type, self->field_type_class);
  244. } else {
  245. new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
  246. element_type);
  247. }
  248. return new_rptfield;
  249. }
  250. /*
  251. * call-seq:
  252. * RepeatedField.dup => repeated_field
  253. *
  254. * Duplicates this repeated field with a shallow copy. References to all
  255. * non-primitive element objects (e.g., submessages) are shared.
  256. */
  257. VALUE RepeatedField_dup(VALUE _self) {
  258. RepeatedField* self = ruby_to_RepeatedField(_self);
  259. VALUE new_rptfield = RepeatedField_new_this_type(_self);
  260. RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  261. RepeatedField_reserve(new_rptfield_self, self->size);
  262. upb_fieldtype_t field_type = self->field_type;
  263. size_t elem_size = native_slot_size(field_type);
  264. size_t off = 0;
  265. for (int i = 0; i < self->size; i++, off += elem_size) {
  266. void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
  267. void* from_mem = (uint8_t *)self->elements + off;
  268. native_slot_dup(field_type, to_mem, from_mem);
  269. new_rptfield_self->size++;
  270. }
  271. return new_rptfield;
  272. }
  273. // Internal only: used by Google::Protobuf.deep_copy.
  274. VALUE RepeatedField_deep_copy(VALUE _self) {
  275. RepeatedField* self = ruby_to_RepeatedField(_self);
  276. VALUE new_rptfield = RepeatedField_new_this_type(_self);
  277. RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  278. RepeatedField_reserve(new_rptfield_self, self->size);
  279. upb_fieldtype_t field_type = self->field_type;
  280. size_t elem_size = native_slot_size(field_type);
  281. size_t off = 0;
  282. for (int i = 0; i < self->size; i++, off += elem_size) {
  283. void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
  284. void* from_mem = (uint8_t *)self->elements + off;
  285. native_slot_deep_copy(field_type, to_mem, from_mem);
  286. new_rptfield_self->size++;
  287. }
  288. return new_rptfield;
  289. }
  290. /*
  291. * call-seq:
  292. * RepeatedField.to_ary => array
  293. *
  294. * Used when converted implicitly into array, e.g. compared to an Array.
  295. * Also called as a fallback of Object#to_a
  296. */
  297. VALUE RepeatedField_to_ary(VALUE _self) {
  298. RepeatedField* self = ruby_to_RepeatedField(_self);
  299. upb_fieldtype_t field_type = self->field_type;
  300. size_t elem_size = native_slot_size(field_type);
  301. size_t off = 0;
  302. VALUE ary = rb_ary_new2(self->size);
  303. for (int i = 0; i < self->size; i++, off += elem_size) {
  304. void* mem = ((uint8_t *)self->elements) + off;
  305. VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
  306. rb_ary_push(ary, elem);
  307. }
  308. return ary;
  309. }
  310. /*
  311. * call-seq:
  312. * RepeatedField.==(other) => boolean
  313. *
  314. * Compares this repeated field to another. Repeated fields are equal if their
  315. * element types are equal, their lengths are equal, and each element is equal.
  316. * Elements are compared as per normal Ruby semantics, by calling their :==
  317. * methods (or performing a more efficient comparison for primitive types).
  318. *
  319. * Repeated fields with dissimilar element types are never equal, even if value
  320. * comparison (for example, between integers and floats) would have otherwise
  321. * indicated that every element has equal value.
  322. */
  323. VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
  324. if (_self == _other) {
  325. return Qtrue;
  326. }
  327. RepeatedField* self = ruby_to_RepeatedField(_self);
  328. if (TYPE(_other) == T_ARRAY) {
  329. VALUE self_ary = RepeatedField_to_ary(_self);
  330. return rb_equal(self_ary, _other);
  331. }
  332. RepeatedField* other = ruby_to_RepeatedField(_other);
  333. if (self->field_type != other->field_type ||
  334. self->field_type_class != other->field_type_class ||
  335. self->size != other->size) {
  336. return Qfalse;
  337. }
  338. upb_fieldtype_t field_type = self->field_type;
  339. size_t elem_size = native_slot_size(field_type);
  340. size_t off = 0;
  341. for (int i = 0; i < self->size; i++, off += elem_size) {
  342. void* self_mem = ((uint8_t *)self->elements) + off;
  343. void* other_mem = ((uint8_t *)other->elements) + off;
  344. if (!native_slot_eq(field_type, self_mem, other_mem)) {
  345. return Qfalse;
  346. }
  347. }
  348. return Qtrue;
  349. }
  350. /*
  351. * call-seq:
  352. * RepeatedField.hash => hash_value
  353. *
  354. * Returns a hash value computed from this repeated field's elements.
  355. */
  356. VALUE RepeatedField_hash(VALUE _self) {
  357. RepeatedField* self = ruby_to_RepeatedField(_self);
  358. VALUE hash = LL2NUM(0);
  359. upb_fieldtype_t field_type = self->field_type;
  360. VALUE field_type_class = self->field_type_class;
  361. size_t elem_size = native_slot_size(field_type);
  362. size_t off = 0;
  363. for (int i = 0; i < self->size; i++, off += elem_size) {
  364. void* mem = ((uint8_t *)self->elements) + off;
  365. VALUE elem = native_slot_get(field_type, field_type_class, mem);
  366. hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2));
  367. hash = rb_funcall(hash, rb_intern("^"), 1,
  368. rb_funcall(elem, rb_intern("hash"), 0));
  369. }
  370. return hash;
  371. }
  372. /*
  373. * call-seq:
  374. * RepeatedField.inspect => string
  375. *
  376. * Returns a string representing this repeated field's elements. It will be
  377. * formated as "[<element>, <element>, ...]", with each element's string
  378. * representation computed by its own #inspect method.
  379. */
  380. VALUE RepeatedField_inspect(VALUE _self) {
  381. VALUE self_ary = RepeatedField_to_ary(_self);
  382. return rb_funcall(self_ary, rb_intern("inspect"), 0);
  383. }
  384. /*
  385. * call-seq:
  386. * RepeatedField.+(other) => repeated field
  387. *
  388. * Returns a new repeated field that contains the concatenated list of this
  389. * repeated field's elements and other's elements. The other (second) list may
  390. * be either another repeated field or a Ruby array.
  391. */
  392. VALUE RepeatedField_plus(VALUE _self, VALUE list) {
  393. VALUE dupped = RepeatedField_dup(_self);
  394. if (TYPE(list) == T_ARRAY) {
  395. for (int i = 0; i < RARRAY_LEN(list); i++) {
  396. VALUE elem = rb_ary_entry(list, i);
  397. RepeatedField_push(dupped, elem);
  398. }
  399. } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
  400. RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
  401. RepeatedField* self = ruby_to_RepeatedField(_self);
  402. RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
  403. if (self->field_type != list_rptfield->field_type ||
  404. self->field_type_class != list_rptfield->field_type_class) {
  405. rb_raise(rb_eArgError,
  406. "Attempt to append RepeatedField with different element type.");
  407. }
  408. for (int i = 0; i < list_rptfield->size; i++) {
  409. void* mem = RepeatedField_index_native(list, i);
  410. RepeatedField_push_native(dupped, mem);
  411. }
  412. } else {
  413. rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
  414. }
  415. return dupped;
  416. }
  417. void validate_type_class(upb_fieldtype_t type, VALUE klass) {
  418. if (rb_iv_get(klass, kDescriptorInstanceVar) == Qnil) {
  419. rb_raise(rb_eArgError,
  420. "Type class has no descriptor. Please pass a "
  421. "class or enum as returned by the DescriptorPool.");
  422. }
  423. if (type == UPB_TYPE_MESSAGE) {
  424. VALUE desc = rb_iv_get(klass, kDescriptorInstanceVar);
  425. if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
  426. RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
  427. rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
  428. }
  429. if (rb_get_alloc_func(klass) != &Message_alloc) {
  430. rb_raise(rb_eArgError,
  431. "Message class was not returned by the DescriptorPool.");
  432. }
  433. } else if (type == UPB_TYPE_ENUM) {
  434. VALUE enumdesc = rb_iv_get(klass, kDescriptorInstanceVar);
  435. if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
  436. RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
  437. rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
  438. }
  439. }
  440. }
  441. void RepeatedField_init_args(int argc, VALUE* argv,
  442. VALUE _self) {
  443. RepeatedField* self = ruby_to_RepeatedField(_self);
  444. VALUE ary = Qnil;
  445. if (argc < 1) {
  446. rb_raise(rb_eArgError, "Expected at least 1 argument.");
  447. }
  448. self->field_type = ruby_to_fieldtype(argv[0]);
  449. if (self->field_type == UPB_TYPE_MESSAGE ||
  450. self->field_type == UPB_TYPE_ENUM) {
  451. if (argc < 2) {
  452. rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
  453. }
  454. self->field_type_class = argv[1];
  455. if (argc > 2) {
  456. ary = argv[2];
  457. }
  458. validate_type_class(self->field_type, self->field_type_class);
  459. } else {
  460. if (argc > 2) {
  461. rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
  462. }
  463. if (argc > 1) {
  464. ary = argv[1];
  465. }
  466. }
  467. if (ary != Qnil) {
  468. if (!RB_TYPE_P(ary, T_ARRAY)) {
  469. rb_raise(rb_eArgError, "Expected array as initialize argument");
  470. }
  471. for (int i = 0; i < RARRAY_LEN(ary); i++) {
  472. RepeatedField_push(_self, rb_ary_entry(ary, i));
  473. }
  474. }
  475. }
  476. // Mark, free, alloc, init and class setup functions.
  477. void RepeatedField_mark(void* _self) {
  478. RepeatedField* self = (RepeatedField*)_self;
  479. rb_gc_mark(self->field_type_class);
  480. upb_fieldtype_t field_type = self->field_type;
  481. int element_size = native_slot_size(field_type);
  482. for (int i = 0; i < self->size; i++) {
  483. void* memory = (((uint8_t *)self->elements) + i * element_size);
  484. native_slot_mark(self->field_type, memory);
  485. }
  486. }
  487. void RepeatedField_free(void* _self) {
  488. RepeatedField* self = (RepeatedField*)_self;
  489. xfree(self->elements);
  490. xfree(self);
  491. }
  492. /*
  493. * call-seq:
  494. * RepeatedField.new(type, type_class = nil, initial_elems = [])
  495. *
  496. * Creates a new repeated field. The provided type must be a Ruby symbol, and
  497. * can take on the same values as those accepted by FieldDescriptor#type=. If
  498. * the type is :message or :enum, type_class must be non-nil, and must be the
  499. * Ruby class or module returned by Descriptor#msgclass or
  500. * EnumDescriptor#enummodule, respectively. An initial list of elements may also
  501. * be provided.
  502. */
  503. VALUE RepeatedField_alloc(VALUE klass) {
  504. RepeatedField* self = ALLOC(RepeatedField);
  505. self->elements = NULL;
  506. self->size = 0;
  507. self->capacity = 0;
  508. self->field_type = -1;
  509. self->field_type_class = Qnil;
  510. VALUE ret = TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
  511. return ret;
  512. }
  513. VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
  514. RepeatedField_init_args(argc, argv, self);
  515. return Qnil;
  516. }
  517. void RepeatedField_register(VALUE module) {
  518. VALUE klass = rb_define_class_under(
  519. module, "RepeatedField", rb_cObject);
  520. rb_define_alloc_func(klass, RepeatedField_alloc);
  521. cRepeatedField = klass;
  522. rb_gc_register_address(&cRepeatedField);
  523. rb_define_method(klass, "initialize",
  524. RepeatedField_init, -1);
  525. rb_define_method(klass, "each", RepeatedField_each, 0);
  526. rb_define_method(klass, "[]", RepeatedField_index, 1);
  527. rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
  528. rb_define_method(klass, "push", RepeatedField_push, 1);
  529. rb_define_method(klass, "<<", RepeatedField_push, 1);
  530. rb_define_method(klass, "pop", RepeatedField_pop, 0);
  531. rb_define_method(klass, "insert", RepeatedField_insert, -1);
  532. rb_define_method(klass, "replace", RepeatedField_replace, 1);
  533. rb_define_method(klass, "clear", RepeatedField_clear, 0);
  534. rb_define_method(klass, "length", RepeatedField_length, 0);
  535. rb_define_method(klass, "dup", RepeatedField_dup, 0);
  536. // Also define #clone so that we don't inherit Object#clone.
  537. rb_define_method(klass, "clone", RepeatedField_dup, 0);
  538. rb_define_method(klass, "==", RepeatedField_eq, 1);
  539. rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
  540. rb_define_method(klass, "hash", RepeatedField_hash, 0);
  541. rb_define_method(klass, "inspect", RepeatedField_inspect, 0);
  542. rb_define_method(klass, "+", RepeatedField_plus, 1);
  543. rb_include_module(klass, rb_mEnumerable);
  544. }