Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62917 - in sandbox/endian_ext/libs/integer: example test
From: vicente.botet_at_[hidden]
Date: 2010-06-13 13:06:53


Author: viboes
Date: 2010-06-13 13:06:52 EDT (Sun, 13 Jun 2010)
New Revision: 62917
URL: http://svn.boost.org/trac/boost/changeset/62917

Log:
Added packet example using endian on user defined types as time_point or quantity
Added:
   sandbox/endian_ext/libs/integer/example/packet_example.cpp (contents, props changed)
Text files modified:
   sandbox/endian_ext/libs/integer/test/Jamfile.v2 | 14 +++++++++++++-
   1 files changed, 13 insertions(+), 1 deletions(-)

Added: sandbox/endian_ext/libs/integer/example/packet_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/endian_ext/libs/integer/example/packet_example.cpp 2010-06-13 13:06:52 EDT (Sun, 13 Jun 2010)
@@ -0,0 +1,155 @@
+// packet_example.cpp -------------------------------------------------------//
+
+// (C) Copyright VicenteJ Botet Escriba 2010
+
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+
+// See library home page at http://www.boost.org/libs/endian
+
+//----------------------------------------------------------------------------//
+
+//~ #define _CRT_SECURE_NO_DEPRECATE // quiet VC++ 8.0 foolishness
+
+//~ #include <iostream>
+//~ #include <cassert>
+//~ #include <cstdio>
+#include <boost/integer/endian_pack.hpp>
+#include <boost/chrono/chrono.hpp>
+#include <boost/units/quantity.hpp>
+#include <boost/units/systems/si/length.hpp>
+#include <boost/units/systems/si/plane_angle.hpp>
+
+using namespace boost::integer;
+using namespace boost::chrono;
+using namespace boost::endianness;
+using namespace boost::units;
+
+namespace bitfield {
+template <typename T, typename Tag, std::size_t Width>
+struct member {
+ static const std::size_t width=Width;
+};
+
+template <std::size_t Width>
+struct filler {
+ static const std::size_t width=Width;
+};
+
+template <typename T>
+struct width {
+ static const std::size_t value=T::width;
+};
+
+
+template<typename Storage,
+ typename T1,
+ typename T2=filler<0>,
+ typename T3=filler<0>,
+ typename T4=filler<0>,
+ typename T5=filler<0>
+>
+struct tuple {
+ char placeholder[
+ (
+ width<T1>::value +
+ width<T2>::value +
+ width<T3>::value +
+ width<T4>::value +
+ width<T5>::value +
+ 7
+ )/8];
+}; // bitfield
+
+}
+
+namespace internet {
+
+
+struct IpHeader {
+ struct version{};
+ struct header_length{};
+ struct precedence{};
+ struct low_delay{};
+ struct high_thruput{};
+ struct high_reliability{};
+ struct minimize_cost{};
+ struct dont_frag{};
+ struct more_frag{};
+ struct frag_offset{};
+
+ /////////////////////////////
+ bitfield::tuple<ubig8_pt,
+ bitfield::member<int, version, 4>,
+ bitfield::member<unsigned int, header_length, 4>
+ > version_headerLength;
+
+ bitfield::tuple<ubig8_pt,
+ bitfield::member<unsigned int, precedence, 3>,
+ bitfield::member<bool, low_delay, 1>,
+ bitfield::member<bool, high_thruput, 1>,
+ bitfield::member<bool, high_reliability, 1>,
+ bitfield::member<bool, minimize_cost, 1>
+ > differentiated_services;
+
+ ubig16_pt total_length;
+ /////////////////////////////
+ ubig16_pt identification;
+ bitfield::tuple<ubig16_pt,
+ bitfield::filler<1>,
+ bitfield::member<bool, dont_frag, 1>,
+ bitfield::member<bool, more_frag, 1>,
+ bitfield::member<int, frag_offset, 13>
+ > flags_frag;
+ /////////////////////////////
+ ubig8_pt time_to_live;
+ ubig8_pt protocol;
+ ubig16_pt header_checksum;
+ /////////////////////////////
+ ubig32_pt source_address;
+ /////////////////////////////
+ ubig32_pt destination_address;
+}; // IpHeader
+
+struct UdpHeader {
+ ubig16_pt source_port;
+ ubig16_pt destination_port;
+ ubig16_pt length;
+ ubig16_pt checksum;
+}; // UdpHeader
+
+
+} // internet
+
+
+struct UserMessage {
+ endian_pack<little, time_point<system_clock, duration<int64_t, nanoseconds> > >
+timestamp;
+ ulittle32_pt aircraft_id;
+ struct Position {
+ endian_pack<little, quantity<si::length, int> > x;
+ endian_pack<little, quantity<si::length, int> > y;
+ endian_pack<little, quantity<si::length, int> > z;
+ } position;
+ struct Attitude {
+ endian_pack<little, quantity<si::plane_angle, int8_t> > heading;
+ endian_pack<little, quantity<si::plane_angle, int8_t> > pitch;
+ endian_pack<little, quantity<si::plane_angle, int8_t> > roll;
+ } attitude;
+}; // UserMessage
+
+struct Packet {
+ internet::IpHeader ipHeader;
+ internet::UdpHeader udpHeader;
+ UserMessage userMessage;
+}; // Packet
+
+
+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
+

Modified: sandbox/endian_ext/libs/integer/test/Jamfile.v2
==============================================================================
--- sandbox/endian_ext/libs/integer/test/Jamfile.v2 (original)
+++ sandbox/endian_ext/libs/integer/test/Jamfile.v2 2010-06-13 13:06:52 EDT (Sun, 13 Jun 2010)
@@ -8,7 +8,12 @@
 # See library home page at http://www.boost.org/libs/integer/doc/endian.html
 
    import testing ;
-
+
+project
+ : requirements
+ <library>/boost/system//boost_system
+ ;
+
    test-suite "endian"
        :
          [ run binary_stream_test.cpp ]
@@ -24,3 +29,10 @@
          [ run endian_convert_test.cpp ]
          [ run endian_convert_unique_endian_test.cpp ]
       ;
+
+ test-suite "examples"
+ :
+ [ run ../example/endian_example.cpp ]
+ [ run ../example/endian_hello_world.cpp ]
+ [ run ../example/packet_example.cpp ]
+ ;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk