1 #include "ace/CDR_Size.h"
2 #include "ace/SString.h"
3 #include "ace/OS_Memory.h"
4 #include "ace/Truncate.h"
6 #if !defined (__ACE_INLINE__)
7 # include "ace/CDR_Size.inl"
8 #endif /* ! __ACE_INLINE__ */
12 "$Id: CDR_Size.cpp 82559 2008-08-07 20:23:07Z parsons $")
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x
)
19 // Note: translator framework is not supported.
21 if (ACE_OutputCDR::wchar_maxbytes () == 0)
24 return (this->good_bit_
= false);
27 if (static_cast<ACE_CDR::Short
> (major_version_
) == 1
28 && static_cast<ACE_CDR::Short
> (minor_version_
) == 2)
31 static_cast<ACE_CDR::Octet
> (ACE_OutputCDR::wchar_maxbytes ());
33 if (this->write_1 (&len
))
35 if (ACE_OutputCDR::wchar_maxbytes () == sizeof(ACE_CDR::WChar
))
38 this->write_octet_array (
39 reinterpret_cast<const ACE_CDR::Octet
*> (&x
),
40 static_cast<ACE_CDR::ULong
> (len
));
44 if (ACE_OutputCDR::wchar_maxbytes () == 2)
46 ACE_CDR::Short sx
= static_cast<ACE_CDR::Short
> (x
);
48 this->write_octet_array (
49 reinterpret_cast<const ACE_CDR::Octet
*> (&sx
),
50 static_cast<ACE_CDR::ULong
> (len
));
54 ACE_CDR::Octet ox
= static_cast<ACE_CDR::Octet
> (x
);
56 this->write_octet_array (
57 reinterpret_cast<const ACE_CDR::Octet
*> (&ox
),
58 static_cast<ACE_CDR::ULong
> (len
));
63 else if (static_cast<ACE_CDR::Short
> (minor_version_
) == 0)
64 { // wchar is not allowed with GIOP 1.0.
66 return (this->good_bit_
= false);
69 if (ACE_OutputCDR::wchar_maxbytes () == sizeof (ACE_CDR::WChar
))
71 const void *temp
= &x
;
72 return this->write_4 (reinterpret_cast<const ACE_CDR::ULong
*> (temp
));
74 else if (ACE_OutputCDR::wchar_maxbytes () == 2)
76 ACE_CDR::Short sx
= static_cast<ACE_CDR::Short
> (x
);
77 return this->write_2 (reinterpret_cast<const ACE_CDR::UShort
*> (&sx
));
80 ACE_CDR::Octet ox
= static_cast<ACE_CDR::Octet
> (x
);
81 return this->write_1 (reinterpret_cast<const ACE_CDR::Octet
*> (&ox
));
85 ACE_SizeCDR::write_string (ACE_CDR::ULong len
,
86 const ACE_CDR::Char
*x
)
88 // Note: translator framework is not supported.
92 if (this->write_ulong (len
+ 1))
93 return this->write_char_array (x
, len
+ 1);
97 // Be nice to programmers: treat nulls as empty strings not
98 // errors. (OMG-IDL supports languages that don't use the C/C++
99 // notion of null v. empty strings; nulls aren't part of the OMG-IDL
101 if (this->write_ulong (1))
102 return this->write_char (0);
105 return (this->good_bit_
= false);
109 ACE_SizeCDR::write_string (const ACE_CString
&x
)
111 // @@ Leave this method in here, not the `.i' file so that we don't
112 // have to unnecessarily pull in the `ace/SString.h' header.
113 return this->write_string (static_cast<ACE_CDR::ULong
> (x
.length ()),
118 ACE_SizeCDR::write_wstring (ACE_CDR::ULong len
,
119 const ACE_CDR::WChar
*x
)
121 // Note: translator framework is not supported.
123 if (ACE_OutputCDR::wchar_maxbytes () == 0)
126 return (this->good_bit_
= false);
129 if (static_cast<ACE_CDR::Short
> (this->major_version_
) == 1
130 && static_cast<ACE_CDR::Short
> (this->minor_version_
) == 2)
134 //In GIOP 1.2 the length field contains the number of bytes
135 //the wstring occupies rather than number of wchars
136 //Taking sizeof might not be a good way! This is a temporary fix.
137 ACE_CDR::Boolean good_ulong
=
139 ACE_Utils::truncate_cast
<ACE_CDR::ULong
> (
140 ACE_OutputCDR::wchar_maxbytes () * len
));
144 return this->write_wchar_array (x
, len
);
149 //In GIOP 1.2 zero length wstrings are legal
150 return this->write_ulong (0);
157 if (this->write_ulong (len
+ 1))
158 return this->write_wchar_array (x
, len
+ 1);
160 else if (this->write_ulong (1))
161 return this->write_wchar (0);
162 return (this->good_bit_
= false);
166 ACE_SizeCDR::write_1 (const ACE_CDR::Octet
*)
173 ACE_SizeCDR::write_2 (const ACE_CDR::UShort
*)
175 this->adjust (ACE_CDR::SHORT_SIZE
);
180 ACE_SizeCDR::write_4 (const ACE_CDR::ULong
*)
182 this->adjust (ACE_CDR::LONG_SIZE
);
187 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong
*)
189 this->adjust (ACE_CDR::LONGLONG_SIZE
);
194 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble
*)
196 this->adjust (ACE_CDR::LONGDOUBLE_SIZE
,
197 ACE_CDR::LONGDOUBLE_ALIGN
);
202 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar
*,
203 ACE_CDR::ULong length
)
208 size_t const align
= (ACE_OutputCDR::wchar_maxbytes () == 2) ?
209 ACE_CDR::SHORT_ALIGN
:
210 ACE_CDR::OCTET_ALIGN
;
212 this->adjust (ACE_OutputCDR::wchar_maxbytes () * length
, align
);
218 ACE_SizeCDR::write_array (const void *,
221 ACE_CDR::ULong length
)
226 this->adjust (size
* length
, align
);
231 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean
*,
232 ACE_CDR::ULong length
)
234 this->adjust (length
, 1);
239 ACE_SizeCDR::adjust (size_t size
)
245 ACE_SizeCDR::adjust (size_t size
,
248 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
249 const size_t offset
= ACE_align_binary (size_
, align
) - size_
;
251 #endif /* ACE_LACKS_CDR_ALIGNMENT */
256 operator<< (ACE_SizeCDR
&ss
, const ACE_CString
&x
)
259 return ss
.good_bit ();
262 ACE_END_VERSIONED_NAMESPACE_DECL