Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Terry Golubiewski (tjgolubi_at_[hidden])
Date: 2010-05-28 23:55:05


----- Original Message -----
From: "vicente.botet" <vicente.botet_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.devel
To: <boost_at_[hidden]>
Sent: Friday, May 28, 2010 5:21 PM
Subject: Re: [boost::endian] Request for comments/interest
>> int main() {
>> struct MyClass { char data[12]; };
>>
>> endian<little, MyClass> lil_1;
>> endian<little, MyClass> lil_2;
>> endian<big, MyClass> big_1;
>>
>> lil_1 = lil_2;
>> big_1 = lil_1;
>> lil_2 = big_1;
>>
>> } // main
>
> Maybe, but what about more heterogeneus structures?

Ok, here's an actual example that compiles on VC9.
(Notice how I worked in "chrono")

terry

#include "endian.hpp"

#include <boost/units/systems/si.hpp>
#include <boost/cstdint.hpp>

#include <std0x/chrono.h>

using namespace boost;
using namespace boost::interface;
using namespace boost::units;
using namespace std0x;
using namespace std0x::chrono;

template<endian_t, int w1, int w2=0, int w3=0, int w4=0, int w5=0>
struct bitfield {
  char placeholder[(w1 + w2 + w3 + w4 + w5 + 7)/8];
}; // bitfield

namespace internet {

#pragma pack(push ,1)

struct IpHeader {
  bitfield<big, 4, 4> version_headerLength;
  enum { version, headerLength };
  bitfield<big, 3, 1, 1, 1, 1> differentiated_services;
  enum { precedence, low_delay, high_thruput, high_reliability,
minimize_cost };
  endian<big, uint16_t> total_length;
  endian<big, uint16_t> identification;
  bitfield<big, 1, 1, 1, 13> flags_frag;
  enum { reserved, dont_frag, more_frag, frag_offset };
  endian<big, uint8_t> time_to_live;
  endian<big, uint8_t> protocol;
  endian<big, uint16_t> header_checksum;
  endian<big, uint32_t> source_address;
  endian<big, uint32_t> destination_address;
}; // IpHeader

struct UdpHeader {
  endian<big, uint16_t> source_port;
  endian<big, uint16_t> destination_port;
  endian<big, uint16_t> length;
  endian<big, uint16_t> checksum;
}; // UdpHeader

#pragma pack(pop)

} // internet

#pragma pack(push, 1)

struct UserMessage {
  endian<little, time_point<system_clock, duration<int64_t, nano> > >
timestamp;
  endian<little, uint32_t> aircraft_id;
  struct Position {
     endian<little, quantity<si::length, int32_t> > x;
     endian<little, quantity<si::length, int32_t> > y;
     endian<little, quantity<si::length, int32_t> > z;
  } position;
  struct Attitude {
      endian<little, quantity<si::plane_angle, int8_t> > heading;
      endian<little, quantity<si::plane_angle, int8_t> > pitch;
      endian<little, quantity<si::plane_angle, int8_t> > roll;
  } attitude;
}; // UserMessage

struct Packet {
  internet::IpHeader ipHeader;
  internet::UdpHeader udpHeader;
  UserMessage userMessage;
}; // Packet

#pragma pack(pop)

int main() {
  Packet packet;
  packet.ipHeader.source_address = 0x12345678;
  packet.udpHeader.destination_port = 1234;
  packet.userMessage.timestamp = system_clock::now();
  packet.userMessage.position.y = 17 * si::meter;
} // main


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk