CDR_Stream.cpp

Go to the documentation of this file.
00001 #include "ace/CDR_Stream.h"
00002 #include "ace/SString.h"
00003 #include "ace/Auto_Ptr.h"
00004 
00005 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00006 #include "Monitor_Size.h"
00007 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00008 
00009 #if !defined (__ACE_INLINE__)
00010 # include "ace/CDR_Stream.inl"
00011 #endif /* ! __ACE_INLINE__ */
00012 
00013 ACE_RCSID (ace,
00014            CDR_Stream,
00015            "$Id: CDR_Stream.cpp 81691 2008-05-14 11:09:21Z johnnyw $")
00016 
00017 // ****************************************************************
00018 
00019 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 size_t ACE_OutputCDR::wchar_maxbytes_ = sizeof (ACE_CDR::WChar);
00022 
00023 ACE_OutputCDR::ACE_OutputCDR (size_t size,
00024                               int byte_order,
00025                               ACE_Allocator *buffer_allocator,
00026                               ACE_Allocator *data_block_allocator,
00027                               ACE_Allocator *message_block_allocator,
00028                               size_t memcpy_tradeoff,
00029                               ACE_CDR::Octet major_version,
00030                               ACE_CDR::Octet minor_version)
00031   :  start_ ((size ? size : (size_t) ACE_CDR::DEFAULT_BUFSIZE) + ACE_CDR::MAX_ALIGNMENT,
00032              ACE_Message_Block::MB_DATA,
00033              0,
00034              0,
00035              buffer_allocator,
00036              0,
00037              0,
00038              ACE_Time_Value::zero,
00039              ACE_Time_Value::max_time,
00040              data_block_allocator,
00041              message_block_allocator),
00042 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00043      current_alignment_ (0),
00044 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00045      current_is_writable_ (true),
00046      do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00047      good_bit_ (true),
00048      memcpy_tradeoff_ (memcpy_tradeoff),
00049      major_version_ (major_version),
00050      minor_version_ (minor_version),
00051      char_translator_ (0),
00052      wchar_translator_ (0)
00053 
00054 {
00055   ACE_CDR::mb_align (&this->start_);
00056   this->current_ = &this->start_;
00057 
00058 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00059   ACE_NEW (this->monitor_,
00060            ACE::Monitor_Control::Size_Monitor);
00061   this->monitor_->receive (this->total_length ());
00062 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00063 }
00064 
00065 ACE_OutputCDR::ACE_OutputCDR (char *data,
00066                               size_t size,
00067                               int byte_order,
00068                               ACE_Allocator *buffer_allocator,
00069                               ACE_Allocator *data_block_allocator,
00070                               ACE_Allocator *message_block_allocator,
00071                               size_t memcpy_tradeoff,
00072                               ACE_CDR::Octet major_version,
00073                               ACE_CDR::Octet minor_version)
00074   :  start_ (size,
00075              ACE_Message_Block::MB_DATA,
00076              0,
00077              data,
00078              buffer_allocator,
00079              0,
00080              0,
00081              ACE_Time_Value::zero,
00082              ACE_Time_Value::max_time,
00083              data_block_allocator,
00084              message_block_allocator),
00085 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00086      current_alignment_ (0),
00087 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00088      current_is_writable_ (true),
00089      do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00090      good_bit_ (true),
00091      memcpy_tradeoff_ (memcpy_tradeoff),
00092      major_version_ (major_version),
00093      minor_version_ (minor_version),
00094      char_translator_ (0),
00095      wchar_translator_ (0)
00096 {
00097   // We cannot trust the buffer to be properly aligned
00098   ACE_CDR::mb_align (&this->start_);
00099   this->current_ = &this->start_;
00100 
00101 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00102   ACE_NEW (this->monitor_,
00103            ACE::Monitor_Control::Size_Monitor);
00104   this->monitor_->receive (this->total_length ());
00105 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00106 }
00107 
00108 ACE_OutputCDR::ACE_OutputCDR (ACE_Data_Block *data_block,
00109                               int byte_order,
00110                               ACE_Allocator *message_block_allocator,
00111                               size_t memcpy_tradeoff,
00112                               ACE_CDR::Octet major_version,
00113                               ACE_CDR::Octet minor_version)
00114   :  start_ (data_block,
00115              ACE_Message_Block::DONT_DELETE,
00116              message_block_allocator),
00117 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00118      current_alignment_ (0),
00119 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00120      current_is_writable_ (true),
00121      do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00122      good_bit_ (true),
00123      memcpy_tradeoff_ (memcpy_tradeoff),
00124      major_version_ (major_version),
00125      minor_version_ (minor_version),
00126      char_translator_ (0),
00127      wchar_translator_ (0)
00128 {
00129   // We cannot trust the buffer to be properly aligned
00130   ACE_CDR::mb_align (&this->start_);
00131   this->current_ = &this->start_;
00132 
00133 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00134   ACE_NEW (this->monitor_,
00135            ACE::Monitor_Control::Size_Monitor);
00136   this->monitor_->receive (this->total_length ());
00137 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00138 }
00139 
00140 ACE_OutputCDR::ACE_OutputCDR (ACE_Message_Block *data,
00141                               int byte_order,
00142                               size_t memcpy_tradeoff,
00143                               ACE_CDR::Octet major_version,
00144                               ACE_CDR::Octet minor_version)
00145   :  start_ (data->data_block ()->duplicate ()),
00146 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00147      current_alignment_ (0),
00148 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00149      current_is_writable_ (true),
00150      do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00151      good_bit_ (true),
00152      memcpy_tradeoff_ (memcpy_tradeoff),
00153      major_version_ (major_version),
00154      minor_version_ (minor_version),
00155      char_translator_ (0),
00156      wchar_translator_ (0)
00157 {
00158   // We cannot trust the buffer to be properly aligned
00159   ACE_CDR::mb_align (&this->start_);
00160   this->current_ = &this->start_;
00161 
00162 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00163   ACE_NEW (this->monitor_,
00164            ACE::Monitor_Control::Size_Monitor);
00165   this->monitor_->receive (this->total_length ());
00166 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00167 }
00168 
00169 /*static*/ void
00170 ACE_OutputCDR::wchar_maxbytes (size_t maxbytes)
00171 {
00172   ACE_OutputCDR::wchar_maxbytes_ = maxbytes;
00173 }
00174 
00175 /*static*/ size_t
00176 ACE_OutputCDR::wchar_maxbytes ()
00177 {
00178   return ACE_OutputCDR::wchar_maxbytes_;
00179 }
00180 
00181 int
00182 ACE_OutputCDR::grow_and_adjust (size_t size,
00183                                 size_t align,
00184                                 char*& buf)
00185 {
00186   if (!this->current_is_writable_
00187       || this->current_->cont () == 0
00188       || this->current_->cont ()->size () < size + ACE_CDR::MAX_ALIGNMENT)
00189     {
00190       // Calculate the new buffer's length; if growing for encode, we
00191       // don't grow in "small" chunks because of the cost.
00192       size_t cursize = this->current_->size ();
00193       if (this->current_->cont () != 0)
00194         cursize = this->current_->cont ()->size ();
00195       size_t minsize = size;
00196 
00197 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00198       minsize += ACE_CDR::MAX_ALIGNMENT;
00199 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00200 
00201       // Make sure that there is enough room for <minsize> bytes, but
00202       // also make it bigger than whatever our current size is.
00203       if (minsize < cursize)
00204         minsize = cursize;
00205 
00206       size_t const newsize = ACE_CDR::next_size (minsize);
00207 
00208       this->good_bit_ = false;
00209       ACE_Message_Block* tmp = 0;
00210       ACE_NEW_RETURN (tmp,
00211                       ACE_Message_Block (newsize,
00212                                          ACE_Message_Block::MB_DATA,
00213                                          0,
00214                                          0,
00215                                          this->current_->data_block ()->allocator_strategy (),
00216                                          0,
00217                                          0,
00218                                          ACE_Time_Value::zero,
00219                                          ACE_Time_Value::max_time,
00220                                          this->current_->data_block ()->data_block_allocator ()),
00221                       -1);
00222 
00223       // Message block initialization may fail while the construction
00224       // succeds.  Since as a matter of policy, ACE may throw no
00225       // exceptions, we have to do a separate check like this.
00226       if (tmp != 0 && tmp->size () < newsize)
00227         {
00228           delete tmp;
00229           errno = ENOMEM;
00230           return -1;
00231         }
00232 
00233       this->good_bit_ = true;
00234 
00235 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00236       // The new block must start with the same alignment as the
00237       // previous block finished.
00238       ptrdiff_t const tmpalign =
00239         reinterpret_cast<ptrdiff_t> (tmp->rd_ptr ()) % ACE_CDR::MAX_ALIGNMENT;
00240       ptrdiff_t const curalign =
00241         static_cast<ptrdiff_t> (this->current_alignment_) % ACE_CDR::MAX_ALIGNMENT;
00242       ptrdiff_t offset = curalign - tmpalign;
00243       if (offset < 0)
00244         offset += ACE_CDR::MAX_ALIGNMENT;
00245       tmp->rd_ptr (static_cast<size_t> (offset));
00246       tmp->wr_ptr (tmp->rd_ptr ());
00247 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00248 
00249       // grow the chain and set the current block.
00250       tmp->cont (this->current_->cont ());
00251       this->current_->cont (tmp);
00252     }
00253   this->current_ = this->current_->cont ();
00254   this->current_is_writable_ = true;
00255 
00256   return this->adjust (size, align, buf);
00257 }
00258 
00259 ACE_CDR::Boolean
00260 ACE_OutputCDR::write_wchar (ACE_CDR::WChar x)
00261 {
00262   if (this->wchar_translator_ != 0)
00263     return (this->good_bit_ = this->wchar_translator_->write_wchar (*this, x));
00264   if (ACE_OutputCDR::wchar_maxbytes_ == 0)
00265     {
00266       errno = EACCES;
00267       return (this->good_bit_ = false);
00268     }
00269   if (static_cast<ACE_CDR::Short> (major_version_) == 1
00270       && static_cast<ACE_CDR::Short> (minor_version_) == 2)
00271     {
00272       ACE_CDR::Octet len =
00273         static_cast<ACE_CDR::Octet> (ACE_OutputCDR::wchar_maxbytes_);
00274       if (this->write_1 (&len))
00275         {
00276           if (ACE_OutputCDR::wchar_maxbytes_ == sizeof(ACE_CDR::WChar))
00277             return
00278               this->write_octet_array (
00279                                        reinterpret_cast<const ACE_CDR::Octet*> (&x),
00280                                        static_cast<ACE_CDR::ULong> (len));
00281           else
00282             if (ACE_OutputCDR::wchar_maxbytes_ == 2)
00283               {
00284                 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00285                 return
00286                   this->write_octet_array (
00287                                            reinterpret_cast<const ACE_CDR::Octet*> (&sx),
00288                                            static_cast<ACE_CDR::ULong> (len));
00289               }
00290             else
00291               {
00292                 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00293                 return
00294                   this->write_octet_array (
00295                                            reinterpret_cast<const ACE_CDR::Octet*> (&ox),
00296                                            static_cast<ACE_CDR::ULong> (len));
00297               }
00298         }
00299     }
00300   else if (static_cast<ACE_CDR::Short> (minor_version_) == 0)
00301     { // wchar is not allowed with GIOP 1.0.
00302       errno = EINVAL;
00303       return (this->good_bit_ = false);
00304     }
00305   if (ACE_OutputCDR::wchar_maxbytes_ == sizeof (ACE_CDR::WChar))
00306     {
00307       void const * const temp = &x;
00308       return
00309         this->write_4 (reinterpret_cast<const ACE_CDR::ULong *> (temp));
00310     }
00311   else if (ACE_OutputCDR::wchar_maxbytes_ == 2)
00312     {
00313       ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00314       return this->write_2 (reinterpret_cast<const ACE_CDR::UShort *> (&sx));
00315     }
00316   ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00317   return this->write_1 (reinterpret_cast<const ACE_CDR::Octet *> (&ox));
00318 }
00319 
00320 ACE_CDR::Boolean
00321 ACE_OutputCDR::write_string (ACE_CDR::ULong len,
00322                              const ACE_CDR::Char *x)
00323 {
00324   // @@ This is a slight violation of "Optimize for the common case",
00325   // i.e. normally the translator will be 0, but OTOH the code is
00326   // smaller and should be better for the cache ;-) ;-)
00327   if (this->char_translator_ != 0)
00328     return this->char_translator_->write_string (*this, len, x);
00329 
00330   if (len != 0)
00331     {
00332       if (this->write_ulong (len + 1))
00333         return this->write_char_array (x, len + 1);
00334     }
00335   else
00336     {
00337       // Be nice to programmers: treat nulls as empty strings not
00338       // errors. (OMG-IDL supports languages that don't use the C/C++
00339       // notion of null v. empty strings; nulls aren't part of the OMG-IDL
00340       // string model.)
00341       if (this->write_ulong (1))
00342         return this->write_char (0);
00343     }
00344 
00345   return (this->good_bit_ = false);
00346 }
00347 
00348 ACE_CDR::Boolean
00349 ACE_OutputCDR::write_string (const ACE_CString &x)
00350 {
00351   // @@ Leave this method in here, not the `.i' file so that we don't
00352   //    have to unnecessarily pull in the `ace/SString.h' header.
00353   return this->write_string (static_cast<ACE_CDR::ULong> (x.length ()),
00354                              x.c_str());
00355 }
00356 
00357 ACE_CDR::Boolean
00358 ACE_OutputCDR::write_wstring (ACE_CDR::ULong len,
00359                               const ACE_CDR::WChar *x)
00360 {
00361   // @@ This is a slight violation of "Optimize for the common case",
00362   // i.e. normally the translator will be 0, but OTOH the code is
00363   // smaller and should be better for the cache ;-) ;-)
00364   // What do we do for GIOP 1.2???
00365   if (this->wchar_translator_ != 0)
00366     return this->wchar_translator_->write_wstring (*this, len, x);
00367   if (ACE_OutputCDR::wchar_maxbytes_ == 0)
00368     {
00369       errno = EACCES;
00370       return (this->good_bit_ = false);
00371     }
00372 
00373   if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
00374       && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
00375     {
00376       if (x != 0)
00377         {
00378           //In GIOP 1.2 the length field contains the number of bytes
00379           //the wstring occupies rather than number of wchars
00380           //Taking sizeof might not be a good way! This is a temporary fix.
00381           if (this->write_ulong (ACE_OutputCDR::wchar_maxbytes_ * len))
00382             return this->write_wchar_array (x, len);
00383         }
00384       else
00385         //In GIOP 1.2 zero length wstrings are legal
00386         return this->write_ulong (0);
00387     }
00388 
00389   else
00390     if (x != 0)
00391       {
00392         if (this->write_ulong (len + 1))
00393           return this->write_wchar_array (x, len + 1);
00394       }
00395     else if (this->write_ulong (1))
00396       return this->write_wchar (0);
00397   return (this->good_bit_ = false);
00398 }
00399 
00400 ACE_CDR::Boolean
00401 ACE_OutputCDR::write_octet_array_mb (const ACE_Message_Block* mb)
00402 {
00403   // If the buffer is small and it fits in the current message
00404   // block it is be cheaper just to copy the buffer.
00405   for (const ACE_Message_Block* i = mb;
00406        i != 0;
00407        i = i->cont ())
00408     {
00409       size_t const length = i->length ();
00410 
00411       // If the mb does not own its data we are forced to make a copy.
00412       if (ACE_BIT_ENABLED (i->flags (),
00413                            ACE_Message_Block::DONT_DELETE))
00414         {
00415           if (! this->write_array (i->rd_ptr (),
00416                                    ACE_CDR::OCTET_SIZE,
00417                                    ACE_CDR::OCTET_ALIGN,
00418                                    static_cast<ACE_CDR::ULong> (length)))
00419             return (this->good_bit_ = false);
00420           continue;
00421         }
00422 
00423       if (length < this->memcpy_tradeoff_
00424           && this->current_->wr_ptr () + length < this->current_->end ())
00425         {
00426           if (! this->write_array (i->rd_ptr (),
00427                                    ACE_CDR::OCTET_SIZE,
00428                                    ACE_CDR::OCTET_ALIGN,
00429                                    static_cast<ACE_CDR::ULong> (length)))
00430             return (this->good_bit_ = false);
00431           continue;
00432         }
00433 
00434       ACE_Message_Block* cont = 0;
00435       this->good_bit_ = false;
00436       ACE_NEW_RETURN (cont,
00437                       ACE_Message_Block (i->data_block ()->duplicate ()),
00438                       false);
00439       this->good_bit_ = true;
00440 
00441       if (this->current_->cont () != 0)
00442         ACE_Message_Block::release (this->current_->cont ());
00443       cont->rd_ptr (i->rd_ptr ());
00444       cont->wr_ptr (i->wr_ptr ());
00445 
00446       this->current_->cont (cont);
00447       this->current_ = cont;
00448       this->current_is_writable_ = false;
00449 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00450       this->current_alignment_ =
00451         (this->current_alignment_ + cont->length ()) % ACE_CDR::MAX_ALIGNMENT;
00452 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00453     }
00454 
00455   return true;
00456 }
00457 
00458 ACE_CDR::Boolean
00459 ACE_OutputCDR::write_1 (const ACE_CDR::Octet *x)
00460 {
00461   char *buf = 0;
00462   if (this->adjust (1, buf) == 0)
00463     {
00464       *reinterpret_cast<ACE_CDR::Octet*> (buf) = *x;
00465       return true;
00466     }
00467 
00468   return false;
00469 }
00470 
00471 ACE_CDR::Boolean
00472 ACE_OutputCDR::write_2 (const ACE_CDR::UShort *x)
00473 {
00474   char *buf = 0;
00475   if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
00476     {
00477 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00478       *reinterpret_cast<ACE_CDR::UShort*> (buf) = *x;
00479       return true;
00480 #else
00481       if (!this->do_byte_swap_)
00482         {
00483           *reinterpret_cast<ACE_CDR::UShort *> (buf) = *x;
00484           return true;
00485         }
00486       else
00487         {
00488           ACE_CDR::swap_2 (reinterpret_cast<const char*> (x), buf);
00489           return true;
00490         }
00491 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00492     }
00493 
00494   return false;
00495 }
00496 
00497 ACE_CDR::Boolean
00498 ACE_OutputCDR::write_4 (const ACE_CDR::ULong *x)
00499 {
00500   char *buf = 0;
00501   if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
00502     {
00503 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00504       *reinterpret_cast<ACE_CDR::ULong*> (buf) = *x;
00505       return true;
00506 #else
00507       if (!this->do_byte_swap_)
00508         {
00509           *reinterpret_cast<ACE_CDR::ULong *> (buf) = *x;
00510           return true;
00511         }
00512       else
00513         {
00514           ACE_CDR::swap_4 (reinterpret_cast<const char*> (x), buf);
00515           return true;
00516         }
00517 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00518     }
00519 
00520   return false;
00521 }
00522 
00523 ACE_CDR::Boolean
00524 ACE_OutputCDR::write_8 (const ACE_CDR::ULongLong *x)
00525 {
00526   char *buf = 0;
00527 
00528   if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
00529     {
00530 #if defined (__arm__)
00531       // Convert to Intel format (12345678 => 56781234)
00532       const char *orig = reinterpret_cast<const char *> (x);
00533       char *target = buf;
00534       register ACE_UINT32 x =
00535         *reinterpret_cast<const ACE_UINT32 *> (orig);
00536       register ACE_UINT32 y =
00537         *reinterpret_cast<const ACE_UINT32 *> (orig + 4);
00538       *reinterpret_cast<ACE_UINT32 *> (target) = y;
00539       *reinterpret_cast<ACE_UINT32 *> (target + 4) = x;
00540       return true;
00541 #else
00542 #  if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00543       *reinterpret_cast<ACE_CDR::ULongLong *> (buf) = *x;
00544       return true;
00545 #  else
00546       if (!this->do_byte_swap_)
00547         {
00548           *reinterpret_cast<ACE_CDR::ULongLong *> (buf) = *x;
00549           return true;
00550         }
00551       else
00552         {
00553           ACE_CDR::swap_8 (reinterpret_cast<const char*> (x), buf);
00554           return true;
00555         }
00556 #  endif /* ACE_ENABLE_SWAP_ON_WRITE */
00557 #endif /* !__arm__ */
00558      }
00559 
00560   return false;
00561 }
00562 
00563 ACE_CDR::Boolean
00564 ACE_OutputCDR::write_16 (const ACE_CDR::LongDouble *x)
00565 {
00566   char* buf = 0;
00567   if (this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
00568                     ACE_CDR::LONGDOUBLE_ALIGN,
00569                     buf) == 0)
00570     {
00571 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00572       *reinterpret_cast<ACE_CDR::LongDouble*> (buf) = *x;
00573       return 1;
00574 #else
00575       if (!this->do_byte_swap_)
00576         {
00577           *reinterpret_cast<ACE_CDR::LongDouble *> (buf) = *x;
00578           return true;
00579         }
00580       else
00581         {
00582           ACE_CDR::swap_16 (reinterpret_cast<const char*> (x), buf);
00583           return true;
00584         }
00585 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00586     }
00587 
00588   return false;
00589 }
00590 
00591 ACE_CDR::Boolean
00592 ACE_OutputCDR::write_wchar_array_i (const ACE_CDR::WChar *x,
00593                                     ACE_CDR::ULong length)
00594 {
00595   if (length == 0)
00596     return true;
00597   char* buf = 0;
00598   size_t const align = (ACE_OutputCDR::wchar_maxbytes_ == 2) ?
00599     ACE_CDR::SHORT_ALIGN :
00600     ACE_CDR::OCTET_ALIGN;
00601 
00602   if (this->adjust (ACE_OutputCDR::wchar_maxbytes_ * length, align, buf) == 0)
00603     {
00604       if (ACE_OutputCDR::wchar_maxbytes_ == 2)
00605         {
00606           ACE_CDR::UShort *sb = reinterpret_cast<ACE_CDR::UShort *> (buf);
00607           for (size_t i = 0; i < length; ++i)
00608 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00609             sb[i] = static_cast<ACE_CDR::UShort> (x[i]);
00610 #else
00611               if (!this->do_byte_swap_)
00612               sb[i] = static_cast<ACE_CDR::UShort> (x[i]);
00613             else
00614               {
00615                 ACE_CDR::UShort sx = static_cast<ACE_CDR::UShort> (x[i]);
00616                 ACE_CDR::swap_2 (reinterpret_cast<char *> (&sx), &buf[i * 2]);
00617               }
00618 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00619         }
00620       else
00621         {
00622           for (size_t i = 0; i < length; ++i)
00623             buf[i] = static_cast<char> (x[i]);
00624         }
00625       return this->good_bit_;
00626     }
00627   return false;
00628 }
00629 
00630 
00631 ACE_CDR::Boolean
00632 ACE_OutputCDR::write_array (const void *x,
00633                             size_t size,
00634                             size_t align,
00635                             ACE_CDR::ULong length)
00636 {
00637   if (length == 0)
00638     return true;
00639   char *buf = 0;
00640   if (this->adjust (size * length, align, buf) == 0)
00641     {
00642 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00643       ACE_OS::memcpy (buf, x, size*length);
00644       return true;
00645 #else
00646       if (!this->do_byte_swap_ || size == 1)
00647         {
00648           ACE_OS::memcpy (buf, x, size*length);
00649           return true;
00650         }
00651       else
00652         {
00653           const char *source = reinterpret_cast<const char *> (x);
00654           switch (size)
00655             {
00656             case 2:
00657               ACE_CDR::swap_2_array (source, buf, length);
00658               return true;
00659             case 4:
00660               ACE_CDR::swap_4_array (source, buf, length);
00661               return true;
00662             case 8:
00663               ACE_CDR::swap_8_array (source, buf, length);
00664               return true;
00665             case 16:
00666               ACE_CDR::swap_16_array (source, buf, length);
00667               return true;
00668             default:
00669               // TODO: print something?
00670               this->good_bit_ = false;
00671               return false;
00672             }
00673         }
00674 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00675     }
00676   this->good_bit_ = false;
00677   return false;
00678 }
00679 
00680 
00681 ACE_CDR::Boolean
00682 ACE_OutputCDR::write_boolean_array (const ACE_CDR::Boolean* x,
00683                                     ACE_CDR::ULong length)
00684 {
00685   // It is hard to optimize this, the spec requires that on the wire
00686   // booleans be represented as a byte with value 0 or 1, but in
00687   // memory it is possible (though very unlikely) that a boolean has
00688   // a non-zero value (different from 1).
00689   // We resort to a simple loop.
00690   ACE_CDR::Boolean const * const end = x + length;
00691 
00692   for (ACE_CDR::Boolean const * i = x;
00693        i != end && this->good_bit ();
00694        ++i)
00695     (void) this->write_boolean (*i);
00696 
00697   return this->good_bit ();
00698 }
00699 
00700 
00701 char *
00702 ACE_OutputCDR::write_long_placeholder (void)
00703 {
00704   char *buf = 0;
00705   if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
00706     *reinterpret_cast<ACE_CDR::ULong*> (buf) = 0;
00707   else
00708     buf = 0;
00709   return buf;
00710 }
00711 
00712 
00713 char *
00714 ACE_OutputCDR::write_short_placeholder (void)
00715 {
00716   char *buf = 0;
00717   if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
00718     *reinterpret_cast<ACE_CDR::UShort*> (buf) = 0;
00719   else
00720     buf = 0;
00721   return buf;
00722 }
00723 
00724 
00725 ACE_CDR::Boolean
00726 ACE_OutputCDR::replace (ACE_CDR::Long x, char* loc)
00727 {
00728   if (this->find (loc) == 0)
00729     return false;
00730 
00731 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00732   *reinterpret_cast<ACE_CDR::Long*> (loc) = x;
00733 #else
00734   if (!this->do_byte_swap_)
00735   {
00736     *reinterpret_cast<ACE_CDR::Long *> (loc) = x;
00737   }
00738   else
00739   {
00740     ACE_CDR::swap_4 (reinterpret_cast<const char*> (&x), loc);
00741   }
00742 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00743 
00744   return true;
00745 }
00746 
00747 
00748 ACE_CDR::Boolean
00749 ACE_OutputCDR::replace (ACE_CDR::Short x, char* loc)
00750 {
00751   if (this->find (loc) == 0)
00752     return false;
00753 
00754 #if !defined (ACE_ENABLE_SWAP_ON_WRITE)
00755   *reinterpret_cast<ACE_CDR::Short*> (loc) = x;
00756 #else
00757   if (!this->do_byte_swap_)
00758   {
00759     *reinterpret_cast<ACE_CDR::Short *> (loc) = x;
00760   }
00761   else
00762   {
00763     ACE_CDR::swap_2 (reinterpret_cast<const char*> (&x), loc);
00764   }
00765 #endif /* ACE_ENABLE_SWAP_ON_WRITE */
00766 
00767   return true;
00768 }
00769 
00770 
00771 int
00772 ACE_OutputCDR::consolidate (void)
00773 {
00774   // Optimize by only doing something if we need to
00775   if (this->current_ != &this->start_)
00776     {
00777       // Set the number of bytes in the top-level block, reallocating
00778       // if necessary.  The rd_ptr and wr_ptr remain at the original offsets
00779       // into the buffer, even if it is reallocated.
00780       // Return an error if the allocation failed.
00781       size_t const newsize =
00782         ACE_CDR::first_size (this->total_length ()
00783                              + ACE_CDR::MAX_ALIGNMENT);
00784       if (this->start_.size (newsize) < 0)
00785         {
00786           return -1;
00787         }
00788 
00789       // Consolidate the chain into the first block.  NOTE that
00790       // ACE_CDR::consolidate can not be used since we don't want to
00791       // overwrite what is already in the first block. We just append it since
00792       // the read and write pointers weren't affected by the resizing above.
00793       // We also don't have to worry about alignment since the start block is
00794       // already aligned.
00795       // NOTE also we know there is a continuation since we checked for it
00796       // above.  There is therefore no reason to check for a 0 continuation
00797       // field here.
00798       ACE_Message_Block *cont = this->start_.cont ();
00799       for (const ACE_Message_Block* i = cont; i != 0; i = i->cont ())
00800         {
00801           this->start_.copy (i->rd_ptr (), i->length ());
00802         }
00803 
00804       // Release the old blocks that were consolidated and reset the
00805       // current_ and current_is_writable_ to reflect the single used block.
00806       ACE_Message_Block::release (cont);
00807       this->start_.cont (0);
00808       this->current_ = &this->start_;
00809       this->current_is_writable_ = true;
00810     }
00811 
00812   return 0;
00813 }
00814 
00815 
00816 ACE_Message_Block*
00817 ACE_OutputCDR::find (char* loc)
00818 {
00819   ACE_Message_Block* mb = 0;
00820   for (mb = &this->start_; mb != 0; mb = mb->cont ())
00821   {
00822     if (loc <= mb->wr_ptr () && loc >= mb->rd_ptr ())
00823     {
00824       break;
00825     }
00826   }
00827 
00828   return mb;
00829 }
00830 
00831 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00832 
00833 void
00834 ACE_OutputCDR::register_monitor (const char *id)
00835 {
00836   this->monitor_->name (id);
00837   this->monitor_->add_to_registry ();
00838 }
00839 
00840 void
00841 ACE_OutputCDR::unregister_monitor (void)
00842 {
00843   this->monitor_->remove_from_registry ();
00844 }
00845 
00846 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00847 
00848 // ****************************************************************
00849 
00850 ACE_InputCDR::ACE_InputCDR (const char *buf,
00851                             size_t bufsiz,
00852                             int byte_order,
00853                             ACE_CDR::Octet major_version,
00854                             ACE_CDR::Octet minor_version)
00855   : start_ (buf, bufsiz),
00856     do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00857     good_bit_ (true),
00858     major_version_ (major_version),
00859     minor_version_ (minor_version),
00860     char_translator_ (0),
00861     wchar_translator_ (0)
00862 {
00863   this->start_.wr_ptr (bufsiz);
00864 
00865 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00866   ACE_NEW (this->monitor_,
00867            ACE::Monitor_Control::Size_Monitor);
00868   this->monitor_->receive (bufsiz);
00869 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00870 }
00871 
00872 ACE_InputCDR::ACE_InputCDR (size_t bufsiz,
00873                             int byte_order,
00874                             ACE_CDR::Octet major_version,
00875                             ACE_CDR::Octet minor_version)
00876   : start_ (bufsiz),
00877     do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00878     good_bit_ (true),
00879     major_version_ (major_version),
00880     minor_version_ (minor_version),
00881     char_translator_ (0),
00882     wchar_translator_ (0)
00883 {
00884 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00885   ACE_NEW (this->monitor_,
00886            ACE::Monitor_Control::Size_Monitor);
00887   this->monitor_->receive (bufsiz);
00888 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00889 }
00890 
00891 ACE_InputCDR::ACE_InputCDR (const ACE_Message_Block *data,
00892                             int byte_order,
00893                             ACE_CDR::Octet major_version,
00894                             ACE_CDR::Octet minor_version,
00895                             ACE_Lock* lock)
00896   : start_ (0, ACE_Message_Block::MB_DATA, 0, 0, 0, lock),
00897     good_bit_ (true),
00898     major_version_ (major_version),
00899     minor_version_ (minor_version),
00900     char_translator_ (0),
00901     wchar_translator_ (0)
00902 {
00903 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00904   ACE_NEW (this->monitor_,
00905            ACE::Monitor_Control::Size_Monitor);
00906   this->monitor_->receive (this->start_.total_size ());
00907 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00908 
00909   this->reset (data, byte_order);
00910 }
00911 
00912 ACE_InputCDR::ACE_InputCDR (ACE_Data_Block *data,
00913                             ACE_Message_Block::Message_Flags flag,
00914                             int byte_order,
00915                             ACE_CDR::Octet major_version,
00916                             ACE_CDR::Octet minor_version)
00917   : start_ (data, flag),
00918     do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00919     good_bit_ (true),
00920     major_version_ (major_version),
00921     minor_version_ (minor_version),
00922     char_translator_ (0),
00923     wchar_translator_ (0)
00924 {
00925 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00926   ACE_NEW (this->monitor_,
00927            ACE::Monitor_Control::Size_Monitor);
00928   this->monitor_->receive (data->size ());
00929 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00930 }
00931 
00932 ACE_InputCDR::ACE_InputCDR (ACE_Data_Block *data,
00933                             ACE_Message_Block::Message_Flags flag,
00934                             size_t rd_pos,
00935                             size_t wr_pos,
00936                             int byte_order,
00937                             ACE_CDR::Octet major_version,
00938                             ACE_CDR::Octet minor_version)
00939   : start_ (data, flag),
00940     do_byte_swap_ (byte_order != ACE_CDR_BYTE_ORDER),
00941     good_bit_ (true),
00942     major_version_ (major_version),
00943     minor_version_ (minor_version),
00944     char_translator_ (0),
00945     wchar_translator_ (0)
00946 {
00947   // Set the read pointer
00948   this->start_.rd_ptr (rd_pos);
00949 
00950   // Set the write pointer after doing a sanity check.
00951   char* wrpos = this->start_.base () + wr_pos;
00952 
00953   if (this->start_.end () >= wrpos)
00954     {
00955       this->start_.wr_ptr (wr_pos);
00956     }
00957 
00958 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
00959   ACE_NEW (this->monitor_,
00960            ACE::Monitor_Control::Size_Monitor);
00961   this->monitor_->receive (data->size ());
00962 #endif /* ACE_HAS_MONITOR_POINTS==1 */
00963 }
00964 
00965 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs,
00966                             size_t size,
00967                             ACE_CDR::Long offset)
00968   : start_ (rhs.start_,
00969             ACE_CDR::MAX_ALIGNMENT),
00970     do_byte_swap_ (rhs.do_byte_swap_),
00971     good_bit_ (true),
00972     major_version_ (rhs.major_version_),
00973     minor_version_ (rhs.minor_version_),
00974     char_translator_ (rhs.char_translator_),
00975     wchar_translator_ (rhs.wchar_translator_)
00976 {
00977 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00978   // Align the base pointer assuming that the incoming stream is also
00979   // aligned the way we are aligned
00980   char *incoming_start = ACE_ptr_align_binary (rhs.start_.base (),
00981                                                ACE_CDR::MAX_ALIGNMENT);
00982 #else
00983   char *incoming_start = rhs.start_.base ();
00984 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00985 
00986   const size_t newpos =
00987     (rhs.start_.rd_ptr() - incoming_start)  + offset;
00988 
00989   if (newpos <= this->start_.space ()
00990       && newpos + size <= this->start_.space ())
00991     {
00992       this->start_.rd_ptr (newpos);
00993       this->start_.wr_ptr (newpos + size);
00994     }
00995   else
00996     {
00997       this->good_bit_ = false;
00998     }
00999 
01000 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01001   ACE_NEW (this->monitor_,
01002            ACE::Monitor_Control::Size_Monitor);
01003   this->monitor_->receive (this->start_.total_size ());
01004 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01005 }
01006 
01007 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs,
01008                             size_t size)
01009   : start_ (rhs.start_,
01010             ACE_CDR::MAX_ALIGNMENT),
01011     do_byte_swap_ (rhs.do_byte_swap_),
01012     good_bit_ (true),
01013     major_version_ (rhs.major_version_),
01014     minor_version_ (rhs.minor_version_),
01015     char_translator_ (rhs.char_translator_),
01016     wchar_translator_ (rhs.wchar_translator_)
01017 {
01018 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
01019   // Align the base pointer assuming that the incoming stream is also
01020   // aligned the way we are aligned
01021   char *incoming_start = ACE_ptr_align_binary (rhs.start_.base (),
01022                                                ACE_CDR::MAX_ALIGNMENT);
01023 #else
01024   char *incoming_start = rhs.start_.base ();
01025 #endif /* ACE_LACKS_CDR_ALIGNMENT */
01026 
01027   const size_t newpos =
01028     rhs.start_.rd_ptr() - incoming_start;
01029 
01030   if (newpos <= this->start_.space ()
01031       && newpos + size <= this->start_.space ())
01032     {
01033       // Notice that ACE_Message_Block::duplicate may leave the
01034       // wr_ptr() with a higher value than what we actually want.
01035       this->start_.rd_ptr (newpos);
01036       this->start_.wr_ptr (newpos + size);
01037 
01038       ACE_CDR::Octet byte_order = 0;
01039       (void) this->read_octet (byte_order);
01040       this->do_byte_swap_ = (byte_order != ACE_CDR_BYTE_ORDER);
01041     }
01042   else
01043     {
01044       this->good_bit_ = false;
01045     }
01046 
01047 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01048   ACE_NEW (this->monitor_,
01049            ACE::Monitor_Control::Size_Monitor);
01050   this->monitor_->receive (this->start_.total_size ());
01051 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01052 }
01053 
01054 ACE_InputCDR::ACE_InputCDR (const ACE_InputCDR& rhs)
01055   : start_ (rhs.start_,
01056             ACE_CDR::MAX_ALIGNMENT),
01057     do_byte_swap_ (rhs.do_byte_swap_),
01058     good_bit_ (true),
01059     major_version_ (rhs.major_version_),
01060     minor_version_ (rhs.minor_version_),
01061     char_translator_ (rhs.char_translator_),
01062     wchar_translator_ (rhs.wchar_translator_)
01063 {
01064 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
01065   char *buf = ACE_ptr_align_binary (rhs.start_.base (),
01066                                     ACE_CDR::MAX_ALIGNMENT);
01067 #else
01068   char *buf = rhs.start_.base ();
01069 #endif /* ACE_LACKS_CDR_ALIGNMENT */
01070 
01071   size_t rd_offset = rhs.start_.rd_ptr () - buf;
01072   size_t wr_offset = rhs.start_.wr_ptr () - buf;
01073   this->start_.rd_ptr (rd_offset);
01074   this->start_.wr_ptr (wr_offset);
01075 
01076 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01077   ACE_NEW (this->monitor_,
01078            ACE::Monitor_Control::Size_Monitor);
01079   this->monitor_->receive (this->start_.total_size ());
01080 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01081 }
01082 
01083 ACE_InputCDR::ACE_InputCDR (ACE_InputCDR::Transfer_Contents x)
01084   : start_ (x.rhs_.start_.data_block ()),
01085     do_byte_swap_ (x.rhs_.do_byte_swap_),
01086     good_bit_ (true),
01087     major_version_ (x.rhs_.major_version_),
01088     minor_version_ (x.rhs_.minor_version_),
01089     char_translator_ (x.rhs_.char_translator_),
01090     wchar_translator_ (x.rhs_.wchar_translator_)
01091 {
01092   this->start_.rd_ptr (x.rhs_.start_.rd_ptr ());
01093   this->start_.wr_ptr (x.rhs_.start_.wr_ptr ());
01094 
01095   ACE_Data_Block* db = this->start_.data_block ()->clone_nocopy ();
01096   (void) x.rhs_.start_.replace_data_block (db);
01097 
01098 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01099   ACE_NEW (this->monitor_,
01100            ACE::Monitor_Control::Size_Monitor);
01101   this->monitor_->receive (this->start_.total_size ());
01102 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01103 }
01104 
01105 ACE_InputCDR&
01106 ACE_InputCDR::operator= (const ACE_InputCDR& rhs)
01107 {
01108   if (this != &rhs)
01109     {
01110       this->start_.data_block (rhs.start_.data_block ()->duplicate ());
01111       this->start_.rd_ptr (rhs.start_.rd_ptr ());
01112       this->start_.wr_ptr (rhs.start_.wr_ptr ());
01113       this->do_byte_swap_ = rhs.do_byte_swap_;
01114       this->good_bit_ = true;
01115       this->char_translator_ = rhs.char_translator_;
01116       this->major_version_ = rhs.major_version_;
01117       this->minor_version_ = rhs.minor_version_;
01118     }
01119 
01120 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01121   this->monitor_->receive (this->start_.total_size ());
01122 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01123 
01124   return *this;
01125 }
01126 
01127 ACE_InputCDR::ACE_InputCDR (const ACE_OutputCDR& rhs,
01128                             ACE_Allocator* buffer_allocator,
01129                             ACE_Allocator* data_block_allocator,
01130                             ACE_Allocator* message_block_allocator)
01131   : start_ (rhs.total_length () + ACE_CDR::MAX_ALIGNMENT,
01132             ACE_Message_Block::MB_DATA,
01133             0,
01134             0,
01135             buffer_allocator,
01136             0,
01137             0,
01138             ACE_Time_Value::zero,
01139             ACE_Time_Value::max_time,
01140             data_block_allocator,
01141             message_block_allocator),
01142     do_byte_swap_ (rhs.do_byte_swap_),
01143     good_bit_ (true),
01144     major_version_ (rhs.major_version_),
01145     minor_version_ (rhs.minor_version_),
01146     char_translator_ (rhs.char_translator_),
01147     wchar_translator_ (rhs.wchar_translator_)
01148 {
01149   ACE_CDR::mb_align (&this->start_);
01150   for (const ACE_Message_Block *i = rhs.begin ();
01151        i != rhs.end ();
01152        i = i->cont ())
01153     {
01154       this->start_.copy (i->rd_ptr (), i->length ());
01155     }
01156 
01157 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01158   ACE_NEW (this->monitor_,
01159            ACE::Monitor_Control::Size_Monitor);
01160   this->monitor_->receive (this->start_.total_size ());
01161 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01162 }
01163 
01164 ACE_CDR::Boolean
01165 ACE_InputCDR::skip_wchar (void)
01166 {
01167   if (static_cast<ACE_CDR::Short> (major_version_) == 1
01168       && static_cast<ACE_CDR::Short> (minor_version_) == 2)
01169     {
01170       ACE_CDR::Octet len;
01171       if (this->read_1 (&len))
01172         return this->skip_bytes (static_cast<size_t> (len));
01173     }
01174   else
01175     {
01176       ACE_CDR::WChar x;
01177       void * const temp = &x;
01178       if (ACE_OutputCDR::wchar_maxbytes_ == 2)
01179         return this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (temp));
01180       else
01181         return this->read_4 (reinterpret_cast<ACE_CDR::ULong *> (temp));
01182     }
01183 
01184   return (this->good_bit_ = false);
01185 }
01186 
01187 ACE_CDR::Boolean
01188 ACE_InputCDR::read_wchar (ACE_CDR::WChar& x)
01189 {
01190   if (this->wchar_translator_ != 0)
01191     {
01192       this->good_bit_ = this->wchar_translator_->read_wchar (*this,x);
01193       return this->good_bit_;
01194     }
01195   if (ACE_OutputCDR::wchar_maxbytes_ == 0)
01196     {
01197       errno = EACCES;
01198       return (this->good_bit_ = false);
01199     }
01200 
01201   if (ACE_OutputCDR::wchar_maxbytes_ == sizeof (ACE_CDR::WChar))
01202     {
01203       if (static_cast<ACE_CDR::Short> (major_version_) == 1
01204           && static_cast<ACE_CDR::Short> (minor_version_) == 2)
01205         {
01206           ACE_CDR::Octet len;
01207 
01208           if (this->read_1 (&len))
01209             return this->read_array
01210               (reinterpret_cast<ACE_CDR::Octet*> (&x),
01211                static_cast<ACE_CDR::ULong> (len),
01212                ACE_CDR::OCTET_ALIGN,
01213                1);
01214 
01215           else
01216             return (this->good_bit_ = false);
01217         }
01218 
01219       void * const temp = &x;
01220       if (sizeof (ACE_CDR::WChar) == 2)
01221         return this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (temp));
01222       else
01223         return this->read_4 (reinterpret_cast<ACE_CDR::ULong *> (temp));
01224     }
01225 
01226   if (static_cast<ACE_CDR::Short> (major_version_) == 1
01227       && static_cast<ACE_CDR::Short> (minor_version_) == 2)
01228     {
01229       ACE_CDR::Octet len;
01230 
01231       if (this->read_1 (&len))
01232         {
01233           if (len == 2)
01234             {
01235               ACE_CDR::Short sx;
01236               if (this->read_array
01237                   (reinterpret_cast<ACE_CDR::Octet*> (&sx),
01238                    static_cast<ACE_CDR::ULong> (len),
01239                    ACE_CDR::OCTET_ALIGN,
01240                    1))
01241                 {
01242                   x = static_cast<ACE_CDR::WChar> (sx);
01243                   return true;
01244                 }
01245             }
01246           else
01247             {
01248               ACE_CDR::Octet ox;
01249               if (this->read_array
01250                   (reinterpret_cast<ACE_CDR::Octet*> (&ox),
01251                    static_cast<ACE_CDR::ULong> (len),
01252                    ACE_CDR::OCTET_ALIGN,
01253                    1))
01254                 {
01255                   x = static_cast<ACE_CDR::WChar> (ox);
01256                   return true;
01257                 }
01258             }
01259         }
01260     }
01261   else
01262     {
01263       if (ACE_OutputCDR::wchar_maxbytes_ == 2)
01264         {
01265           ACE_CDR::UShort sx;
01266           if (this->read_2 (reinterpret_cast<ACE_CDR::UShort *> (&sx)))
01267             {
01268               x = static_cast<ACE_CDR::WChar> (sx);
01269               return true;
01270             }
01271         }
01272       else
01273         {
01274           ACE_CDR::Octet ox;
01275           if (this->read_1 (&ox))
01276             {
01277               x = static_cast<ACE_CDR::WChar> (ox);
01278               return true;
01279             }
01280 
01281         }
01282     }
01283   return (this->good_bit_ = false);
01284 }
01285 
01286 ACE_CDR::Boolean
01287 ACE_InputCDR::read_string (ACE_CDR::Char *&x)
01288 {
01289   // @@ This is a slight violation of "Optimize for the common case",
01290   // i.e. normally the translator will be 0, but OTOH the code is
01291   // smaller and should be better for the cache ;-) ;-)
01292   if (this->char_translator_ != 0)
01293     {
01294       this->good_bit_ = this->char_translator_->read_string (*this, x);
01295       return this->good_bit_;
01296     }
01297 
01298   ACE_CDR::ULong len = 0;
01299 
01300   if (!this->read_ulong (len))
01301     return false;
01302 
01303   // A check for the length being too great is done later in the
01304   // call to read_char_array but we want to have it done before
01305   // the memory is allocated.
01306   if (len > 0 && len <= this->length())
01307     {
01308       ACE_NEW_RETURN (x,
01309                       ACE_CDR::Char[len],
01310                       0);
01311 
01312       ACE_Auto_Basic_Array_Ptr<ACE_CDR::Char> safe_data (x);
01313 
01314       if (this->read_char_array (x, len))
01315         {
01316           (void) safe_data.release ();
01317           return true;
01318         }
01319     }
01320   else if (len == 0)
01321     {
01322       // Convert any null strings to empty strings since empty
01323       // strings can cause crashes. (See bug 58.)
01324       ACE_NEW_RETURN (x,
01325                       ACE_CDR::Char[1],
01326                       0);
01327       ACE_OS::strcpy (const_cast<char *&> (x), "");
01328       return true;
01329     }
01330 
01331   x = 0;
01332   return (this->good_bit_ = false);
01333 }
01334 
01335 ACE_CDR::Boolean
01336 ACE_InputCDR::read_string (ACE_CString &x)
01337 {
01338   ACE_CDR::Char * data = 0;
01339   if (this->read_string (data))
01340     {
01341       ACE_Auto_Basic_Array_Ptr<ACE_CDR::Char> safe_data (data);
01342       x = data;
01343       return true;
01344     }
01345 
01346   x = "";
01347   return (this->good_bit_ = false);
01348 }
01349 
01350 ACE_CDR::Boolean
01351 ACE_InputCDR::read_wstring (ACE_CDR::WChar*& x)
01352 {
01353   // @@ This is a slight violation of "Optimize for the common case",
01354   // i.e. normally the translator will be 0, but OTOH the code is
01355   // smaller and should be better for the cache ;-) ;-)
01356   if (this->wchar_translator_ != 0)
01357     {
01358       this->good_bit_ = this->wchar_translator_->read_wstring (*this, x);
01359       return this->good_bit_;
01360     }
01361   if (ACE_OutputCDR::wchar_maxbytes_ == 0)
01362     {
01363       errno = EACCES;
01364       return (this->good_bit_ = false);
01365     }
01366 
01367   ACE_CDR::ULong len = 0;
01368   if (!this->read_ulong (len))
01369     return false;
01370 
01371   // A check for the length being too great is done later in the
01372   // call to read_char_array but we want to have it done before
01373   // the memory is allocated.
01374   if (len > 0 && len <= this->length ())
01375     {
01376       ACE_Auto_Basic_Array_Ptr<ACE_CDR::WChar> safe_data;
01377 
01378       if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
01379           && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
01380         {
01381           len /= ACE_OutputCDR::wchar_maxbytes_;
01382 
01383           //allocating one extra for the null character needed by applications
01384           ACE_NEW_RETURN (x,
01385                           ACE_CDR::WChar [len + 1],
01386                           false);
01387 
01388           ACE_auto_ptr_reset (safe_data, x);
01389 
01390           if (this->read_wchar_array (x, len))
01391             {
01392 
01393               //Null character used by applications to find the end of
01394               //the wstring
01395               //Is this okay with the GIOP 1.2 spec??
01396               x[len] = '\x00';
01397 
01398               (void) safe_data.release ();
01399 
01400               return true;
01401             }
01402         }
01403       else
01404         {
01405           ACE_NEW_RETURN (x,
01406                           ACE_CDR::WChar [len],
01407                           false);
01408 
01409           ACE_auto_ptr_reset (safe_data, x);
01410 
01411           if (this->read_wchar_array (x, len))
01412             {
01413               (void) safe_data.release ();
01414 
01415               return true;
01416             }
01417         }
01418     }
01419   else if (len == 0)
01420     {
01421       // Convert any null strings to empty strings since empty
01422       // strings can cause crashes. (See bug 58.)
01423       ACE_NEW_RETURN (x,
01424                       ACE_CDR::WChar[1],
01425                       false);
01426       x[0] = '\x00';
01427       return true;
01428     }
01429 
01430   this->good_bit_ = false;
01431   x = 0;
01432   return false;
01433 }
01434 
01435 ACE_CDR::Boolean
01436 ACE_InputCDR::read_array (void* x,
01437                           size_t size,
01438                           size_t align,
01439                           ACE_CDR::ULong length)
01440 {
01441   if (length == 0)
01442     return true;
01443   char* buf = 0;
01444 
01445   if (this->adjust (size * length, align, buf) == 0)
01446     {
01447 #if defined (ACE_DISABLE_SWAP_ON_READ)
01448       ACE_OS::memcpy (x, buf, size*length);
01449 #else
01450       if (!this->do_byte_swap_ || size == 1)
01451         ACE_OS::memcpy (x, buf, size*length);
01452       else
01453         {
01454           char *target = reinterpret_cast<char*> (x);
01455           switch (size)
01456             {
01457             case 2:
01458               ACE_CDR::swap_2_array (buf, target, length);
01459               break;
01460             case 4:
01461               ACE_CDR::swap_4_array (buf, target, length);
01462               break;
01463             case 8:
01464               ACE_CDR::swap_8_array (buf, target, length);
01465               break;
01466             case 16:
01467               ACE_CDR::swap_16_array (buf, target, length);
01468               break;
01469             default:
01470               // TODO: print something?
01471               this->good_bit_ = false;
01472               return false;
01473             }
01474         }
01475 #endif /* ACE_DISABLE_SWAP_ON_READ */
01476       return this->good_bit_;
01477     }
01478   return false;
01479 }
01480 
01481 ACE_CDR::Boolean
01482 ACE_InputCDR::read_wchar_array_i (ACE_CDR::WChar* x,
01483                                   ACE_CDR::ULong length)
01484 {
01485   if (length == 0)
01486     return true;
01487   char* buf = 0;
01488   size_t const align = (ACE_OutputCDR::wchar_maxbytes_ == 2) ?
01489     ACE_CDR::SHORT_ALIGN :
01490     ACE_CDR::OCTET_ALIGN;
01491 
01492   if (this->adjust (ACE_OutputCDR::wchar_maxbytes_ * length, align, buf) == 0)
01493     {
01494       if (ACE_OutputCDR::wchar_maxbytes_ == 2)
01495         {
01496           ACE_CDR::UShort *sb = reinterpret_cast<ACE_CDR::UShort *> (buf);
01497           for (size_t i = 0; i < length; ++i)
01498 #if defined (ACE_DISABLE_SWAP_ON_READ)
01499             x[i] = static_cast<ACE_CDR::WChar> (sb[i]);
01500 #else
01501             if (!this->do_byte_swap_)
01502               x[i] = static_cast<ACE_CDR::WChar> (sb[i]);
01503             else
01504               {
01505                 ACE_CDR::UShort sx;
01506                 ACE_CDR::swap_2 (&buf[i * 2], reinterpret_cast<char *> (&sx));
01507                 x[i] = static_cast<ACE_CDR::WChar> (sx);
01508               }
01509 #endif /* ACE_DISABLE_SWAP_ON_READ */
01510         }
01511       else
01512         {
01513           for (size_t i = 0; i < length; ++i)
01514             x[i] = static_cast<ACE_CDR::Octet> (buf[i]);
01515         }
01516       return this->good_bit_;
01517     }
01518   return false;
01519 }
01520 
01521 
01522 ACE_CDR::Boolean
01523 ACE_InputCDR::read_boolean_array (ACE_CDR::Boolean *x,
01524                                   ACE_CDR::ULong length)
01525 {
01526   // Make sure the length of the array isn't greater than the length of
01527   // the stream.
01528   if (length > this->length ())
01529     {
01530       this->good_bit_ = false;
01531       return false;
01532     }
01533 
01534   // It is hard to optimize this, the spec requires that on the wire
01535   // booleans be represented as a byte with value 0 or 1, but in
01536   // memory it is possible (though very unlikely) that a boolean has
01537   // a non-zero value (different from 1).
01538   // We resort to a simple loop.
01539   for (ACE_CDR::ULong i = 0; i != length && this->good_bit_; ++i)
01540     (void) this->read_boolean (x[i]);
01541 
01542   return this->good_bit_;
01543 }
01544 
01545 ACE_CDR::Boolean
01546 ACE_InputCDR::read_1 (ACE_CDR::Octet *x)
01547 {
01548   if (this->rd_ptr () < this->wr_ptr ())
01549     {
01550       *x = *reinterpret_cast<ACE_CDR::Octet*> (this->rd_ptr ());
01551       this->start_.rd_ptr (1);
01552       return true;
01553     }
01554 
01555   this->good_bit_ = false;
01556   return false;
01557 }
01558 
01559 ACE_CDR::Boolean
01560 ACE_InputCDR::read_2 (ACE_CDR::UShort *x)
01561 {
01562   char *buf = 0;
01563   if (this->adjust (ACE_CDR::SHORT_SIZE, buf) == 0)
01564     {
01565 #if !defined (ACE_DISABLE_SWAP_ON_READ)
01566       if (!this->do_byte_swap_)
01567         *x = *reinterpret_cast<ACE_CDR::UShort*> (buf);
01568       else
01569         ACE_CDR::swap_2 (buf, reinterpret_cast<char*> (x));
01570 #else
01571       *x = *reinterpret_cast<ACE_CDR::UShort*> (buf);
01572 #endif /* ACE_DISABLE_SWAP_ON_READ */
01573       return true;
01574     }
01575   this->good_bit_ = false;
01576   return false;
01577 }
01578 
01579 ACE_CDR::Boolean
01580 ACE_InputCDR::read_4 (ACE_CDR::ULong *x)
01581 {
01582   char *buf = 0;
01583   if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0)
01584     {
01585 #if !defined (ACE_DISABLE_SWAP_ON_READ)
01586       if (!this->do_byte_swap_)
01587         *x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
01588       else
01589         ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (x));
01590 #else
01591       *x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
01592 #endif /* ACE_DISABLE_SWAP_ON_READ */
01593       return true;
01594     }
01595   this->good_bit_ = false;
01596   return false;
01597 }
01598 
01599 ACE_CDR::Boolean
01600 ACE_InputCDR::read_8 (ACE_CDR::ULongLong *x)
01601 {
01602   char *buf = 0;
01603 
01604   if (this->adjust (ACE_CDR::LONGLONG_SIZE, buf) == 0)
01605     {
01606 #if !defined (ACE_DISABLE_SWAP_ON_READ)
01607 #  if defined (__arm__)
01608       if (!this->do_byte_swap_)
01609         {
01610           // Convert from Intel format (12345678 => 56781234)
01611           const char *orig = buf;
01612           char *target = reinterpret_cast<char *> (x);
01613           register ACE_UINT32 x =
01614             *reinterpret_cast<const ACE_UINT32 *> (orig);
01615           register ACE_UINT32 y =
01616             *reinterpret_cast<const ACE_UINT32 *> (orig + 4);
01617           *reinterpret_cast<ACE_UINT32 *> (target) = y;
01618           *reinterpret_cast<ACE_UINT32 *> (target + 4) = x;
01619         }
01620       else
01621         {
01622           // Convert from Sparc format (12345678 => 43218765)
01623           const char *orig = buf;
01624           char *target = reinterpret_cast<char *> (x);
01625           register ACE_UINT32 x =
01626             *reinterpret_cast<const ACE_UINT32 *> (orig);
01627           register ACE_UINT32 y =
01628             *reinterpret_cast<const ACE_UINT32 *> (orig + 4);
01629           x = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
01630           y = (y << 24) | ((y & 0xff00) << 8) | ((y & 0xff0000) >> 8) | (y >> 24);
01631           *reinterpret_cast<ACE_UINT32 *> (target) = x;
01632           *reinterpret_cast<ACE_UINT32 *> (target + 4) = y;
01633         }
01634 #  else
01635       if (!this->do_byte_swap_)
01636         *x = *reinterpret_cast<ACE_CDR::ULongLong *> (buf);
01637       else
01638         ACE_CDR::swap_8 (buf, reinterpret_cast<char *> (x));
01639 #  endif /* !__arm__ */
01640 #else
01641       *x = *reinterpret_cast<ACE_CDR::ULongLong *> (buf);
01642 #endif /* ACE_DISABLE_SWAP_ON_READ */
01643       return true;
01644     }
01645 
01646   this->good_bit_ = false;
01647   return false;
01648 }
01649 
01650 ACE_CDR::Boolean
01651 ACE_InputCDR::read_16 (ACE_CDR::LongDouble *x)
01652 {
01653   char *buf = 0;
01654   if (this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
01655                     ACE_CDR::LONGDOUBLE_ALIGN,
01656                     buf) == 0)
01657     {
01658 #if !defined (ACE_DISABLE_SWAP_ON_READ)
01659       if (!this->do_byte_swap_)
01660         *x = *reinterpret_cast<ACE_CDR::LongDouble *> (buf);
01661       else
01662         ACE_CDR::swap_16 (buf, reinterpret_cast<char*> (x));
01663 #else
01664       *x = *reinterpret_cast<ACE_CDR::LongDouble*> (buf);
01665 #endif /* ACE_DISABLE_SWAP_ON_READ */
01666       return true;
01667     }
01668 
01669   this->good_bit_ = false;
01670   return false;
01671 }
01672 
01673 ACE_CDR::Boolean
01674 ACE_InputCDR::skip_string (void)
01675 {
01676   ACE_CDR::ULong len = 0;
01677   if (this->read_ulong (len))
01678     {
01679       if (this->rd_ptr () + len <= this->wr_ptr ())
01680         {
01681           this->rd_ptr (len);
01682           return true;
01683         }
01684       this->good_bit_ = false;
01685     }
01686   return false;
01687 }
01688 
01689 ACE_CDR::Boolean
01690 ACE_InputCDR::skip_wstring (void)
01691 {
01692   ACE_CDR::Boolean continue_skipping = true;
01693   ACE_CDR::ULong len = 0;
01694 
01695   continue_skipping = read_ulong (len);
01696 
01697   if (continue_skipping && len != 0)
01698     {
01699       if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
01700             && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
01701           continue_skipping = this->skip_bytes ((size_t)len);
01702       else
01703         while (continue_skipping && len--)
01704           continue_skipping = this->skip_wchar ();
01705     }
01706   return continue_skipping;
01707 }
01708 
01709 ACE_CDR::Boolean
01710 ACE_InputCDR::skip_bytes (size_t len)
01711 {
01712   if (this->rd_ptr () + len <= this->wr_ptr ())
01713     {
01714       this->rd_ptr (len);
01715       return true;
01716     }
01717   this->good_bit_ = false;
01718   return false;
01719 }
01720 
01721 int
01722 ACE_InputCDR::grow (size_t newsize)
01723 {
01724   if (ACE_CDR::grow (&this->start_, newsize) == -1)
01725     return -1;
01726 
01727   ACE_CDR::mb_align (&this->start_);
01728   this->start_.wr_ptr (newsize);
01729 
01730 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01731   if (newsize > this->start_.total_size ())
01732     {
01733       this->monitor_->receive (newsize);
01734     }
01735 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01736 
01737   return 0;
01738 }
01739 
01740 void
01741 ACE_InputCDR::reset (const ACE_Message_Block* data,
01742                      int byte_order)
01743 {
01744   this->reset_byte_order (byte_order);
01745   ACE_CDR::consolidate (&this->start_, data);
01746 
01747 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01748   this->monitor_->receive (this->start_.total_size ());
01749 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01750 }
01751 
01752 void
01753 ACE_InputCDR::steal_from (ACE_InputCDR &cdr)
01754 {
01755   this->do_byte_swap_ = cdr.do_byte_swap_;
01756   this->start_.data_block (cdr.start_.data_block ()->duplicate ());
01757 
01758   // If the message block had a DONT_DELETE flags, just clear it off..
01759   this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
01760   this->start_.rd_ptr (cdr.start_.rd_ptr ());
01761 
01762   this->start_.wr_ptr (cdr.start_.wr_ptr ());
01763   this->major_version_ = cdr.major_version_;
01764   this->minor_version_ = cdr.minor_version_;
01765   cdr.reset_contents ();
01766 
01767 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01768   this->monitor_->receive (this->start_.total_size ());
01769 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01770 }
01771 
01772 void
01773 ACE_InputCDR::exchange_data_blocks (ACE_InputCDR &cdr)
01774 {
01775   // Exchange byte orders
01776   int const byte_order = cdr.do_byte_swap_;
01777   cdr.do_byte_swap_ = this->do_byte_swap_;
01778   this->do_byte_swap_ = byte_order;
01779 
01780   // Get the destination read and write pointers
01781   size_t const drd_pos =
01782     cdr.start_.rd_ptr () - cdr.start_.base ();
01783   size_t const dwr_pos =
01784     cdr.start_.wr_ptr () - cdr.start_.base ();
01785 
01786   // Get the source read & write pointers
01787   size_t const srd_pos =
01788     this->start_.rd_ptr () - this->start_.base ();
01789   size_t const swr_pos =
01790     this->start_.wr_ptr () - this->start_.base ();
01791 
01792   // Exchange data_blocks. Dont release any of the data blocks.
01793   ACE_Data_Block *dnb =
01794     this->start_.replace_data_block (cdr.start_.data_block ());
01795   cdr.start_.replace_data_block (dnb);
01796 
01797   // Exchange the flags information..
01798   ACE_Message_Block::Message_Flags df = cdr.start_.self_flags ();
01799   ACE_Message_Block::Message_Flags sf = this->start_.self_flags ();
01800 
01801   cdr.start_.clr_self_flags (df);
01802   this->start_.clr_self_flags (sf);
01803 
01804   cdr.start_.set_self_flags (sf);
01805   this->start_.set_self_flags (df);
01806 
01807   // Reset the <cdr> pointers to zero before it is set again.
01808   cdr.start_.reset ();
01809   this->start_.reset ();
01810 
01811   // Set the read and write pointers.
01812   if (cdr.start_.size () >= srd_pos)
01813     {
01814       cdr.start_.rd_ptr (srd_pos);
01815     }
01816 
01817   if (cdr.start_.size () >= swr_pos)
01818     {
01819       cdr.start_.wr_ptr (swr_pos);
01820     }
01821 
01822   if (this->start_.size () >= drd_pos)
01823     {
01824       this->start_.rd_ptr (drd_pos);
01825     }
01826 
01827   if (this->start_.size () >= dwr_pos)
01828     {
01829       this->start_.wr_ptr (dwr_pos);
01830     }
01831 
01832   ACE_CDR::Octet const dmajor = cdr.major_version_;
01833   ACE_CDR::Octet const dminor = cdr.minor_version_;
01834 
01835   // Exchange the GIOP version info
01836   cdr.major_version_ = this->major_version_;
01837   cdr.minor_version_ = this->minor_version_;
01838 
01839   this->major_version_ = dmajor;
01840   this->minor_version_ = dminor;
01841 
01842 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01843   this->monitor_->receive (this->start_.total_size ());
01844 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01845 }
01846 
01847 ACE_Data_Block *
01848 ACE_InputCDR::clone_from (ACE_InputCDR &cdr)
01849 {
01850   this->do_byte_swap_ = cdr.do_byte_swap_;
01851 
01852   // Get the read & write pointer positions in the incoming CDR
01853   // streams
01854   char *rd_ptr = cdr.start_.rd_ptr ();
01855   char *wr_ptr = cdr.start_.wr_ptr ();
01856 
01857   // Now reset the incoming CDR stream
01858   cdr.start_.reset ();
01859 
01860   // As we have reset the stream, try to align the underlying message
01861   // block in the incoming stream
01862   ACE_CDR::mb_align (&cdr.start_);
01863 
01864   // Get the read & write pointer positions again
01865   char *nrd_ptr = cdr.start_.rd_ptr ();
01866   char *nwr_ptr = cdr.start_.wr_ptr ();
01867 
01868   // Actual length of the stream is..
01869   // @todo: This will look idiotic, but we dont seem to have much of a
01870   // choice. How do we calculate the length of the incoming stream?
01871   // Calling the method before calling reset () would give us the
01872   // wrong length of the stream that needs copying.  So we do the
01873   // calulation like this
01874   // (1) We get the <rd_ptr> and <wr_ptr> positions of the incoming
01875   // stream.
01876   // (2) Then we reset the <incoming> stream and then align it.
01877   // (3) We get the <rd_ptr> and <wr_ptr> positions again. (Points #1
01878   // thru #3 has been done already)
01879   // (4) The difference in the <rd_ptr> and <wr_ptr> positions gives
01880   // us the following, the actual bytes traversed by the <rd_ptr> and
01881   // <wr_ptr>.
01882   // (5) The bytes traversed by the <wr_ptr> is the actual length of
01883   // the stream.
01884 
01885   // Actual bytes traversed
01886   size_t rd_bytes = rd_ptr - nrd_ptr;
01887   size_t wr_bytes = wr_ptr - nwr_ptr;
01888 
01889   ACE_CDR::mb_align (&this->start_);
01890 
01891   ACE_Data_Block *db =
01892     this->start_.data_block ();
01893 
01894   // If the size of the data that needs to be copied are higher than
01895   // what is available, then do a reallocation.
01896   if (wr_bytes > (this->start_.size () - ACE_CDR::MAX_ALIGNMENT))
01897     {
01898       // @@NOTE: We need to probably add another method to the message
01899       // block interface to simplify this
01900       db =
01901         cdr.start_.data_block ()->clone_nocopy ();
01902 
01903       if (db == 0 || db->size ((wr_bytes) +
01904                                ACE_CDR::MAX_ALIGNMENT) == -1)
01905         return 0;
01906 
01907       // Replace our data block by using the incoming CDR stream.
01908       db = this->start_.replace_data_block (db);
01909 
01910       // Align the start_ message block.
01911       ACE_CDR::mb_align (&this->start_);
01912 
01913       // Clear the DONT_DELETE flag if it has been set
01914       this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
01915     }
01916 
01917   // Now do the copy
01918   (void) ACE_OS::memcpy (this->start_.wr_ptr (),
01919                          cdr.start_.rd_ptr (),
01920                          wr_bytes);
01921 
01922   // Set the read pointer position to the same point as that was in
01923   // <incoming> cdr.
01924   this->start_.rd_ptr (rd_bytes);
01925   this->start_.wr_ptr (wr_bytes);
01926 
01927   // We have changed the read & write pointers for the incoming
01928   // stream. Set them back to the positions that they were before..
01929   cdr.start_.rd_ptr (rd_bytes);
01930   cdr.start_.wr_ptr (wr_bytes);
01931 
01932   this->major_version_ = cdr.major_version_;
01933   this->minor_version_ = cdr.minor_version_;
01934 
01935   // Copy the char/wchar translators
01936   this->char_translator_ = cdr.char_translator_;
01937   this->wchar_translator_ = cdr.wchar_translator_;
01938 
01939 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01940   this->monitor_->receive (this->start_.total_size ());
01941 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01942 
01943   return db;
01944 }
01945 
01946 ACE_Message_Block*
01947 ACE_InputCDR::steal_contents (void)
01948 {
01949   ACE_Message_Block* block = this->start_.clone ();
01950   this->start_.data_block (block->data_block ()->clone ());
01951 
01952   // If at all our message had a DONT_DELETE flag set, just clear it
01953   // off.
01954   this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
01955 
01956   ACE_CDR::mb_align (&this->start_);
01957 
01958 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01959   this->monitor_->receive (this->start_.total_size ());
01960 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01961 
01962   return block;
01963 }
01964 
01965 void
01966 ACE_InputCDR::reset_contents (void)
01967 {
01968   this->start_.data_block (this->start_.data_block ()->clone_nocopy ());
01969 
01970   // Reset the flags...
01971   this->start_.clr_self_flags (ACE_Message_Block::DONT_DELETE);
01972 
01973 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01974   this->monitor_->receive (this->start_.total_size ());
01975 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01976 }
01977 
01978 #if defined (ACE_HAS_MONITOR_POINTS) && (ACE_HAS_MONITOR_POINTS == 1)
01979 
01980 void
01981 ACE_InputCDR::register_monitor (const char *id)
01982 {
01983   this->monitor_->name (id);
01984   this->monitor_->add_to_registry ();
01985 }
01986 
01987 void
01988 ACE_InputCDR::unregister_monitor (void)
01989 {
01990   this->monitor_->remove_from_registry ();
01991 }
01992 
01993 #endif /* ACE_HAS_MONITOR_POINTS==1 */
01994 
01995 // --------------------------------------------------------------
01996 
01997 ACE_Char_Codeset_Translator::~ACE_Char_Codeset_Translator (void)
01998 {
01999 }
02000 
02001 // --------------------------------------------------------------
02002 
02003 ACE_WChar_Codeset_Translator::~ACE_WChar_Codeset_Translator (void)
02004 {
02005 }
02006 
02007 // --------------------------------------------------------------
02008 
02009 ACE_CDR::Boolean
02010 operator<< (ACE_OutputCDR &os, const ACE_CString &x)
02011 {
02012   os.write_string (x);
02013   return os.good_bit ();
02014 }
02015 
02016 ACE_CDR::Boolean
02017 operator>> (ACE_InputCDR &is, ACE_CString &x)
02018 {
02019   is.read_string (x);
02020   return is.good_bit ();
02021 }
02022 
02023 #if defined (GEN_OSTREAM_OPS)
02024 
02025 std::ostream&
02026 operator<< (std::ostream &os, ACE_OutputCDR::from_boolean x)
02027 {
02028   return (x.val_ ? os << "true" : os << "false");
02029 }
02030 
02031 std::ostream&
02032 operator<< (std::ostream &os, ACE_OutputCDR::from_char x)
02033 {
02034   return os << '\'' << x.val_ << '\'';
02035 }
02036 
02037 std::ostream&
02038 operator<< (std::ostream &os, ACE_OutputCDR::from_wchar x)
02039 {
02040   os.setf (ios_base::showbase);
02041   os.setf (ios_base::hex, ios_base::basefield);
02042   os << x.val_;
02043   os.unsetf (ios_base::showbase);
02044   os.setf (ios_base::dec, ios_base::basefield);
02045   return os;
02046 }
02047 
02048 std::ostream&
02049 operator<< (std::ostream &os, ACE_OutputCDR::from_octet x)
02050 {
02051   // Same format (hex) and no risk of overflow.
02052   ACE_CDR::WChar w = static_cast<ACE_CDR::WChar> (x.val_);
02053   ACE_OutputCDR::from_wchar tmp (w);
02054   return os << tmp;
02055 }
02056 
02057 #endif /* GEN_OSTREAM_OPS */
02058 
02059 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7