1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #if !defined(BOOST_SPIRIT_KARMA_BINARY_MAY_04_2007_0904AM)
7 #define BOOST_SPIRIT_KARMA_BINARY_MAY_04_2007_0904AM
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>
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>
33 ///////////////////////////////////////////////////////////////////////////////
34 #define BOOST_SPIRIT_ENABLE_BINARY(name) \
36 struct use_terminal<karma::domain, tag::name> \
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> > {}; \
45 struct use_lazy_terminal<karma::domain, tag::name, 1> : mpl::true_ {}; \
49 #define BOOST_SPIRIT_ENABLE_BINARY_IEEE754(name) \
51 struct use_terminal<karma::domain, tag::name>: mpl::true_ {}; \
53 template<typename A0> \
54 struct use_terminal<karma::domain, terminal_ex<tag::name, \
55 fusion::vector1<A0> > >: is_floating_point<A0> {}; \
58 struct use_lazy_terminal<karma::domain, tag::name, 1> : mpl::true_ {}; \
62 namespace boost { namespace spirit
64 ///////////////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////////////
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
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)
88 #undef BOOST_SPIRIT_ENABLE_BINARY
89 #undef BOOST_SPIRIT_ENABLE_BINARY_IEEE754
91 ///////////////////////////////////////////////////////////////////////////////
92 namespace boost { namespace spirit { namespace karma
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;
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;
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;
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;
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, ());
144 BOOST_SPIRIT_ASSERT_MSG(
145 bits == 8 || bits == 16 || bits == 32,
146 not_supported_binary_size, ());
153 typedef uint_least8_t type;
159 typedef uint_least16_t type;
165 typedef uint_least32_t type;
168 #ifdef BOOST_HAS_LONG_LONG
172 typedef uint_least64_t type;
177 struct floating_point
179 BOOST_SPIRIT_ASSERT_MSG(
180 bits == 32 || bits == 64,
181 not_supported_binary_size, ());
185 struct floating_point<32>
191 struct floating_point<64>
196 ///////////////////////////////////////////////////////////////////////
197 template <BOOST_SCOPED_ENUM(boost::endian::endianness) bits>
201 struct what<boost::endian::endianness::native>
205 return info("native-endian binary");
210 struct what<boost::endian::endianness::little>
214 return info("little-endian binary");
219 struct what<boost::endian::endianness::big>
223 return info("big-endian binary");
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> >
233 template <typename Context, typename Unused = unused_type>
234 struct attribute: T {};
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)
242 if (!traits::has_optional_value(attr))
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;
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)
255 typedef typename T::type attribute_type;
256 p = traits::extract_from<attribute_type>(attr, context);
257 #if defined(BOOST_MSVC)
261 unsigned char const* bytes =
262 reinterpret_cast<unsigned char const*>(&p);
264 for (unsigned int i = 0; i < sizeof(p); ++i)
266 if (!detail::generate_to(sink, *bytes++))
269 return karma::delimit_out(sink, d); // always do post-delimiting
272 // this any_byte_director has no parameter attached, it needs to have
273 // been initialized from a direct literal
275 typename OutputIterator, typename Context, typename Delimiter>
276 static bool generate(OutputIterator&, Context&, Delimiter const&
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, ());
288 template <typename Context>
289 static info what(Context const& /*context*/)
291 return karma::detail::what<endian>::is();
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> >
300 template <typename Context, typename Unused>
303 typedef unused_type type;
306 template <typename V>
307 literal_binary_generator(V const& v)
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)
315 #if defined(BOOST_MSVC)
321 typename OutputIterator, typename Context, typename Delimiter
322 , typename Attribute>
323 bool generate(OutputIterator& sink, Context&, Delimiter const& d
324 , Attribute const&) const
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_);
333 for (unsigned int i = 0; i < sizeof(data_type); ++i)
335 if (!detail::generate_to(sink, *bytes++))
338 return karma::delimit_out(sink, d); // always do post-delimiting
341 template <typename Context>
342 static info what(Context const& /*context*/)
344 return karma::detail::what<endian>::is();
347 typedef boost::endian::endian<endian, typename T::type,
353 ///////////////////////////////////////////////////////////////////////////
354 // Generator generators: make_xxx function (objects)
355 ///////////////////////////////////////////////////////////////////////////
358 template <typename T, BOOST_SCOPED_ENUM(boost::endian::endianness) endian
362 typedef any_binary_generator<T, endian, bits> result_type;
364 result_type operator()(unused_type, unused_type) const
366 return result_type();
370 template <typename Modifiers, typename T
371 , BOOST_SCOPED_ENUM(boost::endian::endianness) endian, int bits>
372 struct basic_binary_literal
374 typedef literal_binary_generator<T, endian, bits> result_type;
376 template <typename Terminal>
377 result_type operator()(Terminal const& term, unused_type) const
379 return result_type(fusion::at_c<0>(term.args));
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> {}; \
390 template <typename Modifiers, typename A0> \
391 struct make_primitive<terminal_ex<tag::name, fusion::vector1<A0> > \
393 : detail::basic_binary_literal<Modifiers, detail::integer<bits> \
394 , boost::endian::endianness::endiantype, bits> {}; \
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)
411 #undef BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE
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> {}; \
419 template <typename Modifiers, typename A0> \
420 struct make_primitive<terminal_ex<tag::name, fusion::vector1<A0> > \
422 : detail::basic_binary_literal<Modifiers, detail::floating_point<bits> \
423 , boost::endian::endianness::endiantype, bits> {}; \
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)
434 #undef BOOST_SPIRIT_MAKE_BINARY_IEEE754_PRIMITIVE