diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-03-05 15:41:18 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-03-05 22:11:48 +0100 |
commit | b612cf0e06de231b7f936269db3b51a7f0e8ae3b (patch) | |
tree | 2fc88f5b0a78bc952e99b6ee2d24eed8a8241caa /include | |
parent | efbd217afc52390138195e06e96ef9c8f10bf37b (diff) |
Introduce o3tl::underlyingEnumValue
Change-Id: I6554eb86326159b0da707539f45c411f61c0f3de
Reviewed-on: https://gerrit.libreoffice.org/68761
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/o3tl/typed_flags_set.hxx | 109 | ||||
-rw-r--r-- | include/o3tl/underlyingenumvalue.hxx | 28 |
2 files changed, 77 insertions, 60 deletions
diff --git a/include/o3tl/typed_flags_set.hxx b/include/o3tl/typed_flags_set.hxx index 188258febc58..bf795908bad5 100644 --- a/include/o3tl/typed_flags_set.hxx +++ b/include/o3tl/typed_flags_set.hxx @@ -25,6 +25,7 @@ #include <cassert> #include <type_traits> +#include <o3tl/underlyingenumvalue.hxx> #include <sal/types.h> namespace o3tl { @@ -104,10 +105,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ~(E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( o3tl::typed_flags<E>::mask - & ~static_cast<typename std::underlying_type<E>::type>(rhs)); + & ~o3tl::underlyingEnumValue(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ~( @@ -115,7 +116,7 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ~( { return static_cast<typename o3tl::typed_flags<E>::Wrap>( o3tl::typed_flags<E>::mask - & ~static_cast<typename std::underlying_type<E>::type>(rhs)); + & ~o3tl::underlyingEnumValue<E>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( @@ -123,13 +124,13 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - ^ static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + ^ o3tl::underlyingEnumValue(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( @@ -137,10 +138,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - ^ static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + ^ o3tl::underlyingEnumValue<E>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( @@ -148,10 +149,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator ^( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - ^ static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue<E>(lhs) + ^ o3tl::underlyingEnumValue(rhs)); } template<typename W> constexpr @@ -159,25 +160,21 @@ typename o3tl::typed_flags<typename W::Unwrapped::Self>::Wrap operator ^( W lhs, W rhs) { return static_cast<W>( - static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - lhs) - ^ static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - rhs)); + o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(lhs) + ^ o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator &(E lhs, E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - & static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + & o3tl::underlyingEnumValue(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator &( @@ -185,10 +182,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator &( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - & static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + & o3tl::underlyingEnumValue<E>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator &( @@ -196,10 +193,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator &( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - & static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue<E>(lhs) + & o3tl::underlyingEnumValue(rhs)); } template<typename W> constexpr @@ -207,25 +204,21 @@ typename o3tl::typed_flags<typename W::Unwrapped::Self>::Wrap operator &( W lhs, W rhs) { return static_cast<W>( - static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - lhs) - & static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - rhs)); + o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(lhs) + & o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator |(E lhs, E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - | static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + | o3tl::underlyingEnumValue(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator |( @@ -233,10 +226,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator |( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - | static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue(lhs) + | o3tl::underlyingEnumValue<E>(rhs)); } template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator |( @@ -244,10 +237,10 @@ template<typename E> constexpr typename o3tl::typed_flags<E>::Wrap operator |( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); return static_cast<typename o3tl::typed_flags<E>::Wrap>( - static_cast<typename std::underlying_type<E>::type>(lhs) - | static_cast<typename std::underlying_type<E>::type>(rhs)); + o3tl::underlyingEnumValue<E>(lhs) + | o3tl::underlyingEnumValue(rhs)); } template<typename W> constexpr @@ -255,22 +248,18 @@ typename o3tl::typed_flags<typename W::Unwrapped::Self>::Wrap operator |( W lhs, W rhs) { return static_cast<W>( - static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - lhs) - | static_cast< - typename std::underlying_type<typename W::Unwrapped::Self>::type>( - rhs)); + o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(lhs) + | o3tl::underlyingEnumValue<typename W::Unwrapped::Self>(rhs)); } template<typename E> inline typename o3tl::typed_flags<E>::Self operator &=(E & lhs, E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); lhs = lhs & rhs; return lhs; } @@ -281,7 +270,7 @@ inline typename o3tl::typed_flags<E>::Self operator &=( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); lhs = lhs & rhs; return lhs; } @@ -290,10 +279,10 @@ template<typename E> inline typename o3tl::typed_flags<E>::Self operator |=(E & lhs, E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); lhs = lhs | rhs; return lhs; } @@ -304,7 +293,7 @@ inline typename o3tl::typed_flags<E>::Self operator |=( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); lhs = lhs | rhs; return lhs; } @@ -313,10 +302,10 @@ template<typename E> inline typename o3tl::typed_flags<E>::Self operator ^=(E & lhs, E rhs) { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(rhs))); + o3tl::underlyingEnumValue(rhs))); lhs = lhs ^ rhs; return lhs; } @@ -327,7 +316,7 @@ inline typename o3tl::typed_flags<E>::Self operator ^=( { assert( o3tl::detail::isNonNegative( - static_cast<typename std::underlying_type<E>::type>(lhs))); + o3tl::underlyingEnumValue(lhs))); lhs = lhs ^ rhs; return lhs; } diff --git a/include/o3tl/underlyingenumvalue.hxx b/include/o3tl/underlyingenumvalue.hxx new file mode 100644 index 000000000000..1684b2da17d9 --- /dev/null +++ b/include/o3tl/underlyingenumvalue.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_UNDERLYINGENUMVALUE_HXX +#define INCLUDED_O3TL_UNDERLYINGENUMVALUE_HXX + +#include <sal/config.h> + +#include <type_traits> + +namespace o3tl +{ +// For a value e of an enumeration type T, return the corresponding value of T's underlying type: +template <typename T> constexpr std::underlying_type_t<T> underlyingEnumValue(T e) +{ + return static_cast<std::underlying_type_t<T>>(e); +} +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |