Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TAO_Unbounded_String_Sequence Class Reference

Unbounded sequence of strings. More...

#include <Sequence.h>

Inheritance diagram for TAO_Unbounded_String_Sequence:

Inheritance graph
[legend]
Collaboration diagram for TAO_Unbounded_String_Sequence:

Collaboration graph
[legend]
List of all members.

Public Methods

 TAO_Unbounded_String_Sequence (void)
 TAO_Unbounded_String_Sequence (CORBA::ULong maximum)
 TAO_Unbounded_String_Sequence (CORBA::ULong maximum, CORBA::ULong length, char **data, CORBA::Boolean release=0)
 TAO_Unbounded_String_Sequence (const TAO_Unbounded_String_Sequence &)
 ~TAO_Unbounded_String_Sequence (void)
 The destructor releases all object reference members and frees all string members. More...

TAO_Unbounded_String_Sequence & operator= (const TAO_Unbounded_String_Sequence &)
TAO_SeqElem_String_Manager operator[] (CORBA::ULong slot) const
 read-write accessor. More...

char ** get_buffer (CORBA::Boolean orphan=0)
const char ** get_buffer (void) const
virtual void _allocate_buffer (CORBA::ULong length)
virtual void _deallocate_buffer (void)
 Must deallocate the buffer and then set it to zero. More...

virtual void _shrink_buffer (CORBA::ULong new_length, CORBA::ULong old_length)
void replace (CORBA::ULong maximum, CORBA::ULong length, char **data, CORBA::Boolean release=0)

Static Public Methods

char ** allocbuf (CORBA::ULong)
void freebuf (char **)

Detailed Description

Unbounded sequence of strings.

IDL sequences of strings must automatically duplicate and release their members based on some global <release> flag.

Definition at line 238 of file Sequence.h.


Constructor & Destructor Documentation

ACE_INLINE TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence void   
 

{orbos/97-05-15:16.8} The default constructor initializes object reference members to appropriately typed nil object references and string members to NULL; all other members are initialized via their default constructors.

{orbos/97-05-15:16.11} For both bounded and unbounded sequences, the default constructor (as shown in the example above) sets the sequence length equal to 0.

Definition at line 170 of file Sequence.i.

00171 {
00172 }

TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence CORBA::ULong    maximum
 

Unbounded sequences provide a constructor that allows only the initial value of the maximum length to be set (the ``maximum constructor'' shown in the example above). This allows applications to control how much buffer space is initially allocated by the sequence. This constructor also sets the length to 0 and the release flag to TRUE.

Definition at line 61 of file Sequence.cpp.

ACE_INLINE TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence CORBA::ULong    maximum,
CORBA::ULong    length,
char **    data,
CORBA::Boolean    release = 0
 

The ``T *data'' constructor (as shown in the example above) allows the length and contents of a bounded or unbounded sequence to be set. For unbounded sequences, it also allows the initial value of the maximum length to be set. For this constructor, ownership of the content's vector is determined by the release parameter---FALSE means the caller owns the storage, while TRUE means that the sequence assumes ownership of the storage. If release is TRUE, the content's vector must have been allocated using the sequence allocbuf function, and the sequence will pass it to freebuf when finished with it.

Definition at line 176 of file Sequence.i.

00180   : TAO_Unbounded_Base_Sequence (maximum, length, value, release)
00181 {
00182 }

TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence const TAO_Unbounded_String_Sequence &   
 

The copy constructor performs a deep copy from the existing structure to create a new structure, including calling _duplicate on all object reference members and performing the necessary heap allocations for all string members.

The copy constructor creates a new sequence with the same maximum and length as the given sequence, copies each of its current elements (items zero through length-1), and sets the release flag to TRUE.

Definition at line 71 of file Sequence.cpp.

References allocbuf, TAO_Base_Sequence::buffer_, and CORBA::string_dup.

00074   : TAO_Unbounded_Base_Sequence (rhs)
00075 {
00076   if (rhs.buffer_ != 0)
00077     {
00078       char* *tmp1 =
00079         TAO_Unbounded_String_Sequence::allocbuf (this->maximum_);
00080 
00081       char ** const tmp2 =
00082         ACE_reinterpret_cast (char ** ACE_CAST_CONST, rhs.buffer_);
00083 
00084       for (CORBA::ULong i = 0; i < rhs.length_; ++i)
00085         {
00086           tmp1[i] = CORBA::string_dup (tmp2[i]);
00087         }
00088 
00089       this->buffer_ = tmp1;
00090     }
00091   else
00092     {
00093       this->buffer_ = 0;
00094     }
00095 }

TAO_Unbounded_String_Sequence::~TAO_Unbounded_String_Sequence void   
 

The destructor releases all object reference members and frees all string members.

Definition at line 97 of file Sequence.cpp.

References _deallocate_buffer.

00098 {
00099   this->_deallocate_buffer ();
00100 }


Member Function Documentation

void TAO_Unbounded_String_Sequence::_allocate_buffer CORBA::ULong    length [virtual]
 

Ensure that the buffer contains space for at least <length> elements. The constructor must be called for any new elements, the old ones (if any) must be copied into the buffer using operator= and then their destructors must be called. Finally the old buffer must be released.

Implements TAO_Base_Sequence.

Definition at line 251 of file Sequence.cpp.

References allocbuf, TAO_Base_Sequence::buffer_, TAO_Unbounded_Base_Sequence::length, TAO_Base_Sequence::length_, TAO_Base_Sequence::release_, and CORBA::string_dup.

00252 {
00253   char* *tmp = TAO_Unbounded_String_Sequence::allocbuf (length);
00254 
00255   if (this->buffer_ != 0)
00256     {
00257       char* *old = ACE_reinterpret_cast (char **, this->buffer_);
00258 
00259       for (CORBA::ULong i = 0; i < this->length_; ++i)
00260         {
00261           // Only call duplicate when we did not own the previous
00262           // buffer, since after this method we own it we must also
00263           // own the objects.  If we already own the objects there is
00264           // no need to copy them, if we did we would also have to
00265           // remove the old instances.
00266           if (!this->release_)
00267             {
00268               tmp [i] = CORBA::string_dup (old[i]);
00269             }
00270           else
00271             {
00272               tmp [i] = old[i];
00273             }
00274         }
00275 
00276       if (this->release_)
00277         {
00278           delete [] old;
00279         }
00280     }
00281 
00282   this->buffer_ = tmp;
00283 }

void TAO_Unbounded_String_Sequence::_deallocate_buffer void    [virtual]
 

Must deallocate the buffer and then set it to zero.

Implements TAO_Base_Sequence.

Definition at line 286 of file Sequence.cpp.

References TAO_Base_Sequence::buffer_, freebuf, TAO_Base_Sequence::length_, TAO_Base_Sequence::release_, and CORBA::string_free.

Referenced by ~TAO_Unbounded_String_Sequence.

00287 {
00288   if (this->buffer_ == 0 || this->release_ == 0)
00289     {
00290       return;
00291     }
00292 
00293   char **tmp = ACE_reinterpret_cast (char **, this->buffer_);
00294 
00295   for (CORBA::ULong i = 0; i < this->length_; ++i)
00296     {
00297       CORBA::string_free (tmp[i]);
00298       tmp[i] = 0;
00299     }
00300 
00301   TAO_Unbounded_String_Sequence::freebuf (tmp);
00302   this->buffer_ = 0;
00303 }

void TAO_Unbounded_String_Sequence::_shrink_buffer CORBA::ULong    new_length,
CORBA::ULong    old_length
[virtual]
 

Some sequences (of objects and strings) require some cleanup if the sequence is shrunk. The spec requires the destructor to release the objects only from position <0> to <length-1>; so shrink and then delete could result in a memory leak.

Reimplemented from TAO_Base_Sequence.

Definition at line 306 of file Sequence.cpp.

References CORBA::string_free.

00308 {
00309   char **tmp = ACE_reinterpret_cast (char **, 
00310                                      this->buffer_);
00311 
00312   for (CORBA::ULong i = nl; i < ol; ++i)
00313     {
00314       CORBA::string_free (tmp[i]);
00315       tmp[i] = 0;
00316     }
00317 }

char ** TAO_Unbounded_String_Sequence::allocbuf CORBA::ULong    [static]
 

The allocbuf function allocates a vector of T elements that can be passed to the T *data constructor. The length of the vector is given by the nelems function argument. The allocbuf function initializes each element using its default constructor, except for strings, which are initialized to null pointers, and object references, which are initialized to suitably typed nil object references. A null pointer is returned if allocbuf for some reason cannot allocate the requested vector. Vectors allocated by allocbuf should be freed using the freebuf function.

Definition at line 169 of file Sequence.cpp.

References ACE_NEW_RETURN.

Referenced by _allocate_buffer, get_buffer, operator=, and TAO_Unbounded_String_Sequence.

00170 {
00171   char **buf;
00172   ACE_NEW_RETURN (buf,
00173                   char *[nelems],
00174                   0);
00175 
00176   for (CORBA::ULong i = 0; i < nelems; ++i)
00177     {
00178       buf[i] = 0;
00179     }
00180 
00181   return buf;
00182 }

void TAO_Unbounded_String_Sequence::freebuf char **    [static]
 

The freebuf function ensures that the destructor for each element is called before the buffer is destroyed, except for string elements, which are freed using string_free(), and object reference elements, which are freed using release(). The freebuf function will ignore null pointers passed to it.

Definition at line 185 of file Sequence.cpp.

Referenced by _deallocate_buffer, and operator=.

00186 {
00187   if (buffer == 0)
00188     {
00189       return;
00190     }
00191 
00192   // {orbos/97-05-15:16.11}
00193   // The freebuf function ensures that the destructor for each element
00194   // is called before the buffer is destroyed, except for string
00195   // elements, which are freed using string_free(), and object
00196   // reference elements, which are freed using release(). The freebuf
00197   // function will ignore null pointers passed to it.
00198 
00199   // @@ How are we supposed to implement that! We don't know the
00200   // length of the buffer here.
00201   //  Mark the length in the first four bytes? For the moment we let
00202   //  that be.
00203 
00204   delete [] buffer;
00205 }

const char ** TAO_Unbounded_String_Sequence::get_buffer void    const
 

Definition at line 244 of file Sequence.cpp.

00245 {
00246   return ACE_reinterpret_cast (const char** ACE_CAST_CONST, 
00247                                this->buffer_);
00248 }

char ** TAO_Unbounded_String_Sequence::get_buffer CORBA::Boolean    orphan = 0
 

Definition at line 208 of file Sequence.cpp.

References allocbuf, TAO_Base_Sequence::buffer_, TAO_Base_Sequence::length_, TAO_Base_Sequence::maximum_, and TAO_Base_Sequence::release_.

00209 {
00210   char* *result = 0;
00211 
00212   if (orphan == 0)
00213     {
00214       // We retain ownership.
00215       if (this->buffer_ == 0)
00216         {
00217           result = allocbuf (this->length_);
00218           this->buffer_ = result;
00219                 this->release_ = 1;
00220         }
00221       else
00222         {
00223           result = ACE_reinterpret_cast (char**, this->buffer_);
00224         }
00225     }
00226   else // if (orphan == 1)
00227     {
00228       if (this->release_ != 0)
00229         {
00230           // We set the state back to default and relinquish
00231           // ownership.
00232           result = ACE_reinterpret_cast (char**, this->buffer_);
00233           this->maximum_ = 0;
00234           this->length_ = 0;
00235           this->buffer_ = 0;
00236           this->release_ = 0;
00237         }
00238     }
00239 
00240   return result;
00241 }

TAO_Unbounded_String_Sequence & TAO_Unbounded_String_Sequence::operator= const TAO_Unbounded_String_Sequence &   
 

The assignment operator first releases all object reference members and frees all string members, and then performs a deep­copy to create a new structure.

