]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/include/boost/spirit/home/karma/binary/binary.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / include / boost / spirit / home / karma / binary / binary.hpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #if !defined(BOOST_SPIRIT_KARMA_BINARY_MAY_04_2007_0904AM)
7 #define BOOST_SPIRIT_KARMA_BINARY_MAY_04_2007_0904AM
8
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12
13 #include <boost/spirit/home/support/common_terminals.hpp>
14 #include <boost/spirit/home/support/info.hpp>
15 #include <boost/spirit/home/support/detail/endian.hpp>
16
17 #include <boost/spirit/home/karma/domain.hpp>
18 #include <boost/spirit/home/karma/meta_compiler.hpp>
19 #include <boost/spirit/home/karma/delimit_out.hpp>
20 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
21 #include <boost/spirit/home/karma/detail/generate_to.hpp>
22 #include <boost/spirit/home/karma/detail/extract_from.hpp>
23 #include <boost/spirit/home/support/unused.hpp>
24 #include <boost/spirit/home/support/container.hpp>
25 #include <boost/fusion/include/vector.hpp>
26 #include <boost/fusion/include/at.hpp>
27 #include <boost/mpl/or.hpp>
28 #include <boost/type_traits/is_integral.hpp>
29 #include <boost/type_traits/is_enum.hpp>
30 #include <boost/type_traits/is_floating_point.hpp>
31 #include <boost/config.hpp>
32
33 ///////////////////////////////////////////////////////////////////////////////
34 #define BOOST_SPIRIT_ENABLE_BINARY(name) \
35 template <> \
36 struct use_terminal<karma::domain, tag::name> \
37 : mpl::true_ {}; \
38 \
39 template <typename A0> \
40 struct use_terminal<karma::domain \
41 , terminal_ex<tag::name, fusion::vector1<A0> > > \
42 : mpl::or_<is_integral<A0>, is_enum<A0> > {}; \
43 \
44 template <> \
45 struct use_lazy_terminal<karma::domain, tag::name, 1> : mpl::true_ {}; \
46 \
47 /***/
48
49 #define BOOST_SPIRIT_ENABLE_BINARY_IEEE754(name) \
50 template<> \
51 struct use_terminal<karma::domain, tag::name>: mpl::true_ {}; \
52 \
53 template<typename A0> \
54 struct use_terminal<karma::domain, terminal_ex<tag::name, \
55 fusion::vector1<A0> > >: is_floating_point<A0> {}; \
56 \
57 template<> \
58 struct use_lazy_terminal<karma::domain, tag::name, 1> : mpl::true_ {}; \
59 \
60 /***/
61
62 namespace boost { namespace spirit
63 {
64 ///////////////////////////////////////////////////////////////////////////
65 // Enablers
66 ///////////////////////////////////////////////////////////////////////////
67
68 BOOST_SPIRIT_ENABLE_BINARY(byte_) // enables byte_
69 BOOST_SPIRIT_ENABLE_BINARY(word) // enables word
70 BOOST_SPIRIT_ENABLE_BINARY(big_word) // enables big_word
71 BOOST_SPIRIT_ENABLE_BINARY(little_word) // enables little_word
72 BOOST_SPIRIT_ENABLE_BINARY(dword) // enables dword
73 BOOST_SPIRIT_ENABLE_BINARY(big_dword) // enables big_dword
74 BOOST_SPIRIT_ENABLE_BINARY(little_dword) // enables little_dword
75 #ifdef BOOST_HAS_LONG_LONG
76 BOOST_SPIRIT_ENABLE_BINARY(qword) // enables qword
77 BOOST_SPIRIT_ENABLE_BINARY(big_qword) // enables big_qword
78 BOOST_SPIRIT_ENABLE_BINARY(little_qword) // enables little_qword
79 #endif
80 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(bin_float)
81 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(big_bin_float)
82 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(little_bin_float)
83 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(bin_double)
84 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(big_bin_double)
85 BOOST_SPIRIT_ENABLE_BINARY_IEEE754(little_bin_double)
86 }}
87
88 #undef BOOST_SPIRIT_ENABLE_BINARY
89 #undef BOOST_SPIRIT_ENABLE_BINARY_IEEE754
90
91 ///////////////////////////////////////////////////////////////////////////////
92 namespace boost { namespace spirit { namespace karma
93 {
94 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
95 using boost::spirit::byte_;
96 using boost::spirit::word;
97 using boost::spirit::big_word;
98 using boost::spirit::little_word;
99 using boost::spirit::dword;
100 using boost::spirit::big_dword;
101 using boost::spirit::little_dword;
102 #ifdef BOOST_HAS_LONG_LONG
103 using boost::spirit::qword;
104 using boost::spirit::big_qword;
105 using boost::spirit::little_qword;
106 #endif
107 using boost::spirit::bin_float;
108 using boost::spirit::big_bin_float;
109 using boost::spirit::little_bin_float;
110 using boost::spirit::bin_double;
111 using boost::spirit::big_bin_double;
112 using boost::spirit::little_bin_double;
113 #endif
114
115 using boost::spirit::byte_type;
116 using boost::spirit::word_type;
117 using boost::spirit::big_word_type;
118 using boost::spirit::little_word_type;
119 using boost::spirit::dword_type;
120 using boost::spirit::big_dword_type;
121 using boost::spirit::little_dword_type;
122 #ifdef BOOST_HAS_LONG_LONG
123 using boost::spirit::qword_type;
124 using boost::spirit::big_qword_type;
125 using boost::spirit::little_qword_type;
126 #endif
127 using boost::spirit::bin_float_type;
128 using boost::spirit::big_bin_float_type;
129 using boost::spirit::little_bin_float_type;
130 using boost::spirit::bin_double_type;
131 using boost::spirit::big_bin_double_type;
132 using boost::spirit::little_bin_double_type;
133
134 namespace detail
135 {
136 template <int bits>
137 struct integer
138 {
139 #ifdef BOOST_HAS_LONG_LONG
140 BOOST_SPIRIT_ASSERT_MSG(
141 bits == 8 || bits == 16 || bits == 32 || bits == 64,
142 not_supported_binary_size, ());
143 #else
144 BOOST_SPIRIT_ASSERT_MSG(
145 bits == 8 || bits == 16 || bits == 32,
146 not_supported_binary_size, ());
147 #endif
148 };
149
150 template <>
151 struct integer<8>
152 {
153 typedef uint_least8_t type;
154 };
155
156 template <>
157 struct integer<16>
158 {
159 typedef uint_least16_t type;
160 };
161
162 template <>
163 struct integer<32>
164 {
165 typedef uint_least32_t type;
166 };
167
168 #ifdef BOOST_HAS_LONG_LONG
169 template <>
170 struct integer<64>
171 {
172 typedef uint_least64_t type;
173 };
174 #endif
175
176 template <int bits>
177 struct floating_point
178 {
179 BOOST_SPIRIT_ASSERT_MSG(
180 bits == 32 || bits == 64,
181 not_supported_binary_size, ());
182 };
183
184 template <>
185 struct floating_point<32>
186 {
187 typedef float type;
188 };
189
190 template <>
191 struct floating_point<64>
192 {
193 typedef double type;
194 };
195
196 ///////////////////////////////////////////////////////////////////////
197 template <BOOST_SCOPED_ENUM(boost::endian::endianness) bits>
198 struct what;
199
200 template <>
201 struct what<boost::endian::endianness::native>
202 {
203 static info is()
204 {
205 return info("native-endian binary");
206 }
207 };
208
209 template <>
210 struct what<boost::endian::endianness::little>
211 {
212 static info is()
213 {
214 return info("little-endian binary");
215 }
216 };
217
218 template <>
219 struct what<boost::endian::endianness::big>
220 {
221 static info is()
222 {
223 return info("big-endian binary");
224 }
225 };
226 }
227
228 ///////////////////////////////////////////////////////////////////////////
229 template <typename T, BOOST_SCOPED_ENUM(boost::endian::endianness) endian, int bits>
230 struct any_binary_generator
231 : primitive_generator<any_binary_generator<T, endian, bits> >
232 {
233 template <typename Context, typename Unused = unused_type>
234 struct attribute: T {};
235
236 template <
237 typename OutputIterator, typename Context, typename Delimiter
238 , typename Attribute>
239 static bool generate(OutputIterator& sink, Context& context
240 , Delimiter const& d, Attribute const& attr)
241 {
242 if (!traits::has_optional_value(attr))
243 return false;
244
245 // Even if the endian types are not pod's (at least not in the
246 // definition of C++03) it seems to be safe to assume they are.
247 // This allows us to treat them as a sequence of consecutive bytes.
248 boost::endian::endian<endian, typename T::type, bits> p;
249
250 #if defined(BOOST_MSVC)
251 // warning C4244: 'argument' : conversion from 'const int' to 'foo', possible loss of data
252 #pragma warning(push)
253 #pragma warning(disable: 4244)
254 #endif
255 typedef typename T::type attribute_type;
256 p = traits::extract_from<attribute_type>(attr, context);
257 #if defined(BOOST_MSVC)
258 #pragma warning(pop)
259 #endif
260
261 unsigned char const* bytes =
262 reinterpret_cast<unsigned char const*>(&p);
263
264 for (unsigned int i = 0; i < sizeof(p); ++i)
265 {
266 if (!detail::generate_to(sink, *bytes++))
267 return false;
268 }
269 return karma::delimit_out(sink, d); // always do post-delimiting
270 }
271
272 // this any_byte_director has no parameter attached, it needs to have
273 // been initialized from a direct literal
274 template <
275 typename OutputIterator, typename Context, typename Delimiter>
276 static bool generate(OutputIterator&, Context&, Delimiter const&
277 , unused_type)
278 {
279 // It is not possible (doesn't make sense) to use binary generators
280 // without providing any attribute, as the generator doesn't 'know'
281 // what to output. The following assertion fires if this situation
282 // is detected in your code.
283 BOOST_SPIRIT_ASSERT_FAIL(OutputIterator,
284 binary_generator_not_usable_without_attribute, ());
285 return false;
286 }
287
288 template <typename Context>
289 static info what(Context const& /*context*/)
290 {
291 return karma::detail::what<endian>::is();
292 }
293 };
294
295 ///////////////////////////////////////////////////////////////////////////
296 template <typename T, BOOST_SCOPED_ENUM(boost::endian::endianness) endian, int bits>
297 struct literal_binary_generator
298 : primitive_generator<literal_binary_generator<T, endian, bits> >
299 {
300 template <typename Context, typename Unused>
301 struct attribute
302 {
303 typedef unused_type type;
304 };
305
306 template <typename V>
307 literal_binary_generator(V const& v)
308 {
309 #if defined(BOOST_MSVC)
310 // warning C4244: 'argument' : conversion from 'const int' to 'foo', possible loss of data
311 #pragma warning(push)
312 #pragma warning(disable: 4244)
313 #endif
314 data_ = v;
315 #if defined(BOOST_MSVC)
316 #pragma warning(pop)
317 #endif
318 }
319
320 template <
321 typename OutputIterator, typename Context, typename Delimiter
322 , typename Attribute>
323 bool generate(OutputIterator& sink, Context&, Delimiter const& d
324 , Attribute const&) const
325 {
326 // Even if the endian types are not pod's (at least not in the
327 // definition of C++03) it seems to be safe to assume they are
328 // (but in C++0x the endian types _are_ PODs).
329 // This allows us to treat them as a sequence of consecutive bytes.
330 unsigned char const* bytes =
331 reinterpret_cast<unsigned char const*>(&data_);
332
333 for (unsigned int i = 0; i < sizeof(data_type); ++i)
334 {
335 if (!detail::generate_to(sink, *bytes++))
336 return false;
337 }
338 return karma::delimit_out(sink, d); // always do post-delimiting
339 }
340
341 template <typename Context>
342 static info what(Context const& /*context*/)
343 {
344 return karma::detail::what<endian>::is();
345 }
346
347 typedef boost::endian::endian<endian, typename T::type,
348 bits> data_type;
349
350 data_type data_;
351 };
352
353 ///////////////////////////////////////////////////////////////////////////
354 // Generator generators: make_xxx function (objects)
355 ///////////////////////////////////////////////////////////////////////////
356 namespace detail
357 {
358 template <typename T, BOOST_SCOPED_ENUM(boost::endian::endianness) endian
359 , int bits>
360 struct basic_binary
361 {
362 typedef any_binary_generator<T, endian, bits> result_type;
363
364 result_type operator()(unused_type, unused_type) const
365 {
366 return result_type();
367 }
368 };
369
370 template <typename Modifiers, typename T
371 , BOOST_SCOPED_ENUM(boost::endian::endianness) endian, int bits>
372 struct basic_binary_literal
373 {
374 typedef literal_binary_generator<T, endian, bits> result_type;
375
376 template <typename Terminal>
377 result_type operator()(Terminal const& term, unused_type) const
378 {
379 return result_type(fusion::at_c<0>(term.args));
380 }
381 };
382 }
383
384 #define BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(name, endiantype, bits) \
385 template <typename Modifiers> \
386 struct make_primitive<tag::name, Modifiers> \
387 : detail::basic_binary<detail::integer<bits>, \
388 boost::endian::endianness::endiantype, bits> {}; \
389 \
390 template <typename Modifiers, typename A0> \
391 struct make_primitive<terminal_ex<tag::name, fusion::vector1<A0> > \
392 , Modifiers> \
393 : detail::basic_binary_literal<Modifiers, detail::integer<bits> \
394 , boost::endian::endianness::endiantype, bits> {}; \
395 \
396 /***/
397
398 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(byte_, native, 8)
399 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(word, native, 16)
400 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_word, big, 16)
401 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_word, little, 16)
402 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(dword, native, 32)
403 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_dword, big, 32)
404 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_dword, little, 32)
405 #ifdef BOOST_HAS_LONG_LONG
406 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(qword, native, 64)
407 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_qword, big, 64)
408 BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_qword, little, 64)
409 #endif
410
411 #undef BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE
412
413 #define BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(name, endiantype, bits) \
414 template <typename Modifiers> \
415 struct make_primitive<tag::name, Modifiers> \
416 : detail::basic_binary<detail::floating_point<bits>, \
417 boost::endian::endianness::endiantype, bits> {}; \
418 \
419 template <typename Modifiers, typename A0> \
420 struct make_primitive<terminal_ex<tag::name, fusion::vector1<A0> > \
421 , Modifiers> \
422 : detail::basic_binary_literal<Modifiers, detail::floating_point<bits> \
423 , boost::endian::endianness::endiantype, bits> {}; \
424 \
425 /***/
426
427 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(bin_float, native, 32)
428 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(big_bin_float, big, 32)
429 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(little_bin_float, little, 32)
430 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(bin_double, native, 64)
431 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(big_bin_double, big, 64)
432 BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE(little_bin_double, little, 64)
433
434 #undef BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE
435
436 }}}
437
438 #endif