/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* A class holding a pair of objects that tries to conserve storage space. */ #ifndef mozilla_Pair_h #define mozilla_Pair_h #include "mozilla/Attributes.h" #include "mozilla/Move.h" #include "mozilla/TypeTraits.h" namespace mozilla { namespace detail { enum StorageType { AsBase, AsMember }; // Optimize storage using the Empty Base Optimization -- that empty base classes // don't take up space -- to optimize size when one or the other class is // stateless and can be used as a base class. // // The extra conditions on storage for B are necessary so that PairHelper won't // ambiguously inherit from either A or B, such that one or the other base class // would be inaccessible. template::value ? detail::AsBase : detail::AsMember, detail::StorageType = IsEmpty::value && !IsBaseOf::value && !IsBaseOf::value ? detail::AsBase : detail::AsMember> struct PairHelper; template struct PairHelper { protected: template PairHelper(AArg&& a, BArg&& b) : firstA(Forward(a)), secondB(Forward(b)) {} A& first() { return firstA; } const A& first() const { return firstA; } B& second() { return secondB; } const B& second() const { return secondB; } void swap(PairHelper& other) { Swap(firstA, other.firstA); Swap(secondB, other.secondB); } private: A firstA; B secondB; }; template struct PairHelper : private B { protected: template PairHelper(AArg&& a, BArg&& b) : B(Forward(b)), firstA(Forward(a)) {} A& first() { return firstA; } const A& first() const { return firstA; } B& second() { return *this; } const B& second() const { return *this; } void swap(PairHelper& other) { Swap(firstA, other.firstA); Swap(static_cast(*this), static_cast(other)); } private: A firstA; }; template struct PairHelper : private A { protected: template PairHelper(AArg&& a, BArg&& b) : A(Forward(a)), secondB(Forward(b)) {} A& first() { return *this; } const A& first() const { return *this; } B& second() { return secondB; } const B& second() const { return secondB; } void swap(PairHelper& other) { Swap(static_cast(*this), static_cast(other)); Swap(secondB, other.secondB); } private: B secondB; }; template struct PairHelper : private A, private B { protected: template PairHelper(AArg&& a, BArg&& b) : A(Forward(a)), B(Forward(b)) {} A& first() { return static_cast(*this); } const A& first() const { return static_cast(*this); } B& second() { return static_cast(*this); } const B& second() const { return static_cast(*this); } void swap(PairHelper& other) { Swap(static_cast(*this), static_cast(other)); Swap(static_cast(*this), static_cast(other)); } }; } // namespace detail /** * Pair is the logical concatenation of an instance of A with an instance B. * Space is conserved when possible. Neither A nor B may be a final class. * * It's typically clearer to have individual A and B member fields. Except if * you want the space-conserving qualities of Pair, you're probably better off * not using this! * * No guarantees are provided about the memory layout of A and B, the order of * initialization or destruction of A and B, and so on. (This is approximately * required to optimize space usage.) The first/second names are merely * conceptual! */ template struct Pair : private detail::PairHelper { typedef typename detail::PairHelper Base; public: template Pair(AArg&& a, BArg&& b) : Base(Forward(a), Forward(b)) {} /** The A instance. */ using Base::first; /** The B instance. */ using Base::second; /** Swap this pair with another pair. */ void swap(Pair& other) { Base::swap(other); } private: Pair(const Pair&) MOZ_DELETE; }; template void Swap(Pair& x, Pair& y) { x.swap(y); } } // namespace mozilla #endif /* mozilla_Pair_h */