1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Internal storage class used e.g. by Maybe and Result. This file doesn't
8 * contain any public declarations. */
10 #ifndef mfbt_MaybeStorageBase_h
11 #define mfbt_MaybeStorageBase_h
13 #include <type_traits>
16 namespace mozilla::detail
{
19 constexpr bool IsTriviallyDestructibleAndCopyable
=
20 std::is_trivially_destructible_v
<T
> &&
21 (std::is_trivially_copy_constructible_v
<T
> ||
22 !std::is_copy_constructible_v
<T
>);
24 template <typename T
, bool TriviallyDestructibleAndCopyable
=
25 IsTriviallyDestructibleAndCopyable
<T
>>
26 struct MaybeStorageBase
;
29 struct MaybeStorageBase
<T
, false> {
31 using NonConstT
= std::remove_const_t
<T
>;
35 explicit Union(const T
& aVal
) : val
{aVal
} {}
37 typename
= std::enable_if_t
<std::is_move_constructible_v
<U
>>>
38 explicit Union(U
&& aVal
) : val
{std::forward
<U
>(aVal
)} {}
39 template <typename
... Args
>
40 explicit Union(std::in_place_t
, Args
&&... aArgs
)
41 : val
{std::forward
<Args
>(aArgs
)...} {}
49 MaybeStorageBase() = default;
50 explicit MaybeStorageBase(const T
& aVal
) : mStorage
{aVal
} {}
51 explicit MaybeStorageBase(T
&& aVal
) : mStorage
{std::move(aVal
)} {}
52 template <typename
... Args
>
53 explicit MaybeStorageBase(std::in_place_t
, Args
&&... aArgs
)
54 : mStorage
{std::in_place
, std::forward
<Args
>(aArgs
)...} {}
56 const T
* addr() const { return &mStorage
.val
; }
57 T
* addr() { return &mStorage
.val
; }
61 struct MaybeStorageBase
<T
, true> {
63 using NonConstT
= std::remove_const_t
<T
>;
66 constexpr Union() : dummy() {}
67 constexpr explicit Union(const T
& aVal
) : val
{aVal
} {}
68 constexpr explicit Union(T
&& aVal
) : val
{std::move(aVal
)} {}
69 template <typename
... Args
>
70 constexpr explicit Union(std::in_place_t
, Args
&&... aArgs
)
71 : val
{std::forward
<Args
>(aArgs
)...} {}
78 constexpr MaybeStorageBase() = default;
79 constexpr explicit MaybeStorageBase(const T
& aVal
) : mStorage
{aVal
} {}
80 constexpr explicit MaybeStorageBase(T
&& aVal
) : mStorage
{std::move(aVal
)} {}
82 template <typename
... Args
>
83 constexpr explicit MaybeStorageBase(std::in_place_t
, Args
&&... aArgs
)
84 : mStorage
{std::in_place
, std::forward
<Args
>(aArgs
)...} {}
86 constexpr const T
* addr() const { return &mStorage
.val
; }
87 constexpr T
* addr() { return &mStorage
.val
; }
90 } // namespace mozilla::detail