Kaydet (Commit) 37b4b3ea authored tarafından Thorsten Behrens's avatar Thorsten Behrens

provide uno::Sequence with ostream output operator

Alongside Anys and other UNO data types, you can now also stream
out Sequences, e.g. in SAL_INFO or SAL_DEBUG statements.

Example code:

        uno::Sequence<sal_Int8> aBuffer...
        SAL_DEBUG("my buffer: " << aBuffer);

Would yield:

        debug:<pid>: my buffer: 0xb6, 0x61, 0xa8, ...

Change-Id: I03b0789372c44a4dd057625824862f43b1b8dfdc
Reviewed-on: https://gerrit.libreoffice.org/47779Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
Tested-by: 's avatarThorsten Behrens <Thorsten.Behrens@CIB.de>
üst 53ef918a
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#if defined LIBO_INTERNAL_ONLY
# include <type_traits>
# include <ostream>
#endif
#include "osl/interlck.h" #include "osl/interlck.h"
#include "com/sun/star/uno/Sequence.h" #include "com/sun/star/uno/Sequence.h"
...@@ -200,6 +204,81 @@ inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence( ...@@ -200,6 +204,81 @@ inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence ); return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
} }
#if defined LIBO_INTERNAL_ONLY
/// @cond INTERNAL
namespace uno_detail {
template< typename value_t, typename charT, typename traits >
void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
{
// for integral types, use hex notation
auto const flags = os.setf(std::ios_base::hex);
for(sal_Int32 i=0; i<nLen-1; ++i)
os << "0x" << *pAry++ << ", ";
if( nLen > 1 )
os << "0x" << *pAry++;
os.setf(flags);
}
template< typename value_t, typename charT, typename traits >
void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
{
// every other type: rely on their own ostream operator<<
for(sal_Int32 i=0; i<nLen-1; ++i)
os << *pAry++ << ", ";
if( nLen > 1 )
os << *pAry++;
}
template< typename value_t, typename charT, typename traits >
void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
{
// special case bytes - ostream operator<< outputs those as char
// values, but we need raw ints here
auto const flags = os.setf(std::ios_base::hex);
for(sal_Int32 i=0; i<nLen-1; ++i)
os << "0x" << (0xFF & +*pAry++) << ", ";
if( nLen > 1 )
os << "0x" << (0xFF & +*pAry++);
os.setf(flags);
}
template<class B>
struct negation : std::integral_constant<bool, !bool(B::value)> { };
}
/**
Support for Sequence in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO
macros, for example).
@since LibreOffice 6.1
*/
template< typename value_t, typename charT, typename traits >
inline typename std::enable_if<uno_detail::negation<std::is_same<sal_Int8, value_t>>::value, std::basic_ostream<charT, traits>>::type &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence < value_t > &v)
{
const value_t *pAry = v.getConstArray();
sal_Int32 nLen = v.getLength();
uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
return os;
}
template< typename value_t, typename charT, typename traits >
inline typename std::enable_if<std::is_same<sal_Int8, value_t>::value, std::basic_ostream<charT, traits>>::type &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence < value_t > &v)
{
// specialisation for signed bytes
const sal_Int8 *pAry = v.getConstArray();
sal_Int32 nLen = v.getLength();
uno_detail::sequence_output_bytes(os, pAry, nLen);
return os;
}
/// @endcond
#endif
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment