rustorb 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,324 @@
1
+ # frozen_string_literal: true
2
+
3
+ # What generated types derive from (Rustorb::Struct, Rustorb::Union),
4
+ # CORBA::Any and CORBA::LongDouble.
5
+ #
6
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
7
+
8
+ module Rustorb
9
+ # A struct: attr_accessor members (MEMBERS, in IDL order) and a
10
+ # positional initialize.
11
+ class Struct
12
+ def initialize(*args)
13
+ members = self.class::MEMBERS
14
+ raise ArgumentError, "#{self.class}: #{args.size} values for #{members.size} members" if args.size > members.size
15
+
16
+ defaults = self.class::DEFAULTS
17
+ members.each_with_index do |m, i|
18
+ instance_variable_set("@#{m}", args.size > i ? args[i] : defaults[m])
19
+ end
20
+ end
21
+
22
+ def _values
23
+ self.class::MEMBERS.map { |m| instance_variable_get("@#{m}") }
24
+ end
25
+
26
+ def _tc
27
+ self.class._tc
28
+ end
29
+
30
+ def ==(other)
31
+ other.class == self.class && other._values == _values
32
+ end
33
+ alias eql? ==
34
+
35
+ def hash
36
+ [self.class, _values].hash
37
+ end
38
+
39
+ def inspect
40
+ "#<#{self.class.name} #{self.class::MEMBERS.zip(_values).map { |m, v| "#{m}=#{v.inspect}" }.join(', ')}>"
41
+ end
42
+ alias to_s inspect
43
+ end
44
+
45
+ # A struct's IDL 4 @default values (DEFAULTS, by member) are those of
46
+ # the members new does not get; @range / @min / @max (RANGES, by member:
47
+ # [min, max], either nil) are checked when the struct is written.
48
+ class Struct
49
+ DEFAULTS = {}.freeze
50
+ RANGES = {}.freeze
51
+ end
52
+
53
+ # An IDL 4 bitset (rustorb): FIELDS ([name, position, bits, kind], kind
54
+ # :unsigned, :signed or :boolean), an attr_accessor per field and a
55
+ # positional new. Sent as the unsigned integer that holds the bits.
56
+ class Bitset
57
+ def initialize(*args)
58
+ fields = self.class::FIELDS
59
+ raise ArgumentError, "#{self.class}: #{args.size} values for #{fields.size} fields" if args.size > fields.size
60
+
61
+ fields.each_with_index do |f, i|
62
+ instance_variable_set("@#{f[0]}", args.size > i ? args[i] : (f[3] == :boolean ? false : 0))
63
+ end
64
+ end
65
+
66
+ def _values
67
+ self.class::FIELDS.map { |f| instance_variable_get("@#{f[0]}") }
68
+ end
69
+
70
+ def _tc
71
+ self.class._tc
72
+ end
73
+
74
+ # The bits as sent.
75
+ def _bits
76
+ x = 0
77
+ self.class::FIELDS.each do |name, pos, bits, kind|
78
+ v = instance_variable_get("@#{name}")
79
+ v = if kind == :boolean
80
+ v ? 1 : 0
81
+ else
82
+ Rustorb::Marshal.int(v, kind == :signed ? -(2**(bits - 1)) : 0,
83
+ kind == :signed ? 2**(bits - 1) - 1 : 2**bits - 1, "#{bits}-bit #{name}")
84
+ end
85
+ x |= (v & ((1 << bits) - 1)) << pos
86
+ end
87
+ x
88
+ end
89
+
90
+ def self._from_bits(x)
91
+ b = allocate
92
+ self::FIELDS.each do |name, pos, bits, kind|
93
+ v = (x >> pos) & ((1 << bits) - 1)
94
+ v = v != 0 if kind == :boolean
95
+ v -= 1 << bits if kind == :signed && v >= (1 << (bits - 1))
96
+ b.instance_variable_set("@#{name}", v)
97
+ end
98
+ b
99
+ end
100
+
101
+ def ==(other)
102
+ other.class == self.class && other._values == _values
103
+ end
104
+ alias eql? ==
105
+
106
+ def hash
107
+ [self.class, _values].hash
108
+ end
109
+
110
+ def inspect
111
+ "#<#{self.class.name} #{self.class::FIELDS.map(&:first).zip(_values).map { |m, v| "#{m}=#{v.inspect}" }.join(', ')}>"
112
+ end
113
+ alias to_s inspect
114
+ end
115
+
116
+ # A union: the discriminator (_disc), the value (_value) and an accessor
117
+ # per member, which reads the value if the member is selected (else
118
+ # CORBA::BAD_PARAM) or selects the member with its first label.
119
+ class Union
120
+ attr_reader :_disc, :_value
121
+
122
+ def _set(disc, value)
123
+ @_disc = disc
124
+ @_value = value
125
+ self
126
+ end
127
+
128
+ def _tc
129
+ self.class._tc
130
+ end
131
+
132
+ # The IDL name of the selected member (nil for none).
133
+ def _member
134
+ m = Rustorb::Marshal.union_member(self.class._tc, @_disc)
135
+ m && m[1]
136
+ end
137
+
138
+ # Sets the discriminator to another label of the selected member.
139
+ def _disc=(disc)
140
+ now = _member
141
+ unless now.nil? || Rustorb::Marshal.union_member(self.class._tc, disc)&.[](1) == now
142
+ raise CORBA::BAD_PARAM.new("#{disc.inspect} does not select #{now}")
143
+ end
144
+
145
+ @_disc = disc
146
+ end
147
+
148
+ def _is_at_default?
149
+ m = Rustorb::Marshal.union_member(self.class._tc, @_disc)
150
+ m.nil? || m[0] == :default
151
+ end
152
+
153
+ # Selects no member: the discriminator a value no label has (for a
154
+ # union without a default member), or disc.
155
+ def _default(disc = nil)
156
+ @_disc = disc.nil? ? Rustorb::Union.implicit_default(self.class._tc) : disc
157
+ @_value = nil
158
+ self
159
+ end
160
+
161
+ def _get(name)
162
+ raise CORBA::BAD_PARAM.new("#{self.class.name}: #{name} is not selected") unless _member == name
163
+
164
+ @_value
165
+ end
166
+
167
+ # Selects the member name with value; disc must be one of its labels.
168
+ def _put(name, value, disc = nil)
169
+ labels = self.class._tc._union_members.select { |m| m[1] == name }.map(&:first)
170
+ if disc.nil?
171
+ disc = labels.first
172
+ disc = Rustorb::Union.implicit_default(self.class._tc) if disc == :default
173
+ elsif !labels.include?(disc) && !(labels.include?(:default) && Rustorb::Marshal.union_member(self.class._tc, disc)&.[](0) == :default)
174
+ raise CORBA::BAD_PARAM.new("#{disc.inspect} is not a label of #{name}")
175
+ end
176
+ @_disc = disc
177
+ @_value = value
178
+ self
179
+ end
180
+
181
+ def ==(other)
182
+ other.class == self.class && other._disc == @_disc && other._value == @_value
183
+ end
184
+ alias eql? ==
185
+
186
+ def hash
187
+ [self.class, @_disc, @_value].hash
188
+ end
189
+
190
+ def inspect
191
+ "#<#{self.class.name} _disc=#{@_disc.inspect} #{_member}=#{@_value.inspect}>"
192
+ end
193
+ alias to_s inspect
194
+
195
+ # A discriminator value that no label of tc has.
196
+ def self.implicit_default(tc)
197
+ used = tc._union_members.map(&:first).reject { |l| l == :default }
198
+ disc = tc.discriminator_type.unaliased
199
+ case disc.kind
200
+ when CORBA::TK_BOOLEAN then !used.include?(false) ? false : true
201
+ when CORBA::TK_CHAR then (0..255).map { |c| c.chr(Encoding::UTF_8) }.find { |c| !used.include?(c) }
202
+ else (0..).find { |v| !used.include?(v) }
203
+ end
204
+ end
205
+ end
206
+ end
207
+
208
+ module CORBA
209
+ # long double, as its IEEE 754 binary128 bits (hi, lo), made from and
210
+ # turned into a Float.
211
+ class LongDouble
212
+ attr_reader :hi, :lo
213
+
214
+ def initialize(value = 0.0)
215
+ @hi, @lo = Rustorb::Native.longdouble_from_double(value.to_f)
216
+ end
217
+
218
+ def self._from_bits(hi, lo)
219
+ ld = allocate
220
+ ld.instance_variable_set(:@hi, hi)
221
+ ld.instance_variable_set(:@lo, lo)
222
+ ld
223
+ end
224
+
225
+ def to_f
226
+ Rustorb::Native.longdouble_to_double(@hi, @lo)
227
+ end
228
+
229
+ def ==(other)
230
+ other.is_a?(LongDouble) ? other.hi == @hi && other.lo == @lo : to_f == other
231
+ end
232
+
233
+ def inspect
234
+ "#<CORBA::LongDouble #{to_f}>"
235
+ end
236
+ alias to_s inspect
237
+ end
238
+
239
+ # An any: a TypeCode and a value of that type. Values read from the wire
240
+ # are extracted when first asked for; a value of a type without a Ruby
241
+ # class here comes out as a Rustorb::GenericStruct / GenericUnion, and
242
+ # the any goes back on the wire as it came.
243
+ class Any
244
+ def self.to_any(value, tc = nil)
245
+ new(value, tc)
246
+ end
247
+
248
+ def initialize(value = nil, tc = nil)
249
+ @tc = tc || Any.infer(value)
250
+ @value = value
251
+ @has_value = true
252
+ end
253
+
254
+ def self._from_native(native)
255
+ any = allocate
256
+ any.instance_variable_set(:@native, native)
257
+ any.instance_variable_set(:@tc, CORBA::TypeCode._wrap(native.type))
258
+ any.instance_variable_set(:@has_value, false)
259
+ any
260
+ end
261
+
262
+ def _tc
263
+ @tc
264
+ end
265
+ alias type _tc
266
+
267
+ def value
268
+ unless @has_value
269
+ v = nil
270
+ tc = @tc
271
+ @native.extract(->(inp) { v = Rustorb::Marshal.read(inp, tc) }) unless tc.unaliased.kind == CORBA::TK_NULL
272
+ @value = v
273
+ @has_value = true
274
+ end
275
+ @value
276
+ end
277
+
278
+ def _native
279
+ @native ||= if @tc.unaliased.kind == CORBA::TK_NULL
280
+ Rustorb::Native::Any.empty
281
+ else
282
+ tc = @tc
283
+ v = @value
284
+ Rustorb::Native::Any.insert(tc.resolve.native, ->(out) { Rustorb::Marshal.write(out, tc, v) })
285
+ end
286
+ end
287
+
288
+ def ==(other)
289
+ other.is_a?(Any) && _native.equal(other._native)
290
+ end
291
+
292
+ def inspect
293
+ "#<CORBA::Any #{@tc.inspect} #{value.inspect}>"
294
+ end
295
+ alias to_s inspect
296
+
297
+ # The TypeCode of a Ruby value put in an any without one.
298
+ def self.infer(value)
299
+ case value
300
+ when nil then CORBA._tc_null
301
+ when true, false then CORBA._tc_boolean
302
+ when Integer
303
+ if value.between?(-(2**31), 2**31 - 1) then CORBA._tc_long
304
+ elsif value.between?(-(2**63), 2**63 - 1) then CORBA._tc_longlong
305
+ else CORBA._tc_ulonglong
306
+ end
307
+ when Float then CORBA._tc_double
308
+ when String then CORBA._tc_string
309
+ when CORBA::LongDouble then CORBA._tc_longdouble
310
+ when CORBA::Any then CORBA._tc_any
311
+ when CORBA::TypeCode then CORBA._tc_TypeCode
312
+ when CORBA::Object then value.class.respond_to?(:_tc) ? value.class._tc : CORBA._tc_Object
313
+ when BigDecimal
314
+ digits = value.abs.to_s("F").delete(".").sub(/\A0+(?=\d)/, "")
315
+ scale = value.frac.zero? ? 0 : value.to_s("F").split(".")[1].size
316
+ CORBA::TypeCode.create_fixed_tc([digits.size, scale, 1].max, scale)
317
+ else
318
+ return value._tc if value.respond_to?(:_tc)
319
+
320
+ raise CORBA::BAD_PARAM.new("no TypeCode for #{value.inspect}: give one to CORBA::Any.to_any")
321
+ end
322
+ end
323
+ end
324
+ end
@@ -0,0 +1,389 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Valuetypes (CORBA 3.4 part 1, 9; the CDR of part 2, 9.3.4) and abstract
4
+ # interfaces, on the value functions of the C ABI (rorb_out_begin_value,
5
+ # rorb_in_read_value_header...). See docs/ruby-binding.md.
6
+ #
7
+ # class Point # generated
8
+ # include ::CORBA::ValueBase
9
+ # ID = "IDL:Demo/Point:1.0"
10
+ # MEMBERS = %w[x y].freeze
11
+ # ...
12
+ # end
13
+ #
14
+ # A value is written with its repository ids (its own, then those of the
15
+ # bases it is truncatable to), chunked when it is truncatable or custom; an
16
+ # instance written again in the same message is an indirection, so sharing
17
+ # and cycles survive the trip. A value is read by the factory of the first
18
+ # repository id received that has one (ORB#register_value_factory, else the
19
+ # generated class of a valuetype without operations); without one the
20
+ # stream fails (MARSHAL).
21
+ #
22
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
23
+
24
+ module CORBA
25
+ # What every value includes. A generated valuetype class has ID,
26
+ # TRUNCATABLE_IDS (its own id, then those of the bases it is truncatable
27
+ # to), MEMBERS (its own state members) and _tc; new takes the state
28
+ # members, those of the bases first.
29
+ module ValueBase
30
+ def initialize(*args)
31
+ names = Rustorb::Values.member_names(self.class)
32
+ raise ArgumentError, "#{self.class}: #{args.size} values for #{names.size} members" if args.size > names.size
33
+
34
+ names.each_with_index { |m, i| instance_variable_set("@#{m}", args[i]) }
35
+ end
36
+
37
+ def _truncatable_ids = self.class::TRUNCATABLE_IDS
38
+ def _tc = self.class._tc
39
+ def _add_ref = self
40
+ def _remove_ref = nil
41
+ def _refcount_value = 1
42
+
43
+ # Values seen again (cycles, sharing) are shown as #<Class ...>.
44
+ def inspect
45
+ seen = Thread.current[:__rustorb_inspecting] ||= {}.compare_by_identity
46
+ return "#<#{self.class.name} ...>" if seen.key?(self)
47
+
48
+ seen[self] = true
49
+ begin
50
+ names = Rustorb::Values.member_names(self.class)
51
+ "#<#{self.class.name} #{names.map { |m| "#{m}=#{instance_variable_get("@#{m}").inspect}" }.join(', ')}>"
52
+ ensure
53
+ seen.delete(self)
54
+ end
55
+ end
56
+ alias to_s inspect
57
+ end
58
+
59
+ # A custom valuetype's implementation defines marshal(os) and
60
+ # unmarshal(is) (a CORBA::DataOutputStream and DataInputStream).
61
+ module CustomMarshal
62
+ def marshal(_os)
63
+ raise CORBA::NO_IMPLEMENT.new("#{self.class}#marshal", 0, CORBA::COMPLETED_NO)
64
+ end
65
+
66
+ def unmarshal(_is)
67
+ raise CORBA::NO_IMPLEMENT.new("#{self.class}#unmarshal", 0, CORBA::COMPLETED_NO)
68
+ end
69
+ end
70
+
71
+ # A value factory: create_for_unmarshal returns a new instance, whose
72
+ # state the ORB then reads. ORB#register_value_factory also takes a
73
+ # class (its instances are made with allocate) or a block.
74
+ class ValueFactoryBase
75
+ def initialize(&block)
76
+ @block = block
77
+ end
78
+
79
+ def create_for_unmarshal
80
+ return @block.call if @block
81
+
82
+ raise CORBA::NO_IMPLEMENT.new("#{self.class}#create_for_unmarshal", 0, CORBA::COMPLETED_NO)
83
+ end
84
+ end
85
+ ValueFactory = ValueFactoryBase
86
+
87
+ # The streams of custom marshaling: write_<type>(v) and read_<type> for
88
+ # the basic types, write_<type>_array(seq, offset, length) and
89
+ # read_<type>_array(seq, offset, length) (which fills seq), write_Value /
90
+ # read_Value, write_Abstract / read_Abstract, write_Object / read_Object,
91
+ # write_any / read_any, write_TypeCode / read_TypeCode.
92
+ module DataStreamTypes
93
+ BASIC = {
94
+ "boolean" => :_tc_boolean, "octet" => :_tc_octet, "char" => :_tc_char, "wchar" => :_tc_wchar,
95
+ "short" => :_tc_short, "ushort" => :_tc_ushort, "long" => :_tc_long, "ulong" => :_tc_ulong,
96
+ "longlong" => :_tc_longlong, "ulonglong" => :_tc_ulonglong, "float" => :_tc_float,
97
+ "double" => :_tc_double, "longdouble" => :_tc_longdouble
98
+ }.freeze
99
+ OTHER = {
100
+ "string" => :_tc_string, "wstring" => :_tc_wstring, "any" => :_tc_any,
101
+ "TypeCode" => :_tc_TypeCode, "Object" => :_tc_Object
102
+ }.freeze
103
+ end
104
+
105
+ class DataOutputStream
106
+ def initialize(out)
107
+ @out = out
108
+ end
109
+
110
+ def _out = @out
111
+
112
+ DataStreamTypes::BASIC.merge(DataStreamTypes::OTHER).each do |name, tc|
113
+ define_method("write_#{name}") { |v| Rustorb::Marshal.write(@out, CORBA.public_send(tc), v) }
114
+ end
115
+
116
+ DataStreamTypes::BASIC.each do |name, tc|
117
+ define_method("write_#{name}_array") do |seq, offset = 0, length = nil|
118
+ length ||= seq.size - offset
119
+ element = CORBA.public_send(tc)
120
+ if seq.is_a?(String)
121
+ Rustorb::Marshal.write(@out, CORBA::TypeCode.create_array_tc(length, element), seq.byteslice(offset, length))
122
+ else
123
+ seq[offset, length].each { |v| Rustorb::Marshal.write(@out, element, v) }
124
+ end
125
+ end
126
+ end
127
+
128
+ def write_Value(value, tc = nil)
129
+ if tc && tc.unaliased.kind == CORBA::TK_VALUE_BOX
130
+ Rustorb::Values.write_box(@out, tc.unaliased, value)
131
+ else
132
+ Rustorb::Values.write(@out, value)
133
+ end
134
+ end
135
+
136
+ def write_Abstract(value) = Rustorb::Values.write_abstract(@out, value)
137
+ def write_fixed(value, digits, scale) = Rustorb::Marshal.write(@out, CORBA::TypeCode.create_fixed_tc(digits, scale), value)
138
+ end
139
+
140
+ class DataInputStream
141
+ def initialize(inp)
142
+ @in = inp
143
+ end
144
+
145
+ def _in = @in
146
+
147
+ DataStreamTypes::BASIC.merge(DataStreamTypes::OTHER).each do |name, tc|
148
+ define_method("read_#{name}") { Rustorb::Marshal.read(@in, CORBA.public_send(tc)) }
149
+ end
150
+
151
+ DataStreamTypes::BASIC.each do |name, tc|
152
+ define_method("read_#{name}_array") do |seq, offset, length|
153
+ element = CORBA.public_send(tc)
154
+ length.times { |i| seq[offset + i] = Rustorb::Marshal.read(@in, element) }
155
+ seq
156
+ end
157
+ end
158
+
159
+ # A value of any type, or of the value box tc.
160
+ def read_Value(tc = nil)
161
+ if tc && tc.unaliased.kind == CORBA::TK_VALUE_BOX
162
+ Rustorb::Values.read_box(@in, tc.unaliased)
163
+ else
164
+ Rustorb::Values.read(@in, tc && tc.unaliased.id)
165
+ end
166
+ end
167
+
168
+ def read_Abstract(tc = nil) = Rustorb::Values.read_abstract(@in, tc)
169
+ def read_fixed(digits, scale) = Rustorb::Marshal.read(@in, CORBA::TypeCode.create_fixed_tc(digits, scale))
170
+ end
171
+
172
+ class ORB
173
+ # Registers factory (a ValueFactoryBase, an object with
174
+ # create_for_unmarshal, or a class) for the valuetype id; returns the one
175
+ # it replaces.
176
+ def register_value_factory(id, factory = nil, &block)
177
+ factory ||= CORBA::ValueFactoryBase.new(&block) if block
178
+ unless factory.is_a?(Class) || factory.respond_to?(:create_for_unmarshal)
179
+ raise CORBA::BAD_PARAM.new("not a value factory: #{factory.inspect}", 0, CORBA::COMPLETED_NO)
180
+ end
181
+
182
+ Rustorb::Values.register_factory(id, factory)
183
+ end
184
+
185
+ def unregister_value_factory(id)
186
+ Rustorb::Values.unregister_factory(id)
187
+ nil
188
+ end
189
+
190
+ # The registered factory, else the generated class of a valuetype
191
+ # without operations, else nil.
192
+ def lookup_value_factory(id) = Rustorb::Values.factory(id)
193
+ end
194
+ end
195
+
196
+ module Rustorb
197
+ # What a generated abstract interface module extends itself with: _narrow
198
+ # keeps a value that includes the module and makes a stub of a reference.
199
+ module AbstractInterfaceModule
200
+ include InterfaceModule
201
+
202
+ def _narrow(obj)
203
+ return obj if obj.nil? || obj.is_a?(self)
204
+ return super if obj.is_a?(CORBA::Object)
205
+
206
+ raise CORBA::BAD_PARAM.new("neither a #{self::ID} value nor an object", 0, CORBA::COMPLETED_NO)
207
+ end
208
+
209
+ def _unchecked_narrow(obj)
210
+ return obj unless obj.is_a?(CORBA::Object)
211
+
212
+ super
213
+ end
214
+ end
215
+
216
+ module Values
217
+ @lock = Mutex.new
218
+ @factories = {}
219
+ @defaults = {}
220
+ @members = {}
221
+
222
+ module_function
223
+
224
+ # Called by the generated code for each valuetype and value box: its
225
+ # TypeCode is then known to the core (for values in anys) and, with
226
+ # default, the class is its factory.
227
+ def register(klass, default: false)
228
+ Rustorb.register_type(klass)
229
+ @lock.synchronize { @defaults[klass::ID] = klass } if default
230
+ klass
231
+ end
232
+
233
+ def register_factory(id, factory)
234
+ @lock.synchronize do
235
+ old = @factories[id]
236
+ @factories[id] = factory
237
+ old
238
+ end
239
+ end
240
+
241
+ def unregister_factory(id)
242
+ @lock.synchronize do
243
+ raise CORBA::BAD_PARAM.new("no value factory for #{id}", 0, CORBA::COMPLETED_NO) unless @factories.key?(id)
244
+
245
+ @factories.delete(id)
246
+ end
247
+ end
248
+
249
+ def factory(id)
250
+ @lock.synchronize { @factories[id] || @defaults[id] }
251
+ end
252
+
253
+ def make(factory)
254
+ factory.is_a?(Class) ? factory.allocate : factory.create_for_unmarshal
255
+ end
256
+
257
+ # [names, TypeCode] of each valuetype of klass's state, the concrete
258
+ # bases first: the Ruby names (MEMBERS of the class with the type's ID)
259
+ # and the TypeCode with the member types.
260
+ def state(klass)
261
+ cached = @lock.synchronize { @members[klass] }
262
+ return cached if cached
263
+
264
+ chain = []
265
+ tc = klass._tc.resolve
266
+ while tc
267
+ chain.unshift(tc)
268
+ tc = tc.concrete_base_type&.resolve
269
+ end
270
+ ancestors = klass.ancestors
271
+ list = chain.map do |t|
272
+ owner = ancestors.find { |k| k.const_defined?(:ID, false) && k::ID == t.id && k.const_defined?(:MEMBERS, false) }
273
+ names = owner ? owner::MEMBERS : Array.new(t.member_count) { |i| t.member_name(i) }
274
+ [names, t]
275
+ end.freeze
276
+ @lock.synchronize { @members[klass] = list }
277
+ end
278
+
279
+ def member_names(klass)
280
+ return [] unless klass.respond_to?(:_tc)
281
+
282
+ state(klass).flat_map(&:first)
283
+ end
284
+
285
+ def write(out, v)
286
+ return out.write_null_value if v.nil?
287
+
288
+ unless v.is_a?(CORBA::ValueBase)
289
+ raise CORBA::BAD_PARAM.new("a value expected, got #{v.inspect}", 0, CORBA::COMPLETED_NO)
290
+ end
291
+
292
+ custom = v.is_a?(CORBA::CustomMarshal)
293
+ ids = v._truncatable_ids
294
+ return unless out.begin_value(v, ids, ids.size > 1 || custom)
295
+
296
+ if custom
297
+ v.marshal(CORBA::DataOutputStream.new(out))
298
+ else
299
+ state(v.class).each do |names, tc|
300
+ names.each_with_index do |m, i|
301
+ Rustorb::Marshal.write(out, tc.member_type(i), v.instance_variable_get("@#{m}"))
302
+ end
303
+ end
304
+ end
305
+ out.end_value
306
+ end
307
+
308
+ # expected: the repository id of the declared type (for a value sent
309
+ # without its ids).
310
+ def read(inp, expected)
311
+ kind, slot, ids = inp.read_value_header
312
+ case kind
313
+ when 1
314
+ v = inp.value_at(slot)
315
+ inp.fail if v.nil?
316
+ return v
317
+ when 2 then nil
318
+ else return nil
319
+ end
320
+
321
+ ids = [expected] if ids.empty? && expected
322
+ f = nil
323
+ ids.each { |id| break if (f = factory(id)) }
324
+ return inp.fail && nil unless f
325
+
326
+ v = make(f)
327
+ return inp.fail && nil unless v.is_a?(CORBA::ValueBase)
328
+
329
+ inp.keep_value(slot, v)
330
+ if v.is_a?(CORBA::CustomMarshal)
331
+ v.unmarshal(CORBA::DataInputStream.new(inp))
332
+ else
333
+ state(v.class).each do |names, tc|
334
+ names.each_with_index do |m, i|
335
+ v.instance_variable_set("@#{m}", Rustorb::Marshal.read(inp, tc.member_type(i)))
336
+ end
337
+ end
338
+ end
339
+ inp.end_value
340
+ v
341
+ end
342
+
343
+ # A value box: the boxed value itself, nil for null.
344
+ def write_box(out, tc, v)
345
+ return out.write_null_value if v.nil?
346
+ return unless out.begin_value(v, [tc.id], false)
347
+
348
+ Rustorb::Marshal.write(out, tc.content_type, v)
349
+ out.end_value
350
+ end
351
+
352
+ def read_box(inp, tc)
353
+ kind, slot, = inp.read_value_header
354
+ return inp.value_at(slot) if kind == 1
355
+ return nil unless kind == 2
356
+
357
+ v = Rustorb::Marshal.read(inp, tc.content_type)
358
+ inp.keep_value(slot, v)
359
+ inp.end_value
360
+ v
361
+ end
362
+
363
+ # An abstract interface: TRUE and a reference (nil is a nil one), or
364
+ # FALSE and a value.
365
+ def write_abstract(out, v)
366
+ if v.nil? || v.is_a?(CORBA::Object)
367
+ out.write_boolean(true)
368
+ out.write_object(v&._native_ref)
369
+ elsif v.is_a?(CORBA::ValueBase)
370
+ out.write_boolean(false)
371
+ write(out, v)
372
+ else
373
+ raise CORBA::BAD_PARAM.new("neither an object nor a value: #{v.inspect}", 0, CORBA::COMPLETED_NO)
374
+ end
375
+ end
376
+
377
+ def read_abstract(inp, tc)
378
+ return CORBA::Object._from_ref(inp.read_object, tc) if inp.read_boolean
379
+
380
+ read(inp, nil)
381
+ end
382
+ end
383
+
384
+ # The TypeCode of the valuetype id, for the core (values in anys).
385
+ def self.resolve_value_tc(id)
386
+ klass = find_type(id)
387
+ klass.respond_to?(:_tc) ? klass._tc.resolve.native : nil
388
+ end
389
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rustorb
4
+ VERSION = "0.7.0"
5
+ end