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
12 #include "mozilla/Attributes.h"
13 #include "mozilla/Move.h"
14 #include "mozilla/TypeTraits.h"
20 enum StorageType
{ AsBase
, AsMember
};
22 // Optimize storage using the Empty Base Optimization -- that empty base classes
23 // don't take up space -- to optimize size when one or the other class is
24 // stateless and can be used as a base class.
26 // The extra conditions on storage for B are necessary so that PairHelper won't
27 // ambiguously inherit from either A or B, such that one or the other base class
28 // would be inaccessible.
29 template<typename A
, typename B
,
31 IsEmpty
<A
>::value
? detail::AsBase
: detail::AsMember
,
33 IsEmpty
<B
>::value
&& !IsBaseOf
<A
, B
>::value
&& !IsBaseOf
<B
, A
>::value
38 template<typename A
, typename B
>
39 struct PairHelper
<A
, B
, AsMember
, AsMember
>
42 template<typename AArg
, typename BArg
>
43 PairHelper(AArg
&& aA
, BArg
&& aB
)
44 : mFirstA(Forward
<AArg
>(aA
)),
45 mSecondB(Forward
<BArg
>(aB
))
48 A
& first() { return mFirstA
; }
49 const A
& first() const { return mFirstA
; }
50 B
& second() { return mSecondB
; }
51 const B
& second() const { return mSecondB
; }
53 void swap(PairHelper
& aOther
)
55 Swap(mFirstA
, aOther
.mFirstA
);
56 Swap(mSecondB
, aOther
.mSecondB
);
64 template<typename A
, typename B
>
65 struct PairHelper
<A
, B
, AsMember
, AsBase
> : private B
68 template<typename AArg
, typename BArg
>
69 PairHelper(AArg
&& aA
, BArg
&& aB
)
70 : B(Forward
<BArg
>(aB
)),
71 mFirstA(Forward
<AArg
>(aA
))
74 A
& first() { return mFirstA
; }
75 const A
& first() const { return mFirstA
; }
76 B
& second() { return *this; }
77 const B
& second() const { return *this; }
79 void swap(PairHelper
& aOther
)
81 Swap(mFirstA
, aOther
.mFirstA
);
82 Swap(static_cast<B
&>(*this), static_cast<B
&>(aOther
));
89 template<typename A
, typename B
>
90 struct PairHelper
<A
, B
, AsBase
, AsMember
> : private A
93 template<typename AArg
, typename BArg
>
94 PairHelper(AArg
&& aA
, BArg
&& aB
)
95 : A(Forward
<AArg
>(aA
)),
96 mSecondB(Forward
<BArg
>(aB
))
99 A
& first() { return *this; }
100 const A
& first() const { return *this; }
101 B
& second() { return mSecondB
; }
102 const B
& second() const { return mSecondB
; }
104 void swap(PairHelper
& aOther
)
106 Swap(static_cast<A
&>(*this), static_cast<A
&>(aOther
));
107 Swap(mSecondB
, aOther
.mSecondB
);
114 template<typename A
, typename B
>
115 struct PairHelper
<A
, B
, AsBase
, AsBase
> : private A
, private B
118 template<typename AArg
, typename BArg
>
119 PairHelper(AArg
&& aA
, BArg
&& aB
)
120 : A(Forward
<AArg
>(aA
)),
124 A
& first() { return static_cast<A
&>(*this); }
125 const A
& first() const { return static_cast<A
&>(*this); }
126 B
& second() { return static_cast<B
&>(*this); }
127 const B
& second() const { return static_cast<B
&>(*this); }
129 void swap(PairHelper
& aOther
)
131 Swap(static_cast<A
&>(*this), static_cast<A
&>(aOther
));
132 Swap(static_cast<B
&>(*this), static_cast<B
&>(aOther
));
136 } // namespace detail
139 * Pair is the logical concatenation of an instance of A with an instance B.
140 * Space is conserved when possible. Neither A nor B may be a final class.
142 * It's typically clearer to have individual A and B member fields. Except if
143 * you want the space-conserving qualities of Pair, you're probably better off
146 * No guarantees are provided about the memory layout of A and B, the order of
147 * initialization or destruction of A and B, and so on. (This is approximately
148 * required to optimize space usage.) The first/second names are merely
151 template<typename A
, typename B
>
153 : private detail::PairHelper
<A
, B
>
155 typedef typename
detail::PairHelper
<A
, B
> Base
;
158 template<typename AArg
, typename BArg
>
159 Pair(AArg
&& aA
, BArg
&& aB
)
160 : Base(Forward
<AArg
>(aA
), Forward
<BArg
>(aB
))
163 /** The A instance. */
165 /** The B instance. */
168 /** Swap this pair with another pair. */
169 void swap(Pair
& aOther
) { Base::swap(aOther
); }
172 Pair(const Pair
&) MOZ_DELETE
;
175 template<typename A
, class B
>
177 Swap(Pair
<A
, B
>& aX
, Pair
<A
, B
>& aY
)
182 } // namespace mozilla
184 #endif /* mozilla_Pair_h */