From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- include/o3tl/any.hxx | 325 +++++++++++++++++++++++ include/o3tl/char16_t2wchar_t.hxx | 59 ++++ include/o3tl/cow_wrapper.hxx | 387 +++++++++++++++++++++++++++ include/o3tl/cppunittraitshelper.hxx | 28 ++ include/o3tl/deleter.hxx | 68 +++++ include/o3tl/enumarray.hxx | 140 ++++++++++ include/o3tl/enumrange.hxx | 87 ++++++ include/o3tl/float_int_conversion.hxx | 72 +++++ include/o3tl/functional.hxx | 63 +++++ include/o3tl/hash_combine.hxx | 55 ++++ include/o3tl/intcmp.hxx | 132 +++++++++ include/o3tl/lazy_update.hxx | 89 +++++++ include/o3tl/lru_map.hxx | 299 +++++++++++++++++++++ include/o3tl/make_shared.hxx | 46 ++++ include/o3tl/numeric.hxx | 28 ++ include/o3tl/runtimetooustring.hxx | 47 ++++ include/o3tl/safeCoInitUninit.hxx | 58 ++++ include/o3tl/safeint.hxx | 236 ++++++++++++++++ include/o3tl/sorted_vector.hxx | 421 +++++++++++++++++++++++++++++ include/o3tl/span.hxx | 100 +++++++ include/o3tl/sprintf.hxx | 41 +++ include/o3tl/string_view.hxx | 487 ++++++++++++++++++++++++++++++++++ include/o3tl/strong_int.hxx | 156 +++++++++++ include/o3tl/temporary.hxx | 27 ++ include/o3tl/typed_flags_set.hxx | 325 +++++++++++++++++++++++ include/o3tl/underlyingenumvalue.hxx | 29 ++ include/o3tl/unit_conversion.hxx | 261 ++++++++++++++++++ include/o3tl/unreachable.hxx | 48 ++++ include/o3tl/unsafe_downcast.hxx | 35 +++ include/o3tl/vector_pool.hxx | 123 +++++++++ include/o3tl/vector_utils.hxx | 29 ++ 31 files changed, 4301 insertions(+) create mode 100644 include/o3tl/any.hxx create mode 100644 include/o3tl/char16_t2wchar_t.hxx create mode 100644 include/o3tl/cow_wrapper.hxx create mode 100644 include/o3tl/cppunittraitshelper.hxx create mode 100644 include/o3tl/deleter.hxx create mode 100644 include/o3tl/enumarray.hxx create mode 100644 include/o3tl/enumrange.hxx create mode 100644 include/o3tl/float_int_conversion.hxx create mode 100644 include/o3tl/functional.hxx create mode 100644 include/o3tl/hash_combine.hxx create mode 100644 include/o3tl/intcmp.hxx create mode 100644 include/o3tl/lazy_update.hxx create mode 100644 include/o3tl/lru_map.hxx create mode 100644 include/o3tl/make_shared.hxx create mode 100644 include/o3tl/numeric.hxx create mode 100644 include/o3tl/runtimetooustring.hxx create mode 100644 include/o3tl/safeCoInitUninit.hxx create mode 100644 include/o3tl/safeint.hxx create mode 100644 include/o3tl/sorted_vector.hxx create mode 100644 include/o3tl/span.hxx create mode 100644 include/o3tl/sprintf.hxx create mode 100644 include/o3tl/string_view.hxx create mode 100644 include/o3tl/strong_int.hxx create mode 100644 include/o3tl/temporary.hxx create mode 100644 include/o3tl/typed_flags_set.hxx create mode 100644 include/o3tl/underlyingenumvalue.hxx create mode 100644 include/o3tl/unit_conversion.hxx create mode 100644 include/o3tl/unreachable.hxx create mode 100644 include/o3tl/unsafe_downcast.hxx create mode 100644 include/o3tl/vector_pool.hxx create mode 100644 include/o3tl/vector_utils.hxx (limited to 'include/o3tl') diff --git a/include/o3tl/any.hxx b/include/o3tl/any.hxx new file mode 100644 index 000000000..0acccff64 --- /dev/null +++ b/include/o3tl/any.hxx @@ -0,0 +1,325 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_ANY_HXX +#define INCLUDED_O3TL_ANY_HXX + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +// Some functionality related to css::uno::Any that would ideally be part of +// , but (for now) cannot be for some reason. + +namespace o3tl { + +namespace detail { + +struct Void {}; + +template struct Optional { using type = T const *; }; +template<> struct Optional { using type = std::optional; }; +template<> struct Optional { using type = std::optional; }; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template<> struct Optional { + using type = std::optional; +}; +template struct Optional> { + using type = std::optional const>; +}; +template<> struct Optional> { + using type = css::uno::Reference const *; +}; + +template struct IsDerivedReference: std::false_type {}; +template struct IsDerivedReference>: + std::true_type +{}; +template<> struct IsDerivedReference>: + std::false_type +{}; + +template struct IsUnoSequenceType: std::false_type {}; +template struct IsUnoSequenceType>: + std::true_type +{}; + +template inline std::optional tryGetConverted( + css::uno::Any const & any) +{ + T v; + return (any >>= v) + ? std::optional(std::move(v)) : std::optional(); +} + +} + +/** Try to access the value of a specific type stored in an Any. + + In trying to obtain a value, the same set of conversions as supported by + ">>=" are considered. + + The returned object is a proxy. Proxies can be either positive or negative. + Each proxy can be contextually converted to bool, yielding true iff the + proxy is positive. For a positive proxy P representing a value of requested + type T, for any T other than void, the expression *P yields that value of + type T. (Technically, the proxy is either a plain pointer or a + std::optional, depending on whether a plain pointer into the given Any can + be returned for the specified type.) + + @attention A proxy returned from this function must not outlive the + corresponding Any passed into this function (as it may constitute a pointer + into the Any's internals). That is the reason why this function is + restricted to lvalue arguments (i.e., to non-temporary Any objects), to + avoid misuses like + @code + css::uno::Any f(); + + if (auto p = o3tl::tryAccess(f())) { + return p->Name; + } + @endcode + + @note Ideally this would be a public member function of css::uno::Any (at + least conditional on LIBO_INTERNAL_ONLY, as it requires C++11). However, as + std::optional (which would be needed to implement the proxies) is only + available since C++14, we need to use std::optional for now. But To not + make every entity that includes depend on + boost_headers, keep this here for now. + + @tparam T the C++ representation of a UNO type that can be contained in a + UNO ANY (i.e., any UNO type other than ANY itself). The legacy C++ + representations sal_Bool, cppu::UnoVoidType, cppu::UnoUnsignedShortType, + cppu::UnoCharType, and cppu::UnoSequenceType are not supported. Must be a + complete type or void. + + @param any an Any value. + + @return a positive proxy for the value of the specified type obtained from + the given Any, or a negative proxy if no such value can be obtained. +*/ +template inline +typename std::enable_if< + !(detail::IsDerivedReference::value + || detail::IsUnoSequenceType::value + || std::is_base_of::value), + typename detail::Optional::type>::type +tryAccess(css::uno::Any const & any) { + // CHAR, STRING, TYPE, sequence types, enum types, struct types, exception + // types, and com.sun.star.uno.XInterface interface type: + return cppu::UnoType::get().isAssignableFrom(any.getValueType()) + ? static_cast(any.getValue()) : nullptr; +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return any.hasValue() + ? std::optional() + : std::optional(detail::Void()); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> inline detail::Optional::type tryAccess( + css::uno::Any const & any) +{ + return detail::tryGetConverted(any); +} + +template<> detail::Optional::type tryAccess( + css::uno::Any const &) = delete; + +template<> detail::Optional::type tryAccess( + css::uno::Any const &) = delete; + +/* + +// Already prevented by std::is_base_of requiring T to +// be complete: + +template<> detail::Optional::type +tryAccess(css::uno::Any const &) = delete; + +template<> detail::Optional::type +tryAccess(css::uno::Any const &) = delete; + +template<> detail::Optional::type +tryAccess(css::uno::Any const &) = delete; + +*/ + +template inline +typename std::enable_if< + detail::IsDerivedReference::value, + typename detail::Optional::type>::type +tryAccess(css::uno::Any const & any) { + return detail::tryGetConverted(any); +} + +template typename detail::Optional::type tryAccess( + css::uno::Any const volatile &&) = delete; + +/** Access the value of a specific type stored in an Any, throwing an exception + on failure. + + @attention A proxy returned from this function must not outlive the + corresponding Any passed into this function (as it may constitute a pointer + into the Any's internals). However, unlike with tryAccess, the benefit of + allowing this function to operate on temporaries appears to outweigh its + dangers. + + @note Ideally this would be a public member function of css::uno::Any. See + tryAccess for details. + + @tparam T the C++ representation of a UNO type that can be contained in a + UNO ANY. See tryAccess for details. + + @param any an Any value. + + @return a positive proxy for the value of the specified type obtained from + the given Any. See tryAccess for details. + + @throws css::uno::RuntimeException when a value of the requested type + cannot be obtained. +*/ +template inline typename detail::Optional::type doAccess( + css::uno::Any const & any) +{ + auto opt = tryAccess(any); + if (!opt) { + throw css::uno::RuntimeException( + OUString( + cppu_Any_extraction_failure_msg( + &any, cppu::UnoType::get().getTypeLibType()), + SAL_NO_ACQUIRE)); + } + return opt; +} + +/** Access the value of a specific type stored in an Any, knowing the Any + contains a value of a matching type. + + @attention A proxy returned from this function must not outlive the + corresponding Any passed into this function (as it may constitute a pointer + into the Any's internals). However, unlike with tryAccess, the benefit of + allowing this function to operate on temporaries appears to outweigh its + dangers. + + @note Ideally this would be a public member function of css::uno::Any. See + tryAccess for details. + + @tparam T the C++ representation of a UNO type that can be contained in a + UNO ANY. See tryAccess for details. + + @param any an Any value. + + @return a positive proxy for the value of the specified type obtained from + the given Any. See tryAccess for details. +*/ +template inline typename detail::Optional::type forceAccess( + css::uno::Any const & any) +{ + auto opt = tryAccess(any); + assert(opt); + return opt; +} + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/char16_t2wchar_t.hxx b/include/o3tl/char16_t2wchar_t.hxx new file mode 100644 index 000000000..6ffab02e7 --- /dev/null +++ b/include/o3tl/char16_t2wchar_t.hxx @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX +#define INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX + +#include + +namespace o3tl { + +#if defined _WIN32 +// Helpers for safe conversion between wchar_t and char16_t in MSVC + +static_assert(sizeof(char16_t) == sizeof(wchar_t), + "These helper functions are only applicable to implementations with 16-bit wchar_t"); + +// While other implementations define wchar_t as 32-bit integral value, and mostly use +// char-based UTF-8 string APIs, in MSVC wchar_t is (non-conformant) 16-bit, and Unicode +// support is implemented by Unicode-aware WinAPI functions taking UTF-16 LE strings, +// and also stdlib functions taking those strings. +// +// In LibreOffice, internal string representation is also UTF-16 with system endianness +// (sal_Unicode that is typedef for char16_t); so it is an important implementation concept +// to treat internal strings as directly usable by WinAPI/stdlib functions and vice versa. +// Also, it's important to use safe conversion between unrelated underlying C++ types +// used for MSVC and LibreOffice string storage without plain reinterpret_cast that brings +// risks of masking errors like casting char buffers to wchar_t/char16_t. +// +// Use these helpers for wchar_t (WSTR, WCHAR, OLESTR etc) to char16_t (sal_Unicode) string +// conversions instead of reinterpret-cast in Windows-specific code. +inline wchar_t * toW(char16_t * p) +{ + return reinterpret_cast(p); +} +inline wchar_t const * toW(char16_t const * p) +{ + return reinterpret_cast(p); +} +inline char16_t * toU(wchar_t * p) +{ + return reinterpret_cast(p); +} +inline char16_t const * toU(wchar_t const * p) +{ + return reinterpret_cast(p); +} +#endif + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/cow_wrapper.hxx b/include/o3tl/cow_wrapper.hxx new file mode 100644 index 000000000..cc823f60e --- /dev/null +++ b/include/o3tl/cow_wrapper.hxx @@ -0,0 +1,387 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_COW_WRAPPER_HXX +#define INCLUDED_O3TL_COW_WRAPPER_HXX + +#include + +#include +#include + +namespace o3tl +{ + /** Thread-unsafe refcounting + + This is the default locking policy for cow_wrapper. No + locking/guarding against concurrent access is performed + whatsoever. + */ + struct UnsafeRefCountingPolicy + { + typedef std::size_t ref_count_t; + static void incrementCount( ref_count_t& rCount ) { ++rCount; } + static bool decrementCount( ref_count_t& rCount ) { return --rCount != 0; } + }; + + /** Thread-safe refcounting + + Use this to have the cow_wrapper refcounting mechanisms employ + the thread-safe oslInterlockedCount . + */ + struct ThreadSafeRefCountingPolicy + { + typedef oslInterlockedCount ref_count_t; + static void incrementCount( ref_count_t& rCount ) { osl_atomic_increment(&rCount); } + static bool decrementCount( ref_count_t& rCount ) + { + return osl_atomic_decrement(&rCount) != 0; + } + }; + + /** Copy-on-write wrapper. + + This template provides copy-on-write semantics for the wrapped + type: when copying, the operation is performed shallow, + i.e. different cow_wrapper objects share the same underlying + instance. Only when accessing the underlying object via + non-const methods, a unique copy is provided. + + The type parameter T must satisfy the following + requirements: it must be default-constructible, copyable (it + need not be assignable), and be of non-reference type. Note + that, despite the fact that this template provides access to + the wrapped type via pointer-like methods + (operator->() and operator*()), it does + not work like e.g. the std smart pointer wrappers + (shared_ptr, unique_ptr, etc.). Internally, the cow_wrapper + holds a by-value instance of the wrapped object. This is to + avoid one additional heap allocation, and providing access via + operator->()/operator*() is because + operator.() cannot be overridden. + + Regarding thread safety: this wrapper is not + thread-safe per se, because cow_wrapper has no way of + synchronizing the potentially many different cow_wrapper + instances, that reference a single shared value_type + instance. That said, when passing + ThreadSafeRefCountingPolicy as the + MTPolicy parameter, accessing a thread-safe + pointee through multiple cow_wrapper instances might be + thread-safe, if the individual pointee methods are + thread-safe, including pointee's copy + constructor. Any wrapped object that needs external + synchronisation (e.g. via an external mutex, which arbitrates + access to object methods, and can be held across multiple + object method calls) cannot easily be dealt with in a + thread-safe way, because, as noted, objects are shared behind + the client's back. + + @attention if one wants to use the pimpl idiom together with + cow_wrapper (i.e. put an opaque type into the cow_wrapper), + then all methods in the surrounding class needs to be + non-inline (including destructor, copy constructor + and assignment operator). + + @example +
+class cow_wrapper_client_impl;
+
+class cow_wrapper_client
+{
+public:
+    cow_wrapper_client();
+    cow_wrapper_client( const cow_wrapper_client& );
+    cow_wrapper_client( cow_wrapper_client&& );
+    ~cow_wrapper_client();
+
+    cow_wrapper_client& operator=( const cow_wrapper_client& );
+    cow_wrapper_client& operator=( cow_wrapper_client&& );
+
+    void modify( int nVal );
+    int queryUnmodified() const;
+
+private:
+    o3tl::cow_wrapper< cow_wrapper_client_impl > maImpl;
+};
+        
+ and the implementation file would look like this: +
+class cow_wrapper_client_impl
+{
+public:
+    void setValue( int nVal ) { mnValue = nVal; }
+    int getValue() const { return mnValue; }
+
+private:
+    int mnValue;
+}
+
+cow_wrapper_client::cow_wrapper_client() :
+    maImpl()
+{
+}
+cow_wrapper_client::cow_wrapper_client( const cow_wrapper_client& rSrc ) :
+    maImpl( rSrc.maImpl )
+{
+}
+cow_wrapper_client::cow_wrapper_client( cow_wrapper_client& rSrc ) :
+    maImpl( std::move( rSrc.maImpl ) )
+{
+}
+cow_wrapper_client::~cow_wrapper_client()
+{
+}
+cow_wrapper_client& cow_wrapper_client::operator=( const cow_wrapper_client& rSrc )
+{
+    maImpl = rSrc.maImpl;
+    return *this;
+}
+cow_wrapper_client& cow_wrapper_client::operator=( cow_wrapper_client&& rSrc )
+{
+    maImpl = std::move( rSrc.maImpl );
+    return *this;
+}
+void cow_wrapper_client::modify( int nVal )
+{
+    maImpl->setValue( nVal );
+}
+int cow_wrapper_client::queryUnmodified() const
+{
+    return maImpl->getValue();
+}
+        
+ */ + template class cow_wrapper + { + /** shared value object - gets cloned before cow_wrapper hands + out a non-const reference to it + */ + struct impl_t + { + impl_t(const impl_t&) = delete; + impl_t& operator=(const impl_t&) = delete; + + impl_t() : + m_value(), + m_ref_count(1) + { + } + + explicit impl_t( const T& v ) : + m_value(v), + m_ref_count(1) + { + } + + explicit impl_t( T&& v ) : + m_value(std::move(v)), + m_ref_count(1) + { + } + + T m_value; + typename MTPolicy::ref_count_t m_ref_count; + }; + + void release() + { + if( m_pimpl && !MTPolicy::decrementCount(m_pimpl->m_ref_count) ) + { + delete m_pimpl; + m_pimpl = nullptr; + } + } + + public: + typedef T value_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef MTPolicy mt_policy; + + /** Default-construct wrapped type instance + */ + cow_wrapper() : + m_pimpl( new impl_t() ) + { + } + + /** Copy-construct wrapped type instance from given object + */ + explicit cow_wrapper( const value_type& r ) : + m_pimpl( new impl_t(r) ) + { + } + + /** Move-construct wrapped type instance from given object + */ + explicit cow_wrapper( value_type&& r ) : + m_pimpl( new impl_t(std::move(r)) ) + { + } + + /** Shallow-copy given cow_wrapper + */ + explicit cow_wrapper( const cow_wrapper& rSrc ) : // nothrow + m_pimpl( rSrc.m_pimpl ) + { + MTPolicy::incrementCount( m_pimpl->m_ref_count ); + } + + /** Move-construct and steal rSrc shared resource + */ + explicit cow_wrapper( cow_wrapper&& rSrc ) noexcept : + m_pimpl( rSrc.m_pimpl ) + { + rSrc.m_pimpl = nullptr; + } + + // Only intended to be used by std::optional specialisations + explicit cow_wrapper( std::nullopt_t ) noexcept : + m_pimpl( nullptr ) + { + } + + // Only intended to be used by std::optional specialisations + explicit cow_wrapper( const cow_wrapper& rSrc, std::nullopt_t ) : // nothrow + m_pimpl( rSrc.m_pimpl ) + { + if (m_pimpl) + MTPolicy::incrementCount( m_pimpl->m_ref_count ); + } + + ~cow_wrapper() // nothrow, if ~T does not throw + { + release(); + } + + /// now sharing rSrc cow_wrapper instance with us + cow_wrapper& operator=( const cow_wrapper& rSrc ) // nothrow + { + // this already guards against self-assignment + MTPolicy::incrementCount( rSrc.m_pimpl->m_ref_count ); + + release(); + m_pimpl = rSrc.m_pimpl; + + return *this; + } + + /// stealing rSrc's resource + cow_wrapper& operator=(cow_wrapper&& rSrc) noexcept + { + // self-movement guts ourself, see also 17.6.4.9 + release(); + m_pimpl = rSrc.m_pimpl; + + rSrc.m_pimpl = nullptr; + + return *this; + } + + /// unshare with any other cow_wrapper instance + value_type& make_unique() + { + if( m_pimpl->m_ref_count > 1 ) + { + impl_t* pimpl = new impl_t(m_pimpl->m_value); + release(); + m_pimpl = pimpl; + } + + return m_pimpl->m_value; + } + + /// true, if not shared with any other cow_wrapper instance + bool is_unique() const // nothrow + { + return !m_pimpl || m_pimpl->m_ref_count == 1; + } + + /// return number of shared instances (1 for unique object) + typename MTPolicy::ref_count_t use_count() const // nothrow + { + return m_pimpl ? m_pimpl->m_ref_count : 0; + } + + void swap(cow_wrapper& r) // never throws + { + std::swap(m_pimpl, r.m_pimpl); + } + + pointer operator->() { return &make_unique(); } + value_type& operator*() { return make_unique(); } + const_pointer operator->() const { return &m_pimpl->m_value; } + const value_type& operator*() const { return m_pimpl->m_value; } + + pointer get() { return &make_unique(); } + const_pointer get() const { return &m_pimpl->m_value; } + + /// true, if both cow_wrapper internally share the same object + bool same_object( const cow_wrapper& rOther ) const + { + return rOther.m_pimpl == m_pimpl; + } + + // Only intended to be used by std::optional specialisations + bool empty() const { return m_pimpl == nullptr; } + // Only intended to be used by std::optional specialisations + void set_empty() + { + if (m_pimpl) + { + release(); + m_pimpl = nullptr; + } + } + + private: + impl_t* m_pimpl; + }; + + + template inline bool operator==( const cow_wrapper& a, + const cow_wrapper& b ) + { + return a.same_object(b) || *a == *b; + } + + template inline bool operator!=( const cow_wrapper& a, + const cow_wrapper& b ) + { + return !a.same_object(b) && *a != *b; + } + + template inline bool operator<( const cow_wrapper& a, + const cow_wrapper& b ) + { + return *a < *b; + } + + template inline void swap( cow_wrapper& a, + cow_wrapper& b ) + { + a.swap(b); + } + +} + +#endif /* INCLUDED_O3TL_COW_WRAPPER_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/cppunittraitshelper.hxx b/include/o3tl/cppunittraitshelper.hxx new file mode 100644 index 000000000..2a6fb8836 --- /dev/null +++ b/include/o3tl/cppunittraitshelper.hxx @@ -0,0 +1,28 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_CPPUNITTRAITSHELPER_HXX +#define INCLUDED_O3TL_CPPUNITTRAITSHELPER_HXX + +#include + +#include +#include + +#include + +// ostream << char16_t is deleted since C++20 (but just keep outputting numeric values): +template <> inline std::string CppUnit::assertion_traits::toString(char16_t const& x) +{ + return assertion_traits::toString(std::uint_least16_t(x)); +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/deleter.hxx b/include/o3tl/deleter.hxx new file mode 100644 index 000000000..76d8d5a35 --- /dev/null +++ b/include/o3tl/deleter.hxx @@ -0,0 +1,68 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_DELETER_HXX +#define INCLUDED_O3TL_DELETER_HXX + +#include + +#include + +#if defined(__COVERITY__) +#define suppress_fun_call_w_exception(expr) \ + do \ + { \ + try \ + { \ + expr; \ + } \ + catch (...) \ + { \ + std::terminate(); \ + } \ + } while (false) +#else +#define suppress_fun_call_w_exception(expr) \ + do \ + { \ + expr; \ + } while (false) +#endif + +namespace o3tl +{ +/** To markup std::unique_ptr that coverity warns might throw exceptions + which won't throw in practice, or where std::terminate is + an acceptable response if they do +*/ +template struct default_delete +{ + void operator()(T* p) noexcept { suppress_fun_call_w_exception(delete p); } +}; + +struct free_delete +{ + void operator()(void* p) { std::free(p); } +}; + +template void reset_preserve_ptr_during(uniqueptr& ptr) +{ + // HACK: for the case where the dtor of the obj held by ptr will trigger + // functions which expect ptr to still be set during the dtor. + // e.g. SdrObject::GetBroadcaster() is called during the destructor + // in SdrEdgeObj::Notify(). So delete first, then clear the pointer + delete ptr.get(); + // coverity[leaked_storage] - not a leak, deleted on line above + (void)ptr.release(); +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/enumarray.hxx b/include/o3tl/enumarray.hxx new file mode 100644 index 000000000..a3c09d56b --- /dev/null +++ b/include/o3tl/enumarray.hxx @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_ENUMARRAY_HXX +#define INCLUDED_O3TL_ENUMARRAY_HXX + +#include +#include +#include + +namespace o3tl { + +template +class enumarray_iterator; +template +class enumarray_const_iterator; + +/// +/// This is a container convenience class for arrays indexed by enum values. +/// +/// This assumes that the 'enum class' definition +/// - starts at zero +/// - has no holes in its sequence of values +/// - defines a value called LAST which refers to the greatest constant. +/// +/// \param E the 'enum class' type. +/// \param V the value type to be stored in the array +template +class enumarray final +{ +public: + typedef enumarray self_type; + typedef enumarray_iterator iterator; + typedef enumarray_const_iterator const_iterator; + + typedef V value_type; + typedef E key_type; + typedef size_t size_type; + + static const size_type max_index = static_cast(E::LAST); + + const V& operator[](E index) const + { + assert(index>=static_cast(0) && index<=E::LAST); + return detail_values[static_cast(index)]; + } + + V& operator[](E index) + { + assert(index>=static_cast(0) && index<=E::LAST); + return detail_values[static_cast(index)]; + } + + void fill(V val) + { for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; } + + static size_type size() { return max_index + 1; } + iterator begin() { return iterator(*this, 0); } + iterator end() { return iterator(*this, size()); } + const_iterator begin() const { return const_iterator(*this, 0); } + const_iterator end() const { return const_iterator(*this, size()); } + + V* data() { return detail_values; } + +//private: + V detail_values[max_index + 1]; +}; + + +template +class enumarray_iterator { + EA* m_buf; + size_t m_pos; +public: + typedef enumarray_iterator self_type; + typedef typename EA::value_type value_type; + typedef typename EA::key_type key_type; + typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums + typedef + typename std::make_signed< + typename std::underlying_type::type>::type + difference_type; + typedef typename EA::value_type* pointer; + typedef typename EA::value_type& reference; + + enumarray_iterator(EA& b, size_t start_pos) + : m_buf(&b), m_pos(start_pos) {} + value_type& operator*() const { return (*m_buf)[static_cast(m_pos)]; } + value_type* operator->() const { return &(operator*()); } + self_type& operator++() { ++m_pos; return *this; } + bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; } + bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; } +}; + +template +class enumarray_const_iterator { + EA const * m_buf; + size_t m_pos; +public: + typedef enumarray_const_iterator self_type; + typedef typename EA::value_type const value_type; + typedef typename EA::key_type key_type; + typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums + typedef + typename std::make_signed< + typename std::underlying_type::type>::type + difference_type; + typedef typename EA::value_type const * pointer; + typedef typename EA::value_type const & reference; + + enumarray_const_iterator(EA const & b, size_t start_pos) + : m_buf(&b), m_pos(start_pos) {} + value_type& operator*() const { return (*m_buf)[static_cast(m_pos)]; } + value_type* operator->() const { return &(operator*()); } + self_type& operator++() { ++m_pos; return *this; } + bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; } + bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; } +}; + +}; // namespace o3tl + +#endif /* INCLUDED_O3TL_ENUMARRAY_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/enumrange.hxx b/include/o3tl/enumrange.hxx new file mode 100644 index 000000000..60f069c9f --- /dev/null +++ b/include/o3tl/enumrange.hxx @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_ENUMRANGE_HXX +#define INCLUDED_O3TL_ENUMRANGE_HXX + +namespace o3tl { + +/// +/// This is a container convenience class iterating over scoped enumerations. +/// +/// This assumes that the 'enum class' definition +/// - starts at zero +/// - has no holes in its sequence of values +/// - defines a value called LAST which refers to the greatest constant. +/// +/// Use like this: +/// enum class COLOR { RED, GREEN, BLUE, LAST=BLUE }; +/// for( auto e : o3tl::enumrange() ) +/// ....; +/// +/// \param T the 'enum class' type. +template< typename T> +class enumrange +{ +public: + class Iterator + { + public: + Iterator( int value ) : + m_value( value ) + { + } + + T operator*( void ) const + { + return static_cast(m_value); + } + + void operator++( void ) + { + ++m_value; + } + + bool operator!=( Iterator rhs ) const + { + return m_value != rhs.m_value; + } + + private: + int m_value; + }; +}; + +template< typename T > +typename enumrange::Iterator begin( enumrange ) +{ + return typename enumrange::Iterator( int(0) ); +} + +template< typename T > +typename enumrange::Iterator end( enumrange ) +{ + return typename enumrange::Iterator( static_cast(T::LAST) + 1 ); +} + +}; // namespace o3tl + +#endif /* INCLUDED_O3TL_ENUMRANGE_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/float_int_conversion.hxx b/include/o3tl/float_int_conversion.hxx new file mode 100644 index 000000000..403bed622 --- /dev/null +++ b/include/o3tl/float_int_conversion.hxx @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX +#define INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX + +#include + +#include +#include +#include + +namespace o3tl +{ +// Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no +// smaller than `min`: +template +constexpr std::enable_if_t && std::is_integral_v, bool> +convertsToAtLeast(F value, I min) +{ + // If `F(min)`, `F(min) - F(1)` are too large in magnitude for `F`'s precision, then they either + // fall into the same bucket, in which case we should return false if `value` represents that + // bucket, or they are on the boundary of two adjacent buckets, in which case we should return + // true if `value`represents the higher bucket containing `F(min)`: + return value > F(min) - F(1); +} + +// Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no +// larger than `max`: +template +constexpr std::enable_if_t && std::is_integral_v, bool> +convertsToAtMost(F value, I max) +{ + // If `F(max)`, `F(max) + F(1)` are too large in magnitude for `F`'s precision, then they either + // fall into the same bucket, in which case we should return false if `value` represents that + // bucket, or they are on the boundary of two adjacent buckets, in which case we should return + // true if `value`represents the lower bucket containing `F(max)`: + return value < F(max) + F(1); +} + +// Casts a floating-point to an integer, avoiding overflow. Used like: +// sal_Int64 n = o3tl::saturating_cast(f); +template +constexpr std::enable_if_t && std::is_integral_v, I> +saturating_cast(F f) +{ + if constexpr (std::is_signed_v) + if (!convertsToAtLeast(f, std::numeric_limits::min())) + return std::numeric_limits::min(); + if (!convertsToAtMost(f, std::numeric_limits::max())) + return std::numeric_limits::max(); + return f; +} + +// Return `value` of floating-point type `F` rounded to the nearest integer away from zero (which +// can be useful in calls to convertsToAtLeast/Most(roundAway(x), n), to reject x that are +// smaller/larger than n because they have a fractional part): +template std::enable_if_t, F> roundAway(F value) +{ + return value >= 0 ? std::ceil(value) : std::floor(value); +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/functional.hxx b/include/o3tl/functional.hxx new file mode 100644 index 000000000..9ca86924f --- /dev/null +++ b/include/o3tl/functional.hxx @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * + * Copyright (c) 1994 + * Hewlett-Packard Company + * + * Copyright (c) 1996-1998 + * Silicon Graphics Computer Systems, Inc. + * + * Copyright (c) 1997 + * Moscow Center for SPARC Technology + * + * Copyright (c) 1999 + * Boris Fomitchev + * + * This material is provided "as is", with absolutely no warranty expressed + * or implied. Any use is at your own risk. + * + * Permission to use or copy this software for any purpose is hereby granted + * without fee, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + * + */ + +/* + * Lifted and paraphrased from STLport - with additions from Fridrich + * Strba and Thorsten Behrens + */ + +#ifndef INCLUDED_O3TL_FUNCTIONAL_HXX +#define INCLUDED_O3TL_FUNCTIONAL_HXX + +namespace o3tl +{ +/// Select first value of a pair +template +struct select1st +{ + typedef P argument_type; + typedef typename P::first_type result_type; + const result_type& operator()( const argument_type& cp ) const { + return cp.first; + } +}; + +/// Select second value of a pair +template +struct select2nd +{ + typedef P argument_type; + typedef typename P::second_type result_type; + const result_type& operator()( const argument_type& cp ) const { + return cp.second; + } +}; + +} // namespace o3tl + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/hash_combine.hxx b/include/o3tl/hash_combine.hxx new file mode 100644 index 000000000..48e142622 --- /dev/null +++ b/include/o3tl/hash_combine.hxx @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include +#include +#include + +namespace o3tl +{ +template +inline std::enable_if_t<(sizeof(N) == 4)> hash_combine(N& nSeed, T const* pValue, size_t nCount) +{ + static_assert(sizeof(nSeed) == 4); + for (size_t i = 0; i < nCount; ++i) + { + nSeed ^= std::hash{}(*pValue) + 0x9E3779B9u + (nSeed << 6) + (nSeed >> 2); + ++pValue; + } +} + +template +inline std::enable_if_t<(sizeof(N) == 4)> hash_combine(N& nSeed, T const& nValue) +{ + static_assert(sizeof(nSeed) == 4); + nSeed ^= std::hash{}(nValue) + 0x9E3779B9u + (nSeed << 6) + (nSeed >> 2); +} + +template +inline std::enable_if_t<(sizeof(N) == 8)> hash_combine(N& nSeed, T const* pValue, size_t nCount) +{ + static_assert(sizeof(nSeed) == 8); + for (size_t i = 0; i < nCount; ++i) + { + nSeed ^= std::hash{}(*pValue) + 0x9E3779B97F4A7C15llu + (nSeed << 12) + (nSeed >> 4); + ++pValue; + } +} + +template +inline std::enable_if_t<(sizeof(N) == 8)> hash_combine(N& nSeed, T const& nValue) +{ + static_assert(sizeof(nSeed) == 8); + nSeed ^= std::hash{}(nValue) + 0x9E3779B97F4A7C15llu + (nSeed << 12) + (nSeed >> 4); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/intcmp.hxx b/include/o3tl/intcmp.hxx new file mode 100644 index 000000000..1324afcda --- /dev/null +++ b/include/o3tl/intcmp.hxx @@ -0,0 +1,132 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +#include +#include + +#include + +namespace o3tl +{ +// An approximation of the C++20 integer comparison functions +// ( "Safe integral +// comparisons"): +#if defined __cpp_lib_integer_comparison_functions + +using std::cmp_equal; +using std::cmp_not_equal; +using std::cmp_less; +using std::cmp_greater; +using std::cmp_less_equal; +using std::cmp_greater_equal; + +#else + +template constexpr bool cmp_equal(T1 value1, T2 value2) noexcept +{ + // coverity[same_on_both_sides: FALSE] + if constexpr (std::is_signed_v == std::is_signed_v) + { + return value1 == value2; + } + else if constexpr (std::is_signed_v) + { + return value1 >= 0 && o3tl::make_unsigned(value1) == value2; + } + else + { + return value2 >= 0 && value1 == o3tl::make_unsigned(value2); + } +} + +template constexpr bool cmp_not_equal(T1 value1, T2 value2) noexcept +{ + return !cmp_equal(value1, value2); +} + +template constexpr bool cmp_less(T1 value1, T2 value2) noexcept +{ + if constexpr (std::is_signed_v == std::is_signed_v) + { + return value1 < value2; + } + else if constexpr (std::is_signed_v) + { + return value1 < 0 || o3tl::make_unsigned(value1) < value2; + } + else + { + return value2 >= 0 && value1 < o3tl::make_unsigned(value2); + } +} + +template constexpr bool cmp_greater(T1 value1, T2 value2) noexcept +{ + return cmp_less(value2, value1); +} + +template constexpr bool cmp_less_equal(T1 value1, T2 value2) noexcept +{ + return !cmp_greater(value1, value2); +} + +template constexpr bool cmp_greater_equal(T1 value1, T2 value2) noexcept +{ + return !cmp_less(value1, value2); +} + +#endif + +// A convenient operator syntax around the standard integer comparison functions: +template struct IntCmp +{ + explicit constexpr IntCmp(T theValue) + : value(theValue) + { + } + + T value; +}; + +template constexpr bool operator==(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_equal(value1.value, value2.value); +} + +template constexpr bool operator!=(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_not_equal(value1.value, value2.value); +} + +template constexpr bool operator<(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_less(value1.value, value2.value); +} + +template constexpr bool operator>(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_greater(value1.value, value2.value); +} + +template constexpr bool operator<=(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_less_equal(value1.value, value2.value); +} + +template constexpr bool operator>=(IntCmp value1, IntCmp value2) +{ + return o3tl::cmp_greater_equal(value1.value, value2.value); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/lazy_update.hxx b/include/o3tl/lazy_update.hxx new file mode 100644 index 000000000..6f9325dfd --- /dev/null +++ b/include/o3tl/lazy_update.hxx @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_LAZY_UPDATE_HXX +#define INCLUDED_O3TL_LAZY_UPDATE_HXX + +#include + +namespace o3tl +{ + /** Update output object lazily + + This template collects data in input type, and updates the + output type with the given update functor, but only if the + output is requested. Useful if updating is expensive, or input + changes frequently, but output is only comparatively seldom + used. + + @example +
+LazyUpdate myValue(F);
+*myValue = newInput;
+myValue->updateInput( this, that, those );
+
+output( *myValue );
+        
+ or +
+output( myValue.getOutValue() );
+        
+ if the compiler does not recognize the const context. + */ + template class LazyUpdate { + public: + LazyUpdate(Func func): func_(std::move(func)), input_(), dirty_(true) {} + + In const & getInValue() const { return input_; } + + Out const & getOutValue() const { return update(); } + + In & operator *() { + dirty_ = true; + return input_; + } + + In * operator ->() { + dirty_ = true; + return &input_; + } + + Out const & operator *() const { return update(); } + + Out const * operator ->() const { return &update(); } + + private: + Out const & update() const { + if (dirty_) { + output_ = func_(input_); + dirty_ = false; + } + return output_; + } + + Func const func_; + In input_; + mutable Out output_; + mutable bool dirty_; + }; +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/lru_map.hxx b/include/o3tl/lru_map.hxx new file mode 100644 index 000000000..859617e5d --- /dev/null +++ b/include/o3tl/lru_map.hxx @@ -0,0 +1,299 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#ifndef INCLUDED_O3TL_LRU_MAP_HXX +#define INCLUDED_O3TL_LRU_MAP_HXX + +#include +#include +#include +#include +#include +#include + +namespace o3tl +{ +namespace detail +{ +// Helper base class to keep total cost for lru_map with custom item size. +// Custom size is specified by the ValueSize functor, the default of each +// item counting as 1 is specified using the void type. +template class lru_map_base +{ +public: + // Returns total of ValueSize for all items. + size_t total_size() const { return mCurrentSize; } + +protected: + size_t mCurrentSize = 0; // sum of ValueSize for all items +}; + +// By default cost of each item is 1, so it doesn't need to be tracked. +template <> class lru_map_base +{ +}; +} // namespace + +/** LRU map + * + * Similar to unordered_map (it actually uses it) with additionally functionality + * which removes the entries that have been "least recently used" when the size + * hits the specified capacity. + * + * It only implements the minimal methods needed and the implementation is NOT + * thread safe. + * + * The implementation is as simple as possible but it still uses O(1) complexity + * for most of the operations with a combination unordered map and linked list. + * + * It is optionally possible to specify a function for ValueSize template + * argument (that can be called as 'size_t func(Value)') that will return + * a size (cost) for an item instead of the default size of 1 for each item. + * The size of an item must not change for an item (if needed, re-insert + * the item). A newly inserted item is guaranteed to be in the container, + * even if its size exceeds the maximum size. + * + **/ +template , + class KeyEqual = std::equal_to, class ValueSize = void> +class lru_map final : public detail::lru_map_base +{ +public: + typedef typename std::pair key_value_pair_t; + +private: + typedef std::list list_t; + typedef typename list_t::iterator list_iterator_t; + typedef typename list_t::const_iterator list_const_iterator_t; + + typedef std::unordered_map map_t; + typedef typename map_t::iterator map_iterator_t; + typedef typename map_t::const_iterator map_const_iterator_t; + + list_t mLruList; + map_t mLruMap; + size_t mMaxSize; + + void addSize(const Value& value) + { + // by default total size is equal to number of items + if constexpr (!std::is_void_v) + this->mCurrentSize += ValueSize()(value); + } + + void removeSize(const Value& value) + { + // by default total size is equal to number of items + if constexpr (!std::is_void_v) + { + size_t itemSize = ValueSize()(value); + assert(itemSize <= this->mCurrentSize); + this->mCurrentSize -= itemSize; + } + } + + void removeOldestItem() + { + removeSize(mLruList.back().second); + // remove from map + mLruMap.erase(mLruList.back().first); + // remove from list + mLruList.pop_back(); + } + + void checkLRUItemInsert() + { + if constexpr (std::is_void_v) + { // One added, so it's enough to remove one, if needed. + if (mLruMap.size() > mMaxSize) + removeOldestItem(); + } + else + { + // This must leave at least one item (it's called from insert). + while (this->mCurrentSize > mMaxSize && mLruList.size() > 1) + removeOldestItem(); + } + } + + void checkLRUItemUpdate() + { + // Item update does not change total size by default. + if constexpr (!std::is_void_v) + { + // This must leave at least one item (it's called from insert). + while (this->mCurrentSize > mMaxSize && mLruList.size() > 1) + removeOldestItem(); + } + } + + void checkLRUMaxSize() + { + if constexpr (std::is_void_v) + { + while (mLruMap.size() > mMaxSize) + removeOldestItem(); + } + else + { + while (this->mCurrentSize > mMaxSize) + removeOldestItem(); + } + } + + void clearSize() + { + if constexpr (!std::is_void_v) + { +#ifdef DBG_UTIL + for (const key_value_pair_t& item : mLruList) + removeSize(item.second); + assert(this->mCurrentSize == 0); +#else + this->mCurrentSize = 0; +#endif + } + } + +public: + typedef list_iterator_t iterator; + typedef list_const_iterator_t const_iterator; + + lru_map(size_t nMaxSize) + : mMaxSize(nMaxSize) + { + assert(mMaxSize > 0); + } + ~lru_map() + { + clearSize(); + // Some code .e.g. SalBitmap likes to remove itself from a cache during it's destructor, which means we + // get calls into lru_map while we are in destruction, so use the swap-and-clear idiom to avoid those problems. + mLruMap.clear(); + list_t().swap(mLruList); + } + + void setMaxSize(size_t nMaxSize) + { + mMaxSize = nMaxSize; + assert(mMaxSize > 0); + checkLRUMaxSize(); + } + + void insert(key_value_pair_t& rPair) + { + map_iterator_t i = mLruMap.find(rPair.first); + + if (i == mLruMap.end()) // doesn't exist -> add to queue and map + { + addSize(rPair.second); + // add to front of the list + mLruList.push_front(rPair); + // add the list position (iterator) to the map + auto it = mLruList.begin(); + mLruMap[it->first] = it; + checkLRUItemInsert(); + } + else // already exists -> replace value + { + // update total cost + removeSize(i->second->second); + addSize(rPair.second); + // replace value + i->second->second = rPair.second; + // bring to front of the lru list + mLruList.splice(mLruList.begin(), mLruList, i->second); + checkLRUItemUpdate(); + } + } + + void insert(key_value_pair_t&& rPair) + { + map_iterator_t i = mLruMap.find(rPair.first); + + if (i == mLruMap.end()) // doesn't exist -> add to list and map + { + addSize(rPair.second); + // add to front of the list + mLruList.push_front(std::move(rPair)); + // add the list position (iterator) to the map + auto it = mLruList.begin(); + mLruMap[it->first] = it; + checkLRUItemInsert(); + } + else // already exists -> replace value + { + removeSize(i->second->second); + addSize(rPair.second); + // replace value + i->second->second = std::move(rPair.second); + // push to back of the lru list + mLruList.splice(mLruList.begin(), mLruList, i->second); + checkLRUItemUpdate(); + } + } + + list_const_iterator_t find(const Key& key) + { + const map_iterator_t i = mLruMap.find(key); + if (i == mLruMap.cend()) // can't find entry for the key + { + // return empty iterator + return mLruList.cend(); + } + else + { + // push to back of the lru list + mLruList.splice(mLruList.begin(), mLruList, i->second); + return i->second; + } + } + + // reverse-iterates the list removing all items matching the predicate + template void remove_if(UnaryPredicate pred) + { + auto it = mLruList.rbegin(); + while (it != mLruList.rend()) + { + if (pred(*it)) + { + removeSize(it->second); + mLruMap.erase(it->first); + it = decltype(it){ mLruList.erase(std::next(it).base()) }; + } + else + ++it; + } + } + + list_const_iterator_t begin() const { return mLruList.cbegin(); } + + list_const_iterator_t end() const { return mLruList.cend(); } + + size_t size() const + { + assert(mLruMap.size() == mLruList.size()); + return mLruMap.size(); + } + + // size_t total_size() const; - only if custom ValueSize + + void clear() + { + clearSize(); + mLruMap.clear(); + mLruList.clear(); + } +}; +} + +#endif /* INCLUDED_O3TL_LRU_MAP_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/make_shared.hxx b/include/o3tl/make_shared.hxx new file mode 100644 index 000000000..5d4d98e42 --- /dev/null +++ b/include/o3tl/make_shared.hxx @@ -0,0 +1,46 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_MAKE_SHARED_HXX +#define INCLUDED_O3TL_MAKE_SHARED_HXX + +#include +#include +#include + +namespace o3tl +{ +/** Allocate an array stored in a shared_ptr, calling operator delete[]. + Note that this is only allowed for arithmetic types because shared_ptr + implicitly converts to sub-types. + */ +template std::shared_ptr make_shared_array(size_t const size) +{ + static_assert(std::is_arithmetic::value, "only arrays of arithmetic types allowed"); + return std::shared_ptr(new T[size], std::default_delete()); +} + +/** To markup std::shared_ptr that coverity warns might throw exceptions + which won't throw in practice, or where std::terminate is + an acceptable response if they do +*/ +template std::shared_ptr make_shared(Args&&... args) +{ +#if defined(__COVERITY__) + return std::shared_ptr(new T(std::forward(args)...), o3tl::default_delete()); +#else + return std::make_shared(std::forward(args)...); +#endif +} + +} // namespace o3tl + +#endif // INCLUDED_O3TL_MAKE_SHARED_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/numeric.hxx b/include/o3tl/numeric.hxx new file mode 100644 index 000000000..9980319a6 --- /dev/null +++ b/include/o3tl/numeric.hxx @@ -0,0 +1,28 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_NUMERIC_HXX +#define INCLUDED_O3TL_NUMERIC_HXX + +#include + +namespace o3tl +{ + struct divide_by_zero final : public std::runtime_error + { + explicit divide_by_zero() + : std::runtime_error("divide by zero") + { + } + }; +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/runtimetooustring.hxx b/include/o3tl/runtimetooustring.hxx new file mode 100644 index 000000000..6d6f27ac1 --- /dev/null +++ b/include/o3tl/runtimetooustring.hxx @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_RUNTIMETOOUSTRING_HXX +#define INCLUDED_O3TL_RUNTIMETOOUSTRING_HXX + +#include + +#include +#include + +#include +#include +#include +#include + +namespace o3tl +{ +/** Convert an NTBS from the C++ runtime to an OUString. + + This is used to convert an NTBS as provided by std::exception::what or + std::type_info::name into an OUString in a "lossless" way. The conversion + is done using RTL_TEXTENCODING_ISO_8859_1, so each char in the input maps + to one Unicode character in the output. +*/ +inline OUString runtimeToOUString(char const* runtimeString) +{ + OUString s; + bool ok = rtl_convertStringToUString( + &s.pData, runtimeString, std::strlen(runtimeString), RTL_TEXTENCODING_ISO_8859_1, + (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR + | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)); + assert(ok); + (void)ok; + return s; +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/safeCoInitUninit.hxx b/include/o3tl/safeCoInitUninit.hxx new file mode 100644 index 000000000..0ceb4a974 --- /dev/null +++ b/include/o3tl/safeCoInitUninit.hxx @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#if defined _WIN32 +#include + +// for CoInitializeEx / CoUninitialize +#include + +#include + +// for std::abort +#include + +namespace o3tl +{ +// Helpers for safe calls to CoInitializeEx and CoUninitialize in MSVC +// Indeed if a thread has been already initialized with a concurrency model +// (in LO case COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED) +// CoInitializeEx can't succeed without calling first CoUninitialize +// also, CoUninitialize must be called the number of times CoInitializeEx has been called +inline HRESULT safeCoInitializeEx(DWORD dwCoInit, int& nbReinit) +{ + HRESULT hr; + while ((hr = CoInitializeEx(nullptr, dwCoInit)) == RPC_E_CHANGED_MODE) + { + // so we're in RPC_E_CHANGED_MODE case + // the pb was it was already initialized with a different concurrency model + // close this init + CoUninitialize(); + // and increment counter for dtr part + ++nbReinit; + + // and keep on the loop if there were multi initializations + } + if (FAILED(hr)) + std::abort(); + return hr; +} + +inline void safeCoUninitializeReinit(DWORD dwCoInit, int nbReinit) +{ + CoUninitialize(); + // Put back all the inits, if there were, before the use of the caller to safeCoInitializeEx + for (int i = 0; i < nbReinit; ++i) + CoInitializeEx(nullptr, dwCoInit); +} +} +#endif +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx new file mode 100644 index 000000000..c697a9316 --- /dev/null +++ b/include/o3tl/safeint.hxx @@ -0,0 +1,236 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +#include +#include +#include +#include + +#if defined(_MSC_VER) +#include +#else +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif +#endif + +namespace o3tl +{ + +template inline T saturating_add(T a, T b) +{ + if (b >= 0) { + if (a <= std::numeric_limits::max() - b) { + return a + b; + } else { + return std::numeric_limits::max(); + } + } else { + if (a >= std::numeric_limits::min() - b) { + return a + b; + } else { + return std::numeric_limits::min(); + } + } +} + +template inline T saturating_sub(T a, T b) +{ + if (b >= 0) { + if (a >= std::numeric_limits::min() + b) { + return a - b; + } else { + return std::numeric_limits::min(); + } + } else { + if (a <= std::numeric_limits::max() + b) { + return a - b; + } else { + return std::numeric_limits::max(); + } + } +} + +template inline +typename std::enable_if::value, T>::type saturating_toggle_sign( + T a) +{ + if (a == std::numeric_limits::min()) + return std::numeric_limits::max(); + return a * -1; +} + +#if defined(_MSC_VER) + +template inline bool checked_multiply(T a, T b, T& result) +{ + return !msl::utilities::SafeMultiply(a, b, result); +} + +template inline bool checked_add(T a, T b, T& result) +{ + return !msl::utilities::SafeAdd(a, b, result); +} + +template inline bool checked_sub(T a, T b, T& result) +{ + return !msl::utilities::SafeSubtract(a, b, result); +} + +#elif (defined __GNUC__ && !defined __clang__) || (__has_builtin(__builtin_mul_overflow) && !(defined ANDROID && defined __clang__) && !(defined(__clang__) && (defined(__arm__) || defined(__i386__)))) +// 32-bit clang fails with undefined reference to `__mulodi4' + +template inline bool checked_multiply(T a, T b, T& result) +{ + return __builtin_mul_overflow(a, b, &result); +} + +template inline bool checked_add(T a, T b, T& result) +{ + return __builtin_add_overflow(a, b, &result); +} + +template inline bool checked_sub(T a, T b, T& result) +{ + return __builtin_sub_overflow(a, b, &result); +} + +#else + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (a > 0) { /* a is positive */ + if (b > 0) { /* a and b are positive */ + if (a > (std::numeric_limits::max() / b)) { + return true; /* Handle error */ + } + } else { /* a positive, b nonpositive */ + if (b < (std::numeric_limits::min() / a)) { + return true; /* Handle error */ + } + } /* a positive, b nonpositive */ + } else { /* a is nonpositive */ + if (b > 0) { /* a is nonpositive, b is positive */ + if (a < (std::numeric_limits::min() / b)) { + return true; /* Handle error */ + } + } else { /* a and b are nonpositive */ + if ( (a != 0) && (b < (std::numeric_limits::max() / a))) { + return true; /* Handle error */ + } + } /* End if a and b are nonpositive */ + } /* End if a is nonpositive */ + + result = a * b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_multiply(T a, T b, T& result) +{ + if (b && a > std::numeric_limits::max() / b) { + return true;/* Handle error */ + } + + result = a * b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_add(T a, T b, T& result) +{ + if (((b > 0) && (a > (std::numeric_limits::max() - b))) || + ((b < 0) && (a < (std::numeric_limits::min() - b)))) { + return true; + } + + result = a + b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_add(T a, T b, T& result) +{ + if (std::numeric_limits::max() - a < b) { + return true;/* Handle error */ + } + + result = a + b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow +template inline typename std::enable_if::value, bool>::type checked_sub(T a, T b, T& result) +{ + if ((b > 0 && a < std::numeric_limits::min() + b) || + (b < 0 && a > std::numeric_limits::max() + b)) { + return true; + } + + result = a - b; + + return false; +} + +//https://www.securecoding.cert.org/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap +template inline typename std::enable_if::value, bool>::type checked_sub(T a, T b, T& result) +{ + if (a < b) { + return true; + } + + result = a - b; + + return false; +} + +#endif + +template constexpr std::enable_if_t, std::make_unsigned_t> +make_unsigned(T value) +{ + assert(value >= 0); + return value; +} + +template constexpr std::enable_if_t, T1> +clamp_to_unsigned(T2 value) { + if constexpr (std::is_unsigned_v) { + // coverity[result_independent_of_operands] - suppress warning for template + return value <= std::numeric_limits::max() ? value : std::numeric_limits::max(); + } else { + static_assert(std::is_signed_v); + return value < 0 ? 0 : clamp_to_unsigned(make_unsigned(value)); + } +} + +// An implicit conversion from T2 to T1, useful in places where an explicit conversion from T2 to +// T1 is needed (e.g., in list initialization, if the implicit conversion would be narrowing) but +// tools like -fsanitize=implicit-conversion should still be able to detect truncation: +template constexpr T1 narrowing(T2 value) { return value; } + +// std::min wrapped to inform coverity that the result is now deemed sanitized +// coverity[ -taint_source ] +template [[nodiscard]] inline T sanitizing_min(T a, T b) +{ + return std::min(a, b); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/sorted_vector.hxx b/include/o3tl/sorted_vector.hxx new file mode 100644 index 000000000..13c0b25f8 --- /dev/null +++ b/include/o3tl/sorted_vector.hxx @@ -0,0 +1,421 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_SORTED_VECTOR_HXX +#define INCLUDED_O3TL_SORTED_VECTOR_HXX + +#include +#include +#include +#include +#include +#include +#include + +namespace o3tl +{ + +// forward declared because it's default template arg for sorted_vector +template +struct find_unique; + +/** Represents a sorted vector of values. + + @tpl Value class of item to be stored in container + @tpl Compare comparison method + @tpl Find look up index of a Value in the array +*/ +template< + typename Value, + typename Compare = std::less, + template class Find = find_unique, + bool = std::is_copy_constructible::value > +class sorted_vector +{ +private: + typedef Find Find_t; + typedef typename std::vector vector_t; + typedef typename std::vector::iterator iterator; +public: + typedef typename std::vector::const_iterator const_iterator; + typedef typename std::vector::const_reverse_iterator const_reverse_iterator; + typedef typename std::vector::difference_type difference_type; + typedef typename std::vector::size_type size_type; + typedef Value value_type; + + constexpr sorted_vector( std::initializer_list init ) + : m_vector(init) + { + std::sort(m_vector.begin(), m_vector.end(), Compare()); + } + sorted_vector() = default; + sorted_vector(sorted_vector const&) = default; + sorted_vector(sorted_vector&&) = default; + + sorted_vector& operator=(sorted_vector const&) = default; + sorted_vector& operator=(sorted_vector&&) = default; + + // MODIFIERS + + std::pair insert( Value&& x ) + { + std::pair const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + if (!ret.second) + { + const_iterator const it = m_vector.insert(m_vector.begin() + (ret.first - m_vector.begin()), std::move(x)); + return std::make_pair(it, true); + } + return std::make_pair(ret.first, false); + } + + std::pair insert( const Value& x ) + { + std::pair const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + if (!ret.second) + { + const_iterator const it = m_vector.insert(m_vector.begin() + (ret.first - m_vector.begin()), x); + return std::make_pair(it, true); + } + return std::make_pair(ret.first, false); + } + + size_type erase( const Value& x ) + { + std::pair const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + if (ret.second) + { + m_vector.erase(m_vector.begin() + (ret.first - m_vector.begin())); + return 1; + } + return 0; + } + + void erase_at(size_t index) + { + m_vector.erase(m_vector.begin() + index); + } + + // like C++ 2011: erase with const_iterator (doesn't change sort order) + const_iterator erase(const_iterator const& position) + { // C++98 has vector::erase(iterator), so call that + return m_vector.erase(m_vector.begin() + (position - m_vector.begin())); + } + + void erase(const_iterator const& first, const_iterator const& last) + { + m_vector.erase(m_vector.begin() + (first - m_vector.begin()), + m_vector.begin() + (last - m_vector.begin())); + } + + /** + * make erase return the removed element, otherwise there is no useful way of extracting a std::unique_ptr + * from this. + */ + Value erase_extract( size_t index ) + { + Value val = std::move(m_vector[index]); + m_vector.erase(m_vector.begin() + index); + return val; + } + + void clear() + { + m_vector.clear(); + } + + void swap(sorted_vector & other) + { + m_vector.swap(other.m_vector); + } + + void reserve(size_type amount) + { + m_vector.reserve(amount); + } + + // ACCESSORS + + size_type size() const + { + return m_vector.size(); + } + + bool empty() const + { + return m_vector.empty(); + } + + // Only return a const iterator, so that the vector cannot be directly updated. + const_iterator begin() const + { + return m_vector.begin(); + } + + // Only return a const iterator, so that the vector cannot be directly updated. + const_iterator end() const + { + return m_vector.end(); + } + + // Only return a const iterator, so that the vector cannot be directly updated. + const_reverse_iterator rbegin() const + { + return m_vector.rbegin(); + } + + // Only return a const iterator, so that the vector cannot be directly updated. + const_reverse_iterator rend() const + { + return m_vector.rend(); + } + + const Value& front() const + { + return m_vector.front(); + } + + const Value& back() const + { + return m_vector.back(); + } + + const Value& operator[]( size_t index ) const + { + return m_vector.operator[]( index ); + } + + // OPERATIONS + + const_iterator lower_bound( const Value& x ) const + { + return std::lower_bound( m_vector.begin(), m_vector.end(), x, Compare() ); + } + + const_iterator upper_bound( const Value& x ) const + { + return std::upper_bound( m_vector.begin(), m_vector.end(), x, Compare() ); + } + + /* Searches the container for an element with a value of x + * and returns an iterator to it if found, otherwise it returns an + * iterator to sorted_vector::end (the element past the end of the container). + * + * Only return a const iterator, so that the vector cannot be directly updated. + */ + const_iterator find( const Value& x ) const + { + std::pair const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + return (ret.second) ? ret.first : m_vector.end(); + } + + size_type count(const Value& v) const + { + return find(v) != end() ? 1 : 0; + } + + bool operator==(const sorted_vector & other) const + { + return m_vector == other.m_vector; + } + + bool operator!=(const sorted_vector & other) const + { + return m_vector != other.m_vector; + } + + void insert(sorted_vector const& rOther) + { + // optimization for the rather common case that we are overwriting this with the contents + // of another sorted vector + if ( empty() ) + m_vector.insert(m_vector.begin(), rOther.m_vector.begin(), rOther.m_vector.end()); + else + insert_internal( rOther.m_vector ); + } + + void insert_sorted_unique_vector(const std::vector& rOther) + { + assert( std::is_sorted(rOther.begin(), rOther.end(), Compare())); + assert( std::unique(rOther.begin(), rOther.end(), compare_equal) == rOther.end()); + if ( empty() ) + m_vector.insert(m_vector.begin(), rOther.m_vector.begin(), rOther.m_vector.end()); + else + insert_internal( rOther ); + } + + void insert_sorted_unique_vector(std::vector&& rOther) + { + assert( std::is_sorted(rOther.begin(), rOther.end(), Compare())); + assert( std::unique(rOther.begin(), rOther.end(), compare_equal) == rOther.end()); + if ( empty() ) + m_vector.swap( rOther ); + else + insert_internal( rOther ); + } + + /* Clear() elements in the vector, and free them one by one. */ + void DeleteAndDestroyAll() + { + for (const_iterator it = m_vector.begin(); it != m_vector.end(); ++it) + { + delete *it; + } + + clear(); + } + + // fdo#58793: some existing code in Writer (SwpHintsArray) + // routinely modifies the members of the vector in a way that + // violates the sort order, and then re-sorts the array. + // This is a kludge to enable that code to work. + // If you are calling this function, you are Doing It Wrong! + void Resort() + { + std::stable_sort(m_vector.begin(), m_vector.end(), Compare()); + } + +private: + static bool compare_equal( const Value& v1, const Value& v2 ) + { // Synthetize == check from < check for std::unique asserts above. + return !Compare()( v1, v2 ) && !Compare()( v2, v1 ); + } + + void insert_internal( const std::vector& rOther ) + { + // Do a union in one pass rather than repeated insert() that could repeatedly + // move large amounts of data. + vector_t tmp; + tmp.reserve( m_vector.size() + rOther.size()); + std::set_union( m_vector.begin(), m_vector.end(), + rOther.begin(), rOther.end(), + std::back_inserter( tmp ), Compare()); + m_vector.swap( tmp ); + } + + vector_t m_vector; +}; + +/* Specialise the template for cases like Value = std::unique_ptr, where + MSVC2017 needs some help +*/ +template< + typename Value, + typename Compare, + template class Find > +class sorted_vector : public sorted_vector +{ +public: + using sorted_vector::sorted_vector; + typedef sorted_vector super_sorted_vector; + + sorted_vector(sorted_vector const&) = delete; + sorted_vector& operator=(sorted_vector const&) = delete; + + sorted_vector() = default; + sorted_vector(sorted_vector&&) = default; + sorted_vector& operator=(sorted_vector&&) = default; + + /** + * implement find for sorted_vectors containing std::unique_ptr + */ + typename super_sorted_vector::const_iterator find( typename Value::element_type const * x ) const + { + Value tmp(const_cast(x)); + auto ret = super_sorted_vector::find(tmp); + tmp.release(); + return ret; + } + /** + * implement upper_bound for sorted_vectors containing std::unique_ptr + */ + typename super_sorted_vector::const_iterator upper_bound( typename Value::element_type const * x ) const + { + Value tmp(const_cast(x)); + auto ret = super_sorted_vector::upper_bound(tmp); + tmp.release(); + return ret; + } + /** + * implement lower_bound for sorted_vectors containing std::unique_ptr + */ + typename super_sorted_vector::const_iterator lower_bound( typename Value::element_type const * x ) const + { + Value tmp(const_cast(x)); + auto ret = super_sorted_vector::lower_bound(tmp); + tmp.release(); + return ret; + } +}; + + +/** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types. + Very useful for the cases where we put pointers to objects inside a sorted_vector. +*/ +template struct less_ptr_to +{ + bool operator() ( T* const& lhs, T* const& rhs ) const + { + return (*lhs) < (*rhs); + } +}; + +template struct less_uniqueptr_to +{ + bool operator() ( std::unique_ptr const& lhs, std::unique_ptr const& rhs ) const + { + return (*lhs) < (*rhs); + } +}; + +/** the elements are totally ordered by Compare, + for no 2 elements !Compare(a,b) && !Compare(b,a) is true + */ +template +struct find_unique +{ + typedef typename sorted_vector ::const_iterator const_iterator; + std::pair operator()( + const_iterator first, const_iterator last, + Value const& v) + { + const_iterator const it = std::lower_bound(first, last, v, Compare()); + return std::make_pair(it, (it != last && !Compare()(v, *it))); + } +}; + +/** the elements are partially ordered by Compare, + 2 elements are allowed if they are not the same element (pointer equal) + */ +template +struct find_partialorder_ptrequals +{ + typedef typename sorted_vector::const_iterator const_iterator; + std::pair operator()( + const_iterator first, const_iterator last, + Value const& v) + { + std::pair const its = + std::equal_range(first, last, v, Compare()); + for (const_iterator it = its.first; it != its.second; ++it) + { + if (v == *it) + { + return std::make_pair(it, true); + } + } + return std::make_pair(its.first, false); + } +}; + +} // namespace o3tl +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/span.hxx b/include/o3tl/span.hxx new file mode 100644 index 000000000..cfe4c1ffa --- /dev/null +++ b/include/o3tl/span.hxx @@ -0,0 +1,100 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_SPAN_HXX +#define INCLUDED_O3TL_SPAN_HXX + +#include + +#include + +#if HAVE_CPP_SPAN + +#include + +namespace o3tl { using std::span; } + +#else + +#include +#include +#include +#include +#include + +namespace o3tl { + +/** A barebones approximation of C++20 . +*/ +template +class span { +public: + using value_type = T; + using pointer = value_type *; + using const_pointer = value_type const *; + using reference = value_type &; + using const_reference = value_type const &; + using const_iterator = const_pointer; + using iterator = pointer; + using const_reverse_iterator = std::reverse_iterator; + using reverse_iterator = std::reverse_iterator; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + + constexpr span() noexcept : data_(nullptr), size_(0) {} + + template + constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {} + + constexpr span (T *a, size_type len) noexcept + : data_(a), size_(len) + { + // not terribly sure about this, might need to strengthen it + assert(a != nullptr || len == 0); + } + + template + constexpr span(const std::vector& vec) + : data_(vec.data()), size_(vec.size()) {} + + /** for assigning from span to span */ + constexpr span (const span::type>& other) noexcept + : data_(other.data()), size_(other.size()) {} + + constexpr bool empty() const noexcept { return size_ == 0; } + + constexpr iterator begin() const noexcept { return data_; } + constexpr iterator end() const noexcept { return begin() + size(); } + + reverse_iterator rbegin() const noexcept + { return reverse_iterator(end()); } + reverse_iterator rend() const noexcept + { return reverse_iterator(begin()); } + + constexpr size_type size() const noexcept { return size_; } + + constexpr reference operator [](size_type pos) const { + assert(pos < size()); + return data_[pos]; + } + + constexpr pointer data() const noexcept { return data_; } + +private: + pointer data_; + size_type size_; +}; + +} // namespace o3tl + +#endif + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/sprintf.hxx b/include/o3tl/sprintf.hxx new file mode 100644 index 000000000..6491f4ad1 --- /dev/null +++ b/include/o3tl/sprintf.hxx @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +#include +#include +#include +#include + +#include + +namespace o3tl +{ +// A drop-in replacement for safe uses of std::sprintf where it is statically known that the +// provided buffer is large enough. Compared to a plain use of std::sprintf, using o3tl::sprintf +// for one makes it explicit that the call is considerd safe and for another avoids deprecation +// warnings on platforms like the macOS 13 SDK that mark std::sprintf as deprecated. Many simple +// uses of std::sprintf across the code base can be replaced with alternative code using e.g. +// OString::number. This is for the remaining formatting-rich cases for which there is no easy +// replacement yet in our C++17 baseline. Ultimately, it should be removed again once alternatives +// for those remaining cases, like C++20 std::format, are available. +template +int sprintf(char (&s)[N], char const* format, T&&... arguments) +{ + auto const n = std::snprintf(s, N, format, std::forward(arguments)...); + assert(n < 0 || o3tl::make_unsigned(n) < N); + (void)n; + return n; +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/string_view.hxx b/include/o3tl/string_view.hxx new file mode 100644 index 000000000..f6c2a4aa7 --- /dev/null +++ b/include/o3tl/string_view.hxx @@ -0,0 +1,487 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace o3tl +{ +// Like OUString::equalsIgnoreAsciiCase, but for two std::u16string_view: +inline bool equalsAscii(std::u16string_view s1, const char* s2) +{ + return rtl_ustr_ascii_compare_WithLength(s1.data(), s1.size(), s2) == 0; +} + +// Like OUString::equalsAsciiL +inline bool equalsAscii(std::u16string_view s1, std::string_view s2) +{ + return rtl_ustr_ascii_shortenedCompare_WithLength(s1.data(), s1.size(), s2.data(), s2.size()) + == 0; +} + +// Like OUString::equalsIgnoreAsciiCase, but for two std::u16string_view: +inline bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2) +{ + if (s1.size() != s2.size()) + return false; + if (s1.data() == s2.data()) + return true; + return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size()) + == 0; +}; + +inline bool equalsIgnoreAsciiCase(std::string_view s1, std::string_view s2) +{ + if (s1.size() != s2.size()) + return false; + if (s1.data() == s2.data()) + return true; + return rtl_str_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size()) + == 0; +}; + +// Like OUString::compareToIgnoreAsciiCase, but for two std::u16string_view: +inline int compareToIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2) +{ + return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size()); +}; + +// Like OUString::matchIgnoreAsciiCase, but for two std::u16string_view: +inline bool matchIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2, + sal_Int32 fromIndex = 0) +{ + return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( + s1.data() + fromIndex, s1.size() - fromIndex, s2.data(), s2.size(), s2.size()) + == 0; +} + +// Similar to O[U]String::getToken, returning the first token of a std::[u16]string_view starting +// at a given position. +// +// Attention: There are two sets of o3tl::getToken overloads here. This first set has an interface +// based on std::size_t length parameters, and its semantics don't match those of +// O[U]String::getToken exactly (buf if needed, it can be extended to return the n'th token instead +// of just the first, and/or support an initial position of npos, to make the semantics match). +template > +inline std::basic_string_view getToken(std::basic_string_view sv, + charT delimiter, std::size_t& position) +{ + assert(position <= sv.size()); + auto const n = sv.find(delimiter, position); + std::basic_string_view t; + if (n == std::string_view::npos) + { + t = sv.substr(position); + position = std::string_view::npos; + } + else + { + t = sv.substr(position, n - position); + position = n + 1; + } + return t; +} +// The following two overloads prevent overload resolution mistakes that would occur with their +// template counterpart, when sv is of a type that is implicitly convertible to basic_string_view +// (like OString or OUString), in which case overload resolution would erroneously choose the +// three-argument overloads (taking sv, nToken, cTok) from the second set of +// o3tl::getToken overloads below: +inline std::string_view getToken(std::string_view sv, char delimiter, std::size_t& position) +{ + return getToken(sv, delimiter, position); +} +inline std::u16string_view getToken(std::u16string_view sv, char16_t delimiter, + std::size_t& position) +{ + return getToken(sv, delimiter, position); +} + +// Similar to O[U]String::getToken. +// +// Attention: There are two sets of o3tl::getToken overloads here. This second set has an +// interface based on sal_Int32 length parameters, and is meant to be a drop-in replacement for +// O[U]String::getToken. +template > +inline std::basic_string_view getToken(std::basic_string_view pStr, + sal_Int32 nToken, charT cTok, + sal_Int32& rnIndex) +{ + assert(o3tl::IntCmp(rnIndex) <= o3tl::IntCmp(pStr.size())); + + // Return an empty string and set rnIndex to -1 if either nToken or rnIndex is + // negative: + if (rnIndex >= 0 && nToken >= 0) + { + const charT* pOrgCharStr = pStr.data(); + const charT* pCharStr = pOrgCharStr + rnIndex; + sal_Int32 nLen = pStr.size() - rnIndex; + sal_Int32 nTokCount = 0; + const charT* pCharStrStart = pCharStr; + while (nLen > 0) + { + if (*pCharStr == cTok) + { + nTokCount++; + + if (nTokCount > nToken) + break; + if (nTokCount == nToken) + pCharStrStart = pCharStr + 1; + } + + pCharStr++; + nLen--; + } + if (nTokCount >= nToken) + { + if (nLen > 0) + rnIndex = pCharStr - pOrgCharStr + 1; + else + rnIndex = -1; + return std::basic_string_view(pCharStrStart, pCharStr - pCharStrStart); + } + } + + rnIndex = -1; + return std::basic_string_view(); +} +// The following two overloads prevent deduction failures that would occur with their template +// counterpart, when sv is of a type that is implicitly convertible to basic_string_view (like +// OString or OUString): +inline std::string_view getToken(std::string_view sv, sal_Int32 nToken, char cTok, + sal_Int32& rnIndex) +{ + return getToken(sv, nToken, cTok, rnIndex); +} +inline std::u16string_view getToken(std::u16string_view sv, sal_Int32 nToken, char16_t cTok, + sal_Int32& rnIndex) +{ + return getToken(sv, nToken, cTok, rnIndex); +} +inline std::string_view getToken(std::string_view sv, sal_Int32 nToken, char cTok) +{ + sal_Int32 nIndex = 0; + return getToken(sv, nToken, cTok, nIndex); +} +inline std::u16string_view getToken(std::u16string_view sv, sal_Int32 nToken, char16_t cTok) +{ + sal_Int32 nIndex = 0; + return getToken(sv, nToken, cTok, nIndex); +} + +// Implementations of C++20 std::basic_string_view::starts_with and +// std::basic_string_view::ends_with, until we can use those directly on all platforms: +template > +constexpr bool starts_with(std::basic_string_view sv, + std::basic_string_view x) noexcept +{ +#if defined __cpp_lib_starts_ends_with + return sv.starts_with(x); +#else + return sv.substr(0, x.size()) == x; +#endif +} +template > +constexpr bool starts_with(std::basic_string_view sv, charT x) noexcept +{ +#if defined __cpp_lib_starts_ends_with + return sv.starts_with(x); +#else + return !sv.empty() && traits::eq(sv.front(), x); +#endif +} +template > +constexpr bool starts_with(std::basic_string_view sv, charT const* x) +{ +#if defined __cpp_lib_starts_ends_with + return sv.starts_with(x); +#else + return starts_with(sv, std::basic_string_view(x)); +#endif +} +template > +constexpr bool ends_with(std::basic_string_view sv, + std::basic_string_view x) noexcept +{ +#if defined __cpp_lib_ends_ends_with + return sv.ends_with(x); +#else + return sv.size() >= x.size() + && sv.compare(sv.size() - x.size(), std::basic_string_view::npos, x) == 0; +#endif +} +template > +constexpr bool ends_with(std::basic_string_view sv, charT x) noexcept +{ +#if defined __cpp_lib_ends_ends_with + return sv.ends_with(x); +#else + return !sv.empty() && traits::eq(sv.back(), x); +#endif +} +template > +constexpr bool ends_with(std::basic_string_view sv, charT const* x) +{ +#if defined __cpp_lib_ends_ends_with + return sv.ends_with(x); +#else + return ends_with(sv, std::basic_string_view(x)); +#endif +} +// The following overloads prevent deduction failures that would occur with their template +// counterparts, when x is of a type that is implicitly convertible to basic_string_view (like +// OString or OUString, and we only bother to provide overloads for the char and char16_t cases, not +// also for char32_t and wchar_t, nor for C++20 char8_t): +constexpr bool starts_with(std::string_view sv, std::string_view x) noexcept +{ + return starts_with(sv, x); +} +constexpr bool starts_with(std::u16string_view sv, std::u16string_view x) noexcept +{ + return starts_with(sv, x); +} +constexpr bool ends_with(std::string_view sv, std::string_view x) noexcept +{ + return ends_with(sv, x); +} +constexpr bool ends_with(std::u16string_view sv, std::u16string_view x) noexcept +{ + return ends_with(sv, x); +} + +// Variants of C++20 std::basic_string_view::starts_with and +// std::basic_string_view::ends_with that have a rest out parameter, similar to our OString and +// OUString startsWith and endsWith member functions: +template > +constexpr bool starts_with(std::basic_string_view sv, + std::basic_string_view x, + std::basic_string_view* rest) noexcept +{ + assert(rest != nullptr); + auto const found = starts_with(sv, x); + if (found) + { + *rest = sv.substr(x.length()); + } + return found; +} +template > +constexpr bool starts_with(std::basic_string_view sv, charT x, + std::basic_string_view* rest) noexcept +{ + assert(rest != nullptr); + auto const found = starts_with(sv, x); + if (found) + { + *rest = sv.substr(1); + } + return found; +} +template > +constexpr bool starts_with(std::basic_string_view sv, charT const* x, + std::basic_string_view* rest) +{ + assert(rest != nullptr); + auto const found = starts_with(sv, x); + if (found) + { + *rest = sv.substr(traits::length(x)); + } + return found; +} +template > +constexpr bool ends_with(std::basic_string_view sv, + std::basic_string_view x, + std::basic_string_view* rest) noexcept +{ + assert(rest != nullptr); + auto const found = ends_with(sv, x); + if (found) + { + *rest = sv.substr(0, sv.length() - x.length()); + } + return found; +} +template > +constexpr bool ends_with(std::basic_string_view sv, charT x, + std::basic_string_view* rest) noexcept +{ + assert(rest != nullptr); + auto const found = ends_with(sv, x); + if (found) + { + *rest = sv.substr(0, sv.length() - 1); + } + return found; +} +template > +constexpr bool ends_with(std::basic_string_view sv, charT const* x, + std::basic_string_view* rest) +{ + assert(rest != nullptr); + auto const found = ends_with(sv, x); + if (found) + { + *rest = sv.substr(0, sv.length() - traits::length(x)); + } + return found; +} +// The following overloads prevent deduction failures that would occur with their template +// counterparts, when x is of a type that is implicitly convertible to basic_string_view (like +// OString or OUString, and we only bother to provide overloads for the char and char16_t cases, not +// also for char32_t and wchar_t, nor for C++20 char8_t): +constexpr bool starts_with(std::string_view sv, std::string_view x, std::string_view* rest) noexcept +{ + return starts_with(sv, x, rest); +} +constexpr bool starts_with(std::u16string_view sv, std::u16string_view x, + std::u16string_view* rest) noexcept +{ + return starts_with(sv, x, rest); +} +constexpr bool ends_with(std::string_view sv, std::string_view x, std::string_view* rest) noexcept +{ + return ends_with(sv, x, rest); +} +constexpr bool ends_with(std::u16string_view sv, std::u16string_view x, + std::u16string_view* rest) noexcept +{ + return ends_with(sv, x, rest); +} + +namespace internal +{ +// copy of implIsWhitespace from sal/rtl/strtmpl.hxx +inline bool implIsWhitespace(sal_Unicode c) +{ + /* Space or Control character? */ + if ((c <= 32) && c) + return true; + + /* Only in the General Punctuation area Space or Control characters are included? */ + if ((c < 0x2000) || (c > 0x206F)) + return false; + + if (((c >= 0x2000) && (c <= 0x200B)) || /* All Spaces */ + (c == 0x2028) || /* LINE SEPARATOR */ + (c == 0x2029)) /* PARAGRAPH SEPARATOR */ + return true; + + return false; +} +} // namespace internal + +// Like OUString::trim, but for std::u16string_view: +// copy of the trimView code from sal/rtl/strtmpl.hxx +inline std::u16string_view trim(std::u16string_view str) +{ + sal_Int32 nLen = str.size(); + sal_Int32 nPreSpaces = 0; + sal_Int32 nPostSpaces = 0; + sal_Int32 nIndex = str.size() - 1; + + while ((nPreSpaces < nLen) && internal::implIsWhitespace(*(str.data() + nPreSpaces))) + nPreSpaces++; + + while ((nIndex > nPreSpaces) && internal::implIsWhitespace(*(str.data() + nIndex))) + { + nPostSpaces++; + nIndex--; + } + + return std::u16string_view{ str.data() + nPreSpaces, + static_cast(nLen - nPostSpaces - nPreSpaces) }; +} + +// Like OString::trim, but for std::string_view: +// copy of the trimView code from sal/rtl/strtmpl.hxx +inline std::string_view trim(std::string_view str) +{ + sal_Int32 nLen = str.size(); + sal_Int32 nPreSpaces = 0; + sal_Int32 nPostSpaces = 0; + sal_Int32 nIndex = str.size() - 1; + + while ((nPreSpaces < nLen) && internal::implIsWhitespace(*(str.data() + nPreSpaces))) + nPreSpaces++; + + while ((nIndex > nPreSpaces) && internal::implIsWhitespace(*(str.data() + nIndex))) + { + nPostSpaces++; + nIndex--; + } + + return std::string_view{ str.data() + nPreSpaces, + static_cast(nLen - nPostSpaces - nPreSpaces) }; +} + +// Like OString::toInt32, but for std::string_view: +inline sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix = 10) +{ + sal_Int64 n = rtl_ustr_toInt64_WithLength(str.data(), radix, str.size()); + if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32) + n = 0; + return n; +} +inline sal_Int32 toInt32(std::string_view str, sal_Int16 radix = 10) +{ + sal_Int64 n = rtl_str_toInt64_WithLength(str.data(), radix, str.size()); + if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32) + n = 0; + return n; +} + +// Like OString::toUInt32, but for std::string_view: +inline sal_uInt32 toUInt32(std::u16string_view str, sal_Int16 radix = 10) +{ + sal_Int64 n = rtl_ustr_toInt64_WithLength(str.data(), radix, str.size()); + if (n < 0 || n > SAL_MAX_UINT32) + n = 0; + return n; +} +inline sal_uInt32 toUInt32(std::string_view str, sal_Int16 radix = 10) +{ + sal_Int64 n = rtl_str_toInt64_WithLength(str.data(), radix, str.size()); + if (n < 0 || n > SAL_MAX_UINT32) + n = 0; + return n; +} + +// Like OString::toInt64, but for std::string_view: +inline sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix = 10) +{ + return rtl_ustr_toInt64_WithLength(str.data(), radix, str.size()); +} +inline sal_Int64 toInt64(std::string_view str, sal_Int16 radix = 10) +{ + return rtl_str_toInt64_WithLength(str.data(), radix, str.size()); +} + +// Like OString::toDouble, but for std::string_view: +inline double toDouble(std::u16string_view str) +{ + return rtl_math_uStringToDouble(str.data(), str.data() + str.size(), '.', 0, nullptr, nullptr); +} +inline double toDouble(std::string_view str) +{ + return rtl_math_stringToDouble(str.data(), str.data() + str.size(), '.', 0, nullptr, nullptr); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/strong_int.hxx b/include/o3tl/strong_int.hxx new file mode 100644 index 000000000..821079558 --- /dev/null +++ b/include/o3tl/strong_int.hxx @@ -0,0 +1,156 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_STRONG_INT_HXX +#define INCLUDED_O3TL_STRONG_INT_HXX + +#include +#include +#include +#include + +namespace o3tl +{ + +#if !defined __COVERITY__ + +namespace detail { + +template constexpr +typename std::enable_if< + std::is_signed::value && std::is_signed::value, bool>::type +isInRange(T2 value) { + return value >= std::numeric_limits::min() + && value <= std::numeric_limits::max(); +} + +template constexpr +typename std::enable_if< + std::is_signed::value && std::is_unsigned::value, bool>::type +isInRange(T2 value) { + return value + <= static_cast::type>( + std::numeric_limits::max()); +} + +template constexpr +typename std::enable_if< + std::is_unsigned::value && std::is_signed::value, bool>::type +isInRange(T2 value) { + return value >= 0 + && (static_cast::type>(value) + <= std::numeric_limits::max()); +} + +template constexpr +typename std::enable_if< + std::is_unsigned::value && std::is_unsigned::value, bool>::type +isInRange(T2 value) { + return value <= std::numeric_limits::max(); +} + +} + +#endif + +/// +/// Wrap up an integer type so that we prevent accidental conversion to other integer types. +/// +/// e.g. +/// typedef o3tl::strong_int MyInt; +/// +/// \param UNDERLYING_TYPE the underlying scalar type +/// \param PHANTOM_TYPE a type tag, used to distinguish this instantiation of the template +/// from other instantiations with the same UNDERLYING_TYPE. +/// +template +struct strong_int +{ +public: + template explicit constexpr strong_int( + T value, + typename std::enable_if::value, int>::type = 0): + m_value(value) + { +#if !defined __COVERITY__ + // catch attempts to pass in out-of-range values early + assert(detail::isInRange(value) + && "out of range"); +#endif + } + strong_int() : m_value(0) {} + + explicit constexpr operator UNDERLYING_TYPE() const { return m_value; } + explicit operator bool() const { return m_value != 0; } + UNDERLYING_TYPE get() const { return m_value; } + + bool operator<(strong_int const & other) const { return m_value < other.m_value; } + bool operator<=(strong_int const & other) const { return m_value <= other.m_value; } + bool operator>(strong_int const & other) const { return m_value > other.m_value; } + bool operator>=(strong_int const & other) const { return m_value >= other.m_value; } + bool operator==(strong_int const & other) const { return m_value == other.m_value; } + bool operator!=(strong_int const & other) const { return m_value != other.m_value; } + strong_int& operator++() { ++m_value; return *this; } + strong_int operator++(int) { UNDERLYING_TYPE nOldValue = m_value; ++m_value; return strong_int(nOldValue); } + strong_int& operator--() { --m_value; return *this; } + strong_int operator--(int) { UNDERLYING_TYPE nOldValue = m_value; --m_value; return strong_int(nOldValue); } + strong_int& operator+=(strong_int const & other) { m_value += other.m_value; return *this; } + strong_int& operator-=(strong_int const & other) { m_value -= other.m_value; return *this; } + strong_int& operator%=(strong_int const & other) { m_value %= other.m_value; return *this; } + strong_int& operator*=(strong_int const & other) { m_value *= other.m_value; return *this; } + strong_int& operator/=(strong_int const & other) { m_value /= other.m_value; return *this; } + [[nodiscard]] + strong_int operator%(strong_int const & other) const { return strong_int(m_value % other.m_value); } + [[nodiscard]] + strong_int operator-() const { return strong_int(-m_value); } + [[nodiscard]] + strong_int operator*(strong_int const & other) const { return strong_int(m_value * other.m_value); } + [[nodiscard]] + strong_int operator/(strong_int const & other) const { return strong_int(m_value / other.m_value); } + + bool anyOf(strong_int v) const { + return *this == v; + } + + template + bool anyOf(strong_int first, Args... args) const { + return *this == first || anyOf(args...); + } + +private: + UNDERLYING_TYPE m_value; +}; + +template +strong_int operator+(strong_int const & lhs, strong_int const & rhs) +{ + return strong_int(lhs.get() + rhs.get()); +} + +template +strong_int operator-(strong_int const & lhs, strong_int const & rhs) +{ + return strong_int(lhs.get() - rhs.get()); +} + +}; // namespace o3tl + +#endif /* INCLUDED_O3TL_STRONG_INT_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/temporary.hxx b/include/o3tl/temporary.hxx new file mode 100644 index 000000000..50d006e26 --- /dev/null +++ b/include/o3tl/temporary.hxx @@ -0,0 +1,27 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_TEMPORARY_HXX +#define INCLUDED_O3TL_TEMPORARY_HXX + +#include + +namespace o3tl +{ +// Cast an rvalue to an lvalue. Can be useful if a function parameter is a pointer/reference to T, +// and some call site doesn't need the value beyond the call itself (e.g., in a call like +// std::modf(x, &o3tl::temporary(double())) to obtain the fractional part of x, ignoring the +// integral part). +template constexpr T& temporary(T&& x) { return static_cast(x); } +template constexpr T& temporary(T&) = delete; +} + +#endif /* INCLUDED_O3TL_TEMPORARY_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/typed_flags_set.hxx b/include/o3tl/typed_flags_set.hxx new file mode 100644 index 000000000..413ee9579 --- /dev/null +++ b/include/o3tl/typed_flags_set.hxx @@ -0,0 +1,325 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_TYPED_FLAGS_SET_HXX +#define INCLUDED_O3TL_TYPED_FLAGS_SET_HXX + +#include + +#include +#include + +#include + +namespace o3tl { + +namespace detail { + +template constexpr +typename std::enable_if::value, bool>::type isNonNegative( + T value) +{ + return value >= 0; +} + +template constexpr +typename std::enable_if::value, bool>::type isNonNegative(T) +{ + return true; +} + +} + +template struct typed_flags {}; + +/// Mark a (scoped) enumeration as a set of bit flags, with accompanying +/// operations. +/// +/// template<> +/// struct o3tl::typed_flags: o3tl::is_typed_flags {}; +/// +/// All relevant values must be non-negative. (Typically, the enumeration's +/// underlying type will either be fixed and unsigned, or it will be unfixed--- +/// and can thus default to a signed type---and all enumerators will have non- +/// negative values.) +/// +/// \param E the enumeration type. +/// \param M the all-bits-set value for the bit flags. +template::type M> +struct is_typed_flags { + static_assert( + M >= 0, "is_typed_flags expects only non-negative bit values"); + + typedef E Self; + + class Wrap { + public: + typedef is_typed_flags Unwrapped; + + explicit constexpr Wrap(typename std::underlying_type::type value): + value_(value) + { + assert(detail::isNonNegative(value)); + assert( + static_cast::type>(~0) == M + // avoid "operands don't affect result" warnings when M + // covers all bits of the underlying type + || (value & ~M) == 0); + } + + constexpr operator E() const { return static_cast(value_); } + + explicit constexpr operator typename std::underlying_type::type() + const + { return value_; } + + explicit constexpr operator bool() const { return value_ != 0; } + + private: + typename std::underlying_type::type value_; + }; + + static typename std::underlying_type::type const mask = M; +}; + +} + +template +constexpr typename o3tl::typed_flags::Wrap operator ~(E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::typed_flags::mask + & ~o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator ~( + typename o3tl::typed_flags::Wrap rhs) +{ + return static_cast::Wrap>( + o3tl::typed_flags::mask + & ~o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator ^( + E lhs, E rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + ^ o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator ^( + E lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + ^ o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator ^( + typename o3tl::typed_flags::Wrap lhs, E rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + ^ o3tl::to_underlying(rhs)); +} + +template constexpr +typename o3tl::typed_flags::Wrap operator ^( + W lhs, W rhs) +{ + return static_cast( + o3tl::to_underlying(lhs) + ^ o3tl::to_underlying(rhs)); +} + +template +constexpr typename o3tl::typed_flags::Wrap operator &(E lhs, E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + & o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator &( + E lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + & o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator &( + typename o3tl::typed_flags::Wrap lhs, E rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + & o3tl::to_underlying(rhs)); +} + +template constexpr +typename o3tl::typed_flags::Wrap operator &( + W lhs, W rhs) +{ + return static_cast( + o3tl::to_underlying(lhs) + & o3tl::to_underlying(rhs)); +} + +template +constexpr typename o3tl::typed_flags::Wrap operator |(E lhs, E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + | o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator |( + E lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + | o3tl::to_underlying(rhs)); +} + +template constexpr typename o3tl::typed_flags::Wrap operator |( + typename o3tl::typed_flags::Wrap lhs, E rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + return static_cast::Wrap>( + o3tl::to_underlying(lhs) + | o3tl::to_underlying(rhs)); +} + +template constexpr +typename o3tl::typed_flags::Wrap operator |( + W lhs, W rhs) +{ + return static_cast( + o3tl::to_underlying(lhs) + | o3tl::to_underlying(rhs)); +} + +template +inline typename o3tl::typed_flags::Self operator &=(E & lhs, E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + lhs = lhs & rhs; + return lhs; +} + +template +inline typename o3tl::typed_flags::Self operator &=( + E & lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + lhs = lhs & rhs; + return lhs; +} + +template +inline typename o3tl::typed_flags::Self operator |=(E & lhs, E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + lhs = lhs | rhs; + return lhs; +} + +template +inline typename o3tl::typed_flags::Self operator |=( + E & lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + lhs = lhs | rhs; + return lhs; +} + +template +inline typename o3tl::typed_flags::Self operator ^=(E & lhs, E rhs) { + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(rhs))); + lhs = lhs ^ rhs; + return lhs; +} + +template +inline typename o3tl::typed_flags::Self operator ^=( + E & lhs, typename o3tl::typed_flags::Wrap rhs) +{ + assert( + o3tl::detail::isNonNegative( + o3tl::to_underlying(lhs))); + lhs = lhs ^ rhs; + return lhs; +} + +#endif /* INCLUDED_O3TL_TYPED_FLAGS_SET_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/underlyingenumvalue.hxx b/include/o3tl/underlyingenumvalue.hxx new file mode 100644 index 000000000..a913ab385 --- /dev/null +++ b/include/o3tl/underlyingenumvalue.hxx @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_UNDERLYINGENUMVALUE_HXX +#define INCLUDED_O3TL_UNDERLYINGENUMVALUE_HXX + +#include + +#include + +namespace o3tl +{ +// An implementation of C++23 std::to_underlying +// For a value e of an enumeration type T, return the corresponding value of T's underlying type: +template constexpr std::underlying_type_t to_underlying(T e) +{ + return static_cast>(e); +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/unit_conversion.hxx b/include/o3tl/unit_conversion.hxx new file mode 100644 index 000000000..67830f0d1 --- /dev/null +++ b/include/o3tl/unit_conversion.hxx @@ -0,0 +1,261 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace o3tl +{ +// Length units +enum class Length +{ + mm100 = 0, // 1/100th mm + mm10, // 1/10 mm, corresponds to MapUnit::Map10thMM + mm, // millimeter + cm, // centimeter + m, // meter + km, // kilometer + emu, // English Metric Unit: 1/360000 cm, 1/914400 in + twip, // "Twentieth of a point" aka "dxa": 1/20 pt + pt, // Point: 1/72 in + pc, // Pica: 1/6 in, corresponds to FieldUnit::PICA and MeasureUnit::PICA + in1000, // 1/1000 in, corresponds to MapUnit::Map1000thInch + in100, // 1/100 in, corresponds to MapUnit::Map100thInch + in10, // 1/10 in, corresponds to MapUnit::Map10thInch + in, // inch + ft, // foot + mi, // mile + master, // PPT Master Unit: 1/576 in + px, // "pixel" unit: 15 twip (96 ppi), corresponds to MeasureUnit::PIXEL + ch, // "char" unit: 210 twip (14 px), corresponds to FieldUnit::CHAR + line, // "line" unit: 312 twip, corresponds to FieldUnit::LINE + count, // <== add new units above this last entry + invalid = -1 +}; + +// If other categories of units would be needed (like time), a separate scoped enum +// should be created, respective conversion array prepared in detail namespace, and +// respective md(NewUnit, NewUnit) overload introduced, which would allow using +// o3tl::convert(), o3tl::convertSaturate() and o3tl::getConversionMulDiv() with the +// new category in a type-safe way, without mixing unrelated units. + +namespace detail +{ +// Common utilities + +// A special function to avoid compiler warning comparing signed and unsigned values +template constexpr bool isBetween(I n, sal_Int64 min, sal_Int64 max) +{ + assert(max > 0 && min < 0); + if constexpr (std::is_signed_v) + return n >= min && n <= max; + else + return n <= sal_uInt64(max); +} + +// Ensure correct rounding for both positive and negative integers +template , int> = 0> +constexpr sal_Int64 MulDiv(I n, sal_Int64 m, sal_Int64 d) +{ + assert(m > 0 && d > 0); + assert(isBetween(n, (SAL_MIN_INT64 + d / 2) / m, (SAL_MAX_INT64 - d / 2) / m)); + return (n >= 0 ? (n * m + d / 2) : (n * m - d / 2)) / d; +} +template , int> = 0> +constexpr double MulDiv(F f, sal_Int64 m, sal_Int64 d) +{ + assert(m > 0 && d > 0); + return f * (double(m) / d); +} + +template , int> = 0> +constexpr sal_Int64 MulDiv(I n, sal_Int64 m, sal_Int64 d, bool& bOverflow, sal_Int64 nDefault) +{ + if (!isBetween(n, (SAL_MIN_INT64 + d / 2) / m, (SAL_MAX_INT64 - d / 2) / m)) + { + bOverflow = true; + return nDefault; + } + bOverflow = false; + return MulDiv(n, m, d); +} + +template , int> = 0> +constexpr sal_Int64 MulDivSaturate(I n, sal_Int64 m, sal_Int64 d) +{ + if (!isBetween(n, (SAL_MIN_INT64 + d / 2) / m, (SAL_MAX_INT64 - d / 2) / m)) + { + if (m > d && !isBetween(n, SAL_MIN_INT64 / m * d + d / 2, SAL_MAX_INT64 / m * d - d / 2)) + return n > 0 ? SAL_MAX_INT64 : SAL_MIN_INT64; // saturate + return (n >= 0 ? n + d / 2 : n - d / 2) / d * m; // divide before multiplication + } + return MulDiv(n, m, d); +} + +template constexpr std::common_type_t asserting_gcd(M m, N n) +{ + auto ret = std::gcd(m, n); + assert(ret != 0); + return ret; +} + +// Packs integral multiplier and divisor for conversion from one unit to another +struct m_and_d +{ + sal_Int64 m; // multiplier + sal_Int64 d; // divisor + constexpr m_and_d(sal_Int64 _m, sal_Int64 _d) + : m(_m / asserting_gcd(_m, _d)) // make sure to use smallest quotients here because + , d(_d / asserting_gcd(_m, _d)) // they will be multiplied when building final table + { + assert(_m > 0 && _d > 0); + } +}; + +// Resulting static array N x N of all quotients to convert between all units. The +// quotients are minimal to allow largest range of converted numbers without overflow. +// Maybe o3tl::enumarray could be used here, but it's not constexpr yet. +template constexpr auto prepareMDArray(const m_and_d (&mdBase)[N]) +{ + std::array, N> a{}; + for (int i = 0; i < N; ++i) + { + a[i][i] = 1; + for (int j = 0; j < i; ++j) + { + assert(mdBase[i].m < SAL_MAX_INT64 / mdBase[j].d); + assert(mdBase[i].d < SAL_MAX_INT64 / mdBase[j].m); + const sal_Int64 m = mdBase[i].m * mdBase[j].d, d = mdBase[i].d * mdBase[j].m; + const sal_Int64 g = asserting_gcd(m, d); + a[i][j] = m / g; + a[j][i] = d / g; + } + } + return a; +} + +// A generic template used for fundamental arithmetic types +template constexpr sal_Int64 md(U i, U /*j*/) { return i; } + +// Length units implementation + +// Array of conversion quotients for mm, used to build final conversion table. Entries +// are { multiplier, divider } to convert respective unit *to* mm. Order of elements +// corresponds to order in o3tl::Length enum (Length::count and Length::invalid omitted). +constexpr m_and_d mdBaseLen[] = { + { 1, 100 }, // mm100 => mm + { 1, 10 }, // mm10 => mm + { 1, 1 }, // mm => mm + { 10, 1 }, // cm => mm + { 1000, 1 }, // m => mm + { 1000000, 1 }, // km => mm + { 1, 36000 }, // emu => mm + { 254, 10 * 1440 }, // twip => mm + { 254, 10 * 72 }, // pt => mm + { 254, 10 * 6 }, // pc => mm + { 254, 10000 }, // in1000 => mm + { 254, 1000 }, // in100 => mm + { 254, 100 }, // in10 => mm + { 254, 10 }, // in => mm + { 254 * 12, 10 }, // ft => mm + { 254 * 12 * 5280, 10 }, // mi => mm + { 254, 10 * 576 }, // master => mm + { 254 * 15, 10 * 1440 }, // px => mm + { 254 * 210, 10 * 1440 }, // ch => mm + { 254 * 312, 10 * 1440 }, // line => mm +}; +static_assert(std::size(mdBaseLen) == static_cast(Length::count), + "mdBaseL must have an entry for each unit in o3tl::Length"); + +// The resulting multipliers and divisors array +constexpr auto aLengthMDArray = prepareMDArray(mdBaseLen); + +// an overload taking Length +constexpr sal_Int64 md(Length i, Length j) +{ + const int nI = static_cast(i), nJ = static_cast(j); + assert(nI >= 0 && o3tl::make_unsigned(nI) < aLengthMDArray.size()); + assert(nJ >= 0 && o3tl::make_unsigned(nJ) < aLengthMDArray.size()); + return aLengthMDArray[nI][nJ]; +} + +// here might go overloads of md() taking other units ... +} + +// Unchecked conversion. Takes a number value, multiplier and divisor +template constexpr auto convert(N n, sal_Int64 mul, sal_Int64 div) +{ + return detail::MulDiv(n, mul, div); +} + +// Unchecked conversion. Takes a number value and units defined in this header +template constexpr auto convert(N n, U from, U to) +{ + return convert(n, detail::md(from, to), detail::md(to, from)); +} + +// Convert to twips - for convenience as we do this a lot +template constexpr auto toTwips(N number, Length from) +{ + return convert(number, from, Length::twip); +} + +// Returns nDefault if intermediate multiplication overflows sal_Int64 (only for integral types). +// On return, bOverflow indicates if overflow happened. nDefault is returned when overflow occurs. +template +constexpr auto convert(N n, U from, U to, bool& bOverflow, sal_Int64 nDefault = 0) +{ + return detail::MulDiv(n, detail::md(from, to), detail::md(to, from), bOverflow, nDefault); +} + +// Conversion with saturation (only for integral types). For too large input returns SAL_MAX_INT64. +// When intermediate multiplication would overflow, but the end result is in sal_Int64 range, the +// precision is decreased because of inversion of multiplication and division. +template constexpr auto convertSaturate(N n, U from, U to) +{ + return detail::MulDivSaturate(n, detail::md(from, to), detail::md(to, from)); +} + +// Conversion with saturation (only for integral types), optimized for return types smaller than +// sal_Int64. In this case, it's easier to clamp input values to known bounds, than to do some +// preprocessing to handle too large input values, just to clamp the result anyway. Use it like: +// +// sal_Int32 n = convertNarrowing(m); +template && std::is_integral_v && sizeof(Out) < sizeof(sal_Int64), + int> = 0> +constexpr Out convertNarrowing(N n) +{ + constexpr sal_Int64 nMin = convertSaturate(std::numeric_limits::min(), to, from); + constexpr sal_Int64 nMax = convertSaturate(std::numeric_limits::max(), to, from); + if (static_cast(n) > nMax) + return std::numeric_limits::max(); + if (static_cast(n) < nMin) + return std::numeric_limits::min(); + return convert(n, from, to); +} + +// Return a pair { multiplier, divisor } for a given conversion +template constexpr std::pair getConversionMulDiv(U from, U to) +{ + return { detail::md(from, to), detail::md(to, from) }; +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/unreachable.hxx b/include/o3tl/unreachable.hxx new file mode 100644 index 000000000..604f7a2f7 --- /dev/null +++ b/include/o3tl/unreachable.hxx @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_UNREACHABLE_HXX +#define INCLUDED_O3TL_UNREACHABLE_HXX + +#include + +#include +#include + +// An approximation of C++23 std::unreachable +// ( "Function to mark +// unreachable code"). + +#if defined __cpp_lib_unreachable + +#define O3TL_UNREACHABLE (::std::unreachable()) + +#else + +// This fallback implementation is inspired by LLVM's LLVM_BUILTIN_UNREACHABLE +// (llvm/include/llvm/Support/Compiler.h). + +#if defined _MSC_VER +#define O3TL_UNREACHABLE_detail __assume(false) +#else // assuming Clang or GCC with support for: +#define O3TL_UNREACHABLE_detail __builtin_unreachable() +#endif + +#define O3TL_UNREACHABLE \ + do \ + { \ + assert(false); \ + O3TL_UNREACHABLE_detail; \ + } while (false) + +#endif + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/unsafe_downcast.hxx b/include/o3tl/unsafe_downcast.hxx new file mode 100644 index 000000000..b49994eff --- /dev/null +++ b/include/o3tl/unsafe_downcast.hxx @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_UNSAFE_DOWNCAST_HXX +#define INCLUDED_O3TL_UNSAFE_DOWNCAST_HXX + +#include +#include + +namespace o3tl +{ +// Do a downcast from polymorphic `BasePtr` to `DerivedPtr` when it is known that `p` is actually +// pointing to an object of derived type (or is a nullptr). This is potentially cheaper than +// dynamic_cast and helps to avoid Coverity warnings about unchecked dynamic_cast. +template +std::enable_if_t< + (std::is_pointer_v< + DerivedPtr> && std::is_pointer_v && std::is_base_of_v, std::remove_pointer_t>), + DerivedPtr> +unsafe_downcast(BasePtr p) +{ + assert(p == nullptr || dynamic_cast(p) != nullptr); + return static_cast(p); +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/vector_pool.hxx b/include/o3tl/vector_pool.hxx new file mode 100644 index 000000000..eec1a1a3c --- /dev/null +++ b/include/o3tl/vector_pool.hxx @@ -0,0 +1,123 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_O3TL_VECTOR_POOL_HXX +#define INCLUDED_O3TL_VECTOR_POOL_HXX + +#include +#include + +namespace o3tl +{ + namespace detail + { + template class simple_pool_impl : + public Container + { + typedef typename Container::value_type value_type; + std::ptrdiff_t mnFirstFreeIndex; + + public: + simple_pool_impl() : + mnFirstFreeIndex(-1) + {} + + std::ptrdiff_t alloc() + { + return store(ValueType()); + } + + std::ptrdiff_t store(const ValueType& rCopy) + { + if( mnFirstFreeIndex != -1 ) + { + std::ptrdiff_t nIdx=mnFirstFreeIndex; + mnFirstFreeIndex = this->at(mnFirstFreeIndex).nextFree; + this->at(nIdx).value = rCopy; + this->at(nIdx).nextFree = -1; + + return nIdx; + } + else + { + this->push_back(value_type(rCopy)); + return this->size()-1; + } + } + + void free( std::ptrdiff_t nIdx ) + { + this->at(nIdx).nextFree = mnFirstFreeIndex; + mnFirstFreeIndex = nIdx; + } + + const ValueType& get( std::ptrdiff_t nIdx ) const + { + return this->operator[](nIdx).value; + } + ValueType& get( std::ptrdiff_t nIdx ) + { + return this->operator[](nIdx).value; + } + }; + + template< typename ValueType > struct struct_from_value + { + struct type + { + type() : + value(), + nextFree(-1) + {} + explicit type( ValueType val ) : + value(std::move(val)), + nextFree(-1) + {} + + ValueType value; + std::ptrdiff_t nextFree; + }; + }; + } + + /** Simple vector-based memory pool allocator + + This template can be used to provide simple pooled memory + allocation from a container class that adheres to the stl + random access container concept. Note that alloc/free works + with _indices_ into the container! + + @example +
+vector_pool myPool;
+int nIdx=myPool.alloc();
+myPool[nIdx] = myVal;
+ ... do stuff ...
+myPool.free(nIdx);
+        
+ */ + template struct vector_pool : + public detail::simple_pool_impl::type > > + {}; +} + +#endif /* INCLUDED_O3TL_VECTOR_POOL_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/o3tl/vector_utils.hxx b/include/o3tl/vector_utils.hxx new file mode 100644 index 000000000..1fe34bcaf --- /dev/null +++ b/include/o3tl/vector_utils.hxx @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#pragma once + +#include +#include +#include + +namespace o3tl +{ +// removes duplicated elements in a vector +template void remove_duplicates(std::vector& rVector) +{ + std::unordered_set aSet; + auto aEnd = std::copy_if(rVector.begin(), rVector.end(), rVector.begin(), + [&aSet](T const& rElement) { return aSet.insert(rElement).second; }); + rVector.erase(aEnd, rVector.end()); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3