Imported existing code
This commit is contained in:
194
libraries/include/boost/spirit/home/qi/binary/binary.hpp
Normal file
194
libraries/include/boost/spirit/home/qi/binary/binary.hpp
Normal file
@@ -0,0 +1,194 @@
|
||||
// Copyright (c) 2001-2009 Hartmut Kaiser
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#if !defined(BOOST_SPIRIT_BINARY_MAY_08_2007_0808AM)
|
||||
#define BOOST_SPIRIT_BINARY_MAY_08_2007_0808AM
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
||||
#pragma once // MS compatible compilers support #pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/spirit/home/support/component.hpp>
|
||||
#include <boost/spirit/home/support/detail/integer/endian.hpp>
|
||||
#include <boost/spirit/home/support/attribute_of.hpp>
|
||||
#include <boost/spirit/home/qi/domain.hpp>
|
||||
#include <boost/spirit/home/qi/detail/assign_to.hpp>
|
||||
#include <boost/spirit/home/qi/skip.hpp>
|
||||
|
||||
namespace boost { namespace spirit { namespace qi
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <int bits>
|
||||
struct integer
|
||||
{
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
BOOST_MPL_ASSERT_MSG(
|
||||
bits == 8 || bits == 16 || bits == 32 || bits == 64,
|
||||
not_supported_binary_size, ());
|
||||
#else
|
||||
BOOST_MPL_ASSERT_MSG(
|
||||
bits == 8 || bits == 16 || bits == 32,
|
||||
not_supported_binary_size, ());
|
||||
#endif
|
||||
};
|
||||
|
||||
template <>
|
||||
struct integer<8>
|
||||
{
|
||||
typedef uint_least8_t type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct integer<16>
|
||||
{
|
||||
typedef uint_least16_t type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct integer<32>
|
||||
{
|
||||
typedef uint_least32_t type;
|
||||
};
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
template <>
|
||||
struct integer<64>
|
||||
{
|
||||
typedef uint_least64_t type;
|
||||
};
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
template <boost::integer::endianness bits>
|
||||
struct what;
|
||||
|
||||
template <>
|
||||
struct what<boost::integer::native>
|
||||
{
|
||||
static std::string is()
|
||||
{
|
||||
return "native-endian binary";
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct what<boost::integer::little>
|
||||
{
|
||||
static char const* is()
|
||||
{
|
||||
return "little-endian binary";
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct what<boost::integer::big>
|
||||
{
|
||||
static char const* is()
|
||||
{
|
||||
return "big-endian binary";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <integer::endianness endian, int bits>
|
||||
struct any_binary_director
|
||||
{
|
||||
template <typename Component, typename Context, typename Iterator>
|
||||
struct attribute
|
||||
{
|
||||
typedef boost::integer::endian<
|
||||
endian, typename qi::detail::integer<bits>::type, bits
|
||||
> type;
|
||||
};
|
||||
|
||||
template <
|
||||
typename Component
|
||||
, typename Iterator, typename Context
|
||||
, typename Skipper, typename Attribute>
|
||||
static bool parse(
|
||||
Component const&
|
||||
, Iterator& first, Iterator const& last
|
||||
, Context&, Skipper const& skipper
|
||||
, Attribute& attr)
|
||||
{
|
||||
qi::skip(first, last, skipper);
|
||||
|
||||
typename
|
||||
traits::attribute_of<
|
||||
qi::domain, Component, Context, Iterator>::type
|
||||
attr_;
|
||||
unsigned char* bytes = reinterpret_cast<unsigned char*>(&attr_);
|
||||
|
||||
Iterator it = first;
|
||||
for (unsigned int i = 0; i < sizeof(attr_); ++i)
|
||||
{
|
||||
if (it == last)
|
||||
return false;
|
||||
*bytes++ = *it++;
|
||||
}
|
||||
|
||||
first = it;
|
||||
detail::assign_to(attr_, attr);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Component, typename Context>
|
||||
static std::string what(Component const& component, Context const& ctx)
|
||||
{
|
||||
return qi::detail::what<endian>::is();
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <integer::endianness endian, int bits>
|
||||
struct binary_lit_director
|
||||
{
|
||||
template <typename Component, typename Context, typename Iterator>
|
||||
struct attribute
|
||||
{
|
||||
typedef unused_type type;
|
||||
};
|
||||
|
||||
template <
|
||||
typename Component
|
||||
, typename Iterator, typename Context
|
||||
, typename Skipper, typename Attribute>
|
||||
static bool parse(
|
||||
Component const& component
|
||||
, Iterator& first, Iterator const& last
|
||||
, Context&, Skipper const& skipper
|
||||
, Attribute& attr)
|
||||
{
|
||||
qi::skip(first, last, skipper);
|
||||
|
||||
boost::integer::endian<
|
||||
endian, typename qi::detail::integer<bits>::type, bits
|
||||
> attr_ (fusion::at_c<0>(component.elements));
|
||||
unsigned char* bytes = reinterpret_cast<unsigned char*>(&attr_);
|
||||
|
||||
Iterator it = first;
|
||||
for (unsigned int i = 0; i < sizeof(attr_); ++i)
|
||||
{
|
||||
if (it == last || *bytes++ != *it++)
|
||||
return false;
|
||||
}
|
||||
|
||||
first = it;
|
||||
detail::assign_to(attr_, attr);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Component, typename Context>
|
||||
static std::string what(Component const& component, Context const& ctx)
|
||||
{
|
||||
return qi::detail::what<endian>::is();
|
||||
}
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif
|
||||
259
libraries/include/boost/spirit/home/qi/binary/meta_grammar.hpp
Normal file
259
libraries/include/boost/spirit/home/qi/binary/meta_grammar.hpp
Normal file
@@ -0,0 +1,259 @@
|
||||
// Copyright (c) 2001-2009 Hartmut Kaiser
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#if !defined(BOOST_SPIRIT_META_GRAMMAR_MAY_08_2007_0824AM)
|
||||
#define BOOST_SPIRIT_META_GRAMMAR_MAY_08_2007_0824AM
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
||||
#pragma once // MS compatible compilers support #pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/spirit/home/qi/domain.hpp>
|
||||
#include <boost/spirit/home/support/placeholders.hpp>
|
||||
#include <boost/spirit/home/support/meta_grammar.hpp>
|
||||
#include <boost/spirit/home/support/detail/integer/endian.hpp>
|
||||
#include <boost/mpl/identity.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
|
||||
namespace boost { namespace spirit { namespace qi
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// forwards
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <integer::endianness endian, int bits>
|
||||
struct any_binary_director;
|
||||
|
||||
template <integer::endianness endian, int bits>
|
||||
struct binary_lit_director;
|
||||
|
||||
struct main_meta_grammar;
|
||||
|
||||
template <typename Expr, typename Enable>
|
||||
struct is_valid_expr;
|
||||
|
||||
template <typename Expr, typename Enable>
|
||||
struct expr_transform;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// get the director of an integer based binary literal type
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
struct extract_literal_bin_director
|
||||
{
|
||||
typedef binary_lit_director<
|
||||
boost::integer::native, sizeof(T)*CHAR_BIT
|
||||
> type;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// get the director of a binary tag
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename Tag>
|
||||
struct extract_binary_director;
|
||||
|
||||
// native endian binaries
|
||||
template <>
|
||||
struct extract_binary_director<tag::byte>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::native, 8> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::word>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::native, 16> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::dword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::native, 32> type;
|
||||
};
|
||||
|
||||
// big endian binaries
|
||||
template <>
|
||||
struct extract_binary_director<tag::big_word>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::big, 16> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::big_dword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::big, 32> type;
|
||||
};
|
||||
|
||||
// little endian binaries
|
||||
template <>
|
||||
struct extract_binary_director<tag::little_word>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::little, 16> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::little_dword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::little, 32> type;
|
||||
};
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
template <>
|
||||
struct extract_binary_director<tag::qword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::native, 64> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::big_qword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::big, 64> type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct extract_binary_director<tag::little_qword>
|
||||
{
|
||||
typedef any_binary_director<boost::integer::little, 64> type;
|
||||
};
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// get the director of a binary literal tag
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename Tag, typename T>
|
||||
struct extract_binary_lit_director;
|
||||
|
||||
// native endian binaries
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::byte, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::native, 8> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::word, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::native, 16> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::dword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::native, 32> type;
|
||||
};
|
||||
|
||||
// big endian binaries
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::big_word, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::big, 16> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::big_dword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::big, 32> type;
|
||||
};
|
||||
|
||||
// little endian binaries
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::little_word, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::little, 16> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::little_dword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::little, 32> type;
|
||||
};
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::qword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::native, 64> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::big_qword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::big, 64> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct extract_binary_lit_director<tag::little_qword, T>
|
||||
{
|
||||
typedef binary_lit_director<boost::integer::little, 64> type;
|
||||
};
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// binary meta-grammar
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// literals: 10, 10L, 10LL
|
||||
struct int_binary_meta_grammar
|
||||
: meta_grammar::compose_empty<
|
||||
proto::if_<
|
||||
is_int_lit_tag<proto::_child, qi::domain>()
|
||||
>,
|
||||
qi::domain,
|
||||
mpl::identity<extract_literal_bin_director<mpl::_> >
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
struct binary_meta_grammar
|
||||
: proto::or_<
|
||||
meta_grammar::compose_empty<
|
||||
proto::if_<
|
||||
is_binary_tag<proto::_child, qi::domain>()
|
||||
>,
|
||||
qi::domain,
|
||||
mpl::identity<extract_binary_director<mpl::_> >
|
||||
>,
|
||||
meta_grammar::compose_function1_eval<
|
||||
proto::function<
|
||||
proto::if_<
|
||||
is_binary_tag<proto::_child, qi::domain>()
|
||||
>,
|
||||
int_binary_meta_grammar
|
||||
>,
|
||||
qi::domain,
|
||||
mpl::identity<extract_binary_lit_director<mpl::_, mpl::_> >
|
||||
>
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// These specializations non-intrusively hooks into the Qi meta-grammar.
|
||||
// (see qi/meta_grammar.hpp)
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename Expr>
|
||||
struct is_valid_expr<
|
||||
Expr,
|
||||
typename enable_if<
|
||||
proto::matches<Expr, binary_meta_grammar>
|
||||
>::type
|
||||
>
|
||||
: mpl::true_
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Expr>
|
||||
struct expr_transform<
|
||||
Expr,
|
||||
typename enable_if<
|
||||
proto::matches<Expr, binary_meta_grammar>
|
||||
>::type
|
||||
>
|
||||
: mpl::identity<binary_meta_grammar>
|
||||
{
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user