1 /* -*- Mode: C++; tab-width: 8; 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 /* A class holding a pair of objects that tries to conserve storage space. */
10 #define mozilla_Pair_h
14 #include "mozilla/Attributes.h"
15 #include "mozilla/TypeTraits.h"
21 enum StorageType
{ AsBase
, AsMember
};
23 // Optimize storage using the Empty Base Optimization -- that empty base classes
24 // don't take up space -- to optimize size when one or the other class is
25 // stateless and can be used as a base class.
27 // The extra conditions on storage for B are necessary so that PairHelper won't
28 // ambiguously inherit from either A or B, such that one or the other base class
29 // would be inaccessible.
31 typename A
, typename B
,
32 detail::StorageType
= IsEmpty
<A
>::value
? detail::AsBase
: detail::AsMember
,
33 detail::StorageType
= IsEmpty
<B
>::value
&& !std::is_base_of
<A
, B
>::value
&&
34 !std::is_base_of
<B
, A
>::value
39 template <typename A
, typename B
>
40 struct PairHelper
<A
, B
, AsMember
, AsMember
> {
42 template <typename AArg
, typename BArg
>
43 PairHelper(AArg
&& aA
, BArg
&& aB
)
44 : mFirstA(std::forward
<AArg
>(aA
)), mSecondB(std::forward
<BArg
>(aB
)) {}
46 A
& first() { return mFirstA
; }
47 const A
& first() const { return mFirstA
; }
48 B
& second() { return mSecondB
; }
49 const B
& second() const { return mSecondB
; }
51 void swap(PairHelper
& aOther
) {
52 std::swap(mFirstA
, aOther
.mFirstA
);
53 std::swap(mSecondB
, aOther
.mSecondB
);
61 template <typename A
, typename B
>
62 struct PairHelper
<A
, B
, AsMember
, AsBase
> : private B
{
64 template <typename AArg
, typename BArg
>
65 PairHelper(AArg
&& aA
, BArg
&& aB
)
66 : B(std::forward
<BArg
>(aB
)), mFirstA(std::forward
<AArg
>(aA
)) {}
68 A
& first() { return mFirstA
; }
69 const A
& first() const { return mFirstA
; }
70 B
& second() { return *this; }
71 const B
& second() const { return *this; }
73 void swap(PairHelper
& aOther
) {
74 std::swap(mFirstA
, aOther
.mFirstA
);
75 std::swap(static_cast<B
&>(*this), static_cast<B
&>(aOther
));
82 template <typename A
, typename B
>
83 struct PairHelper
<A
, B
, AsBase
, AsMember
> : private A
{
85 template <typename AArg
, typename BArg
>
86 PairHelper(AArg
&& aA
, BArg
&& aB
)
87 : A(std::forward
<AArg
>(aA
)), mSecondB(std::forward
<BArg
>(aB
)) {}
89 A
& first() { return *this; }
90 const A
& first() const { return *this; }
91 B
& second() { return mSecondB
; }
92 const B
& second() const { return mSecondB
; }
94 void swap(PairHelper
& aOther
) {
95 std::swap(static_cast<A
&>(*this), static_cast<A
&>(aOther
));
96 std::swap(mSecondB
, aOther
.mSecondB
);
103 template <typename A
, typename B
>
104 struct PairHelper
<A
, B
, AsBase
, AsBase
> : private A
, private B
{
106 template <typename AArg
, typename BArg
>
107 PairHelper(AArg
&& aA
, BArg
&& aB
)
108 : A(std::forward
<AArg
>(aA
)), B(std::forward
<BArg
>(aB
)) {}
110 A
& first() { return static_cast<A
&>(*this); }
111 const A
& first() const { return static_cast<A
&>(*this); }
112 B
& second() { return static_cast<B
&>(*this); }
113 const B
& second() const { return static_cast<B
&>(*this); }
115 void swap(PairHelper
& aOther
) {
116 std::swap(static_cast<A
&>(*this), static_cast<A
&>(aOther
));
117 std::swap(static_cast<B
&>(*this), static_cast<B
&>(aOther
));
121 } // namespace detail
124 * Pair is the logical concatenation of an instance of A with an instance B.
125 * Space is conserved when possible. Neither A nor B may be a final class.
127 * It's typically clearer to have individual A and B member fields. Except if
128 * you want the space-conserving qualities of Pair, you're probably better off
131 * No guarantees are provided about the memory layout of A and B, the order of
132 * initialization or destruction of A and B, and so on. (This is approximately
133 * required to optimize space usage.) The first/second names are merely
136 template <typename A
, typename B
>
137 struct Pair
: private detail::PairHelper
<A
, B
> {
138 typedef typename
detail::PairHelper
<A
, B
> Base
;
141 template <typename AArg
, typename BArg
>
142 Pair(AArg
&& aA
, BArg
&& aB
)
143 : Base(std::forward
<AArg
>(aA
), std::forward
<BArg
>(aB
)) {}
146 : Base(std::move(aOther
.first()), std::move(aOther
.second())) {}
148 Pair(const Pair
& aOther
) = default;
150 Pair
& operator=(Pair
&& aOther
) {
151 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
153 first() = std::move(aOther
.first());
154 second() = std::move(aOther
.second());
159 Pair
& operator=(const Pair
& aOther
) = default;
161 /** The A instance. */
163 /** The B instance. */
166 /** Swap this pair with another pair. */
167 void swap(Pair
& aOther
) { Base::swap(aOther
); }
171 * MakePair allows you to construct a Pair instance using type inference. A call
174 * MakePair(Foo(), Bar())
176 * will return a Pair<Foo, Bar>.
178 template <typename A
, typename B
>
179 Pair
<typename RemoveCV
<typename RemoveReference
<A
>::Type
>::Type
,
180 typename RemoveCV
<typename RemoveReference
<B
>::Type
>::Type
>
181 MakePair(A
&& aA
, B
&& aB
) {
182 return Pair
<typename RemoveCV
<typename RemoveReference
<A
>::Type
>::Type
,
183 typename RemoveCV
<typename RemoveReference
<B
>::Type
>::Type
>(
184 std::forward
<A
>(aA
), std::forward
<B
>(aB
));
187 } // namespace mozilla
191 template <typename A
, class B
>
192 void swap(mozilla::Pair
<A
, B
>& aX
, mozilla::Pair
<A
, B
>& aY
) {
198 #endif /* mozilla_Pair_h */