Revert to Current Include Style
[ACE_TAO.git] / ACE / ace / CDR_Size.cpp
blob4263532eb5318644b81383b2ad6322ff5726e3fd
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
12 ACE_CDR::Boolean
13 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x)
15 // Note: translator framework is not supported.
17 if (ACE_OutputCDR::wchar_maxbytes () == 0)
19 errno = EACCES;
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)
26 ACE_CDR::Octet len =
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))
33 return
34 this->write_octet_array (
35 reinterpret_cast<const ACE_CDR::Octet*> (&x),
36 static_cast<ACE_CDR::ULong> (len));
38 else
40 if (ACE_OutputCDR::wchar_maxbytes () == 2)
42 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
43 return
44 this->write_octet_array (
45 reinterpret_cast<const ACE_CDR::Octet*> (&sx),
46 static_cast<ACE_CDR::ULong> (len));
48 else
50 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
51 return
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.
61 errno = EINVAL;
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));
80 ACE_CDR::Boolean
81 ACE_SizeCDR::write_string (ACE_CDR::ULong len,
82 const ACE_CDR::Char *x)
84 // Note: translator framework is not supported.
86 if (len != 0)
88 if (this->write_ulong (len + 1))
89 return this->write_char_array (x, len + 1);
91 else
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
96 // string model.)
97 if (this->write_ulong (1))
98 return this->write_char (0);
101 return (this->good_bit_ = false);
104 ACE_CDR::Boolean
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 ()),
110 x.c_str());
113 ACE_CDR::Boolean
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)
121 errno = EACCES;
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)
128 if (x != 0)
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 =
134 this->write_ulong (
135 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
136 ACE_OutputCDR::wchar_maxbytes () * len));
138 if (good_ulong)
140 return this->write_wchar_array (x, len);
143 else
145 //In GIOP 1.2 zero length wstrings are legal
146 return this->write_ulong (0);
150 else
151 if (x != 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);
161 ACE_CDR::Boolean
162 ACE_SizeCDR::write_1 (const ACE_CDR::Octet *)
164 this->adjust (1);
165 return true;
168 ACE_CDR::Boolean
169 ACE_SizeCDR::write_2 (const ACE_CDR::UShort *)
171 this->adjust (ACE_CDR::SHORT_SIZE);
172 return true;
175 ACE_CDR::Boolean
176 ACE_SizeCDR::write_4 (const ACE_CDR::ULong *)
178 this->adjust (ACE_CDR::LONG_SIZE);
179 return true;
182 ACE_CDR::Boolean
183 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong *)
185 this->adjust (ACE_CDR::LONGLONG_SIZE);
186 return true;
189 ACE_CDR::Boolean
190 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble *)
192 this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
193 ACE_CDR::LONGDOUBLE_ALIGN);
194 return true;
197 ACE_CDR::Boolean
198 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar *,
199 ACE_CDR::ULong length)
201 if (length == 0)
202 return true;
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);
209 return true;
213 ACE_CDR::Boolean
214 ACE_SizeCDR::write_array (const void *,
215 size_t size,
216 size_t align,
217 ACE_CDR::ULong length)
219 if (length == 0)
220 return true;
222 this->adjust (size * length, align);
223 return true;
226 ACE_CDR::Boolean
227 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean*,
228 ACE_CDR::ULong length)
230 this->adjust (length, 1);
231 return true;
234 void
235 ACE_SizeCDR::adjust (size_t size)
237 adjust (size, size);
240 void
241 ACE_SizeCDR::adjust (size_t size,
242 size_t align)
244 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
245 const size_t offset = ACE_align_binary (size_, align) - size_;
246 size_ += offset;
247 #endif /* ACE_LACKS_CDR_ALIGNMENT */
248 size_ += size;
251 ACE_CDR::Boolean
252 operator<< (ACE_SizeCDR &ss, const ACE_CString &x)
254 ss.write_string (x);
255 return ss.good_bit ();
258 ACE_END_VERSIONED_NAMESPACE_DECL