summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/byte_orderer
diff options
context:
space:
mode:
Diffstat (limited to 'ml/dlib/dlib/byte_orderer')
-rw-r--r--ml/dlib/dlib/byte_orderer/byte_orderer_kernel_1.h176
-rw-r--r--ml/dlib/dlib/byte_orderer/byte_orderer_kernel_abstract.h149
2 files changed, 325 insertions, 0 deletions
diff --git a/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_1.h b/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_1.h
new file mode 100644
index 000000000..9f8e8342f
--- /dev/null
+++ b/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_1.h
@@ -0,0 +1,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_
+
diff --git a/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_abstract.h b/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_abstract.h
new file mode 100644
index 000000000..f7ea15103
--- /dev/null
+++ b/ml/dlib/dlib/byte_orderer/byte_orderer_kernel_abstract.h
@@ -0,0 +1,149 @@
+// Copyright (C) 2006 Davis E. King (davis@dlib.net)
+// License: Boost Software License See LICENSE.txt for the full license.
+#undef DLIB_BYTE_ORDEREr_ABSTRACT_
+#ifdef DLIB_BYTE_ORDEREr_ABSTRACT_
+
+#include "../algs.h"
+
+namespace dlib
+{
+
+ class byte_orderer
+ {
+ /*!
+ INITIAL VALUE
+ This object has no state.
+
+ WHAT THIS OBJECT REPRESENTS
+ This object simply provides a mechanism to convert data from a
+ host machine's own byte ordering to big or little endian and to
+ also do the reverse.
+
+ It also provides a pair of functions to convert to/from network byte
+ order where network byte order is big endian byte order. This pair of
+ functions does the exact same thing as the host_to_big() and big_to_host()
+ functions and is provided simply so that client code can use the most
+ self documenting name appropriate.
+
+ Also note that this object is capable of correctly flipping the contents
+ of arrays when the arrays are declared on the stack. e.g. You can
+ say things like:
+ int array[10];
+ bo.host_to_network(array);
+ !*/
+
+ public:
+
+ byte_orderer (
+ );
+ /*!
+ ensures
+ - #*this is properly initialized
+ throws
+ - std::bad_alloc
+ !*/
+
+ virtual ~byte_orderer (
+ );
+ /*!
+ ensures
+ - any resources associated with *this have been released
+ !*/
+
+ bool host_is_big_endian (
+ ) const;
+ /*!
+ ensures
+ - if (the host computer is a big endian machine) then
+ - returns true
+ - else
+ - returns false
+ !*/
+
+ bool host_is_little_endian (
+ ) const;
+ /*!
+ ensures
+ - if (the host computer is a little endian machine) then
+ - returns true
+ - else
+ - returns false
+ !*/
+
+ template <
+ typename T
+ >
+ void host_to_network (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from host byte order
+ to network byte order.
+ !*/
+
+ template <
+ typename T
+ >
+ void network_to_host (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from network byte order
+ to host byte order.
+ !*/
+
+ template <
+ typename T
+ >
+ void host_to_big (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from host byte order
+ to big endian byte order.
+ !*/
+
+ template <
+ typename T
+ >
+ void big_to_host (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from big endian byte order
+ to host byte order.
+ !*/
+
+ template <
+ typename T
+ >
+ void host_to_little (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from host byte order
+ to little endian byte order.
+ !*/
+
+ template <
+ typename T
+ >
+ void little_to_host (
+ T& item
+ ) const;
+ /*!
+ ensures
+ - #item == the value of item converted from little endian byte order
+ to host byte order.
+ !*/
+
+ };
+}
+
+#endif // DLIB_BYTE_ORDEREr_ABSTRACT_
+