[omniORB] How to copy a corba struct with sequences?

baileyk@slxi.com baileyk@slxi.com
Tue, 9 Oct 2001 11:28:42 -0500



All IDL struct types have suitable copy-assignment operators generated by
the IDL compiler.  The operator=() that is generated will do the deep copy
for you... so here's your copy method

void copy( const Event& aEvent )
{
     theEvent = aEvent;
}

The problem with your code is that the length of a sequence must be set
before you use an element.  So the minimal fix to your code is:


   theEvent.variables.length( aEvent.variables.length() ); // <--- need
this line added
   for( int i = 0; i < aEvent.variables.length(); ++i )
   {
      AttributeValue av;    // Sequence a attribute values.
      av.Attribute = CORBA::string_dup( aEvent.variables[i].Attribute );
      av.Value     = CORBA::string_dup( aEvent.variables[i].Value );

      theEvent.variables[i] = av;  // program freezed here.....
   }

But using the highest grained operator=() method is the best approach to
your problem.

- Kendall



                                                                                                                               
                    Mark Johnson                                                                                               
                    <mark.johnson@onfiber.com>          To:     "OmniOrb Mailing List (E-mail)"                                
                    Sent by:                            <omniorb-list@uk.research.att.com>                                     
                    owner-omniorb-list@uk.resear        cc:                                                                    
                    ch.att.com                          Fax to:                                                                
                                                        Subject:     [omniORB] How to copy a corba struct with sequences?      
                                                                                                                               
                    10/09/2001 10:36 AM                                                                                        
                                                                                                                               
                                                                                                                               



I have built a C++ wrapper around a corba struct to carry temporary
information before passing it along, here is an excerpt of the class:

  class EventInfo
  {
  public:
           void copy( const Event & aEvent )
           {
                     theEvent.identify = CORBA::string_dup( aEvent.identity
);
                     theEvent.priority = aEvent.priority;

                     // how to copy the 'variables'?
           }
           const Event & getEvent() const { return theEvent; }

  private:
           Event theEvent; // corba struct
  };

the Event IDL looks like this:

  struct AttributeValue
  {
        string _Attribute;
        string _Value;
  };
  typedef sequence<AttributeValue> AtrributeValueSeq;
  struct Event
  {
    string _identity;
    short  _priority;
    AtrributeValueSeq    _variables;    // Sequence a attribute values.

  };

This is how I was copying the variables but the for() loop froze when I
went
to add a new item to the sequence:

   for( int i = 0; i < aEvent.variables.length(); ++i )
   {
      AttributeValue av;    // Sequence a attribute values.
      av.Attribute = CORBA::string_dup( aEvent.variables[i].Attribute );
      av.Value     = CORBA::string_dup( aEvent.variables[i].Value );

      theEvent.variables[i] = av;  // program freezed here.....
   }

Anyway, if someone could help me with this or has a better idea as to how
to
carry build the EventInfo class it would be very helpful...

thanks for your time!