Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72091 - sandbox/endian/boost/integer
From: bdawes_at_[hidden]
Date: 2011-05-22 15:25:03


Author: bemandawes
Date: 2011-05-22 15:25:03 EDT (Sun, 22 May 2011)
New Revision: 72091
URL: http://svn.boost.org/trac/boost/changeset/72091

Log:
Add changes from conversion.hpp, providing synopsis with in-place and non-modifying operations, unconditional and conditional.
Text files modified:
   sandbox/endian/boost/integer/endian_flip.hpp | 134 +++++++++++++++++++++++++++++++++++----
   1 files changed, 120 insertions(+), 14 deletions(-)

Modified: sandbox/endian/boost/integer/endian_flip.hpp
==============================================================================
--- sandbox/endian/boost/integer/endian_flip.hpp (original)
+++ sandbox/endian/boost/integer/endian_flip.hpp 2011-05-22 15:25:03 EDT (Sun, 22 May 2011)
@@ -1,19 +1,60 @@
-// endian_flip.hpp -------------------------------------------------------------------//
+// boost/endian/conversion.hpp -------------------------------------------------------//
 
-// Copyright Beman Dawes 2010
+// Copyright Beman Dawes 2010, 2011
 
 // Distributed under the Boost Software License, Version 1.0.
 // http://www.boost.org/LICENSE_1_0.txt
 
-#ifndef BOOST_ENDIAN_FLIP_HPP
-#define BOOST_ENDIAN_FLIP_HPP
+#ifndef BOOST_ENDIAN_CONVERSION_HPP
+#define BOOST_ENDIAN_CONVERSION_HPP
+
+#include <boost/detail/endian.hpp>
+#include <boost/cstdint.hpp>
+
+//------------------------------------- synopsis ---------------------------------------//
 
-#include <boost/integer.hpp>
 namespace boost
 {
-namespace integer
+namespace endian
 {
- inline void endian_flip(int16_t& x)
+ // unconditional modifying (i.e. in-place) endianness reversal
+
+ inline void flip(int16_t& x);
+ inline void flip(int32_t& x);
+ inline void flip(int64_t& x);
+ inline void flip(uint16_t& x);
+ inline void flip(uint32_t& x);
+ inline void flip(uint64_t& x);
+
+ // unconditional non-modifying endianness reversing copy
+
+ inline void flip(int16_t source, int16_t& target);
+ inline void flip(int32_t source, int32_t& target);
+ inline void flip(int64_t source, int64_t& target);
+ inline void flip(uint16_t source, uint16_t& target);
+ inline void flip(uint32_t source, uint32_t& target);
+ inline void flip(uint64_t source, uint64_t& target);
+
+ // conditional modifying (i.e. in-place) endianness reversal;
+ // no effect if native endianness and specified endianness are the same
+
+ template <class T> inline void to_big(T& x); // if different, convert native to big
+ template <class T> inline void to_little(T& x); // if different, convert native to little
+ template <class T> inline void from_big(T& x); // if different, convert big to native
+ template <class T> inline void from_little(T& x); // if different, convert little to native
+
+ // non-modifying copy, conditionally reversing endianness;
+ // copy the first argument to the second argument, converting to or from the
+ // specified endianness if different than native endianness
+
+ template <class T> inline void to_big(T native, T& big);
+ template <class T> inline void to_little(T native, T& little);
+ template <class T> inline void from_big(T big, T& native);
+ template <class T> inline void from_little(T little, T& native);
+
+//----------------------------------- implementation -----------------------------------//
+
+ inline void flip(int16_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -22,7 +63,7 @@
     *(rep+1) = tmp;
   }
 
- inline void endian_flip(int32_t& x)
+ inline void flip(int32_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -34,7 +75,7 @@
     *(rep+2) = tmp;
   }
 
- inline void endian_flip(int64_t& x)
+ inline void flip(int64_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -52,7 +93,7 @@
     *(rep+4) = tmp;
   }
 
- inline void endian_flip(uint16_t& x)
+ inline void flip(uint16_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -61,7 +102,7 @@
     *(rep+1) = tmp;
   }
 
- inline void endian_flip(uint32_t& x)
+ inline void flip(uint32_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -73,7 +114,7 @@
     *(rep+2) = tmp;
   }
 
- inline void endian_flip(uint64_t& x)
+ inline void flip(uint64_t& x)
   {
     char* rep = reinterpret_cast<char*>(&x);
     char tmp;
@@ -90,7 +131,72 @@
     *(rep+3) = *(rep+4);
     *(rep+4) = tmp;
   }
-} // namespace integer
+
+ inline void flip(int16_t source, int16_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ }
+
+ inline void flip(int32_t source, int32_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ }
+
+ inline void flip(int64_t source, int64_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ }
+
+ inline void flip(uint16_t source, uint16_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ }
+
+ inline void flip(uint32_t source, uint32_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ }
+
+ inline void flip(uint64_t source, uint64_t& target)
+ {
+ const char* s (reinterpret_cast<const char*>(&source));
+ char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
+ *t = *s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ *++t = *++s;
+ }
+
+} // namespace endian
 } // namespace boost
 
-#endif // BOOST_ENDIAN_FLIP_HPP
+#endif // BOOST_ENDIAN_CONVERSION_HPP


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