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__ */
10 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
13 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x
)
15 // Note: translator framework is not supported.
17 if (ACE_OutputCDR::wchar_maxbytes () == 0)
20 return (this->good_bit_
= false);
23 if (static_cast<ACE_CDR::Short
> (major_version_
) == 1
24 && static_cast<ACE_CDR::Short
> (minor_version_
) == 2)
27 static_cast<ACE_CDR::Octet
> (ACE_OutputCDR::wchar_maxbytes ());
29 if (this->write_1 (&len
))
31 if (ACE_OutputCDR::wchar_maxbytes () == sizeof(ACE_CDR::WChar
))
34 this->write_octet_array (
35 reinterpret_cast<const ACE_CDR::Octet
*> (&x
),
36 static_cast<ACE_CDR::ULong
> (len
));
40 if (ACE_OutputCDR::wchar_maxbytes () == 2)
42 ACE_CDR::Short sx
= static_cast<ACE_CDR::Short
> (x
);
44 this->write_octet_array (
45 reinterpret_cast<const ACE_CDR::Octet
*> (&sx
),
46 static_cast<ACE_CDR::ULong
> (len
));
50 ACE_CDR::Octet ox
= static_cast<ACE_CDR::Octet
> (x
);
52 this->write_octet_array (
53 reinterpret_cast<const ACE_CDR::Octet
*> (&ox
),
54 static_cast<ACE_CDR::ULong
> (len
));
59 else if (static_cast<ACE_CDR::Short
> (minor_version_
) == 0)
60 { // wchar is not allowed with GIOP 1.0.
62 return (this->good_bit_
= false);
65 if (ACE_OutputCDR::wchar_maxbytes () == sizeof (ACE_CDR::WChar
))
67 const void *temp
= &x
;
68 return this->write_4 (reinterpret_cast<const ACE_CDR::ULong
*> (temp
));
70 else if (ACE_OutputCDR::wchar_maxbytes () == 2)
72 ACE_CDR::Short sx
= static_cast<ACE_CDR::Short
> (x
);
73 return this->write_2 (reinterpret_cast<const ACE_CDR::UShort
*> (&sx
));
76 ACE_CDR::Octet ox
= static_cast<ACE_CDR::Octet
> (x
);
77 return this->write_1 (reinterpret_cast<const ACE_CDR::Octet
*> (&ox
));
81 ACE_SizeCDR::write_string (ACE_CDR::ULong len
,
82 const ACE_CDR::Char
*x
)
84 // Note: translator framework is not supported.
88 if (this->write_ulong (len
+ 1))
89 return this->write_char_array (x
, len
+ 1);
93 // Be nice to programmers: treat nulls as empty strings not
94 // errors. (OMG-IDL supports languages that don't use the C/C++
95 // notion of null v. empty strings; nulls aren't part of the OMG-IDL
97 if (this->write_ulong (1))
98 return this->write_char (0);
101 return (this->good_bit_
= false);
105 ACE_SizeCDR::write_string (const ACE_CString
&x
)
107 // @@ Leave this method in here, not the `.i' file so that we don't
108 // have to unnecessarily pull in the `ace/SString.h' header.
109 return this->write_string (static_cast<ACE_CDR::ULong
> (x
.length ()),
114 ACE_SizeCDR::write_wstring (ACE_CDR::ULong len
,
115 const ACE_CDR::WChar
*x
)
117 // Note: translator framework is not supported.
119 if (ACE_OutputCDR::wchar_maxbytes () == 0)
122 return (this->good_bit_
= false);
125 if (static_cast<ACE_CDR::Short
> (this->major_version_
) == 1
126 && static_cast<ACE_CDR::Short
> (this->minor_version_
) == 2)
130 //In GIOP 1.2 the length field contains the number of bytes
131 //the wstring occupies rather than number of wchars
132 //Taking sizeof might not be a good way! This is a temporary fix.
133 ACE_CDR::Boolean good_ulong
=
135 ACE_Utils::truncate_cast
<ACE_CDR::ULong
> (
136 ACE_OutputCDR::wchar_maxbytes () * len
));
140 return this->write_wchar_array (x
, len
);
145 //In GIOP 1.2 zero length wstrings are legal
146 return this->write_ulong (0);
153 if (this->write_ulong (len
+ 1))
154 return this->write_wchar_array (x
, len
+ 1);
156 else if (this->write_ulong (1))
157 return this->write_wchar (0);
158 return (this->good_bit_
= false);
162 ACE_SizeCDR::write_1 (const ACE_CDR::Octet
*)
169 ACE_SizeCDR::write_2 (const ACE_CDR::UShort
*)
171 this->adjust (ACE_CDR::SHORT_SIZE
);
176 ACE_SizeCDR::write_4 (const ACE_CDR::ULong
*)
178 this->adjust (ACE_CDR::LONG_SIZE
);
183 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong
*)
185 this->adjust (ACE_CDR::LONGLONG_SIZE
);
190 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble
*)
192 this->adjust (ACE_CDR::LONGDOUBLE_SIZE
,
193 ACE_CDR::LONGDOUBLE_ALIGN
);
198 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar
*,
199 ACE_CDR::ULong length
)
204 size_t const align
= (ACE_OutputCDR::wchar_maxbytes () == 2) ?
205 ACE_CDR::SHORT_ALIGN
:
206 ACE_CDR::OCTET_ALIGN
;
208 this->adjust (ACE_OutputCDR::wchar_maxbytes () * length
, align
);
214 ACE_SizeCDR::write_array (const void *,
217 ACE_CDR::ULong length
)
222 this->adjust (size
* length
, align
);
227 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean
*,
228 ACE_CDR::ULong length
)
230 this->adjust (length
, 1);
235 ACE_SizeCDR::adjust (size_t size
)
241 ACE_SizeCDR::adjust (size_t size
,
244 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
245 const size_t offset
= ACE_align_binary (size_
, align
) - size_
;
247 #endif /* ACE_LACKS_CDR_ALIGNMENT */
252 operator<< (ACE_SizeCDR
&ss
, const ACE_CString
&x
)
255 return ss
.good_bit ();
258 ACE_END_VERSIONED_NAMESPACE_DECL