From 321c2ea91cb1aed5dfbbdbb868d525ff64972398 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Wed, 7 Jul 2021 21:27:27 +0200 Subject: [PATCH] [libc++][NFC] Move monostate to its own header. The format library uses `std::monostate`, but not a `std::variant`. Moving `std::monostate` to its own header allows the format library to reduce the amount of included code. Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D105582 --- libcxx/include/CMakeLists.txt | 1 + libcxx/include/__variant/monostate.h | 65 ++++++++++++++++++++++++++++ libcxx/include/module.modulemap | 4 ++ libcxx/include/variant | 32 +------------- 4 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 libcxx/include/__variant/monostate.h diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index a1d5374c30a9..b9c585dbf1aa 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -237,6 +237,7 @@ set(files __utility/rel_ops.h __utility/swap.h __utility/to_underlying.h + __variant/monostate.h algorithm any array diff --git a/libcxx/include/__variant/monostate.h b/libcxx/include/__variant/monostate.h new file mode 100644 index 000000000000..36e3eead1a47 --- /dev/null +++ b/libcxx/include/__variant/monostate.h @@ -0,0 +1,65 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___VARIANT_MONOSTATE_H +#define _LIBCPP___VARIANT_MONOSTATE_H + +#include <__config> +#include <__functional/hash.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 14 + +struct _LIBCPP_TEMPLATE_VIS monostate {}; + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator<(monostate, monostate) noexcept { return false; } + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator>(monostate, monostate) noexcept { return false; } + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator<=(monostate, monostate) noexcept { return true; } + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator>=(monostate, monostate) noexcept { return true; } + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator==(monostate, monostate) noexcept { return true; } + +inline _LIBCPP_INLINE_VISIBILITY +constexpr bool operator!=(monostate, monostate) noexcept { return false; } + +template <> +struct _LIBCPP_TEMPLATE_VIS hash { + using argument_type = monostate; + using result_type = size_t; + + inline _LIBCPP_INLINE_VISIBILITY + result_type operator()(const argument_type&) const _NOEXCEPT { + return 66740831; // return a fundamentally attractive random value. + } +}; + +#endif // _LIBCPP_STD_VER > 14 + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___VARIANT_MONOSTATE_H diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index eb4488b6342a..c2e6564bb124 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -735,6 +735,10 @@ module std [system] { module variant { header "variant" export * + + module __variant { + module monostate { header "__variant/monostate.h" } + } } module vector { header "vector" diff --git a/libcxx/include/variant b/libcxx/include/variant index 2145700e5a68..08b64612d133 100644 --- a/libcxx/include/variant +++ b/libcxx/include/variant @@ -202,6 +202,7 @@ namespace std { #include <__availability> #include <__config> #include <__utility/forward.h> +#include <__variant/monostate.h> #include <__tuple> #include #include @@ -1703,26 +1704,6 @@ inline _LIBCPP_INLINE_VISIBILITY } #endif -struct _LIBCPP_TEMPLATE_VIS monostate {}; - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator<(monostate, monostate) noexcept { return false; } - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator>(monostate, monostate) noexcept { return false; } - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator<=(monostate, monostate) noexcept { return true; } - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator>=(monostate, monostate) noexcept { return true; } - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator==(monostate, monostate) noexcept { return true; } - -inline _LIBCPP_INLINE_VISIBILITY -constexpr bool operator!=(monostate, monostate) noexcept { return false; } - template inline _LIBCPP_INLINE_VISIBILITY auto swap(variant<_Types...>& __lhs, @@ -1755,17 +1736,6 @@ struct _LIBCPP_TEMPLATE_VIS hash< } }; -template <> -struct _LIBCPP_TEMPLATE_VIS hash { - using argument_type = monostate; - using result_type = size_t; - - inline _LIBCPP_INLINE_VISIBILITY - result_type operator()(const argument_type&) const _NOEXCEPT { - return 66740831; // return a fundamentally attractive random value. - } -}; - #endif // _LIBCPP_STD_VER > 14 _LIBCPP_END_NAMESPACE_STD