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

Sequence.i

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: Sequence.i,v 1.1.1.4 2003/02/21 18:36:45 chad Exp $
00003 
00004 // Operations on the unbounded sequence class.
00005 
00006 ACE_INLINE
00007 TAO_Base_Sequence::TAO_Base_Sequence (void)
00008   : maximum_ (0),
00009     length_ (0),
00010     buffer_ (0),
00011     release_ (0)
00012 {
00013 }
00014 
00015 ACE_INLINE
00016 TAO_Base_Sequence::TAO_Base_Sequence (CORBA::ULong maximum,
00017                                       CORBA::ULong length,
00018                                       void *buffer,
00019                                       CORBA::Boolean release)
00020   : maximum_ (maximum),
00021     length_ (length),
00022     buffer_ (buffer),
00023     release_ (release)
00024 {
00025 }
00026 
00027 ACE_INLINE
00028 TAO_Base_Sequence::TAO_Base_Sequence (CORBA::ULong maximum,
00029                                       void *data)
00030   : maximum_ (maximum),
00031     length_ (0),
00032     buffer_ (data),
00033     release_ (1)
00034 {
00035 }
00036 
00037 ACE_INLINE
00038 TAO_Base_Sequence::TAO_Base_Sequence (const TAO_Base_Sequence &rhs)
00039   : maximum_ (rhs.maximum_),
00040     length_ (rhs.length_),
00041     buffer_ (0),
00042     release_ (1)
00043 {
00044 }
00045 
00046 ACE_INLINE CORBA::Boolean
00047 TAO_Base_Sequence::release (void) const
00048 {
00049   return this->release_;
00050 }
00051 
00052 ACE_INLINE TAO_Base_Sequence &
00053 TAO_Base_Sequence::operator= (const TAO_Base_Sequence &rhs)
00054 {
00055   this->maximum_ = rhs.maximum_;
00056   this->length_ = rhs.length_;
00057   this->release_ = 1;
00058   return *this;
00059 }
00060 
00061 ACE_INLINE CORBA::ULong
00062 TAO_Base_Sequence::maximum (void) const
00063 {
00064   return this->maximum_;
00065 }
00066 
00067 // ****************************************************************
00068 
00069 ACE_INLINE
00070 TAO_Unbounded_Base_Sequence::TAO_Unbounded_Base_Sequence (void)
00071 {
00072 }
00073 
00074 ACE_INLINE
00075 
00076 TAO_Unbounded_Base_Sequence::TAO_Unbounded_Base_Sequence (CORBA::ULong maximum,
00077                              CORBA::ULong length,
00078                              void* buffer,
00079                              CORBA::Boolean release)
00080   :  TAO_Base_Sequence (maximum, length, buffer, release)
00081 {
00082 }
00083 
00084 ACE_INLINE
00085 
00086 TAO_Unbounded_Base_Sequence::TAO_Unbounded_Base_Sequence (CORBA::ULong maximum,
00087                              void* buffer)
00088   :  TAO_Base_Sequence (maximum, buffer)
00089 {
00090 }
00091 
00092 ACE_INLINE CORBA::ULong
00093 TAO_Unbounded_Base_Sequence::length (void) const
00094 {
00095   return this->length_;
00096 }
00097 
00098 ACE_INLINE void
00099 TAO_Unbounded_Base_Sequence::length (CORBA::ULong length)
00100 {
00101   if (length > this->maximum_)
00102     {
00103       this->_allocate_buffer (length);
00104       this->maximum_ = length;
00105       this->release_ = 1;
00106     }
00107   else if (length < this->length_)
00108     {
00109       this->_shrink_buffer (length, this->length_);
00110     }
00111   this->length_ = length;
00112 }
00113 
00114 // ****************************************************************
00115 
00116 ACE_INLINE
00117 TAO_Bounded_Base_Sequence::TAO_Bounded_Base_Sequence (void)
00118 {
00119 }
00120 
00121 ACE_INLINE
00122 
00123 TAO_Bounded_Base_Sequence::TAO_Bounded_Base_Sequence (CORBA::ULong maximum,
00124                            CORBA::ULong length,
00125                            void* buffer,
00126                            CORBA::Boolean release)
00127   :  TAO_Base_Sequence (maximum, length, buffer, release)
00128 {
00129 }
00130 
00131 ACE_INLINE
00132 
00133 TAO_Bounded_Base_Sequence::TAO_Bounded_Base_Sequence (CORBA::ULong maximum,
00134                            void* buffer)
00135   :  TAO_Base_Sequence (maximum, buffer)
00136 {
00137 }
00138 
00139 ACE_INLINE CORBA::ULong
00140 TAO_Bounded_Base_Sequence::length (void) const
00141 {
00142   return this->length_;
00143 }
00144 
00145 ACE_INLINE void
00146 TAO_Bounded_Base_Sequence::length (CORBA::ULong length)
00147 {
00148   if (this->buffer_ == 0)
00149     {
00150       this->_allocate_buffer (this->maximum_);
00151       this->release_ = 1;
00152     }
00153 
00154   if (length > this->maximum_)
00155     {
00156       return;
00157     }
00158   else if (length < this->length_)
00159     {
00160       this->_shrink_buffer (length, this->length_);
00161     }
00162 
00163   this->length_ = length;
00164 }
00165 
00166 // ****************************************************************
00167 
00168 //default constructors
00169 ACE_INLINE
00170 TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence (void)
00171 {
00172 }
00173 
00174 ACE_INLINE
00175 
00176 TAO_Unbounded_String_Sequence::TAO_Unbounded_String_Sequence (CORBA::ULong maximum,
00177                                CORBA::ULong length,
00178                                char* *value,
00179                                CORBA::Boolean release)
00180   : TAO_Unbounded_Base_Sequence (maximum, length, value, release)
00181 {
00182 }
00183 
00184 ACE_INLINE
00185 TAO_Unbounded_WString_Sequence::TAO_Unbounded_WString_Sequence (void)
00186 {
00187 }
00188 
00189 ACE_INLINE
00190 
00191 TAO_Unbounded_WString_Sequence::TAO_Unbounded_WString_Sequence (CORBA::ULong maximum,
00192                                CORBA::ULong length,
00193                                CORBA::WChar* *value,
00194                                CORBA::Boolean release)
00195   : TAO_Unbounded_Base_Sequence (maximum, length, value, release)
00196 {
00197 }
00198 
00199 // ****************************************************************
00200 
00201 ACE_INLINE CORBA::Octet *
00202 TAO_Unbounded_Sequence<CORBA::Octet>::allocbuf (CORBA::ULong size)
00203 {
00204   return new CORBA::Octet[size];
00205 }
00206 
00207 ACE_INLINE void
00208 TAO_Unbounded_Sequence<CORBA::Octet>::freebuf (CORBA::Octet *buffer)
00209 {
00210   delete [] buffer;
00211 }
00212 
00213 ACE_INLINE
00214 TAO_Unbounded_Sequence<CORBA::Octet>::TAO_Unbounded_Sequence (void)
00215 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00216   :  mb_ (0)
00217 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00218 {
00219 }
00220 
00221 ACE_INLINE
00222 TAO_Unbounded_Sequence<CORBA::Octet>::TAO_Unbounded_Sequence (CORBA::ULong maximum)
00223   : TAO_Unbounded_Base_Sequence (maximum,
00224                                  TAO_Unbounded_Sequence<CORBA::Octet>::allocbuf (maximum))
00225 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00226   ,  mb_ (0)
00227 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00228 {
00229 }
00230 
00231 ACE_INLINE
00232 TAO_Unbounded_Sequence<CORBA::Octet>::TAO_Unbounded_Sequence (CORBA::ULong maximum,
00233                                                    CORBA::ULong length,
00234                                                    CORBA::Octet *data,
00235                                                    CORBA::Boolean release)
00236   : TAO_Unbounded_Base_Sequence (maximum, length, data, release)
00237 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00238   , mb_ (0)
00239 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00240 {
00241 }
00242 
00243 ACE_INLINE const CORBA::Octet *
00244 TAO_Unbounded_Sequence<CORBA::Octet>::get_buffer (void) const
00245 {
00246   return ACE_reinterpret_cast(const CORBA::Octet * ACE_CAST_CONST, this->buffer_);
00247 }
00248 
00249 // This function is a little too big to be inlined, but some compilers
00250 // (Sun/CC 4.1?) die if it isn't :-(
00251 ACE_INLINE CORBA::Octet *
00252 TAO_Unbounded_Sequence<CORBA::Octet>::get_buffer (CORBA::Boolean orphan)
00253 {
00254   CORBA::Octet *result = 0;
00255   if (orphan == 0)
00256     {
00257       // We retain ownership.
00258 
00259       if (this->buffer_ == 0)
00260         {
00261           // The buffer was not allocated, we must allocate it now.
00262           result = TAO_Unbounded_Sequence<CORBA::Octet>::allocbuf (this->length_);
00263           this->buffer_ = result;
00264           this->release_ = 1;
00265         }
00266       else
00267         {
00268           result =
00269             ACE_reinterpret_cast (CORBA::Octet*,this->buffer_);
00270         }
00271     }
00272 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00273   else if (this->mb_ != 0) // (orphan == 1)
00274     {
00275       // We must create a copy anyway:
00276       //   the user is supposed to call freebuf() to release the
00277       //   buffer, but the buffer is inside a Message_Block...
00278       //   We thought about storing the pointer to the Message_Block
00279       //   somewhere at the beginning of the buffer (before the actual
00280       //   data), but that will not work in 64 bit machines when the
00281       //   buffer comes from a CDR stream.
00282       //
00283       result = TAO_Unbounded_Sequence<CORBA::Octet>::allocbuf (this->length_);
00284       ACE_OS::memcpy (result, this->buffer_, this->length_);
00285     }
00286   else if (this->release_ != 0)
00287     {
00288       // We set the state back to default and relinquish
00289       // ownership.
00290       result = ACE_reinterpret_cast(CORBA::Octet*,this->buffer_);
00291       this->maximum_ = 0;
00292       this->length_ = 0;
00293       this->buffer_ = 0;
00294       this->release_ = 0;
00295     }
00296 #else /* TAO_NO_COPY_OCTET_SEQUENCES == 0 */
00297   else
00298     {
00299       result = ACE_reinterpret_cast (CORBA::Octet*,this->buffer_);
00300       if (this->release_ != 0)
00301         {
00302           // We set the state back to default and relinquish
00303           // ownership.
00304           this->maximum_ = 0;
00305           this->length_ = 0;
00306           this->buffer_ = 0;
00307           this->release_ = 0;
00308         }
00309     }
00310 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 0 */
00311   /* else
00312      // Oops, it's not our buffer to relinquish...
00313      return 0;
00314   */
00315   return result;
00316 }
00317 
00318 ACE_INLINE CORBA::Octet &
00319 TAO_Unbounded_Sequence<CORBA::Octet>::operator[] (CORBA::ULong i)
00320 {
00321   ACE_ASSERT (i < this->maximum_);
00322   CORBA::Octet* tmp = ACE_reinterpret_cast(CORBA::Octet*,this->buffer_);
00323   return tmp[i];
00324 }
00325 
00326 ACE_INLINE const CORBA::Octet &
00327 TAO_Unbounded_Sequence<CORBA::Octet>::operator[] (CORBA::ULong i) const
00328 {
00329   ACE_ASSERT (i < this->maximum_);
00330   CORBA::Octet * const tmp = ACE_reinterpret_cast (CORBA::Octet* ACE_CAST_CONST, this->buffer_);
00331   return tmp[i];
00332 }
00333 
00334 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00335 ACE_INLINE ACE_Message_Block*
00336 TAO_Unbounded_Sequence<CORBA::Octet>::mb (void) const
00337 {
00338   return this->mb_;
00339 }
00340 
00341 ACE_INLINE void
00342 TAO_Unbounded_Sequence<CORBA::Octet>::replace (CORBA::ULong length,
00343                                                const ACE_Message_Block* mb)
00344 {
00345   this->_deallocate_buffer ();
00346 
00347   // Get the message block flags.
00348   ACE_Message_Block::Message_Flags flg = mb->self_flags ();
00349 
00350   // If the DONT_DELETE flag is disabled just a duplicate would
00351   // help. If the DONT_DELETE flag is enabled a deep copy is needed as
00352   // the contents would be on stack. Just incrementing the ref count
00353   // on the stack based data block would only crash the program when
00354   // the stack unwinds
00355   if (ACE_BIT_DISABLED (flg,
00356                         ACE_Message_Block::DONT_DELETE))
00357     {
00358       this->mb_ = ACE_Message_Block::duplicate (mb);
00359     }
00360   else
00361     {
00362       // As we are in CORBA mode, all the data blocks would be aligned
00363       // on an 8 byte boundary
00364       ACE_Message_Block msgb (*mb,
00365                               ACE_CDR::MAX_ALIGNMENT);
00366 
00367       // Get the base pointer of the incoming message block
00368       char *start = ACE_ptr_align_binary (mb->base (),
00369                                           ACE_CDR::MAX_ALIGNMENT);
00370 
00371       // Get the read and write displacements in the incoming stream
00372       size_t rd_pos = mb->rd_ptr () - start;
00373       size_t wr_pos = mb->wr_ptr () - start;
00374 
00375       this->mb_ = ACE_Message_Block::duplicate (&msgb);
00376 
00377       this->mb_->rd_ptr (rd_pos);
00378       this->mb_->wr_ptr (wr_pos);
00379     }
00380 
00381   this->buffer_ = this->mb_->rd_ptr ();
00382   this->maximum_ = length;
00383   this->length_ = length;
00384   this->release_ = 0;
00385 }
00386 
00387 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00388 
00389 ACE_INLINE void
00390 TAO_Unbounded_Sequence<CORBA::Octet>::replace (CORBA::ULong max,
00391                                                CORBA::ULong length,
00392                                                CORBA::Octet *data,
00393                                                CORBA::Boolean release)
00394 {
00395   this->maximum_ = max;
00396   this->length_ = length;
00397 
00398 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00399   if (this->mb_ != 0)
00400     {
00401       ACE_Message_Block::release (this->mb_);
00402       this->mb_ = 0;
00403     }
00404   else
00405 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00406     if (this->buffer_ && this->release_ == 1)
00407     {
00408       CORBA::Octet* tmp = ACE_reinterpret_cast(CORBA::Octet*,this->buffer_);
00409       TAO_Unbounded_Sequence<CORBA::Octet>::freebuf (tmp);
00410     }
00411   this->buffer_ = data;
00412   this->release_ = release;
00413 }

Generated on Mon Jun 16 13:49:43 2003 for TAO by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002