summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorsten Behrens <Thorsten.Behrens@CIB.de>2018-01-11 23:14:11 +0100
committerThorsten Behrens <Thorsten.Behrens@CIB.de>2018-01-13 02:05:31 +0100
commit37b4b3ea042ab380106f4eca001b605d79980296 (patch)
tree6c75ef53d069fd3cc476756f1aabcc6fa0b009f5
parent53ef918a6839c8d587dec1bb635e6b39397c53d0 (diff)
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/47779 Reviewed-by: Stephan Bergmann <sbergman@redhat.com> Tested-by: Thorsten Behrens <Thorsten.Behrens@CIB.de>
-rw-r--r--include/com/sun/star/uno/Sequence.hxx79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/com/sun/star/uno/Sequence.hxx b/include/com/sun/star/uno/Sequence.hxx
index 7fe9f0bda64f..64f84ffc7bc3 100644
--- a/include/com/sun/star/uno/Sequence.hxx
+++ b/include/com/sun/star/uno/Sequence.hxx
@@ -23,6 +23,10 @@
#include <cassert>
#include <cstddef>
+#if defined LIBO_INTERNAL_ONLY
+# include <type_traits>
+# include <ostream>
+#endif
#include "osl/interlck.h"
#include "com/sun/star/uno/Sequence.h"
@@ -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 );
}
+#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
+
}
}
}