1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_BYTE_ORDEREr_KERNEL_1_
#define DLIB_BYTE_ORDEREr_KERNEL_1_
#include "byte_orderer_kernel_abstract.h"
#include "../algs.h"
#include "../assert.h"
namespace dlib
{
class byte_orderer
{
/*!
INITIAL VALUE
- if (this machine is little endian) then
- little_endian == true
- else
- little_endian == false
CONVENTION
- host_is_big_endian() == !little_endian
- host_is_little_endian() == little_endian
- if (this machine is little endian) then
- little_endian == true
- else
- little_endian == false
!*/
public:
// this is here for backwards compatibility with older versions of dlib.
typedef byte_orderer kernel_1a;
byte_orderer (
)
{
// This will probably never be false but if it is then it means chars are not 8bits
// on this system. Which is a problem for this object.
COMPILE_TIME_ASSERT(sizeof(short) >= 2);
unsigned long temp = 1;
unsigned char* ptr = reinterpret_cast<unsigned char*>(&temp);
if (*ptr == 1)
little_endian = true;
else
little_endian = false;
}
virtual ~byte_orderer (
){}
bool host_is_big_endian (
) const { return !little_endian; }
bool host_is_little_endian (
) const { return little_endian; }
template <
typename T
>
inline void host_to_network (
T& item
) const
{ if (little_endian) flip(item); }
template <
typename T
>
inline void network_to_host (
T& item
) const { if (little_endian) flip(item); }
template <
typename T
>
void host_to_big (
T& item
) const { if (little_endian) flip(item); }
template <
typename T
>
void big_to_host (
T& item
) const { if (little_endian) flip(item); }
template <
typename T
>
void host_to_little (
T& item
) const { if (!little_endian) flip(item); }
template <
typename T
>
void little_to_host (
T& item
) const { if (!little_endian) flip(item); }
private:
template <
typename T,
size_t size
>
inline void flip (
T (&array)[size]
) const
/*!
ensures
- flips the bytes in every element of this array
!*/
{
for (size_t i = 0; i < size; ++i)
{
flip(array[i]);
}
}
template <
typename T
>
inline void flip (
T& item
) const
/*!
ensures
- reverses the byte ordering in item
!*/
{
DLIB_ASSERT_HAS_STANDARD_LAYOUT(T);
T value;
// If you are getting this as an error then you are probably using
// this object wrong. If you think you aren't then send me (Davis) an
// email and I'll either set you straight or change/remove this check so
// your stuff works :)
COMPILE_TIME_ASSERT(sizeof(T) <= sizeof(long double));
// If you are getting a compile error on this line then it means T is
// a pointer type. It doesn't make any sense to byte swap pointers
// since they have no meaning outside the context of their own process.
// So you probably just forgot to dereference that pointer before passing
// it to this function :)
COMPILE_TIME_ASSERT(is_pointer_type<T>::value == false);
const size_t size = sizeof(T);
unsigned char* const ptr = reinterpret_cast<unsigned char*>(&item);
unsigned char* const ptr_temp = reinterpret_cast<unsigned char*>(&value);
for (size_t i = 0; i < size; ++i)
ptr_temp[size-i-1] = ptr[i];
item = value;
}
bool little_endian;
};
// make flip not do anything at all for chars
template <> inline void byte_orderer::flip<char> ( char& ) const {}
template <> inline void byte_orderer::flip<unsigned char> ( unsigned char& ) const {}
template <> inline void byte_orderer::flip<signed char> ( signed char& ) const {}
}
#endif // DLIB_BYTE_ORDEREr_KERNEL_1_
|