From 28fd6ef4b0d08683d1aa2a42a8e515a9c75d9c1d Mon Sep 17 00:00:00 2001
From: Beman
@@ -375,19 +376,21 @@ usual operations on integers are supplied.
<boost/endian/conversion.hpp>
+ <boost/endian/buffers.hpp>
<boost/endian/arithmetic.hpp>
Last revised: -19 November, 2014
+05 December, 2014© Copyright Beman Dawes, 2006-2009, 2013
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/doc/bikeshed.txt b/doc/bikeshed.txt index 891784d..1ef2309 100644 --- a/doc/bikeshed.txt +++ b/doc/bikeshed.txt @@ -3,12 +3,17 @@ Conversion function naming bikeshed return-by-value modify-argument ------------------ --------------- -reverse_endianness reverse_endianness_in_place <------- +reverse_endianness reverse_endianness_in_place " reverse_endianness_arg +endian_reverse endian_reverse_in_place + " endian_reverse_here + " endian_reverse_this + " endian_reverse_self + " endian_reverse_arg + " endian_reverse_in <------ reverse reverse_in_place reverse_endian reverse_endian_in_place -endian_reverse endian_reverse_in_place swap_endianness swap_endianness_in_place swap_endian swap_endian_in_place @@ -31,7 +36,16 @@ Key points: the various conditional functions instead. So explicitness is more important than brevity. +big_to_native native_to_big reverse_unless_native_big little_to_native native_to_little reverse_unless_native_little + reverse_if_not_big + reverse_if_little + reverse_unless_big +big_to_host host_to_big reverse_unless_host_big + reverse_if_host_little +be_to_ne ne_to_be + +from_big, to_big, reverse_unless_big merriam-webster.com/dictionary @@ -48,4 +62,7 @@ flip (verb) : to cause (something) to turn or turn over quickly : to move (something) with a quick light movement + + + \ No newline at end of file diff --git a/doc/buffers.html b/doc/buffers.html index c747a54..f6dbb33 100644 --- a/doc/buffers.html +++ b/doc/buffers.html @@ -1,4 +1,4 @@ - + @@ -64,26 +64,34 @@
<boost/endian/conversion.hpp>
+ <boost/endian/buffers.hpp>
<boost/endian/arithmetic.hpp>
Header boost/endian/buffers.hpp -provides portable integer and floating-point binary buffer types with control over -byte order, value type, size, and alignment independent of the native computer -architecture. Typedefs provide easy-to-use names -for common configurations. Use cases almost always involve I/O, either via files or -network connections.
-Although data portability is the primary motivation, these byte-holders may -also be used to reduce memory use, file size, or network activity since they -also -provide binary numeric sizes not otherwise available.
-The byte order of arithmetic types is traditionally called endianness. See the +
The internal byte order of arithmetic types is traditionally called endianness. See the Wikipedia for a full exploration of endianness, including definitions of big endian and little endian.
+Header boost/endian/buffers.hpp
+provides endian_buffer
, a portable endian integer and floating-point binary buffer
+class template with control over
+byte order, value type, size, and alignment independent of the platform's native
+endianness. Typedefs provide easy-to-use names
+for common configurations.
Use cases primarily involve data portability, either via files or network +connections, but these byte-holders may +also be used to reduce memory use, file size, or network activity since they + +provide binary numeric sizes not otherwise available.
+Class endian_buffer
is aimed at users who wish
+explicit control over when endianness conversions occur. It also serves as the
+base class for the endian_arithmetic
+class template, which is aimed at users who wish fully automatic endianness
+conversion and direct support for all normal arithmetic operations.
The endian_example.cpp program writes a binary file containing four byte big-endian and little-endian integers:
@@ -360,13 +368,11 @@ usual operations on integers are supplied. public: typedef T value_type; - // if BOOST_ENDIAN_FORCE_PODNESS is defined && C++11 POD's are not - // available then these two constructors will not be present endian_buffer() noexcept = default; explicit endian_buffer(T v) noexcept; endian_buffer& operator=(T v) noexcept; - value_type value() const noexcept; + value_type value() const noexcept; const char* data() const noexcept; protected: implementaton-defined endian_value; // for exposition only @@ -489,35 +495,49 @@ requirements imposed by theNbits
and Align
template
parameters. The CHAR_BIT
macro is defined in <climits>
.
The only value of CHAR_BIT
that
-is required to be supported is 8.
+is required to be supported is 8.
+Template parameter T
is
+required to be a standard integer type (C++std, 3.9.1) and
+sizeof(T)*CHAR_BIT
is required to be
+greater or equal to Nbits
.
endian_buffer() = default; // C++03: endian(){}
-endian_buffer() noexcept = default;
-Effects: Constructs an object of type
endian_buffer<Order, T, Nbits, Align>
.
explicit endian_buffer(T v);
+explicit endian_buffer(T v) noexcept;
-Effects: Constructs an object of type
-endian_buffer<Order, T, Nbits, Align>
.Postcondition:
+value() == v
.Postcondition:
+value() == v & mask
, wheremask
+is a constant of typevalue_type
withNbits
low-order +bits set to one.Remarks: If
Align
isalign::yes
then +endianness conversion if required is performed by+boost::endian::reverse_endianness
.
endian_buffer& operator=(T v);
+endian_buffer& operator=(T v) noexcept;
--Postcondition:
+value() == v
.Postcondition:
value() == v & mask
, wheremask
+ is a constant of typevalue_type
withNbits
+ low-order bits set to one..Returns:
+*this
.Remarks: If
Align
isalign::yes
then +endianness conversion if required is performed by+boost::endian::reverse_endianness
.
value_type value() const;
+value_type value() const noexcept;
--Returns: The current value stored in
+*this
, converted to -value_type
.Returns:
+endian_value
, converted tovalue_type
+if necessary and having the endianness of the native platform.Remarks: If
Align
isalign::yes
then +endianness conversion if required is performed by+boost::endian::reverse_endianness
.
const char* data() const;
+const char* data() const noexcept;
-Returns: A pointer to the first byte of the endian binary value stored -in
+*this
.Returns: A pointer to the first byte of
endian_value
.
Last revised: -05 December, 2014
+06 December, 2014© Copyright Beman Dawes, 2006-2009, 2013
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/doc/conversion.html b/doc/conversion.html index 4054e00..f2a309b 100644 --- a/doc/conversion.html +++ b/doc/conversion.html @@ -1,4 +1,4 @@ - + @@ -45,7 +45,10 @@ ReferenceReversible
Header boost/endian/conversion.hpp
provides byte order reversal and conversion functions that convert objects of
-the multi-byte built-in
+the built-in
integer types, and also types float
and double,
between native, big, or little endian byte
ordering. User defined types are also supported.
<boost/endian/conversion.hpp>
Synopsis#define BOOST_ENDIAN_INTRINSIC_MSG "message describing presence or absence of intrinsics" @@ -92,16 +97,16 @@ namespace endian }; // reverse byte order (i.e. endianness) - int8_t reverse_value(int8_t x) noexcept; - int16_t reverse_value(int16_t x) noexcept; - int32_t reverse_value(int32_t x) noexcept; - int64_t reverse_value(int64_t x) noexcept; - uint8_t reverse_value(uint8_t x) noexcept; - uint16_t reverse_value(uint16_t x) noexcept; - uint32_t reverse_value(uint32_t x) noexcept; - uint64_t reverse_value(uint64_t x) noexcept; - float reverse_value(float x) noexcept; - double reverse_value(double x) noexcept; + int8_t reverse_endianness(int8_t x) noexcept; + int16_t reverse_endianness(int16_t x) noexcept; + int32_t reverse_endianness(int32_t x) noexcept; + int64_t reverse_endianness(int64_t x) noexcept; + uint8_t reverse_endianness(uint8_t x) noexcept; + uint16_t reverse_endianness(uint16_t x) noexcept; + uint32_t reverse_endianness(uint32_t x) noexcept; + uint64_t reverse_endianness(uint64_t x) noexcept; + float reverse_endianness(float x) noexcept; + double reverse_endianness(double x) noexcept; template <class Value> void reverse(Value& x) noexcept; @@ -118,30 +123,12 @@ namespace endian template <class Reversible> void little_endian(Reversible& x) noexcept; - // synonyms, based on names popularized by BSD (e.g. OS X, Linux) endian.h - // "h" for "host" (i.e. native), "be" for "big endian", - // "le" for "little endian", "m" for "modify" in place - template <class T> T bswap(T x) noexcept {return reverse_value(x);} - template <class T> T htobe(T host) noexcept {return big_endian_value(host);} - template <class T> T htole(T host) noexcept {return little_endian_value(host);} - template <class T> T betoh(T big) noexcept {return big_endian_value(big);} - template <class T> T letoh(T little) noexcept {return little_endian_value(little);} - - template <class T> void bswapm(T& x) noexcept {reverse(x);} - template <class T> void htobem(T& host) noexcept {big_endian(host);} - template <class T> void htole(mT& host noexcept) {little_endian(host);} - template <class T> void betohm(T& big) noexcept {big_endian(big);} - template <class T> void letohm(T& little) noexcept {little_endian(little);} - // generic byte order conversion template <order From, order To, class ReversibleValue> ReversibleValue convert_value(ReversibleValue from) noexcept; template <order From, order To, class Reversible> void convert(Reversible& x) noexcept; - // runtime effective byte order determination - order effective_order(order x) noexcept; - // runtime byte-order conversion template <class ReversibleValue> ReversibleValue convert_value(ReversibleValue from, @@ -152,67 +139,83 @@ namespace endian } // namespace endian } // namespace boost-
The implementation-defined
text above is either
-big
or little
according to the endianness of the
-platform.
The implementation is required to define the enum class order
+constant native
as
+big
on big endian platforms and little
on little
+endian platforms.
The template definitions in this header refer to named -requirements whose details are set out in this section. User defined types may -be used in the function templates in this header only if they meet the -function's template parameter requirements.
-ReversibleValue
is an object type to be
-supplied by a C++ program instantiating a template; x
is a value of
-type (possibly const
) ReversibleValue
.
Reversible
requirementsThis subsection describes names that are used to specify constraints on +template arguments.
+User-defined types may be used in the function templates in this header only +if they meet these requirements.
+Reversible
is an object type to be supplied by a C++ program
+instantiating a template.
Expression | -Return type | -Requirement | +Meaning of x | +Requirements |
-
|
+
-
|
+
- The returned value is the value of |
-
Reversible
is an object type to be
-supplied by a C++ program instantiating a template; x
is a
-modifiable lvalue of type Reversible
.
Expression | -Post-condition | +|
-
|
+
+ x is a
+modifiable lvalue of type Reversible . |
- The order of the constituent bytes of |
See
-udt_conversion_example.cpp for an example of a UDT that can used in the
-big_endian
,
-little_endian
, and
-convert
function templates.
+ ++ +[Note: A user-defined type meets these requirements by defining a +non-member function in the same namespace as the UDT itself so that the function +can be found by argument dependent lookup (ADL). —end note]
+ +
This subsection describes requirements on the endian library's own +implementation.
+ + The endianness conversion function templates that return values are
+required to perform reversal of endianness if needed by making an unqualified
+call to reverse_endianness(argument)
, as described in the
+preceding table.
The endianness conversion function templates that modify their argument in
+place are required to perform reversal of endianness if needed by making an
+unqualified call to reverse_endianness_in_place(argument)
,
+as described in the preceding table.
See +udt_conversion_example.cpp for an example user-defined type.
+int8_t reverse_value(int8_t x) noexcept; -int16_t reverse_value(int16_t x) noexcept; -int32_t reverse_value(int32_t x) noexcept; -int64_t reverse_value(int64_t x) noexcept; -uint8_t reverse_value(uint8_t x) noexcept; -uint16_t reverse_value(uint16_t x) noexcept; -uint32_t reverse_value(uint32_t x) noexcept; -uint64_t reverse_value(uint64_t x) noexcept; -float reverse_value(float x) noexcept; -double reverse_value(double x) noexcept;+
int8_t reverse_endianness(int8_t x) noexcept; +int16_t reverse_endianness(int16_t x) noexcept; +int32_t reverse_endianness(int32_t x) noexcept; +int64_t reverse_endianness(int64_t x) noexcept; +uint8_t reverse_endianness(uint8_t x) noexcept; +uint16_t reverse_endianness(uint16_t x) noexcept; +uint32_t reverse_endianness(uint32_t x) noexcept; +uint64_t reverse_endianness(uint64_t x) noexcept; +float reverse_endianness(float x) noexcept; +double reverse_endianness(double x) noexcept;
Returns:
@@ -229,7 +232,7 @@ double reverse_value(double x) noexcept; void big_endian(Reversible& x) noexcept;x
, with the order of its constituent bytes reversed.-Returns (first form):
+ endian, otherwisex
if the native byte order is big - endian, otherwisereverse_value(x)
.reverse_endianness(x)
.Effects (second form): None if the native byte order is big endian, otherwise
reverse(x)
.Example:
@@ -245,7 +248,7 @@ big_endian(x); // reverses the byte order of x, unless void little_endian(Reversible& x) noexcept;Returns (first form):
+ endian, otherwisex
if the native byte order is little - endian, otherwisereverse_value(x)
.reverse_endianness(x)
.Effects (second form): None if the native byte order is little endian, otherwise
reverse(x)
.Example:
@@ -268,7 +271,7 @@ int32_t y(little_endian(x));order::little
that represents the actual native byte order.Returns (first form):
+ reverse_endianness(from).from
ifFrom
andTo
have the same effective order, otherwise- reverse_value(from)
.Effects (second form): None if
From
andTo
have the same effective order, otherwisereverse(x)
.Example:
@@ -278,18 +281,12 @@ int32_t y(little_endian(x)); convert<order::big, order::native>(x); // more generic equivalent of big_endian(x);order effective_order(order x) noexcept; ---Returns:
x
ifx != order::native
, otherwise theorder
constant for the actual native byte order.Example:
effective_order(order::big); // returns order::big -effective_order(order::little); // returns order::little -effective_order(order::native); // returns order::big if the native order - // is big-endian, otherwise order::littletemplate <class ReversibleValue> +template <class ReversibleValue> ReversibleValue convert_value(ReversibleValue from, order from_order, order to_order) noexcept; template <class Reversible> void convert(Reversible& x, - order from_order, order to_order) noexcept;Returns (first form):
+ order from_order, order to_order) noexcept;from
ifeffect_order(from_order) == effective_order(to_order)
, otherwisereverse_value(from)
.Returns (first form):
from
ifeffect_order(from_order) == effective_order(to_order)
, otherwisereverse_endianness(from)
.Effects (second form): None if
effect_order(from_order) == effective_order(to_order)
, otherwisereverse(x)
.Example:
@@ -304,13 +301,13 @@ convert(x, some_order, order::native); // convert to native byte order if needeSee the Endian home page FAQ for a library-wide FAQ.
-Why are the template versions of
reverse()
andreverse_value()
+Why are the template versions of
reverse()
andreverse_endianness()
in a detail namespace?@@ -98,7 +99,7 @@ might used inadvertently or inappropriately. The impact of adding an endian_buffThey are unsafe for general use. Consider reversing -the bytes of a
@@ -330,11 +327,11 @@ provided.std::pair
as a whole - the bytes fromfirst
+the bytes of astd::pair
as a whole - the bytes fromfirst
would end up insecond
and visa versa, and this is totally wrong!Acknowledgements
Tomas Puverle was instrumental in identifying and articulating the need to support endian conversion as separate from endian integer types. Phil Endecott suggested the form of the value returning signatures. Vicente Botet and other reviewers suggested supporting floating point types and user defined types. General reverse template implementation approach using std::reverse suggested by Mathias Gaunard. Portable implementation approach for 16, 32, and 64-bit integers suggested by tymofey, with avoidance of undefined behavior as suggested by Giovanni Piero Deretta, and a further refinement suggested by Pyry Jahkola. Intrinsic builtins implementation approach for 16, 32, and 64-bit integers suggested by several reviewers, and by David Stone, who provided his Boost licensed macro implementation that became the starting point for boost/endian/detail/intrinsic.hpp. -Pierre Talbot provided the
int8_t reverse_value()
and templated +Pierre Talbot provided theint8_t reverse_endianness()
and templatedreverse()
implementations.
Last revised: -19 November, 2014
+07 December, 2014© Copyright Beman Dawes, 2011, 2013
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/doc/index.html b/doc/index.html index 607947b..d6ebcfe 100644 --- a/doc/index.html +++ b/doc/index.html @@ -58,6 +58,7 @@@@ -199,8 +200,8 @@ application needs. <boost/endian/conversion.hpp>
+ <boost/endian/buffers.hpp>
<boost/endian/arithmetic.hpp>These problems are eliminated by defining S like this:
@@ -677,7 +678,7 @@ and 16, 32, and 64-bit aligned integers.
struct S {
- big_uint16_t a;
- big_uint32_t b;
+ big_uint16_ut a;
+ big_uint32_ut b;
};- order::native
is now a synonym fororder::big
ororder::little
according to the endianness of the platform, as requested. This reduces the number of template specializations required.reverse_value()
overloads forint8_t
and+
and class templatereverse_endianness()
overloads forint8_t
anduint8_t
have been added for improved generality. (Pierre Talbot)Overloads of @@ -701,7 +702,7 @@ Blechmann, Tim Moore, tymofey, Tomas Puverle, Vincente Botet, Yuval Ronen and Vitaly Budovski,.reverse()
have been replaced with a singlereverse()
template. (Pierre Talbot)
Last revised: -19 November, 2014
+05 December, 2014© Copyright Beman Dawes, 2011, 2013
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/doc/mini_review_topics.html b/doc/mini_review_topics.html index e182842..286033b 100644 --- a/doc/mini_review_topics.html +++ b/doc/mini_review_topics.html @@ -71,14 +71,15 @@ the primary use case.Support for user defined types (UDTs) is desirable, and should be provided where there would be no conflict with the other concerns.
-Done. Need docs.
+Done. See conversion requirements.
There is some concern that endian integer/float arithmetic operations might used inadvertently or inappropriately. The impact of adding an endian_buffer class without arithmetic operations should be investigated.
Done. The endian types have been decomposed into class template
- endian_buffer
and class templateendian_arithmetic
. Class + endian_buffer+ endian_arithmetic
. Classendian_buffer
is a public base class forendian_arithmetic
, and can also be used by users as a stand-alone class.
Last revised: -27 November, 2014
+05 December, 2014© Copyright Beman Dawes, 2014
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt