annotate DEPENDENCIES/generic/include/boost/spirit/home/karma/binary/binary.hpp @ 133:4acb5d8d80b6 tip

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