repeated_field.c 20 KB

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