1 | // file : xsd/cxx/tree/ace-cdr-stream-insertion.hxx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #ifndef XSD_CXX_TREE_ACE_CDR_STREAM_INSERTION_HXX |
---|
7 | #define XSD_CXX_TREE_ACE_CDR_STREAM_INSERTION_HXX |
---|
8 | |
---|
9 | #include <cstddef> // std::size_t |
---|
10 | #include <string> |
---|
11 | |
---|
12 | #include <ace/CDR_Stream.h> |
---|
13 | |
---|
14 | #include <xsd/cxx/tree/buffer.hxx> |
---|
15 | #include <xsd/cxx/tree/ostream.hxx> |
---|
16 | #include <xsd/cxx/tree/ace-cdr-stream-common.hxx> |
---|
17 | |
---|
18 | namespace xsd |
---|
19 | { |
---|
20 | namespace cxx |
---|
21 | { |
---|
22 | namespace tree |
---|
23 | { |
---|
24 | struct ace_cdr_stream_insertion: ace_cdr_stream_operation |
---|
25 | { |
---|
26 | virtual const char* |
---|
27 | what () const throw () |
---|
28 | { |
---|
29 | return "ACE CDR stream insertion operation failed"; |
---|
30 | } |
---|
31 | }; |
---|
32 | |
---|
33 | |
---|
34 | // as_size |
---|
35 | // |
---|
36 | |
---|
37 | #ifdef XSD_CXX_TREE_USE_64_BIT_SIZE |
---|
38 | template <typename X> |
---|
39 | inline ostream<ACE_OutputCDR>& |
---|
40 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
41 | ostream<ACE_OutputCDR>::as_size<X> x) |
---|
42 | { |
---|
43 | if (!s.impl ().write_ulonglong ( |
---|
44 | static_cast<ACE_CDR::ULongLong> (x.x_))) |
---|
45 | throw ace_cdr_stream_insertion (); |
---|
46 | return s; |
---|
47 | } |
---|
48 | #else |
---|
49 | template <typename X> |
---|
50 | inline ostream<ACE_OutputCDR>& |
---|
51 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
52 | ostream<ACE_OutputCDR>::as_size<X> x) |
---|
53 | { |
---|
54 | if (x.x_ > ~(ACE_CDR::ULong (0)) || |
---|
55 | !s.impl ().write_ulong (static_cast<ACE_CDR::ULong> (x.x_))) |
---|
56 | throw ace_cdr_stream_insertion (); |
---|
57 | |
---|
58 | return s; |
---|
59 | } |
---|
60 | #endif |
---|
61 | |
---|
62 | |
---|
63 | // 8-bit |
---|
64 | // |
---|
65 | template <typename X> |
---|
66 | inline ostream<ACE_OutputCDR>& |
---|
67 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
68 | ostream<ACE_OutputCDR>::as_int8<X> x) |
---|
69 | { |
---|
70 | ACE_CDR::Octet r (static_cast<ACE_CDR::Octet> (x.x_)); |
---|
71 | |
---|
72 | if (!s.impl ().write_octet (r)) |
---|
73 | throw ace_cdr_stream_insertion (); |
---|
74 | |
---|
75 | return s; |
---|
76 | } |
---|
77 | |
---|
78 | template <typename X> |
---|
79 | inline ostream<ACE_OutputCDR>& |
---|
80 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
81 | ostream<ACE_OutputCDR>::as_uint8<X> x) |
---|
82 | { |
---|
83 | ACE_CDR::Octet r (static_cast<ACE_CDR::Octet> (x.x_)); |
---|
84 | |
---|
85 | if (!s.impl ().write_octet (r)) |
---|
86 | throw ace_cdr_stream_insertion (); |
---|
87 | |
---|
88 | return s; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | // 16-bit |
---|
93 | // |
---|
94 | template <typename X> |
---|
95 | inline ostream<ACE_OutputCDR>& |
---|
96 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
97 | ostream<ACE_OutputCDR>::as_int16<X> x) |
---|
98 | { |
---|
99 | if (!s.impl ().write_short (static_cast<ACE_CDR::Short> (x.x_))) |
---|
100 | throw ace_cdr_stream_insertion (); |
---|
101 | return s; |
---|
102 | } |
---|
103 | |
---|
104 | template <typename X> |
---|
105 | inline ostream<ACE_OutputCDR>& |
---|
106 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
107 | ostream<ACE_OutputCDR>::as_uint16<X> x) |
---|
108 | { |
---|
109 | if (!s.impl ().write_ushort (static_cast<ACE_CDR::UShort> (x.x_))) |
---|
110 | throw ace_cdr_stream_insertion (); |
---|
111 | return s; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | // 32-bit |
---|
116 | // |
---|
117 | template <typename X> |
---|
118 | inline ostream<ACE_OutputCDR>& |
---|
119 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
120 | ostream<ACE_OutputCDR>::as_int32<X> x) |
---|
121 | { |
---|
122 | if (!s.impl ().write_long (static_cast<ACE_CDR::Long> (x.x_))) |
---|
123 | throw ace_cdr_stream_insertion (); |
---|
124 | return s; |
---|
125 | } |
---|
126 | |
---|
127 | template <typename X> |
---|
128 | inline ostream<ACE_OutputCDR>& |
---|
129 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
130 | ostream<ACE_OutputCDR>::as_uint32<X> x) |
---|
131 | { |
---|
132 | if (!s.impl ().write_ulong (static_cast<ACE_CDR::ULong> (x.x_))) |
---|
133 | throw ace_cdr_stream_insertion (); |
---|
134 | return s; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | // 64-bit |
---|
139 | // |
---|
140 | template <typename X> |
---|
141 | inline ostream<ACE_OutputCDR>& |
---|
142 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
143 | ostream<ACE_OutputCDR>::as_int64<X> x) |
---|
144 | { |
---|
145 | if (!s.impl ().write_longlong (static_cast<ACE_CDR::LongLong> (x.x_))) |
---|
146 | throw ace_cdr_stream_insertion (); |
---|
147 | return s; |
---|
148 | } |
---|
149 | |
---|
150 | template <typename X> |
---|
151 | inline ostream<ACE_OutputCDR>& |
---|
152 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
153 | ostream<ACE_OutputCDR>::as_uint64<X> x) |
---|
154 | { |
---|
155 | if (!s.impl ().write_ulonglong ( |
---|
156 | static_cast<ACE_CDR::ULongLong> (x.x_))) |
---|
157 | throw ace_cdr_stream_insertion (); |
---|
158 | return s; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | // Boolean |
---|
163 | // |
---|
164 | template <typename X> |
---|
165 | inline ostream<ACE_OutputCDR>& |
---|
166 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
167 | ostream<ACE_OutputCDR>::as_bool<X> x) |
---|
168 | { |
---|
169 | if (!s.impl ().write_boolean (static_cast<ACE_CDR::Boolean> (x.x_))) |
---|
170 | throw ace_cdr_stream_insertion (); |
---|
171 | return s; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | // Floating-point |
---|
176 | // |
---|
177 | template <typename X> |
---|
178 | inline ostream<ACE_OutputCDR>& |
---|
179 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
180 | ostream<ACE_OutputCDR>::as_float32<X> x) |
---|
181 | { |
---|
182 | if (!s.impl ().write_float (static_cast<ACE_CDR::Float> (x.x_))) |
---|
183 | throw ace_cdr_stream_insertion (); |
---|
184 | return s; |
---|
185 | } |
---|
186 | |
---|
187 | template <typename X> |
---|
188 | inline ostream<ACE_OutputCDR>& |
---|
189 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
190 | ostream<ACE_OutputCDR>::as_float64<X> x) |
---|
191 | { |
---|
192 | if (!s.impl ().write_double (static_cast<ACE_CDR::Double> (x.x_))) |
---|
193 | throw ace_cdr_stream_insertion (); |
---|
194 | return s; |
---|
195 | } |
---|
196 | |
---|
197 | // Insertion of std::basic_string. |
---|
198 | // |
---|
199 | |
---|
200 | inline ostream<ACE_OutputCDR>& |
---|
201 | operator<< (ostream<ACE_OutputCDR>& s, const std::basic_string<char>& x) |
---|
202 | { |
---|
203 | // ACE CDR strings are hard-wired with a 32 bit length. |
---|
204 | // |
---|
205 | if (x.length () > ~(ACE_CDR::ULong (0)) || |
---|
206 | !s.impl ().write_string ( |
---|
207 | static_cast<ACE_CDR::ULong> (x.length ()), x.c_str ())) |
---|
208 | throw ace_cdr_stream_insertion (); |
---|
209 | return s; |
---|
210 | } |
---|
211 | |
---|
212 | #ifdef ACE_HAS_WCHAR |
---|
213 | inline ostream<ACE_OutputCDR>& |
---|
214 | operator<< (ostream<ACE_OutputCDR>& s, |
---|
215 | const std::basic_string<wchar_t>& x) |
---|
216 | { |
---|
217 | // ACE CDR strings are hard-wired with a 32 bit length. |
---|
218 | // |
---|
219 | if (x.length () > ~(ACE_CDR::ULong (0)) || |
---|
220 | !s.impl ().write_wstring ( |
---|
221 | static_cast<ACE_CDR::ULong> (x.length ()), x.c_str ())) |
---|
222 | throw ace_cdr_stream_insertion (); |
---|
223 | return s; |
---|
224 | } |
---|
225 | #endif |
---|
226 | |
---|
227 | |
---|
228 | // Insertion of a binary buffer. |
---|
229 | // |
---|
230 | template <typename C> |
---|
231 | ostream<ACE_OutputCDR>& |
---|
232 | operator<< (ostream<ACE_OutputCDR>& s, const buffer<C>& x) |
---|
233 | { |
---|
234 | std::size_t size (x.size ()); |
---|
235 | |
---|
236 | // It is not possible to write an array with a 64-bit size. |
---|
237 | // |
---|
238 | if (size > ~(ACE_CDR::ULong (0)) || |
---|
239 | !s.impl ().write_ulong (static_cast<ACE_CDR::ULong> (size)) || |
---|
240 | !s.impl ().write_octet_array ( |
---|
241 | reinterpret_cast<const ACE_CDR::Octet*> (x.data ()), size)) |
---|
242 | throw ace_cdr_stream_insertion (); |
---|
243 | |
---|
244 | return s; |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | #endif // XSD_CXX_TREE_ACE_CDR_STREAM_INSERTION_HXX |
---|