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 #include "ace/Truncate.h"
00005
00006 #if !defined (__ACE_INLINE__)
00007 # include "ace/CDR_Size.inl"
00008 #endif
00009
00010 ACE_RCSID (ace,
00011 CDR_Size,
00012 "$Id: CDR_Size.cpp 82559 2008-08-07 20:23:07Z parsons $")
00013
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015
00016 ACE_CDR::Boolean
00017 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x)
00018 {
00019
00020
00021 if (ACE_OutputCDR::wchar_maxbytes () == 0)
00022 {
00023 errno = EACCES;
00024 return (this->good_bit_ = false);
00025 }
00026
00027 if (static_cast<ACE_CDR::Short> (major_version_) == 1
00028 && static_cast<ACE_CDR::Short> (minor_version_) == 2)
00029 {
00030 ACE_CDR::Octet len =
00031 static_cast<ACE_CDR::Octet> (ACE_OutputCDR::wchar_maxbytes ());
00032
00033 if (this->write_1 (&len))
00034 {
00035 if (ACE_OutputCDR::wchar_maxbytes () == sizeof(ACE_CDR::WChar))
00036 {
00037 return
00038 this->write_octet_array (
00039 reinterpret_cast<const ACE_CDR::Octet*> (&x),
00040 static_cast<ACE_CDR::ULong> (len));
00041 }
00042 else
00043 {
00044 if (ACE_OutputCDR::wchar_maxbytes () == 2)
00045 {
00046 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00047 return
00048 this->write_octet_array (
00049 reinterpret_cast<const ACE_CDR::Octet*> (&sx),
00050 static_cast<ACE_CDR::ULong> (len));
00051 }
00052 else
00053 {
00054 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00055 return
00056 this->write_octet_array (
00057 reinterpret_cast<const ACE_CDR::Octet*> (&ox),
00058 static_cast<ACE_CDR::ULong> (len));
00059 }
00060 }
00061 }
00062 }
00063 else if (static_cast<ACE_CDR::Short> (minor_version_) == 0)
00064 {
00065 errno = EINVAL;
00066 return (this->good_bit_ = false);
00067 }
00068
00069 if (ACE_OutputCDR::wchar_maxbytes () == sizeof (ACE_CDR::WChar))
00070 {
00071 const void *temp = &x;
00072 return this->write_4 (reinterpret_cast<const ACE_CDR::ULong *> (temp));
00073 }
00074 else if (ACE_OutputCDR::wchar_maxbytes () == 2)
00075 {
00076 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
00077 return this->write_2 (reinterpret_cast<const ACE_CDR::UShort *> (&sx));
00078 }
00079
00080 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
00081 return this->write_1 (reinterpret_cast<const ACE_CDR::Octet *> (&ox));
00082 }
00083
00084 ACE_CDR::Boolean
00085 ACE_SizeCDR::write_string (ACE_CDR::ULong len,
00086 const ACE_CDR::Char *x)
00087 {
00088
00089
00090 if (len != 0)
00091 {
00092 if (this->write_ulong (len + 1))
00093 return this->write_char_array (x, len + 1);
00094 }
00095 else
00096 {
00097
00098
00099
00100
00101 if (this->write_ulong (1))
00102 return this->write_char (0);
00103 }
00104
00105 return (this->good_bit_ = false);
00106 }
00107
00108 ACE_CDR::Boolean
00109 ACE_SizeCDR::write_string (const ACE_CString &x)
00110 {
00111
00112
00113 return this->write_string (static_cast<ACE_CDR::ULong> (x.length ()),
00114 x.c_str());
00115 }
00116
00117 ACE_CDR::Boolean
00118 ACE_SizeCDR::write_wstring (ACE_CDR::ULong len,
00119 const ACE_CDR::WChar *x)
00120 {
00121
00122
00123 if (ACE_OutputCDR::wchar_maxbytes () == 0)
00124 {
00125 errno = EACCES;
00126 return (this->good_bit_ = false);
00127 }
00128
00129 if (static_cast<ACE_CDR::Short> (this->major_version_) == 1
00130 && static_cast<ACE_CDR::Short> (this->minor_version_) == 2)
00131 {
00132 if (x != 0)
00133 {
00134
00135
00136
00137 ACE_CDR::Boolean good_ulong =
00138 this->write_ulong (
00139 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
00140 ACE_OutputCDR::wchar_maxbytes () * len));
00141
00142 if (good_ulong)
00143 {
00144 return this->write_wchar_array (x, len);
00145 }
00146 }
00147 else
00148 {
00149
00150 return this->write_ulong (0);
00151 }
00152 }
00153
00154 else
00155 if (x != 0)
00156 {
00157 if (this->write_ulong (len + 1))
00158 return this->write_wchar_array (x, len + 1);
00159 }
00160 else if (this->write_ulong (1))
00161 return this->write_wchar (0);
00162 return (this->good_bit_ = false);
00163 }
00164
00165 ACE_CDR::Boolean
00166 ACE_SizeCDR::write_1 (const ACE_CDR::Octet *)
00167 {
00168 this->adjust (1);
00169 return true;
00170 }
00171
00172 ACE_CDR::Boolean
00173 ACE_SizeCDR::write_2 (const ACE_CDR::UShort *)
00174 {
00175 this->adjust (ACE_CDR::SHORT_SIZE);
00176 return true;
00177 }
00178
00179 ACE_CDR::Boolean
00180 ACE_SizeCDR::write_4 (const ACE_CDR::ULong *)
00181 {
00182 this->adjust (ACE_CDR::LONG_SIZE);
00183 return true;
00184 }
00185
00186 ACE_CDR::Boolean
00187 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong *)
00188 {
00189 this->adjust (ACE_CDR::LONGLONG_SIZE);
00190 return true;
00191 }
00192
00193 ACE_CDR::Boolean
00194 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble *)
00195 {
00196 this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
00197 ACE_CDR::LONGDOUBLE_ALIGN);
00198 return true;
00199 }
00200
00201 ACE_CDR::Boolean
00202 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar *,
00203 ACE_CDR::ULong length)
00204 {
00205 if (length == 0)
00206 return true;
00207
00208 size_t const align = (ACE_OutputCDR::wchar_maxbytes () == 2) ?
00209 ACE_CDR::SHORT_ALIGN :
00210 ACE_CDR::OCTET_ALIGN;
00211
00212 this->adjust (ACE_OutputCDR::wchar_maxbytes () * length, align);
00213 return true;
00214 }
00215
00216
00217 ACE_CDR::Boolean
00218 ACE_SizeCDR::write_array (const void *,
00219 size_t size,
00220 size_t align,
00221 ACE_CDR::ULong length)
00222 {
00223 if (length == 0)
00224 return true;
00225
00226 this->adjust (size * length, align);
00227 return true;
00228 }
00229
00230 ACE_CDR::Boolean
00231 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean*,
00232 ACE_CDR::ULong length)
00233 {
00234 this->adjust (length, 1);
00235 return true;
00236 }
00237
00238 void
00239 ACE_SizeCDR::adjust (size_t size)
00240 {
00241 adjust (size, size);
00242 }
00243
00244 void
00245 ACE_SizeCDR::adjust (size_t size,
00246 size_t align)
00247 {
00248 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
00249 const size_t offset = ACE_align_binary (size_, align) - size_;
00250 size_ += offset;
00251 #endif
00252 size_ += size;
00253 }
00254
00255 ACE_CDR::Boolean
00256 operator<< (ACE_SizeCDR &ss, const ACE_CString &x)
00257 {
00258 ss.write_string (x);
00259 return ss.good_bit ();
00260 }
00261
00262 ACE_END_VERSIONED_NAMESPACE_DECL