Class: Google::Protobuf::RepeatedField
- Inherits:
-
Object
- Object
- Google::Protobuf::RepeatedField
show all
- Extended by:
- Forwardable, Internal::Convert
- Includes:
- Enumerable, Internal::Convert
- Defined in:
- lib/google/protobuf/repeated_field.rb,
lib/google/protobuf/ffi/repeated_field.rb
Defined Under Namespace
Classes: ProxyingEnumerator
Class Method Summary
collapse
Instance Method Summary
collapse
convert_ruby_to_upb, convert_upb_to_ruby, map_create_hash, message_value_deep_copy, repeated_field_create_array, scalar_create_hash, to_h_internal
Class Method Details
.define_array_wrapper_method(method_name) ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/google/protobuf/repeated_field.rb', line 103
def define_array_wrapper_method(method_name)
define_method(method_name) do |*args, &block|
arr = self.to_a
result = arr.send(method_name, *args)
self.replace(arr)
return result if result
return block ? block.call : result
end
end
|
.define_array_wrapper_with_result_method(method_name) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/google/protobuf/repeated_field.rb', line 115
def define_array_wrapper_with_result_method(method_name)
define_method(method_name) do |*args, &block|
result = self.to_a.send(method_name, *args, &block)
if result
new_arr = result.to_a
self.replace(new_arr)
if result.is_a?(Enumerator)
result = new_arr.to_enum
ie = ProxyingEnumerator.new(self, result)
result = ie.to_enum
end
end
result
end
end
|
.new(type, type_class = nil, initial_values = []) ⇒ Object
call-seq:
RepeatedField.new(type, type_class = nil, initial_values = [])
Creates a new repeated field. The provided type must be a Ruby symbol, and an take on the same values as those accepted by FieldDescriptor#type=. If the type is :message or :enum, type_class must be non-nil, and must be the Ruby class or module returned by Descriptor#msgclass or EnumDescriptor#enummodule, respectively. An initial list of elements may also be provided.
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 50
def self.new(type, type_class = nil, initial_values = [])
instance = allocate
if type_class.is_a?(Enumerable) and initial_values.empty? and ![:enum, :message].include?(type)
initial_values = type_class
type_class = nil
end
instance.send(:initialize, type, type_class: type_class, initial_values: initial_values)
instance
end
|
Instance Method Details
#+(other) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 260
def +(other)
if other.is_a? RepeatedField
if type != other.instance_variable_get(:@type) or descriptor != other.instance_variable_get(:@descriptor)
raise ArgumentError.new "Attempt to append RepeatedField with different element type."
end
fuse_arena(other.send(:arena))
super_set = dup
other.send(:each_msg_val) do |msg_val|
super_set.send(:append_msg_val, msg_val)
end
super_set
elsif other.is_a? Enumerable
super_set = dup
super_set.push(*other.to_a)
else
raise ArgumentError.new "Unknown type appending to RepeatedField"
end
end
|
#<<(element) ⇒ Object
157
158
159
160
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 157
def <<(element)
raise FrozenError if frozen?
push element
end
|
#==(other) ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 221
def ==(other)
return true if other.object_id == object_id
if other.is_a? RepeatedField
return false unless other.length == length
each_msg_val_with_index do |msg_val, i|
other_msg_val = Google::Protobuf::FFI.get_msgval_at(other.send(:array), i)
unless Google::Protobuf::FFI.message_value_equal(msg_val, other_msg_val, type, descriptor)
return false
end
end
return true
elsif other.is_a? Enumerable
return to_ary == other.to_a
end
false
end
|
#[](*args) ⇒ Object
Also known as:
at
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 76
def [](*args)
count = length
if args.size < 1
raise ArgumentError.new "Index or range is a required argument."
end
if args[0].is_a? Range
if args.size > 1
raise ArgumentError.new "Expected 1 when passing Range argument, but got #{args.size}"
end
range = args[0]
index_of_first = range.respond_to?(:begin) ? range.begin : range.last
index_of_first = 0 if index_of_first.nil?
end_of_range = range.respond_to?(:end) ? range.end : range.last
index_of_last = end_of_range.nil? ? -1 : end_of_range
if index_of_last < 0
index_of_last += count
end
unless range.exclude_end? and !end_of_range.nil?
index_of_last += 1
end
index_of_first += count if index_of_first < 0
length = index_of_last - index_of_first
return [] if length.zero?
elsif args[0].is_a? Integer
index_of_first = args[0]
index_of_first += count if index_of_first < 0
if args.size > 2
raise ArgumentError.new "Expected 1 or 2 arguments, but got #{args.size}"
end
if args.size == 1 if array.null? or index_of_first < 0 or index_of_first >= count
return nil
else
return convert_upb_to_ruby(Google::Protobuf::FFI.get_msgval_at(array, index_of_first), type, descriptor, arena)
end
else
length = [args[1],count].min
end
else
raise NotImplementedError
end
if array.null? or index_of_first < 0 or index_of_first >= count
nil
else
if index_of_first + length > count
length = count - index_of_first
end
if length < 0
nil
else
subarray(index_of_first, length)
end
end
end
|
#[]=(index, value) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 136
def []=(index, value)
raise FrozenError if frozen?
count = length
index += count if index < 0
return nil if index < 0
if index >= count
resize(index+1)
empty_message_value = Google::Protobuf::FFI::MessageValue.new count.upto(index-1) do |i|
Google::Protobuf::FFI.array_set(array, i, empty_message_value)
end
end
Google::Protobuf::FFI.array_set(array, index, convert_ruby_to_upb(value, arena, type, descriptor))
nil
end
|
#clear ⇒ Object
168
169
170
171
172
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 168
def clear
raise FrozenError if frozen?
resize 0
self
end
|
#concat(other) ⇒ Object
279
280
281
282
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 279
def concat(other)
raise ArgumentError.new "Expected Enumerable, but got #{other.class}" unless other.is_a? Enumerable
push(*other.to_a)
end
|
#dup ⇒ Object
Also known as:
clone
211
212
213
214
215
216
217
218
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 211
def dup
instance = self.class.allocate
instance.send(:initialize, type, descriptor: descriptor, arena: arena)
each_msg_val do |element|
instance.send(:append_msg_val, element)
end
instance
end
|
#each(&block) ⇒ Object
call-seq:
RepeatedField.each(&block)
Invokes the block once for each element of the repeated field. RepeatedField also includes Enumerable; combined with this method, the repeated field thus acts like an ordinary Ruby sequence.
69
70
71
72
73
74
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 69
def each &block
each_msg_val do |element|
yield(convert_upb_to_ruby(element, type, descriptor, arena))
end
self
end
|
#empty? ⇒ Boolean
92
93
94
|
# File 'lib/google/protobuf/repeated_field.rb', line 92
def empty?
self.size == 0
end
|
#first(n = nil) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/google/protobuf/repeated_field.rb', line 58
def first(n=nil)
if n.nil?
return self[0]
elsif n < 0
raise ArgumentError, "negative array size"
else
return self[0...n]
end
end
|
#freeze ⇒ Object
Freezes the RepeatedField object. We have to intercept this so we can freeze the underlying representation, not just the Ruby wrapper. Returns self.
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 197
def freeze
if method(:frozen?).super_method.call
unless Google::Protobuf::FFI.array_frozen? array
raise RuntimeError.new "Underlying representation of repeated field still mutable despite frozen wrapper"
end
return self
end
unless Google::Protobuf::FFI.array_frozen? array
mini_table = (type == :message) ? Google::Protobuf::FFI.get_mini_table(@descriptor) : nil
Google::Protobuf::FFI.array_freeze(array, mini_table)
end
super
end
|
#frozen? ⇒ Boolean
Is this object frozen? Returns true if either this Ruby wrapper or the underlying representation are frozen. Freezes the wrapper if the underlying representation is already frozen but this wrapper isn’t.
184
185
186
187
188
189
190
191
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 184
def frozen?
unless Google::Protobuf::FFI.array_frozen? array
raise RuntimeError.new "Ruby frozen RepeatedField with mutable representation" if super
return false
end
method(:freeze).super_method.call unless super
true
end
|
#hash ⇒ Object
252
253
254
255
256
257
258
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 252
def hash
return_value = 0
each_msg_val do |msg_val|
return_value = Google::Protobuf::FFI.message_value_hash(msg_val, type, descriptor, return_value)
end
return_value
end
|
#last(n = nil) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/google/protobuf/repeated_field.rb', line 69
def last(n=nil)
if n.nil?
return self[-1]
elsif n < 0
raise ArgumentError, "negative array size"
else
start = [self.size-n, 0].max
return self[start...self.size]
end
end
|
#length ⇒ Object
Also known as:
size
174
175
176
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 174
def length
array.null? ? 0 : Google::Protobuf::FFI.array_size(array)
end
|
#pop(n = nil) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/google/protobuf/repeated_field.rb', line 81
def pop(n=nil)
if n
results = []
n.times{ results << pop_one }
return results
else
return pop_one
end
end
|
#push(*elements) ⇒ Object
152
153
154
155
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 152
def push(*elements)
raise FrozenError if frozen?
internal_push(*elements)
end
|
#replace(replacements) ⇒ Object
162
163
164
165
166
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 162
def replace(replacements)
raise FrozenError if frozen?
clear
push(*replacements)
end
|
#slice ⇒ Object
array aliases into enumerable
97
|
# File 'lib/google/protobuf/repeated_field.rb', line 97
alias_method :slice, :[]
|
#to_ary ⇒ Object
call-seq:
RepeatedField.to_ary => array
Used when converted implicitly into array, e.g. compared to an Array. Also called as a fallback of Object#to_a
244
245
246
247
248
249
250
|
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 244
def to_ary
return_value = []
each do |element|
return_value << element
end
return_value
end
|