[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / CDR_Size.cpp
blob96970927037803a1c88418bb5861779e621f6e98
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_RCSID (ace,
11 CDR_Size,
12 "$Id: CDR_Size.cpp 82559 2008-08-07 20:23:07Z parsons $")
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 ACE_CDR::Boolean
17 ACE_SizeCDR::write_wchar (ACE_CDR::WChar x)
19 // Note: translator framework is not supported.
21 if (ACE_OutputCDR::wchar_maxbytes () == 0)
23 errno = EACCES;
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)
30 ACE_CDR::Octet len =
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))
37 return
38 this->write_octet_array (
39 reinterpret_cast<const ACE_CDR::Octet*> (&x),
40 static_cast<ACE_CDR::ULong> (len));
42 else
44 if (ACE_OutputCDR::wchar_maxbytes () == 2)
46 ACE_CDR::Short sx = static_cast<ACE_CDR::Short> (x);
47 return
48 this->write_octet_array (
49 reinterpret_cast<const ACE_CDR::Octet*> (&sx),
50 static_cast<ACE_CDR::ULong> (len));
52 else
54 ACE_CDR::Octet ox = static_cast<ACE_CDR::Octet> (x);
55 return
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.
65 errno = EINVAL;
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));
84 ACE_CDR::Boolean
85 ACE_SizeCDR::write_string (ACE_CDR::ULong len,
86 const ACE_CDR::Char *x)
88 // Note: translator framework is not supported.
90 if (len != 0)
92 if (this->write_ulong (len + 1))
93 return this->write_char_array (x, len + 1);
95 else
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
100 // string model.)
101 if (this->write_ulong (1))
102 return this->write_char (0);
105 return (this->good_bit_ = false);
108 ACE_CDR::Boolean
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 ()),
114 x.c_str());
117 ACE_CDR::Boolean
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)
125 errno = EACCES;
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)
132 if (x != 0)
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 =
138 this->write_ulong (
139 ACE_Utils::truncate_cast<ACE_CDR::ULong> (
140 ACE_OutputCDR::wchar_maxbytes () * len));
142 if (good_ulong)
144 return this->write_wchar_array (x, len);
147 else
149 //In GIOP 1.2 zero length wstrings are legal
150 return this->write_ulong (0);
154 else
155 if (x != 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);
165 ACE_CDR::Boolean
166 ACE_SizeCDR::write_1 (const ACE_CDR::Octet *)
168 this->adjust (1);
169 return true;
172 ACE_CDR::Boolean
173 ACE_SizeCDR::write_2 (const ACE_CDR::UShort *)
175 this->adjust (ACE_CDR::SHORT_SIZE);
176 return true;
179 ACE_CDR::Boolean
180 ACE_SizeCDR::write_4 (const ACE_CDR::ULong *)
182 this->adjust (ACE_CDR::LONG_SIZE);
183 return true;
186 ACE_CDR::Boolean
187 ACE_SizeCDR::write_8 (const ACE_CDR::ULongLong *)
189 this->adjust (ACE_CDR::LONGLONG_SIZE);
190 return true;
193 ACE_CDR::Boolean
194 ACE_SizeCDR::write_16 (const ACE_CDR::LongDouble *)
196 this->adjust (ACE_CDR::LONGDOUBLE_SIZE,
197 ACE_CDR::LONGDOUBLE_ALIGN);
198 return true;
201 ACE_CDR::Boolean
202 ACE_SizeCDR::write_wchar_array_i (const ACE_CDR::WChar *,
203 ACE_CDR::ULong length)
205 if (length == 0)
206 return true;
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);
213 return true;
217 ACE_CDR::Boolean
218 ACE_SizeCDR::write_array (const void *,
219 size_t size,
220 size_t align,
221 ACE_CDR::ULong length)
223 if (length == 0)
224 return true;
226 this->adjust (size * length, align);
227 return true;
230 ACE_CDR::Boolean
231 ACE_SizeCDR::write_boolean_array (const ACE_CDR::Boolean*,
232 ACE_CDR::ULong length)
234 this->adjust (length, 1);
235 return true;
238 void
239 ACE_SizeCDR::adjust (size_t size)
241 adjust (size, size);
244 void
245 ACE_SizeCDR::adjust (size_t size,
246 size_t align)
248 #if !defined (ACE_LACKS_CDR_ALIGNMENT)
249 const size_t offset = ACE_align_binary (size_, align) - size_;
250 size_ += offset;
251 #endif /* ACE_LACKS_CDR_ALIGNMENT */
252 size_ += size;
255 ACE_CDR::Boolean
256 operator<< (ACE_SizeCDR &ss, const ACE_CString &x)
258 ss.write_string (x);
259 return ss.good_bit ();
262 ACE_END_VERSIONED_NAMESPACE_DECL