Class: R2CORBA::CORBA::TypeCode::Union

Inherits:
IdentifiedTypeCode show all
Defined in:
lib/corba/cbase/Typecode.rb,
lib/corba/jbase/Typecode.rb,
lib/corba/common/Typecode.rb

Overview

Except

Constant Summary

Constants inherited from R2CORBA::CORBA::TypeCode

LongLongRange, LongRange, OctetRange, ShortRange, ULongLongRange, ULongRange, UShortRange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from R2CORBA::CORBA::TypeCode

_tc, _wrap_native, #concrete_base_type, #content_type, #equal?, #equivalent?, #fixed_digits, #fixed_scale, from_native, #get_compact_typecode, get_primitive_tc, #is_recursive_tc?, #kind, #length, #member_visibility, native_kind, register_id_type, #resolved_tc, #type_modifier, typecode_for_id, typecodes_for_name

Constructor Details

#initialize(*args) ⇒ Union

Returns a new instance of Union.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/corba/cbase/Typecode.rb', line 324

def initialize(*args)
  if CORBA::Native::TypeCode === args.first
    @tc_ = args.first
    @type = nil
    @switchtype = TypeCode.from_native(@tc_.discriminator_type)
    @labels = {}
    @members = []
    def_inx = @tc_.default_index
    @tc_.member_count.times do |i|
      if def_inx < 0 || def_inx != i
        ml_ = @tc_.member_label(i)
      else
        ml_ = :default
      end
      @labels[ml] = i
      @members << [ml, @tc_.member_name(i), TypeCode.from_native(@tc_.member_type(i))]
    end
    super(id)
  else
    id, name, switchtype_, members_, type_, implicit_default_ = args
    raise ::CORBA::BAD_PARAM unless members_.is_a? ::Array
    raise ::CORBA::BAD_PARAM unless switchtype_.is_a?(CORBA::TypeCode)

    @type = type_
    @implicit_default = implicit_default_
    @switchtype = switchtype_.resolved_tc
    @labels = {}
    @members = []
    members_.each { |mlabel, mname, mtc|
      add_member(mlabel, mname, mtc)
    }
    @id = id
    @name = name
    n_members = @members.collect do |ml, mn, mtc|
      [ml, mn.to_s, mtc.tc_]
    end
    @tc = _create_tc(@id, @name, @switchtype, n_members)
    super(id)
  end
end

Instance Attribute Details

#implicit_defaultObject (readonly)

Returns the value of attribute implicit_default.



909
910
911
# File 'lib/corba/common/Typecode.rb', line 909

def implicit_default
  @implicit_default
end

#membersObject (readonly)

Returns the value of attribute members.



907
908
909
# File 'lib/corba/common/Typecode.rb', line 907

def members
  @members
end

#switchtypeObject (readonly)

Returns the value of attribute switchtype.



908
909
910
# File 'lib/corba/common/Typecode.rb', line 908

def switchtype
  @switchtype
end

Class Method Details

.define_type(tc) ⇒ Object



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'lib/corba/common/Typecode.rb', line 935

def Union.define_type(tc)
  union_type = ::Object.module_eval(%Q{
    class #{tc.name} < ::CORBA::Portable::Union
      def _tc
        @@tc_#{tc.name} ||= TypeCode.typecode_for_id('#{tc.id}')
      end
    end
    #{tc.name}
  })
  accessors = {}
  tc.members.each_with_index do |_m, ix|
    accessors[_m[1]] = ix unless accessors.has_key?(_m[1])
  end
  accessors.each do |mname, ix|
    union_type.module_eval(%Q{
      def #{mname}; @value; end
      def #{mname}=(val); _set_value(#{ix.to_s}, val); end
    })
  end
  if tc.implicit_default
    union_type.module_eval(%Q{
      def _default; @discriminator = #{tc.implicit_default}; @value = nil; end
    })
  end
  union_type
end

Instance Method Details

#add_member(label, name, tc) ⇒ Object

Raises:

  • (ArgumentError)


927
928
929
930
931
932
933
# File 'lib/corba/common/Typecode.rb', line 927

def add_member(label, name, tc)
  raise ArgumentError, 'expected CORBA::TypeCode' unless tc.is_a?(CORBA::TypeCode)

  @switchtype.validate(label) unless label == :default
  @labels[label] = @members.size
  @members << [label, name.to_s, tc]
