CDR_Size.cpp

Go to the documentation of this file.
00001 #include "ace/CDR_Size.h"
00002 #include "ace/SString.h"
00003 #include "ace/OS_Memory.h"
00004 
00005 #if !defined (__ACE_INLINE__)
00006 # include "ace/CDR_Size.inl"
00007 #endif /* ! __ACE_INLINE__ */
00008 
00009 ACE_RCSID (ace,
00010            CDR_Size,
00011            "$Id: CDR_Size.cpp 79028 2007-07-25 08:24:55Z johnnyw $")
00012 
00013 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 ACE_CDR::Boolean
00016 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x)
00017 {
00018   // Note: translator framework is not supported.
00019   //
00020   if (ACE_OutputCDR::wchar_maxbytes () == 0)
00021     {
00022       errno = EACCES;
00023       return (this->good_bit_ = false);
00024     }
00025   if (static_cast<ACE_CDR::Short> (major_version_) == 1
00026           && static_cast<ACE_CDR::Short> (minor_version_) == 2)
00027     {
00028       ACE_CDR::Octet len =
00029         static_cast<ACE_CDR::Octet> (ACE_OutputCDR::wchar_maxbytes ());
00030       if (this->write_1 (&len))
00031         {
00032           if (ACE_OutputCDR::wchar_maxbytes () == sizeof(ACE_CDR::WChar))
00033             return
00034               this->write_octet_array (
00035                 reinterpret_cast<const ACE_CDR::Octet*> (&x),
00036                 static_cast<ACE_CDR::ULong> (len));
00037           else
00038             if (ACE_OutputCDR::wchar_maxbytes () == 2)
00039               {
00040                 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00041                 return
00042                   this->write_octet_array (
00043                     reinterpret_cast<const ACE_CDR::Octet*> (&sx),
00044                     static_cast<ACE_CDR::ULong> (len));
00045               }
00046             else
00047               {
00048                 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00049                 return
00050                   this->write_octet_array (
00051                     reinterpret_cast<const ACE_CDR::Octet*> (&ox),
00052                     static_cast<ACE_CDR::ULong> (len));
00053               }
00054         }
00055     }
00056   else if (static_cast<ACE_CDR::Short> (minor_version_) == 0)
00057     { // wchar is not allowed with GIOP 1.0.
00058       errno = EINVAL;
00059       return (this->good_bit_ = false);
00060     }
00061   if (ACE_OutputCDR::wchar_maxbytes () == sizeof (ACE_CDR::WChar))
00062     {
00063       const void *temp = &x;
00064       return this->write_4 (reinterpret_cast<const ACE_CDR::ULong *> (temp));
00065     }
00066   else if (ACE_OutputCDR::wchar_maxbytes () == 2)
00067     {
00068       ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00069       return this->write_2 (reinterpret_cast<const ACE_CDR::UShort *> (&sx));
00070     }
00071   ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00072   return this->write_1 (reinterpret_cast<const ACE_CDR::Octet *> (&ox));
00073 }
00074 
00075 ACE_CDR::Boolean
00076 ACE_SizeCDR::write_string (ACE_CDR::ULong len,
00077                              const ACE_CDR::Char *x)
00078 {
00079   // Note: translator framework is not supported.
00080   //
00081   if (len != 0)
00082     {
00083       if (this->write_ulong (len + 1))
00084         return this->write_char_array (x, len + 1);
00085     }
00086   else
00087     {
00088       // Be nice to programmers: treat nulls as empty strings not
00089       // errors. (OMG-IDL supports languages that don't use the C/C++
00090       // notion of null v. empty strings; nulls aren't part of the OMG-IDL
00091       // string model.)
00092       if (this->write_ulong (1))
00093         return this->write_char (0);
00094     }
00095 
00096   return (this->good_bit_ = false);
00097 }
00098 
00099 ACE_CDR::Boolean
00100 ACE_SizeCDR::write_string (const ACE_CString &x)
00101 {
00102   // @@ Leave this method in here, not the `.i' file so that we don't
00103   //    have to unnecessarily pull in the `ace/SString.h' header.
00104   return this->write_string (static_cast<ACE_CDR::ULong> (x.length ()),
00105                              x.c_str());
00106 }
00107 
00108 ACE_CDR::Boolean
00109 ACE_SizeCDR::write_wstring (ACE_CDR::ULong len,
00110                               const ACE_CDR::WChar *x)
00111 {
00112   // Note: translator framework is not supported.
00113   //
00114   if (ACE_OutputCDR::wchar_maxbytes () == 0)
00115     {
00116       errno = EACCES;
00117       return (this->good_bit_ = false);
00118     }
00119 
00120   if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
00121       && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
00122     {
00123       if (x != 0)
00124         {
00125           //In GIOP 1.2 the length field contains the number of bytes
00126           //the wstring occupies rather than number of wchars
00127           //Taking sizeof might not be a good way! This is a temporary fix.
00128           if (this->write_ulong (ACE_OutputCDR::wchar_maxbytes () * len))
00129             return this->write_wchar_array (x, len);
00130         }
00131       else
00132         //In GIOP 1.2 zero length wstrings are legal
00133         return this->write_ulong (0);
00134     }
00135 
00136   else
00137     if (x != 0)
00138       {
00139         if (this->write_ulong (len + 1))
00140           return this->write_wchar_array (x, len + 1);
00141       }
00142     else if (this->write_ulong (1))
00143       return this->write_wchar (0);
00144    return (this->good_bit_ = false);
00145 }
00146 
00147 ACE_CDR::Boolean
00148 ACE_SizeCDR::write_1 (const ACE_CDR::Octet *)
00149 {
00150   this->adjust (1);
00151   return true;
00152 }
00153 
00154 ACE_CDR::Boolean
00155 ACE_SizeCDR::write_2 (const ACE_CDR::UShort *)
00156 {
00157   this->adjust (ACE_CDR::SHORT_SIZE);
00158   return true;
00159 }
00160 
00161 ACE_CDR::Boolean
00162 ACE_SizeCDR::write_4 (const ACE_CDR::ULong *)
00163 {
00164   this->adjust (ACE_CDR::LONG_SIZE);
00165   return true;
00166 }
00167 
00168 ACE_CDR::Boolean
00169 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong *)
00170 {
00171   this->adjust (ACE_CDR::LONGLONG_SIZE);
00172   return true;
00173 }
00174 
00175 ACE_CDR::Boolean
00176 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble *)
00177 {
00178   this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
00179                 ACE_CDR::LONGDOUBLE_ALIGN);
00180   return true;
00181 }
00182 
00183 ACE_CDR::Boolean
00184 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar *,
00185                                     ACE_CDR::ULong length)
00186 {
00187   if (length == 0)
00188     return true;
00189 
00190   size_t const align = (ACE_OutputCDR::wchar_maxbytes () == 2) ?
00191     ACE_CDR::SHORT_ALIGN :
00192     ACE_CDR::OCTET_ALIGN;
00193 
00194   this->adjust (ACE_OutputCDR::wchar_maxbytes () * length, align);
00195   return true;
00196 }
00197 
00198 
00199 ACE_CDR::Boolean
00200 ACE_SizeCDR::write_array (const void *,
00201                           size_t size,
00202                           size_t align,
00203                           ACE_CDR::ULong length)
00204 {
00205   if (length == 0)
00206     return true;
00207 
00208   this->adjust (size * length, align);
00209   return true;
00210 }
00211 
00212 ACE_CDR::Boolean
00213 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean*,
00214                                   ACE_CDR::ULong length)
00215 {
00216   this->adjust (length, 1);
00217   return true;
00218 }
00219 
00220 void
00221 ACE_SizeCDR::adjust (size_t size)
00222 {
00223   adjust (size, size);
00224 }
00225 
00226 void
00227 ACE_SizeCDR::adjust (size_t size,
00228                      size_t align)
00229 {
00230 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00231   const size_t offset = ACE_align_binary (size_, align) - size_;
00232   size_ += offset;
00233 #endif /* ACE_LACKS_CDR_ALIGNMENT */
00234   size_ += size;
00235 }
00236 
00237 ACE_CDR::Boolean
00238 operator<< (ACE_SizeCDR &ss, const ACE_CString &x)
00239 {
00240   ss.write_string (x);
00241   return ss.good_bit ();
00242 }
00243 
00244 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 12:05:22 2008 for ACE by doxygen 1.3.6