[ create a new paste ] login | about

Link: http://codepad.org/5y3RPj9r    [ raw code | fork ]

C++, pasted on Apr 27:
#include "raw_cast.hpp"

#include <boost/cstdint.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/range/algorithm.hpp>

#include <limits>
#include <iostream>
#include <sstream>


template<typename Iterator>
  boost::iterator_range<Iterator> range(Iterator begin, Iterator end)
{
  return boost::make_iterator_range(begin, end);
}


std::string bytes_ords(std::string const& s)
{
  std::stringstream out;
  out << std::hex;
  for (std::size_t i = 0, size = s.size(); i < size; ++i)
    out << (static_cast<unsigned>(s[i]) & 0x0ff) << ' ';
  return out.str();
}


void eliminated_copying_interface_test()
{
  struct header
  {
    double          f1;
    boost::int32_t  f2;
    char            f3;
    boost::uint32_t f4;
    char            f5;
    boost::uint16_t f6;
  } head_out = {1.0, 127, 'X', std::numeric_limits<boost::uint32_t>::max(), 'q', 5};

  std::string s(20, '\0');
  typedef boost::iterator_range<std::string::const_iterator>  range_t;
  boost::copy(endian::little::raw_cast<range_t>(head_out.f1), s.begin());
  boost::copy(endian::little::raw_cast<range_t>(head_out.f2), s.begin() + 8);
  boost::copy(endian::little::raw_cast<range_t>(head_out.f3), s.begin() + 12);
  boost::copy(endian::little::raw_cast<range_t>(head_out.f4), s.begin() + 13);
  boost::copy(endian::little::raw_cast<range_t>(head_out.f5), s.begin() + 17);
  boost::copy(endian::little::raw_cast<range_t>(head_out.f6), s.begin() + 18);
  std::cout << "raw: " << bytes_ords(s) << std::endl;

  header head_in;
  head_in.f1 = endian::little::raw_cast<double>         (range(s.begin(),      s.begin() +  8));
  head_in.f2 = endian::little::raw_cast<boost::int32_t> (range(s.begin() +  8, s.begin() + 12));
  head_in.f3 = endian::little::raw_cast<char>           (range(s.begin() + 12, s.begin() + 13));
  head_in.f4 = endian::little::raw_cast<boost::uint32_t>(range(s.begin() + 13, s.begin() + 17));
  head_in.f5 = endian::little::raw_cast<char>           (range(s.begin() + 17, s.begin() + 18));
  head_in.f6 = endian::little::raw_cast<boost::uint16_t>(range(s.begin() + 18, s.begin() + 20));

  std::cout << "native: {" << head_in.f1 << " : "
                           << head_in.f2 << " : "
                           << head_in.f3 << " : " << std::showbase << std::hex
                           << head_in.f4 << " : "
                           << head_in.f5 << " : " << std::dec
                           << head_in.f6 << '}' << std::endl;
}


int main()
{
  std::string  little_endian_0x04030201 = "\x01\x02\x03\x04";
  boost::int32_t  n = endian::little::raw_cast<boost::int32_t>(little_endian_0x04030201);
  std::cout << n << " (raw: "
            << bytes_ords(endian::little::raw_cast<std::string>(n)) // out: 1 2 3 4
            << "[little endian]; "
            << bytes_ords(endian::big::raw_cast<std::string>(n))    // out: 4 3 2 1
            << "[big endian])\n" << std::endl;

  eliminated_copying_interface_test();

  return 0;
}


Create a new paste based on this one


Comments: