2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
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)
|
|
|
|
=============================================================================*/
|
2011-10-25 09:16:27 +00:00
|
|
|
#ifndef SPROUT_ENDIAN_TRAITS_HPP
|
|
|
|
#define SPROUT_ENDIAN_TRAITS_HPP
|
|
|
|
|
|
|
|
#include <climits>
|
2012-08-08 13:27:06 +00:00
|
|
|
#include <type_traits>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-08-08 13:27:06 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename Enable = void>
|
|
|
|
class default_big_endian_traits;
|
|
|
|
template<typename T>
|
|
|
|
class default_big_endian_traits<
|
|
|
|
T,
|
2012-08-11 12:53:12 +00:00
|
|
|
typename std::enable_if<std::is_integral<T>::value && sizeof(T) == 1>::type
|
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef T type;
|
|
|
|
public:
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CONSTEXPR std::size_t
|
|
|
|
size() {
|
2012-08-11 12:53:12 +00:00
|
|
|
return sizeof(type);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CONSTEXPR unsigned char
|
|
|
|
get_byte(type const& t, std::size_t) {
|
2012-08-11 12:53:12 +00:00
|
|
|
return static_cast<unsigned char>(t);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CXX14_CONSTEXPR void
|
|
|
|
set_byte(type& t, std::size_t, unsigned char value) {
|
|
|
|
t = static_cast<type>(value);
|
|
|
|
}
|
2012-08-11 12:53:12 +00:00
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
class default_big_endian_traits<
|
|
|
|
T,
|
|
|
|
typename std::enable_if<std::is_integral<T>::value && !(sizeof(T) == 1)>::type
|
2012-08-08 13:27:06 +00:00
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef T type;
|
|
|
|
public:
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CONSTEXPR std::size_t
|
|
|
|
size() {
|
2012-08-08 13:27:06 +00:00
|
|
|
return sizeof(type);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CONSTEXPR unsigned char
|
|
|
|
get_byte(type const& t, std::size_t i) {
|
2012-08-08 13:27:06 +00:00
|
|
|
return static_cast<unsigned char>(
|
|
|
|
(t & (UCHAR_MAX << CHAR_BIT * ((size() - 1) - i)))
|
|
|
|
>> CHAR_BIT * ((size() - 1) - i)
|
|
|
|
);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CXX14_CONSTEXPR void
|
|
|
|
set_byte(type& t, std::size_t i, unsigned char value) {
|
|
|
|
t &= ~(UCHAR_MAX << CHAR_BIT * ((size() - 1) - i));
|
|
|
|
t |= (value << CHAR_BIT * ((size() - 1) - i));
|
|
|
|
}
|
2012-08-08 13:27:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename Enable = void>
|
|
|
|
class default_little_endian_traits;
|
|
|
|
template<typename T>
|
|
|
|
class default_little_endian_traits<
|
|
|
|
T,
|
2012-08-11 12:53:12 +00:00
|
|
|
typename std::enable_if<std::is_integral<T>::value && sizeof(T) == 1>::type
|
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef T type;
|
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR std::size_t size() {
|
|
|
|
return sizeof(type);
|
|
|
|
}
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t) {
|
2012-08-11 12:53:12 +00:00
|
|
|
return static_cast<unsigned char>(t);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CXX14_CONSTEXPR void
|
|
|
|
set_byte(type& t, std::size_t, unsigned char value) {
|
|
|
|
t = static_cast<type>(value);
|
|
|
|
}
|
2012-08-11 12:53:12 +00:00
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
class default_little_endian_traits<
|
|
|
|
T,
|
|
|
|
typename std::enable_if<std::is_integral<T>::value && !(sizeof(T) == 1)>::type
|
2012-08-08 13:27:06 +00:00
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef T type;
|
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR std::size_t size() {
|
|
|
|
return sizeof(type);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) {
|
|
|
|
return static_cast<unsigned char>(
|
|
|
|
(t & (UCHAR_MAX << CHAR_BIT * i))
|
|
|
|
>> CHAR_BIT * i
|
|
|
|
);
|
|
|
|
}
|
2014-05-28 15:25:37 +00:00
|
|
|
static SPROUT_CXX14_CONSTEXPR void
|
|
|
|
set_byte(type& t, std::size_t i, unsigned char value) {
|
|
|
|
t &= ~(UCHAR_MAX << CHAR_BIT * i);
|
|
|
|
t |= (value << CHAR_BIT * i);
|
|
|
|
}
|
2012-08-08 13:27:06 +00:00
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
|
|
|
// big_endian_traits
|
|
|
|
//
|
|
|
|
template<typename T>
|
2012-08-08 13:27:06 +00:00
|
|
|
class big_endian_traits
|
|
|
|
: public sprout::detail::default_big_endian_traits<T>
|
|
|
|
{};
|
|
|
|
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
|
|
|
// little_endian_traits
|
|
|
|
//
|
|
|
|
template<typename T>
|
2012-08-08 13:27:06 +00:00
|
|
|
class little_endian_traits
|
|
|
|
: public sprout::detail::default_little_endian_traits<T>
|
|
|
|
{};
|
|
|
|
|
|
|
|
//
|
|
|
|
// endian_traits
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
class endian_traits
|
|
|
|
: public sprout::big_endian_traits<T>
|
|
|
|
{};
|
2011-10-25 09:16:27 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ENDIAN_TRAITS_HPP
|