Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

IntParser.hh

Go to the documentation of this file.
00001 // $Id: IntParser.hh 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef HH_SENF_Packets_IntParser_
00027 #define HH_SENF_Packets_IntParser_ 1
00028 
00029 // Custom includes
00030 #include <iostream>
00031 #include <boost/cstdint.hpp>
00032 #include <boost/static_assert.hpp>
00033 #include <boost/integer/integer_mask.hpp>
00034 #include "PacketParser.hh"
00035 
00036 //#include "IntParser.mpp"
00037 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00038 #include "IntParser.ih"
00039 
00040 namespace senf {
00041 
00073     struct Int8Parser
00074         : public detail::packet::IntParserOps<Int8Parser,boost::int8_t>,
00075           public PacketParserBase
00076     {
00077         Int8Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00078 
00079         //-////////////////////////////////////////////////////////////////////////
00080 
00081         typedef boost::int8_t value_type;
00082         static size_type const fixed_bytes = 1;
00083         static value_type const min_value = -128;
00084         static value_type const max_value = 127;
00085 
00086         value_type value() const { return i()[0]; }
00087         void value(value_type v) { i()[0] = v; }
00088         Int8Parser const & operator= (value_type other) { value(other); return *this; }
00089     };
00093     inline std::ostream & operator<<(std::ostream & os, Int8Parser const & i)
00094     { os << i.value(); return os; }
00095 
00100     struct UInt8Parser
00101         : public detail::packet::IntParserOps<UInt8Parser,boost::uint8_t>,
00102           public PacketParserBase
00103     {
00104         UInt8Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00105 
00106         //-////////////////////////////////////////////////////////////////////////
00107 
00108         typedef boost::uint8_t value_type;
00109         static size_type const fixed_bytes = 1;
00110         static value_type const min_value = 0u;
00111         static value_type const max_value = 255u;
00112 
00113         value_type value() const { return i()[0]; }
00114         void value(value_type v) { i()[0] = v; }
00115         UInt8Parser const & operator= (value_type other) { value(other); return *this; }
00116     };
00120     inline std::ostream & operator<<(std::ostream & os, UInt8Parser const & i)
00121     { os << i.value(); return os; }
00122 
00127     struct Int16Parser
00128         : public detail::packet::IntParserOps<Int16Parser,boost::int16_t>,
00129           public PacketParserBase
00130     {
00131         Int16Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00132 
00133         //-////////////////////////////////////////////////////////////////////////
00134 
00135         typedef boost::int16_t value_type;
00136         static size_type const fixed_bytes = 2;
00137         static value_type const min_value = -32768;
00138         static value_type const max_value = 32767;
00139 
00140 
00141         value_type value() const { return detail::packet::parse_uint16(i()); }
00142         void value(value_type v) { detail::packet::write_uint16(i(),v); }
00143         Int16Parser const & operator= (value_type other) { value(other); return *this; }
00144     };
00148     inline std::ostream & operator<<(std::ostream & os, Int16Parser const & i)
00149     { os << i.value(); return os; }
00150 
00155     struct Int16LSBParser
00156         : public detail::packet::IntParserOps<Int16LSBParser,boost::int16_t>,
00157           public PacketParserBase
00158     {
00159         Int16LSBParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00160 
00161         //-////////////////////////////////////////////////////////////////////////
00162 
00163         typedef boost::int16_t value_type;
00164         static size_type const fixed_bytes = 2;
00165         static value_type const min_value = -32768;
00166         static value_type const max_value = 32767;
00167 
00168 
00169         value_type value() const { return detail::packet::parse_uint16LSB(i()); }
00170         void value(value_type v) { detail::packet::write_uint16LSB(i(),v); }
00171         Int16LSBParser const & operator= (value_type other) { value(other); return *this; }
00172     };
00176     inline std::ostream & operator<<(std::ostream & os, Int16LSBParser const & i)
00177     { os << i.value(); return os; }
00178 
00183     struct UInt16Parser
00184         : public detail::packet::IntParserOps<UInt16Parser,boost::uint16_t>,
00185           public PacketParserBase
00186     {
00187         UInt16Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00188 
00189         //-////////////////////////////////////////////////////////////////////////
00190 
00191         typedef boost::uint16_t value_type;
00192         static size_type const fixed_bytes = 2;
00193         static value_type const min_value = 0u;
00194         static value_type const max_value = 65535u;
00195 
00196         value_type value() const { return detail::packet::parse_uint16(i()); }
00197         void value(value_type v) { detail::packet::write_uint16(i(),v); }
00198         UInt16Parser const & operator= (value_type other) { value(other); return *this; }
00199     };
00203     inline std::ostream & operator<<(std::ostream & os, UInt16Parser const & i)
00204     { os << i.value(); return os; }
00205 
00210     struct UInt16LSBParser
00211         : public detail::packet::IntParserOps<UInt16LSBParser,boost::uint16_t>,
00212           public PacketParserBase
00213     {
00214         UInt16LSBParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00215 
00216         //-////////////////////////////////////////////////////////////////////////
00217 
00218         typedef boost::uint16_t value_type;
00219         static size_type const fixed_bytes = 2;
00220         static value_type const min_value = 0u;
00221         static value_type const max_value = 65535u;
00222 
00223         value_type value() const { return detail::packet::parse_uint16LSB(i()); }
00224         void value(value_type v) { detail::packet::write_uint16LSB(i(),v); }
00225         UInt16LSBParser const & operator= (value_type other) { value(other); return *this; }
00226     };
00230     inline std::ostream & operator<<(std::ostream & os, UInt16LSBParser const & i)
00231     { os << i.value(); return os; }
00232 
00237     struct Int24Parser
00238         : public detail::packet::IntParserOps<Int24Parser,boost::int32_t>,
00239           public PacketParserBase
00240     {
00241         Int24Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00242 
00243         //-////////////////////////////////////////////////////////////////////////
00244 
00245         typedef boost::int32_t value_type;
00246         static size_type const fixed_bytes = 3;
00247         static value_type const min_value = -8388608;
00248         static value_type const max_value = 8388607;
00249 
00250         value_type value() const {
00251             value_type v (detail::packet::parse_uint24(i())); return v&0x800000 ? v|0xff000000 : v; }
00252         void value(value_type v) { detail::packet::write_uint24(i(),v); }
00253         Int24Parser const & operator= (value_type other) { value(other); return *this; }
00254     };
00258     inline std::ostream & operator<<(std::ostream & os, Int24Parser const & i)
00259     { os << i.value(); return os; }
00260 
00265     struct UInt24Parser
00266         : public detail::packet::IntParserOps<UInt24Parser,boost::uint32_t>,
00267           public PacketParserBase
00268     {
00269         UInt24Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00270 
00271         //-////////////////////////////////////////////////////////////////////////
00272 
00273         typedef boost::uint32_t value_type;
00274         static size_type const fixed_bytes = 3;
00275         static value_type const min_value = 0u;
00276         static value_type const max_value = 16777215u;
00277 
00278         value_type value() const { return detail::packet::parse_uint24(i()); }
00279         void value(value_type v) { detail::packet::write_uint24(i(),v); }
00280         UInt24Parser const & operator= (value_type other) { value(other); return *this; }
00281     };
00285     inline std::ostream & operator<<(std::ostream & os, UInt24Parser const & i)
00286     { os << i.value(); return os; }
00287 
00292     struct Int32Parser
00293         : public detail::packet::IntParserOps<Int32Parser,boost::int32_t>,
00294           public PacketParserBase
00295     {
00296         Int32Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00297 
00298         //-////////////////////////////////////////////////////////////////////////
00299 
00300         typedef boost::int32_t value_type;
00301         static size_type const fixed_bytes = 4;
00302         static value_type const min_value = -2147483647 - 1;
00303         static value_type const max_value = 2147483647;
00304 
00305         value_type value() const { return detail::packet::parse_uint32(i()); }
00306         void value(value_type v) { detail::packet::write_uint32(i(),v); }
00307         Int32Parser const & operator= (value_type other) { value(other); return *this; }
00308     };
00312     inline std::ostream & operator<<(std::ostream & os, Int32Parser const & i)
00313     { os << i.value(); return os; }
00314 
00319     struct UInt32Parser
00320         : public detail::packet::IntParserOps<UInt32Parser,boost::uint32_t>,
00321           public PacketParserBase
00322     {
00323         UInt32Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00324 
00325         //-////////////////////////////////////////////////////////////////////////
00326 
00327         typedef boost::uint32_t value_type;
00328         static size_type const fixed_bytes = 4;
00329         static value_type const min_value = 0u;
00330         static value_type const max_value = 4294967295u;
00331 
00332         value_type value() const { return detail::packet::parse_uint32(i()); }
00333         void value(value_type v) { detail::packet::write_uint32(i(),v); }
00334         UInt32Parser const & operator= (value_type other) { value(other); return *this; }
00335     };
00339     inline std::ostream & operator<<(std::ostream & os, UInt32Parser const & i)
00340     { os << i.value(); return os; }
00341 
00342     struct UInt32LSBParser
00343         : public detail::packet::IntParserOps<UInt32LSBParser,boost::uint32_t>,
00344           public PacketParserBase
00345     {
00346         UInt32LSBParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00347 
00348         //-////////////////////////////////////////////////////////////////////////
00349 
00350         typedef boost::uint32_t value_type;
00351         static size_type const fixed_bytes = 4;
00352         static value_type const min_value = 0u;
00353         static value_type const max_value = 4294967295u;
00354 
00355         value_type value() const { return detail::packet::parse_uint32LSB(i()); }
00356         void value(value_type v) { detail::packet::write_uint32LSB(i(),v); }
00357         UInt32LSBParser const & operator= (value_type other) { value(other); return *this; }
00358     };
00362     inline std::ostream & operator<<(std::ostream & os, UInt32LSBParser const & i)
00363     { os << i.value(); return os; }
00364 
00365 
00366 
00367 
00372     struct Int64Parser
00373         : public detail::packet::IntParserOps<Int64Parser,boost::int64_t>,
00374           public PacketParserBase
00375     {
00376         Int64Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00377 
00378         //-////////////////////////////////////////////////////////////////////////
00379 
00380         typedef boost::int64_t value_type;
00381         static size_type const fixed_bytes = 8;
00382 
00383         value_type value() const { return detail::packet::parse_uint64(i()); }
00384         void value(value_type v) { detail::packet::write_uint64(i(),v); }
00385         Int64Parser const & operator= (value_type other) { value(other); return *this; }
00386     };
00390     inline std::ostream & operator<<(std::ostream & os, Int64Parser const & i)
00391     { os << i.value(); return os; }
00392 
00393 
00398     struct UInt64Parser
00399         : public detail::packet::IntParserOps<UInt64Parser,boost::uint64_t>,
00400           public PacketParserBase
00401     {
00402         UInt64Parser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00403 
00404         //-////////////////////////////////////////////////////////////////////////
00405 
00406         typedef boost::uint64_t value_type;
00407         static size_type const fixed_bytes = 8;
00408 
00409         value_type value() const { return detail::packet::parse_uint64(i()); }
00410         void value(value_type v) { detail::packet::write_uint64(i(),v); }
00411         UInt64Parser const & operator= (value_type other) { value(other); return *this; }
00412     };
00416     inline std::ostream & operator<<(std::ostream & os, UInt64Parser const & i)
00417     { os << i.value(); return os; }
00418 
00423     struct UInt64LSBParser
00424         : public detail::packet::IntParserOps<UInt64LSBParser,boost::uint64_t>,
00425           public PacketParserBase
00426     {
00427         UInt64LSBParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00428 
00429         //-////////////////////////////////////////////////////////////////////////
00430 
00431         typedef boost::uint64_t value_type;
00432         static size_type const fixed_bytes = 8;
00433 
00434         value_type value() const { return detail::packet::parse_uint64LSB(i()); }
00435         void value(value_type v) { detail::packet::write_uint64LSB(i(),v); }
00436         UInt64LSBParser const & operator= (value_type other) { value(other); return *this; }
00437     };
00441     inline std::ostream & operator<<(std::ostream & os, UInt64LSBParser const & i)
00442     { os << i.value(); return os; }
00443 
00466     template <unsigned Start, unsigned End>
00467     struct IntFieldParser
00468         : public detail::packet::IntParserOps<IntFieldParser<Start,End>,boost::int32_t>,
00469           public PacketParserBase
00470     {
00471         IntFieldParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00472 
00473         //-////////////////////////////////////////////////////////////////////////
00474 
00475         typedef boost::int32_t value_type;
00476         static size_type const start_bit = Start;
00477         static size_type const end_bit = End;
00478         static size_type const fixed_bytes = (End-1)/8+1;
00479         static value_type const max_value = boost::low_bits_mask_t<End-Start-1>::sig_bits;
00480         static value_type const min_value = - max_value - 1;
00481 
00482 
00483         value_type value() const {
00484             value_type v (detail::packet::parse_bitfield<Start,End>::parse(i()));
00485             return v&boost::high_bit_mask_t<End-Start-1>::high_bit ?
00486                 v | ~boost::low_bits_mask_t<End-Start>::sig_bits : v;
00487         }
00488         void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
00489         IntFieldParser const & operator= (value_type other) { value(other); return *this; }
00490 
00491     private:
00492         BOOST_STATIC_ASSERT( Start<End );
00493         BOOST_STATIC_ASSERT( End-Start<=32 );
00494     };
00498     template <unsigned Start, unsigned End>
00499     inline std::ostream & operator<<(std::ostream & os, IntFieldParser<Start,End> const & i)
00500     { os << i.value(); return os; }
00501 
00524     template <unsigned Start, unsigned End>
00525     struct UIntFieldParser
00526         : public detail::packet::IntParserOps<UIntFieldParser<Start,End>,boost::uint32_t>,
00527           public PacketParserBase
00528     {
00529         UIntFieldParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00530 
00531         //-////////////////////////////////////////////////////////////////////////
00532 
00533         typedef boost::uint32_t value_type;
00534         static size_type const start_bit = Start;
00535         static size_type const end_bit = End;
00536         static size_type const fixed_bytes = (End-1)/8+1;
00537         static value_type const min_value = 0u;
00538         static value_type const max_value = boost::low_bits_mask_t<End-Start>::sig_bits;
00539 
00540         value_type value() const { return detail::packet::parse_bitfield<Start,End>::parse(i()); }
00541         void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
00542         UIntFieldParser const & operator= (value_type other) { value(other); return *this; }
00543 
00544     private:
00545         BOOST_STATIC_ASSERT( Start<End );
00546         BOOST_STATIC_ASSERT( End-Start<=32 );
00547     };
00551     template <unsigned Start, unsigned End>
00552     inline std::ostream & operator<<(std::ostream & os, UIntFieldParser<Start,End> const & i)
00553     { os << i.value(); return os; }
00554 
00569     template <unsigned Bit>
00570     struct FlagParser
00571         : public detail::packet::IntParserOps<FlagParser<Bit>,bool>,
00572           public PacketParserBase
00573     {
00574         FlagParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
00575 
00576         //-////////////////////////////////////////////////////////////////////////
00577 
00578         typedef bool value_type;
00579         static size_type const bit = Bit;
00580         static size_type const fixed_bytes = Bit/8+1;
00581         static value_type const min_value = 0;
00582         static value_type const max_value = 1;
00583 
00584         value_type value() const { return i()[Bit/8] & (1<<(7-(Bit%8))); }
00585         void value(value_type v) {
00586             if (v) i()[Bit/8] |= 1<<(7-(Bit%8));
00587             else   i()[Bit/8] &= ~(1<<(7-(Bit%8)));
00588         }
00589         FlagParser const & operator= (value_type other) { value(other); return *this; }
00590     };
00594     template <unsigned Bit>
00595     inline std::ostream & operator<<(std::ostream & os, FlagParser<Bit> const & i)
00596     { os << i.value(); return os; }
00597 
00598 }
00599 
00600 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00601 #endif
00602 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_IntParser_i_)
00603 #define HH_SENF_Packets_IntParser_i_
00604 //#include "IntParser.cci"
00605 //#include "IntParser.ct"
00606 //#include "IntParser.cti"
00607 #endif
00608 
00609 
00610 // Local Variables:
00611 // mode: c++
00612 // fill-column: 100
00613 // c-file-style: "senf"
00614 // indent-tabs-mode: nil
00615 // ispell-local-dictionary: "american"
00616 // compile-command: "scons -u test"
00617 // comment-column: 40
00618 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research