Class: R2CORBA::CORBA::TypeCode::Struct

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

Overview

AbstractInterface

Direct Known Subclasses

Except, SysExcept

Constant Summary

Constants inherited from R2CORBA::CORBA::TypeCode

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

Instance Attribute Summary collapse

Attributes inherited from R2CORBA::CORBA::TypeCode

#tc_

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from R2CORBA::CORBA::TypeCode

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

Constructor Details

#initialize(*args) ⇒ Struct

Returns a new instance of Struct.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/corba/cbase/Typecode.rb', line 274

def initialize(*args)
  if CORBA::Native::TypeCode === args.first
    @tc_ = args.first
    @type = nil
    @members = []
    @tc_.member_count.times do |i|
      @members << [@tc_.member_name(i), TypeCode.from_native(@tc_.member_type(i))]
    end
    super(id)
  else
    id, name, members_, type_ = args
    raise ArgumentError, 'expected members Array' unless members_.is_a?(::Array) || type_.nil?

    if type_.nil? && !members_.is_a?(::Array)
      type_ = members_
      members_ = []
    end
    @type = type_
    @members = []
    members_.each { |n, tc| add_member(n, tc) }
    n_members = @members.collect { |n, tc| [n.to_s, tc.tc_] }
    @tc_ = _create_tc(id, name, n_members)
    super(id)
  end
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



792
793
794
# File 'lib/corba/common/Typecode.rb', line 792

def members
  @members
end

Class Method Details

.define_type(tc) ⇒ Object



803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/corba/common/Typecode.rb', line 803

def Struct.define_type(tc)
  code = %Q{
    class #{tc.name} < ::CORBA::Portable::Struct
      def self._tc
        @@tc_#{tc.name} ||= TypeCode.typecode_for_id('#{tc.id}')
      end
      def initialize(*param_)
        #{tc.members.collect { |n, t| "@#{n}" }.join(', ')} = param_
      end
    end
    #{tc.name}
  }
  struct_type = ::Object.module_eval(code)
  tc.members.each do |nm_, tc_|
    struct_type.module_eval(%Q{attr_accessor :#{nm_}})
  end
  struct_type
end

Instance Method Details

#add_member(name, tc) ⇒ Object

Raises:

  • (ArgumentError)


797
798
799
800
801
# File 'lib/corba/common/Typecode.rb', line 797

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

  @members << [name, tc]
end

#get_typeObject



822
823
824
# File 'lib/corba/common/Typecode.rb', line 822

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

#inspectObject



860
861
862
863
864
# File 'lib/corba/common/Typecode.rb', line 860

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

#member_countObject



844
845
846
# File 'lib/corba/common/Typecode.rb', line 844

def member_count
  @members.size
end

#member_name(index) ⇒ Object



848
849
850
851
852
# File 'lib/corba/common/Typecode.rb', line 848

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

  @members[index][0]
end

#member_type(index) ⇒ Object



854
855
856
857
858
# File 'lib/corba/common/Typecode.rb', line 854

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

  @members[index][1]
end

#needs_conversion(val) ⇒ Object



840
841
842
# File 'lib/corba/common/Typecode.rb', line 840

def needs_conversion(val)
  @members.any? { |name, tc| tc.needs_conversion(val.__send__(name.intern)) }
end

#validate(val) ⇒ Object



826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/corba/common/Typecode.rb', line 826

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

  super(val)
  if needs_conversion(val)
    vorg = val
    val = vorg.class.new
    @members.each { |name, tc| val.__send__((name + '=').intern, tc.validate(vorg.__send__(name.intern))) }
  else
    @members.each { |name, tc| tc.validate(val.__send__(name.intern)) }
  end
  val
end