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.

Raises:

  • (RuntimeError)


351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/corba/cbase/Typecode.rb', line 351

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.



883
884
885
# File 'lib/corba/common/Typecode.rb', line 883

def implicit_default
  @implicit_default
end

#membersObject (readonly)

Returns the value of attribute members.



881
882
883
# File 'lib/corba/common/Typecode.rb', line 881

def members
  @members
end

#switchtypeObject (readonly)

Returns the value of attribute switchtype.



882
883
884
# File 'lib/corba/common/Typecode.rb', line 882

def switchtype
  @switchtype
end

Class Method Details

.define_type(tc) ⇒ Object



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/corba/common/Typecode.rb', line 904

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)


897
898
899
900
901
902
# File 'lib/corba/common/Typecode.rb', line 897

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



977
978
979
# File 'lib/corba/common/Typecode.rb', line 977

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

#discriminator_typeObject



974
975
976
# File 'lib/corba/common/Typecode.rb', line 974

def discriminator_type
  @switchtype
end

#get_typeObject



931
932
933
# File 'lib/corba/common/Typecode.rb', line 931

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

#idObject



891
892
893
# File 'lib/corba/common/Typecode.rb', line 891

def id
  @id
end

#inspectObject



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

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



981
982
983
984
985
986
987
# File 'lib/corba/common/Typecode.rb', line 981

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



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

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

#member_countObject



959
960
961
# File 'lib/corba/common/Typecode.rb', line 959

def member_count
  @members.size
end

#member_label(index) ⇒ Object



970
971
972
973
# File 'lib/corba/common/Typecode.rb', line 970

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

#member_name(index) ⇒ Object



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

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

#member_type(index) ⇒ Object



966
967
968
969
# File 'lib/corba/common/Typecode.rb', line 966

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

#nameObject



894
895
896
# File 'lib/corba/common/Typecode.rb', line 894

def name
  @name
end

#needs_conversion(val) ⇒ Object



955
956
957
# File 'lib/corba/common/Typecode.rb', line 955

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

Raises:

  • (RuntimeError)


454
455
456
# File 'lib/corba/jbase/Typecode.rb', line 454

def tc_
  @tc
end

#validate(val) ⇒ Object



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

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