Add an overload of endian_reverse_inplace for arrays

This commit is contained in:
Peter Dimov
2020-05-05 20:06:21 +03:00
parent a6e1da6a79
commit 62091e5d0c
3 changed files with 83 additions and 0 deletions

View File

@ -161,6 +161,17 @@ template<class T> inline
x = endian_reverse( x );
}
// endian_reverse_inplace for arrays
template<class T, std::size_t N>
inline void endian_reverse_inplace( T (&x)[ N ] )
{
for( std::size_t i = 0; i < N; ++i )
{
endian_reverse_inplace( x[i] );
}
}
} // namespace endian
} // namespace boost

View File

@ -96,7 +96,12 @@ run-ni endian_hpp_test.cpp ;
run order_test.cpp ;
run endian_reverse_test2.cpp ;
run-ni endian_reverse_test2.cpp ;
run is_scoped_enum_test.cpp ;
run endian_reverse_test3.cpp ;
run-ni endian_reverse_test3.cpp ;
run endian_reverse_test4.cpp ;
run-ni endian_reverse_test4.cpp ;

View File

@ -0,0 +1,67 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <cstddef>
template<class T> void test_reverse_inplace( T x )
{
using boost::endian::endian_reverse_inplace;
T x2( x );
endian_reverse_inplace( x2 );
endian_reverse_inplace( x2 );
BOOST_TEST( x == x2 );
}
struct X
{
int v1_;
int v2_;
};
inline bool operator==( X const& x1, X const& x2 )
{
return x1.v1_ == x2.v1_ && x1.v2_ == x2.v2_;
}
inline void endian_reverse_inplace( X & x )
{
using boost::endian::endian_reverse_inplace;
endian_reverse_inplace( x.v1_ );
endian_reverse_inplace( x.v2_ );
}
struct Y
{
X x1_;
X x2_[ 2 ];
};
inline bool operator==( Y const& y1, Y const& y2 )
{
return y1.x1_ == y2.x1_ && y1.x2_[0] == y2.x2_[0] && y1.x2_[1] == y2.x2_[1];
}
inline void endian_reverse_inplace( Y & y )
{
using boost::endian::endian_reverse_inplace;
endian_reverse_inplace( y.x1_ );
endian_reverse_inplace( y.x2_ );
}
int main()
{
Y y = { { 1, 2 }, { { 3, 4 }, { 5, 6 } } };
test_reverse_inplace( y );
return boost::report_errors();
}