gecko-dev/mfbt/CompactPair.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
6.0 KiB
C
Raw Normal View History

/* -*- 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_CompactPair_h
#define mozilla_CompactPair_h
#include <type_traits>
#include <utility>
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj This was done by: This was done by applying: ``` diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py index 789affde7bbf..fe33c4c7d4d1 100644 --- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py +++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py @@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase): from subprocess import Popen, PIPE, check_output, CalledProcessError diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE) - args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format] + args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes'] if not output_file: args.append("-i") ``` Then running `./mach clang-format -c <commit-hash>` Then undoing that patch. Then running check_spidermonkey_style.py --fixup Then running `./mach clang-format` I had to fix four things: * I needed to move <utility> back down in GuardObjects.h because I was hitting obscure problems with our system include wrappers like this: 0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration 0:03.94 extern void *realloc (void *__ptr, size_t __size) 0:03.94 ^ 0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here 0:03.94 MALLOC_DECL(realloc, void*, void*, size_t) 0:03.94 ^ 0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL' 0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__); 0:03.94 ^ 0:03.94 <scratch space>:178:1: note: expanded from here 0:03.94 realloc_impl 0:03.94 ^ 0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl' 0:03.94 #define realloc_impl mozmem_malloc_impl(realloc) Which I really didn't feel like digging into. * I had to restore the order of TrustOverrideUtils.h and related files in nss because the .inc files depend on TrustOverrideUtils.h being included earlier. * I had to add a missing include to RollingNumber.h * Also had to partially restore include order in JsepSessionImpl.cpp to avoid some -WError issues due to some static inline functions being defined in a header but not used in the rest of the compilation unit. Differential Revision: https://phabricator.services.mozilla.com/D60327 --HG-- extra : moz-landing-system : lando
2020-01-20 16:19:48 +00:00
#include "mozilla/Attributes.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 CompactPairHelper
// won't ambiguously inherit from either A or B, such that one or the other base
// class would be inaccessible.
template <typename A, typename B,
detail::StorageType =
std::is_empty_v<A> ? detail::AsBase : detail::AsMember,
detail::StorageType = std::is_empty_v<B> &&
!std::is_base_of<A, B>::value &&
!std::is_base_of<B, A>::value
? detail::AsBase
: detail::AsMember>
struct CompactPairHelper;
template <typename A, typename B>
struct CompactPairHelper<A, B, AsMember, AsMember> {
protected:
template <typename AArg, typename BArg>
CompactPairHelper(AArg&& aA, BArg&& aB)
: mFirstA(std::forward<AArg>(aA)), mSecondB(std::forward<BArg>(aB)) {}
A& first() { return mFirstA; }
const A& first() const { return mFirstA; }
B& second() { return mSecondB; }
const B& second() const { return mSecondB; }
void swap(CompactPairHelper& aOther) {
std::swap(mFirstA, aOther.mFirstA);
std::swap(mSecondB, aOther.mSecondB);
}
private:
A mFirstA;
B mSecondB;
};
template <typename A, typename B>
struct CompactPairHelper<A, B, AsMember, AsBase> : private B {
protected:
template <typename AArg, typename BArg>
CompactPairHelper(AArg&& aA, BArg&& aB)
: B(std::forward<BArg>(aB)), mFirstA(std::forward<AArg>(aA)) {}
A& first() { return mFirstA; }
const A& first() const { return mFirstA; }
B& second() { return *this; }
const B& second() const { return *this; }
void swap(CompactPairHelper& aOther) {
std::swap(mFirstA, aOther.mFirstA);
std::swap(static_cast<B&>(*this), static_cast<B&>(aOther));
}
private:
A mFirstA;
};
template <typename A, typename B>
struct CompactPairHelper<A, B, AsBase, AsMember> : private A {
protected:
template <typename AArg, typename BArg>
CompactPairHelper(AArg&& aA, BArg&& aB)
: A(std::forward<AArg>(aA)), mSecondB(std::forward<BArg>(aB)) {}
A& first() { return *this; }
const A& first() const { return *this; }
B& second() { return mSecondB; }
const B& second() const { return mSecondB; }
void swap(CompactPairHelper& aOther) {
std::swap(static_cast<A&>(*this), static_cast<A&>(aOther));
std::swap(mSecondB, aOther.mSecondB);
}
private:
B mSecondB;
};
template <typename A, typename B>
struct CompactPairHelper<A, B, AsBase, AsBase> : private A, private B {
protected:
template <typename AArg, typename BArg>
CompactPairHelper(AArg&& aA, BArg&& aB)
: A(std::forward<AArg>(aA)), B(std::forward<BArg>(aB)) {}
A& first() { return static_cast<A&>(*this); }
const A& first() const { return static_cast<A&>(*this); }
B& second() { return static_cast<B&>(*this); }
const B& second() const { return static_cast<B&>(*this); }
void swap(CompactPairHelper& aOther) {
std::swap(static_cast<A&>(*this), static_cast<A&>(aOther));
std::swap(static_cast<B&>(*this), static_cast<B&>(aOther));
}
};
} // namespace detail
/**
* CompactPair 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.
*
* In general if space conservation is not critical is preferred to use
* std::pair.
*
* It's typically clearer to have individual A and B member fields. Except if
* you want the space-conserving qualities of CompactPair, 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 <typename A, typename B>
struct CompactPair : private detail::CompactPairHelper<A, B> {
typedef typename detail::CompactPairHelper<A, B> Base;
public:
template <typename AArg, typename BArg>
CompactPair(AArg&& aA, BArg&& aB)
: Base(std::forward<AArg>(aA), std::forward<BArg>(aB)) {}
CompactPair(CompactPair&& aOther) = default;
CompactPair(const CompactPair& aOther) = default;
CompactPair& operator=(CompactPair&& aOther) = default;
CompactPair& operator=(const CompactPair& aOther) = default;
/** The A instance. */
using Base::first;
/** The B instance. */
using Base::second;
/** Swap this pair with another pair. */
void swap(CompactPair& aOther) { Base::swap(aOther); }
};
/**
* MakeCompactPair allows you to construct a CompactPair instance using type
* inference. A call like this:
*
* MakeCompactPair(Foo(), Bar())
*
* will return a CompactPair<Foo, Bar>.
*/
template <typename A, typename B>
CompactPair<std::remove_cv_t<std::remove_reference_t<A>>,
std::remove_cv_t<std::remove_reference_t<B>>>
MakeCompactPair(A&& aA, B&& aB) {
return CompactPair<std::remove_cv_t<std::remove_reference_t<A>>,
std::remove_cv_t<std::remove_reference_t<B>>>(
std::forward<A>(aA), std::forward<B>(aB));
}
} // namespace mozilla
namespace std {
template <typename A, class B>
void swap(mozilla::CompactPair<A, B>& aX, mozilla::CompactPair<A, B>& aY) {
aX.swap(aY);
}
} // namespace std
#endif /* mozilla_CompactPair_h */