forked from boostorg/endian
Add BOOST_MINIMAL_INTEGER_COVER_OPERATORS to reduce coupling to boost/operators.hpp, make constructor from T explicit to eliminate unintended consequences, add logging feature, extend test coverage.
git-svn-id: http://svn.boost.org/svn/boost/sandbox/endian@48004 b8fc166d-592f-0410-95f2-cb63ce0dd405
This commit is contained in:
1 parent
5610e8d97b
commit
f37a6cd5ca
5 files changed
+347
-147
No files matched your search
@@ -1,16 +1,26 @@
|
||||
// boost/integer/cover_operators.hpp ----------------------------------------//
|
||||
|
||||
// (C) Copyright Darin Adler 2000
|
||||
// Copyright Darin Adler 2000
|
||||
// Copyright Beman Dawes 2008
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
// If the class being covered has a non-explicit conversion to an integer type
|
||||
// then a smaller number of cover operations are needed. Define the macro
|
||||
// BOOST_MINIMAL_INTEGER_COVER_OPERATORS to indicate this.
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
#ifndef BOOST_INTEGER_COVER_OPERATORS_HPP
|
||||
#define BOOST_INTEGER_COVER_OPERATORS_HPP
|
||||
|
||||
#include <boost/operators.hpp>
|
||||
# ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
# include <boost/operators.hpp>
|
||||
# endif
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
namespace boost
|
||||
@@ -21,13 +31,17 @@ namespace boost
|
||||
// A class that adds integer operators to an integer cover class
|
||||
|
||||
template <typename T, typename IntegerType>
|
||||
class cover_operators : boost::operators<T>
|
||||
class cover_operators
|
||||
# ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
: boost::operators<T>
|
||||
# endif
|
||||
{
|
||||
// The other operations take advantage of the type conversion that's
|
||||
// built into unary +.
|
||||
|
||||
// Unary operations.
|
||||
friend IntegerType operator+(const T& x) { return x; }
|
||||
# ifndef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
friend IntegerType operator-(const T& x) { return -+x; }
|
||||
friend IntegerType operator~(const T& x) { return ~+x; }
|
||||
friend IntegerType operator!(const T& x) { return !+x; }
|
||||
@@ -35,6 +49,7 @@ namespace boost
|
||||
// The basic ordering operations.
|
||||
friend bool operator==(const T& x, IntegerType y) { return +x == y; }
|
||||
friend bool operator<(const T& x, IntegerType y) { return +x < y; }
|
||||
# endif
|
||||
|
||||
// The basic arithmetic operations.
|
||||
friend T& operator+=(T& x, IntegerType y) { return x = +x + y; }
|
||||
@@ -57,8 +72,23 @@ namespace boost
|
||||
friend T& operator++(T& x) { return x += 1; }
|
||||
friend T& operator--(T& x) { return x -= 1; }
|
||||
|
||||
/// TODO: stream I/O needs to be templatized on the stream type, so will
|
||||
/// work with wide streams, etc.
|
||||
# ifdef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
friend T operator++(T& x, int)
|
||||
{
|
||||
T tmp(x);
|
||||
x += 1;
|
||||
return tmp;
|
||||
}
|
||||
friend T operator--(T& x, int)
|
||||
{
|
||||
T tmp(x);
|
||||
x -= 1;
|
||||
return tmp;
|
||||
}
|
||||
# endif
|
||||
|
||||
// TODO: stream I/O needs to be templatized on the stream type, so will
|
||||
// work with wide streams, etc.
|
||||
|
||||
// Stream input and output.
|
||||
friend std::ostream& operator<<(std::ostream& s, const T& x)
|
||||
|
||||
+61
-21
@@ -26,8 +26,15 @@
|
||||
#ifndef BOOST_ENDIAN_HPP
|
||||
#define BOOST_ENDIAN_HPP
|
||||
|
||||
// Pending updates to boost/config.hpp for C++0x, assume that the C++0x feature
|
||||
// we care about is not present:
|
||||
#define BOOST_NO_DEFAULTED_FUNCTIONS
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/endian.hpp>
|
||||
//#include <boost/integer/cover_operators.hpp>
|
||||
#define BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
#include <boost/integer/cover_operators.hpp>
|
||||
#undef BOOST_MINIMAL_INTEGER_COVER_OPERATORS
|
||||
#include <boost/type_traits/is_signed.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
@@ -39,9 +46,9 @@
|
||||
# endif
|
||||
|
||||
# ifndef BOOST_NO_DEFAULTED_FUNCTIONS
|
||||
# define BOOST_ENDIAN_DEFAULTED = default; // C++0x
|
||||
# define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
|
||||
# else
|
||||
# define BOOST_ENDIAN_DEFAULTED {} // C++03
|
||||
# define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03
|
||||
# endif
|
||||
|
||||
# if defined(BOOST_NO_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIANS_IN_UNIONS)
|
||||
@@ -142,6 +149,11 @@ namespace boost
|
||||
namespace integer
|
||||
{
|
||||
|
||||
# ifdef BOOST_ENDIAN_LOG
|
||||
bool endian_log(true);
|
||||
# endif
|
||||
|
||||
|
||||
// endian class template and specializations -----------------------------//
|
||||
|
||||
// simulate C++0x scoped enums
|
||||
@@ -160,17 +172,31 @@ namespace boost
|
||||
// unaligned big endian specialization
|
||||
template <typename T, std::size_t n_bits>
|
||||
class endian< endianness::big, T, n_bits, alignment::unaligned >
|
||||
// : cover_operators< endian< endianness::big, T, n_bits >, T >
|
||||
: cover_operators< endian< endianness::big, T, n_bits >, T >
|
||||
{
|
||||
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
|
||||
public:
|
||||
typedef T value_type;
|
||||
# ifndef BOOST_ENDIAN_NO_CTORS
|
||||
endian() BOOST_ENDIAN_DEFAULTED
|
||||
endian(T val) { detail::store_big_endian<T, n_bits/8>(m_value, val); }
|
||||
endian() BOOST_ENDIAN_DEFAULT_CONSTRUCT
|
||||
explicit endian(T val)
|
||||
{
|
||||
# ifdef BOOST_ENDIAN_LOG
|
||||
if ( endian_log )
|
||||
std::clog << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
|
||||
# endif
|
||||
detail::store_big_endian<T, n_bits/8>(m_value, val);
|
||||
}
|
||||
# endif
|
||||
endian & operator=(T val) { detail::store_big_endian<T, n_bits/8>(m_value, val); return *this; }
|
||||
operator T() const { return detail::load_big_endian<T, n_bits/8>(m_value); }
|
||||
operator T() const
|
||||
{
|
||||
# ifdef BOOST_ENDIAN_LOG
|
||||
if ( endian_log )
|
||||
std::clog << "big, unaligned, " << n_bits << "-bits, convert(" << detail::load_big_endian<T, n_bits/8>(m_value) << ")\n";
|
||||
# endif
|
||||
return detail::load_big_endian<T, n_bits/8>(m_value);
|
||||
}
|
||||
private:
|
||||
char m_value[n_bits/8];
|
||||
};
|
||||
@@ -178,17 +204,31 @@ namespace boost
|
||||
// unaligned little endian specialization
|
||||
template <typename T, std::size_t n_bits>
|
||||
class endian< endianness::little, T, n_bits, alignment::unaligned >
|
||||
// : cover_operators< endian< endianness::little, T, n_bits >, T >
|
||||
: cover_operators< endian< endianness::little, T, n_bits >, T >
|
||||
{
|
||||
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
|
||||
public:
|
||||
typedef T value_type;
|
||||
# ifndef BOOST_ENDIAN_NO_CTORS
|
||||
endian() BOOST_ENDIAN_DEFAULTED
|
||||
endian(T val) { detail::store_little_endian<T, n_bits/8>(m_value, val); }
|
||||
endian() BOOST_ENDIAN_DEFAULT_CONSTRUCT
|
||||
explicit endian(T val)
|
||||
{
|
||||
# ifdef BOOST_ENDIAN_LOG
|
||||
if ( endian_log )
|
||||
std::clog << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
|
||||
# endif
|
||||
detail::store_little_endian<T, n_bits/8>(m_value, val);
|
||||
}
|
||||
# endif
|
||||
endian & operator=(T val) { detail::store_little_endian<T, n_bits/8>(m_value, val); return *this; }
|
||||
operator T() const { return detail::load_little_endian<T, n_bits/8>(m_value); }
|
||||
operator T() const
|
||||
{
|
||||
# ifdef BOOST_ENDIAN_LOG
|
||||
if ( endian_log )
|
||||
std::clog << "little, unaligned, " << n_bits << "-bits, convert(" << detail::load_little_endian<T, n_bits/8>(m_value) << ")\n";
|
||||
# endif
|
||||
return detail::load_little_endian<T, n_bits/8>(m_value);
|
||||
}
|
||||
private:
|
||||
char m_value[n_bits/8];
|
||||
};
|
||||
@@ -196,17 +236,17 @@ namespace boost
|
||||
// unaligned native endian specialization
|
||||
template <typename T, std::size_t n_bits>
|
||||
class endian< endianness::native, T, n_bits, alignment::unaligned >
|
||||
// : cover_operators< endian< endianness::native, T, n_bits >, T >
|
||||
: cover_operators< endian< endianness::native, T, n_bits >, T >
|
||||
{
|
||||
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
|
||||
public:
|
||||
typedef T value_type;
|
||||
# ifndef BOOST_ENDIAN_NO_CTORS
|
||||
endian() BOOST_ENDIAN_DEFAULTED
|
||||
endian() BOOST_ENDIAN_DEFAULT_CONSTRUCT
|
||||
# ifdef BOOST_BIG_ENDIAN
|
||||
endian(T val) { detail::store_big_endian<T, n_bits/8>(m_value, val); }
|
||||
explicit endian(T val) { detail::store_big_endian<T, n_bits/8>(m_value, val); }
|
||||
# else
|
||||
endian(T val) { detail::store_little_endian<T, n_bits/8>(m_value, val); }
|
||||
explicit endian(T val) { detail::store_little_endian<T, n_bits/8>(m_value, val); }
|
||||
# endif
|
||||
# endif
|
||||
# ifdef BOOST_BIG_ENDIAN
|
||||
@@ -226,18 +266,18 @@ namespace boost
|
||||
// aligned big endian specialization
|
||||
template <typename T, std::size_t n_bits>
|
||||
class endian< endianness::big, T, n_bits, alignment::aligned >
|
||||
// : cover_operators< endian< endianness::big, T, n_bits, alignment::aligned >, T >
|
||||
: cover_operators< endian< endianness::big, T, n_bits, alignment::aligned >, T >
|
||||
{
|
||||
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
|
||||
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
|
||||
public:
|
||||
typedef T value_type;
|
||||
# ifndef BOOST_ENDIAN_NO_CTORS
|
||||
endian() BOOST_ENDIAN_DEFAULTED
|
||||
endian() BOOST_ENDIAN_DEFAULT_CONSTRUCT
|
||||
# ifdef BOOST_BIG_ENDIAN
|
||||
endian(T val) : m_value(val) { }
|
||||
# else
|
||||
endian(T val) { detail::store_big_endian<T, sizeof(T)>(&m_value, val); }
|
||||
explicit endian(T val) { detail::store_big_endian<T, sizeof(T)>(&m_value, val); }
|
||||
# endif
|
||||
# endif
|
||||
# ifdef BOOST_BIG_ENDIAN
|
||||
@@ -254,18 +294,18 @@ namespace boost
|
||||
// aligned little endian specialization
|
||||
template <typename T, std::size_t n_bits>
|
||||
class endian< endianness::little, T, n_bits, alignment::aligned >
|
||||
// : cover_operators< endian< endianness::little, T, n_bits, alignment::aligned >, T >
|
||||
: cover_operators< endian< endianness::little, T, n_bits, alignment::aligned >, T >
|
||||
{
|
||||
BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
|
||||
BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
|
||||
public:
|
||||
typedef T value_type;
|
||||
# ifndef BOOST_ENDIAN_NO_CTORS
|
||||
endian() BOOST_ENDIAN_DEFAULTED
|
||||
endian() BOOST_ENDIAN_DEFAULT_CONSTRUCT
|
||||
# ifdef BOOST_BIG_ENDIAN
|
||||
endian(T val) : m_value(val) { }
|
||||
# else
|
||||
endian(T val) { detail::store_little_endian<T, sizeof(T)>(&m_value, val); }
|
||||
explicit endian(T val) { detail::store_little_endian<T, sizeof(T)>(&m_value, val); }
|
||||
# endif
|
||||
# endif
|
||||
# ifdef BOOST_LITTLE_ENDIAN
|
||||
|
||||
Reference in new issue
Block a user