Fix Centurion name.
[0ad.git] / libraries / source / spidermonkey / include-win32-debug / mozilla / Pair.h
blob73901ded06dad0e5ee9957153a236809c8a50192
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. */
9 #ifndef mozilla_Pair_h
10 #define mozilla_Pair_h
12 #include "mozilla/Attributes.h"
13 #include "mozilla/Move.h"
14 #include "mozilla/TypeTraits.h"
16 namespace mozilla {
18 namespace detail {
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,
30 detail::StorageType =
31 IsEmpty<A>::value ? detail::AsBase : detail::AsMember,
32 detail::StorageType = IsEmpty<B>::value && !IsBaseOf<A, B>::value &&
33 !IsBaseOf<B, A>::value
34 ? detail::AsBase
35 : detail::AsMember>
36 struct PairHelper;
38 template <typename A, typename B>
39 struct PairHelper<A, B, AsMember, AsMember> {
40 protected:
41 template <typename AArg, typename BArg>
42 PairHelper(AArg&& aA, BArg&& aB)
43 : mFirstA(std::forward<AArg>(aA)), mSecondB(std::forward<BArg>(aB)) {}
45 A& first() { return mFirstA; }
46 const A& first() const { return mFirstA; }
47 B& second() { return mSecondB; }
48 const B& second() const { return mSecondB; }
50 void swap(PairHelper& aOther) {
51 Swap(mFirstA, aOther.mFirstA);
52 Swap(mSecondB, aOther.mSecondB);
55 private:
56 A mFirstA;
57 B mSecondB;
60 template <typename A, typename B>
61 struct PairHelper<A, B, AsMember, AsBase> : private B {
62 protected:
63 template <typename AArg, typename BArg>
64 PairHelper(AArg&& aA, BArg&& aB)
65 : B(std::forward<BArg>(aB)), mFirstA(std::forward<AArg>(aA)) {}
67 A& first() { return mFirstA; }
68 const A& first() const { return mFirstA; }
69 B& second() { return *this; }
70 const B& second() const { return *this; }
72 void swap(PairHelper& aOther) {
73 Swap(mFirstA, aOther.mFirstA);
74 Swap(static_cast<B&>(*this), static_cast<B&>(aOther));
77 private:
78 A mFirstA;
81 template <typename A, typename B>
82 struct PairHelper<A, B, AsBase, AsMember> : private A {
83 protected:
84 template <typename AArg, typename BArg>
85 PairHelper(AArg&& aA, BArg&& aB)
86 : A(std::forward<AArg>(aA)), mSecondB(std::forward<BArg>(aB)) {}
88 A& first() { return *this; }
89 const A& first() const { return *this; }
90 B& second() { return mSecondB; }
91 const B& second() const { return mSecondB; }
93 void swap(PairHelper& aOther) {
94 Swap(static_cast<A&>(*this), static_cast<A&>(aOther));
95 Swap(mSecondB, aOther.mSecondB);
98 private:
99 B mSecondB;
102 template <typename A, typename B>
103 struct PairHelper<A, B, AsBase, AsBase> : private A, private B {
104 protected:
105 template <typename AArg, typename BArg>
106 PairHelper(AArg&& aA, BArg&& aB)
107 : A(std::forward<AArg>(aA)), B(std::forward<BArg>(aB)) {}
109 A& first() { return static_cast<A&>(*this); }
110 const A& first() const { return static_cast<A&>(*this); }
111 B& second() { return static_cast<B&>(*this); }
112 const B& second() const { return static_cast<B&>(*this); }
114 void swap(PairHelper& aOther) {
115 Swap(static_cast<A&>(*this), static_cast<A&>(aOther));
116 Swap(static_cast<B&>(*this), static_cast<B&>(aOther));
120 } // namespace detail
123 * Pair is the logical concatenation of an instance of A with an instance B.
124 * Space is conserved when possible. Neither A nor B may be a final class.
126 * It's typically clearer to have individual A and B member fields. Except if
127 * you want the space-conserving qualities of Pair, you're probably better off
128 * not using this!
130 * No guarantees are provided about the memory layout of A and B, the order of
131 * initialization or destruction of A and B, and so on. (This is approximately
132 * required to optimize space usage.) The first/second names are merely
133 * conceptual!
135 template <typename A, typename B>
136 struct Pair : private detail::PairHelper<A, B> {
137 typedef typename detail::PairHelper<A, B> Base;
139 public:
140 template <typename AArg, typename BArg>
141 Pair(AArg&& aA, BArg&& aB)
142 : Base(std::forward<AArg>(aA), std::forward<BArg>(aB)) {}
144 Pair(Pair&& aOther)
145 : Base(std::move(aOther.first()), std::move(aOther.second())) {}
147 Pair(const Pair& aOther) = default;
149 Pair& operator=(Pair&& aOther) {
150 MOZ_ASSERT(this != &aOther, "Self-moves are prohibited");
152 first() = std::move(aOther.first());
153 second() = std::move(aOther.second());
155 return *this;
158 Pair& operator=(const Pair& aOther) = default;
160 /** The A instance. */
161 using Base::first;
162 /** The B instance. */
163 using Base::second;
165 /** Swap this pair with another pair. */
166 void swap(Pair& aOther) { Base::swap(aOther); }
169 template <typename A, class B>
170 void Swap(Pair<A, B>& aX, Pair<A, B>& aY) {
171 aX.swap(aY);
175 * MakePair allows you to construct a Pair instance using type inference. A call
176 * like this:
178 * MakePair(Foo(), Bar())
180 * will return a Pair<Foo, Bar>.
182 template <typename A, typename B>
183 Pair<typename RemoveCV<typename RemoveReference<A>::Type>::Type,
184 typename RemoveCV<typename RemoveReference<B>::Type>::Type>
185 MakePair(A&& aA, B&& aB) {
186 return Pair<typename RemoveCV<typename RemoveReference<A>::Type>::Type,
187 typename RemoveCV<typename RemoveReference<B>::Type>::Type>(
188 std::forward<A>(aA), std::forward<B>(aB));
191 } // namespace mozilla
193 #endif /* mozilla_Pair_h */