Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74353 - sandbox/endian/boost/endian
From: bdawes_at_[hidden]
Date: 2011-09-11 16:20:39


Author: bemandawes
Date: 2011-09-11 16:20:33 EDT (Sun, 11 Sep 2011)
New Revision: 74353
URL: http://svn.boost.org/trac/boost/changeset/74353

Log:
For now, revert to the general reorder template. Think about ways to make it safe and efficient.
Text files modified:
   sandbox/endian/boost/endian/conversion2.hpp | 28 +++++++++++++++++++++++-----
   1 files changed, 23 insertions(+), 5 deletions(-)

Modified: sandbox/endian/boost/endian/conversion2.hpp
==============================================================================
--- sandbox/endian/boost/endian/conversion2.hpp (original)
+++ sandbox/endian/boost/endian/conversion2.hpp 2011-09-11 16:20:33 EDT (Sun, 11 Sep 2011)
@@ -21,9 +21,10 @@
 // -- reorder implementation approach suggested by tymofey, with avoidance of
 // undefined behavior as suggested by Giovanni Piero Deretta, and a further
 // refinement suggested by Pyry Jahkola.
-//
-// Q: Why no general reorder template? A: It wouldn't work for classes with more than
-// one member; each member must be reordered individually.
+// -- general reorder function template to meet requests for UDT support by
+// Vicente Botet and others.
+// -- general reorder function template implementation approach using std::reverse
+// suggested by Mathias Gaunard
 //
 //--------------------------------------------------------------------------------------//
 
@@ -44,11 +45,16 @@
 
   // TODO: Need implementation
   // TODO: Need to verify the return does not invoke undefined behavior (as might happen
- // if there are unutterable floating point values, similar to the unutterable pointer
- // values on some architectures
+ // if there are unutterable floating point values, such as happens with the unutterable
+ // pointer values on some architectures
   inline float reorder(float x);
   inline double reorder(double x);
 
+ // TODO: Would pass by value be better for the following functions?
+
+ template <class T>
+ inline T reorder(const T& x);
+
   template <class T>
   inline T big(const T& x);
     // Return: x if native endianness is big, otherwise reorder(x);
@@ -109,6 +115,18 @@
            | (step16 & 0xFF00FF00FF00FF00) >> 8;
   }
 
+
+ template <class T>
+ inline T reorder(const T& x)
+ {
+ T tmp;
+ std::reverse(
+ reinterpret_cast<const char*>(&x),
+ reinterpret_cast<const char*>(&x) + sizeof(T),
+ reinterpret_cast<char*>(&tmp));
+ return tmp;
+ }
+
   template <class T>
   inline T big(const T& x)
   {


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