CDR_Size.cpp

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

Generated on Thu Nov 9 09:41:48 2006 for ACE by doxygen 1.3.6