end

#default_indexObject



1017
1018
1019
# File 'lib/corba/common/Typecode.rb', line 1017

def default_index
  if @labels.has_key? :default then @labels[:default]; else -1; end
end

#discriminator_typeObject



1013
1014
1015
# File 'lib/corba/common/Typecode.rb', line 1013

def discriminator_type
  @switchtype
end

#get_typeObject



962
963
964
# File 'lib/corba/common/Typecode.rb', line 962

def get_type
  @type ||= CORBA::TypeCode::Union.define_type(self)
end

#idObject



919
920
921
# File 'lib/corba/common/Typecode.rb', line 919

def id
  @id
end

#inspectObject



1035
1036
1037
1038
1039
# File 'lib/corba/common/Typecode.rb', line 1035

def inspect
  s = "#{self.class.name}: #{name} - #{id}\n"
  @members.each { |l, n, t| s += "  case #{l.to_s}: #{n} = " + t.inspect + "\n" }
  s
end

#label_index(val) ⇒ Object



1021
1022
1023
1024
1025
1026
1027
# File 'lib/corba/common/Typecode.rb', line 1021

def label_index(val)
  val = @switchtype.validate(val) unless val == :default
  # raise CORBA::MARSHAL.new(
  #  "invalid discriminator value (#{val}) for union #{name}",
  #  1, CORBA::COMPLETED_NO) unless val == :default || @labels.has_key?(val) || @labels.has_key?(:default)
  if val == :default then @labels[:default]; elsif @labels.has_key?(val) then @labels[val] else nil end
end

#label_member(val) ⇒ Object



1029
1030
1031
1032
1033
# File 'lib/corba/common/Typecode.rb', line 1029

def label_member(val)
  return nil unless (lbl_ix = label_index(val))

  member_name(lbl_ix)
end

#member_countObject



991
992
993
# File 'lib/corba/common/Typecode.rb', line 991

def member_count
  @members.size
end

#member_label(index) ⇒ Object



1007
1008
1009
1010
1011
# File 'lib/corba/common/Typecode.rb', line 1007

def member_label(index)
  raise CORBA::TypeCode::Bounds.new if (index < 0) || (index >= @members.size)

  @members[index][0]
end

#member_name(index) ⇒ Object



995
996
997
998
999
# File 'lib/corba/common/Typecode.rb', line 995

def member_name(index)
  raise CORBA::TypeCode::Bounds.new if (index < 0) || (index >= @members.size)

  @members[index][1]
end

#member_type(index) ⇒ Object



1001
1002
1003
1004
1005
# File 'lib/corba/common/Typecode.rb', line 1001

def member_type(index)
  raise CORBA::TypeCode::Bounds.new if (index < 0) || (index >= @members.size)

  @members[index][2]
end

#nameObject



923
924
925
# File 'lib/corba/common/Typecode.rb', line 923

def name
  @name
end

#needs_conversion(val) ⇒ Object



987
988
989
# File 'lib/corba/common/Typecode.rb', line 987

def needs_conversion(val)
  @members[@labels[val._disc]][2].needs_conversion(val._value)
end

#tc_Object

because creating the native tc involves creating Any’s we postpone until actually needed



430
431
432
# File 'lib/corba/jbase/Typecode.rb', line 430

def tc_
  @tc
end

#validate(val) ⇒ Object



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/corba/common/Typecode.rb', line 966

def validate(val)
  return val if val.nil?

  super(val)
  @switchtype.validate(val._disc) unless val._disc == :default
  # raise CORBA::MARSHAL.new(
  #  "invalid discriminator value (#{val._disc.to_s}) for union #{name}",
  #  1, CORBA::COMPLETED_NO) unless @labels.has_key?(val._disc)
  if @labels.has_key?(val._disc) # no need to check for implicit defaults
    if needs_conversion(val)
      vorg = val
      val = vorg.class.new
      val.__send__((@members[@labels[vorg._disc]][1] + '=').intern,
                    @members[@labels[vorg._disc]][2].validate(vorg._value))
    else
      @members[@labels[val._disc]][2].validate(val._value)
    end
  end
  val
end