The assignment operator deep­copies its parameter, releasing old storage if necessary. It behaves as if the original sequence is destroyed via its destructor and then the source sequence copied using the copy constructor. If release=TRUE, the destructor destroys each of the current elements (items zero through length--1). For an unbounded sequence, if a reallocation is necessary due to a change in the length and the sequence was created using the release=TRUE parameter in its constructor, the sequence will deallocate the old storage. If release is FALSE under these circumstances, old storage will not be freed before the reallocation is performed. After reallocation, the release flag is always set to TRUE.

Definition at line 104 of file Sequence.cpp.

References allocbuf, TAO_Base_Sequence::buffer_, freebuf, TAO_Base_Sequence::length_, TAO_Base_Sequence::maximum_, TAO_Base_Sequence::operator=, TAO_Base_Sequence::release_, CORBA::string_dup, and CORBA::string_free.

00105 {
00106   if (this == &rhs)
00107     {
00108       return *this;
00109     }
00110 
00111   if (this->release_)
00112     {
00113       char **tmp = ACE_reinterpret_cast (char **, 
00114                                          this->buffer_);
00115 
00116       for (CORBA::ULong i = 0; i < this->length_; ++i)
00117         {
00118           CORBA::string_free (tmp[i]);
00119           tmp[i] = 0;
00120         }
00121 
00122       if (this->maximum_ < rhs.maximum_)
00123         {
00124           // free the older buffer
00125           TAO_Unbounded_String_Sequence::freebuf (tmp);
00126           this->buffer_ =
00127             TAO_Unbounded_String_Sequence::allocbuf (rhs.maximum_);
00128         }
00129     }
00130   else
00131     {
00132       if (rhs.maximum_ == 0)
00133         {
00134           this->buffer_ = 0;
00135         }
00136       else
00137         {
00138           this->buffer_ =
00139             TAO_Unbounded_String_Sequence::allocbuf (rhs.maximum_);
00140         }
00141     }
00142 
00143   TAO_Unbounded_Base_Sequence::operator= (rhs);
00144 
00145   char **tmp1 = ACE_reinterpret_cast (char **, this->buffer_);
00146   char ** const tmp2 = ACE_reinterpret_cast (char ** ACE_CAST_CONST,
00147                                              rhs.buffer_);
00148 
00149   for (CORBA::ULong i = 0; i < rhs.length_; ++i)
00150     {
00151       tmp1[i] = CORBA::string_dup (tmp2[i]);
00152     }
00153 
00154   return *this;
00155 }

TAO_SeqElem_String_Manager TAO_Unbounded_String_Sequence::operator[] CORBA::ULong    slot const
 

read-write accessor.

Definition at line 158 of file Sequence.cpp.

References ACE_ASSERT, and TAO_Base_Sequence::maximum_.

00159 {
00160   ACE_ASSERT (slot < this->maximum_);
00161   char **const tmp =
00162     ACE_reinterpret_cast (char **ACE_CAST_CONST,
00163                           this->buffer_);
00164   return TAO_SeqElem_String_Manager (tmp + slot,
00165                                      this->release_);
00166 }

void TAO_Unbounded_String_Sequence::replace CORBA::ULong    maximum,
CORBA::ULong    length,
char **    data,
CORBA::Boolean    release = 0
 

Definition at line 320 of file Sequence.cpp.

References TAO_Base_Sequence::buffer_, TAO_Unbounded_Base_Sequence::length, TAO_Base_Sequence::length_, TAO_Base_Sequence::maximum, TAO_Base_Sequence::maximum_, TAO_Base_Sequence::release, TAO_Base_Sequence::release_, and CORBA::string_free.

00324 {
00325   if (this->release_ == 1)
00326     {
00327       char **tmp = ACE_reinterpret_cast (char **, 
00328                                          this->buffer_);
00329 
00330       for (CORBA::ULong i = 0; i < this->length_; ++i)
00331         {
00332           CORBA::string_free (tmp[i]);
00333           tmp[i] = 0;
00334         }
00335     }
00336 
00337   this->maximum_ = maximum;
00338   this->length_ = length;
00339 
00340   // If 'release' is 1, it is the caller's responsibility to allocate
00341   // 'data' with CORBA::string_alloc. 
00342   this->buffer_ = data;
00343   this->release_ = release;
00344 }


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 15:45:47 2003 for TAO by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002