OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 rb_raise(rb_eArgError, "No value provided to setter."); | 171 rb_raise(rb_eArgError, "No value provided to setter."); |
172 } | 172 } |
173 layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); | 173 layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); |
174 return Qnil; | 174 return Qnil; |
175 } else { | 175 } else { |
176 return layout_get(self->descriptor->layout, Message_data(self), f); | 176 return layout_get(self->descriptor->layout, Message_data(self), f); |
177 } | 177 } |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) { | |
182 MessageHeader* self; | |
183 VALUE method_name, method_str; | |
184 char* name; | |
185 size_t name_len; | |
186 bool setter; | |
187 const upb_oneofdef* o; | |
188 const upb_fielddef* f; | |
189 | |
190 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); | |
191 if (argc < 1) { | |
192 rb_raise(rb_eArgError, "Expected method name as first argument."); | |
193 } | |
194 method_name = argv[0]; | |
195 if (!SYMBOL_P(method_name)) { | |
196 rb_raise(rb_eArgError, "Expected symbol as method name."); | |
197 } | |
198 method_str = rb_id2str(SYM2ID(method_name)); | |
199 name = RSTRING_PTR(method_str); | |
200 name_len = RSTRING_LEN(method_str); | |
201 setter = false; | |
202 | |
203 // Setters have names that end in '='. | |
204 if (name[name_len - 1] == '=') { | |
205 setter = true; | |
206 name_len--; | |
207 } | |
208 | |
209 // See if this name corresponds to either a oneof or field in this message. | |
210 if (!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len, &f, | |
211 &o)) { | |
212 return rb_call_super(argc, argv); | |
213 } | |
214 if (o != NULL) { | |
215 return setter ? Qfalse : Qtrue; | |
216 } | |
217 return Qtrue; | |
218 } | |
219 | |
220 int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { | 181 int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { |
221 MessageHeader* self; | 182 MessageHeader* self; |
222 VALUE method_str; | 183 VALUE method_str; |
223 char* name; | 184 char* name; |
224 const upb_fielddef* f; | 185 const upb_fielddef* f; |
225 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); | 186 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); |
226 | 187 |
227 if (!SYMBOL_P(key)) { | 188 if (!SYMBOL_P(key)) { |
228 rb_raise(rb_eArgError, | 189 rb_raise(rb_eArgError, |
229 "Expected symbols as hash keys in initialization map."); | 190 "Expected symbols as hash keys in initialization map."); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 * Message.==(other) => boolean | 298 * Message.==(other) => boolean |
338 * | 299 * |
339 * Performs a deep comparison of this message with another. Messages are equal | 300 * Performs a deep comparison of this message with another. Messages are equal |
340 * if they have the same type and if each field is equal according to the :== | 301 * if they have the same type and if each field is equal according to the :== |
341 * method's semantics (a more efficient comparison may actually be done if the | 302 * method's semantics (a more efficient comparison may actually be done if the |
342 * field is of a primitive type). | 303 * field is of a primitive type). |
343 */ | 304 */ |
344 VALUE Message_eq(VALUE _self, VALUE _other) { | 305 VALUE Message_eq(VALUE _self, VALUE _other) { |
345 MessageHeader* self; | 306 MessageHeader* self; |
346 MessageHeader* other; | 307 MessageHeader* other; |
347 if (TYPE(_self) != TYPE(_other)) { | |
348 return Qfalse; | |
349 } | |
350 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); | 308 TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); |
351 TypedData_Get_Struct(_other, MessageHeader, &Message_type, other); | 309 TypedData_Get_Struct(_other, MessageHeader, &Message_type, other); |
352 | 310 |
353 if (self->descriptor != other->descriptor) { | 311 if (self->descriptor != other->descriptor) { |
354 return Qfalse; | 312 return Qfalse; |
355 } | 313 } |
356 | 314 |
357 return layout_eq(self->descriptor->layout, | 315 return layout_eq(self->descriptor->layout, |
358 Message_data(self), | 316 Message_data(self), |
359 Message_data(other)); | 317 Message_data(other)); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 rb_ivar_set(klass, descriptor_instancevar_interned, | 452 rb_ivar_set(klass, descriptor_instancevar_interned, |
495 get_def_obj(desc->msgdef)); | 453 get_def_obj(desc->msgdef)); |
496 rb_define_alloc_func(klass, Message_alloc); | 454 rb_define_alloc_func(klass, Message_alloc); |
497 rb_require("google/protobuf/message_exts"); | 455 rb_require("google/protobuf/message_exts"); |
498 rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts")); | 456 rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts")); |
499 rb_extend_object( | 457 rb_extend_object( |
500 klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods")); | 458 klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods")); |
501 | 459 |
502 rb_define_method(klass, "method_missing", | 460 rb_define_method(klass, "method_missing", |
503 Message_method_missing, -1); | 461 Message_method_missing, -1); |
504 rb_define_method(klass, "respond_to_missing?", | |
505 Message_respond_to_missing, -1); | |
506 rb_define_method(klass, "initialize", Message_initialize, -1); | 462 rb_define_method(klass, "initialize", Message_initialize, -1); |
507 rb_define_method(klass, "dup", Message_dup, 0); | 463 rb_define_method(klass, "dup", Message_dup, 0); |
508 // Also define #clone so that we don't inherit Object#clone. | 464 // Also define #clone so that we don't inherit Object#clone. |
509 rb_define_method(klass, "clone", Message_dup, 0); | 465 rb_define_method(klass, "clone", Message_dup, 0); |
510 rb_define_method(klass, "==", Message_eq, 1); | 466 rb_define_method(klass, "==", Message_eq, 1); |
511 rb_define_method(klass, "hash", Message_hash, 0); | 467 rb_define_method(klass, "hash", Message_hash, 0); |
512 rb_define_method(klass, "to_h", Message_to_h, 0); | 468 rb_define_method(klass, "to_h", Message_to_h, 0); |
513 rb_define_method(klass, "to_hash", Message_to_h, 0); | 469 rb_define_method(klass, "to_hash", Message_to_h, 0); |
514 rb_define_method(klass, "inspect", Message_inspect, 0); | 470 rb_define_method(klass, "inspect", Message_inspect, 0); |
515 rb_define_method(klass, "[]", Message_index, 1); | 471 rb_define_method(klass, "[]", Message_index, 1); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) { | 569 VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) { |
614 VALUE klass = CLASS_OF(obj); | 570 VALUE klass = CLASS_OF(obj); |
615 if (klass == cRepeatedField) { | 571 if (klass == cRepeatedField) { |
616 return RepeatedField_deep_copy(obj); | 572 return RepeatedField_deep_copy(obj); |
617 } else if (klass == cMap) { | 573 } else if (klass == cMap) { |
618 return Map_deep_copy(obj); | 574 return Map_deep_copy(obj); |
619 } else { | 575 } else { |
620 return Message_deep_copy(obj); | 576 return Message_deep_copy(obj); |
621 } | 577 } |
622 } | 578 } |
OLD | NEW |