Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62901 - sandbox/endian_ext/boost/integer
From: vicente.botet_at_[hidden]
Date: 2010-06-13 04:30:19


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

Log:
Make endian_view generic, able to manage with UDT
Text files modified:
   sandbox/endian_ext/boost/integer/endian_view.hpp | 94 ++++++++++++++++++++++++++++++++++-----
   1 files changed, 80 insertions(+), 14 deletions(-)

Modified: sandbox/endian_ext/boost/integer/endian_view.hpp
==============================================================================
--- sandbox/endian_ext/boost/integer/endian_view.hpp (original)
+++ sandbox/endian_ext/boost/integer/endian_view.hpp 2010-06-13 04:30:18 EDT (Sun, 13 Jun 2010)
@@ -14,47 +14,111 @@
 #define BOOST_INTEGER_ENDIAN_VIEW_HPP
 
 #include <boost/integer/endian_pack.hpp>
+#include <boost/fusion/include/is_sequence.hpp>
+#include <boost/type_traits/is_fundamental.hpp>
 
 namespace boost {
 
 namespace integer {
 
- template <typename Endian>
- class endian_view {
- typedef typename Endian::value_type T;
- T &ref_;
+ template <typename EndianSource, typename T>
+ void convert_from(T& r);
+ template <typename EndianTarget, typename T>
+ void convert_to(T& r);
+ template <typename EndianTarget, typename EndianSource, typename T>
+ void convert_to_from(T& r);
+
+ template <typename Endian, typename T,
+ bool IsFundamental = is_fundamental<T>::value,
+ bool IsSeq = fusion::traits::is_sequence<T>::value
+ >
+ class endian_view;
+
+ template <typename Endian, typename T >
+ class endian_view<Endian,T,true,false>
+ {
+ //~ typedef typename Endian::value_type T;
+ //~ template friend
     public:
- typedef Endian endian_t;
- endian_view(T& ref) : ref_(ref) {};
- operator T() const {
+ typedef T value_type;
+ value_type &ref_;
+
+ typedef endian_pack<Endian,value_type> endian_t;
+ endian_view(value_type& ref) : ref_(ref) {};
+ operator value_type() const {
             endian_t& v=reinterpret_cast<endian_t&>(ref_);
             return v;
         }
- endian_view& operator=(T val) {
+ endian_view& operator=(value_type val) {
             endian_t& v=reinterpret_cast<endian_t&>(ref_);
             v=val;
             return *this;
         }
+ endian_view& operator=(endian_view const& rhs) {
+ if (this!=&rhs) {
+ ref_=rhs.ref_;
+ }
+ return *this;
+ }
+ template <typename Endian2 >
+ endian_view& operator=(endian_view<Endian2,T,true,false> const& rhs) {
+ endian_t& v=reinterpret_cast<endian_t&>(ref_);
+ endian_pack<Endian2,value_type>& val=reinterpret_cast<endian_pack<Endian2,value_type>&>(rhs.ref_);
+ v=val;
+ return *this;
+ }
     };
 
+ template <typename Endian, typename T >
+ class endian_view<Endian,T,false,true>
+ {
+ public:
+ typedef T value_type;
+ value_type &ref_;
+
+ endian_view(value_type& ref) : ref_(ref) {};
+ operator value_type() const {
+ value_type res=ref_;
+ convert_from<Endian>(res);
+ return res;
+ }
+ endian_view& operator=(value_type val) {
+ ref_=val;
+ convert_to<Endian>(ref_);
+ return *this;
+ }
+ endian_view& operator=(endian_view const& rhs) {
+ if (this!=&rhs) {
+ ref_=rhs.ref_;
+ }
+ return *this;
+ }
+ template <typename Endian2 >
+ endian_view& operator=(endian_view<Endian2,T,true,false> const& rhs) {
+ ref_=rhs.ref_;
+ convert_to_from<Endian2,Endian>(ref_);
+ return *this;
+ }
+ };
+
     template <typename E, typename T>
- endian_view<endian_pack<E,T> > as_endian(T& v) {
- return endian_view<endian_pack<E,T> >(v);
+ endian_view<E,T> as_endian(T& v) {
+ return endian_view<E,T>(v);
     }
  
     template <typename T>
- endian_view<endian_pack<native_endian,T> > as(T& v)
+ endian_view<native_endian,T> as(T& v)
     {
         return as_endian<native_endian>(v);
     }
     template <typename T>
- endian_view<endian_pack<little_endian,T> > as_little(T& v)
+ endian_view<little_endian,T> as_little(T& v)
     {
         return as_endian<little_endian>(v);
     }
     
     template <typename T>
- endian_view<endian_pack<big_endian,T> > as_big(T& v)
+ endian_view<big_endian,T> as_big(T& v)
     {
         return as_endian<big_endian>(v);
     }
@@ -63,5 +127,7 @@
 } // namespace integer
 } // namespace boost
 
+#include <boost/integer/endian_conversion.hpp>
+
 
-#endif // BOOST_ENDIAN_HPP
+#endif // BOOST_INTEGER_ENDIAN_VIEW_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