2015-05-22 05:33:49 +03:00
|
|
|
/* -*- 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 variadic tuple class. */
|
|
|
|
|
|
|
|
#ifndef mozilla_Tuple_h
|
|
|
|
#define mozilla_Tuple_h
|
|
|
|
|
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 19:19:48 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2020-03-28 16:57:15 +03:00
|
|
|
#include <type_traits>
|
2020-01-20 19:18:20 +03:00
|
|
|
#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 19:19:48 +03:00
|
|
|
|
2020-03-17 15:42:12 +03:00
|
|
|
#include "mozilla/CompactPair.h"
|
2015-05-22 05:33:49 +03:00
|
|
|
#include "mozilla/TemplateLib.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A helper class that allows passing around multiple variadic argument lists
|
|
|
|
* by grouping them.
|
|
|
|
*/
|
|
|
|
template <typename... Ts>
|
|
|
|
struct Group;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CheckConvertibility checks whether each type in a source pack of types
|
|
|
|
* is convertible to the corresponding type in a target pack of types.
|
|
|
|
*
|
|
|
|
* It is intended to be invoked like this:
|
|
|
|
* CheckConvertibility<Group<SourceTypes...>, Group<TargetTypes...>>
|
|
|
|
* 'Group' is used to separate types in the two packs (otherwise if we just
|
|
|
|
* wrote 'CheckConvertibility<SourceTypes..., TargetTypes...', it couldn't
|
|
|
|
* know where the first pack ends and the second begins).
|
|
|
|
*
|
|
|
|
* Note that we need to check explicitly that the two packs are of the same
|
|
|
|
* size, because attempting to simultaneously expand two parameter packs
|
|
|
|
* is an error (and it would be a hard error, because it wouldn't be in the
|
|
|
|
* immediate context of the caller).
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename Source, typename Target, bool SameSize>
|
|
|
|
struct CheckConvertibilityImpl;
|
|
|
|
|
|
|
|
template <typename Source, typename Target>
|
|
|
|
struct CheckConvertibilityImpl<Source, Target, false> : FalseType {};
|
|
|
|
|
|
|
|
template <typename... SourceTypes, typename... TargetTypes>
|
|
|
|
struct CheckConvertibilityImpl<Group<SourceTypes...>, Group<TargetTypes...>,
|
|
|
|
true>
|
|
|
|
: IntegralConstant<
|
|
|
|
bool,
|
2020-03-28 16:57:17 +03:00
|
|
|
tl::And<std::is_convertible_v<SourceTypes, TargetTypes>...>::value> {
|
|
|
|
};
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
template <typename Source, typename Target>
|
|
|
|
struct CheckConvertibility;
|
|
|
|
|
|
|
|
template <typename... SourceTypes, typename... TargetTypes>
|
|
|
|
struct CheckConvertibility<Group<SourceTypes...>, Group<TargetTypes...>>
|
|
|
|
: CheckConvertibilityImpl<Group<SourceTypes...>, Group<TargetTypes...>,
|
|
|
|
sizeof...(SourceTypes) ==
|
|
|
|
sizeof...(TargetTypes)> {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TupleImpl is a helper class used to implement mozilla::Tuple.
|
|
|
|
* It represents one node in a recursive inheritance hierarchy.
|
|
|
|
* 'Index' is the 0-based index of the tuple element stored in this node;
|
|
|
|
* 'Elements...' are the types of the elements stored in this node and its
|
|
|
|
* base classes.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* Tuple<int, float, char> inherits from
|
|
|
|
* TupleImpl<0, int, float, char>, which stores the 'int' and inherits from
|
|
|
|
* TupleImpl<1, float, char>, which stores the 'float' and inherits from
|
|
|
|
* TupleImpl<2, char>, which stores the 'char' and inherits from
|
|
|
|
* TupleImpl<3>, which stores nothing and terminates the recursion.
|
|
|
|
*
|
|
|
|
* The purpose of the 'Index' parameter is to allow efficient index-based
|
|
|
|
* access to a tuple element: given a tuple, and an index 'I' that we wish to
|
|
|
|
* access, we can cast the tuple to the base which stores the I'th element
|
|
|
|
* by performing template argument deduction against 'TupleImpl<I, E...>',
|
|
|
|
* where 'I' is specified explicitly and 'E...' is deduced (this is what the
|
|
|
|
* non-member 'Get<N>(t)' function does).
|
|
|
|
*
|
|
|
|
* This implementation strategy is borrowed from libstdc++'s std::tuple
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
struct TupleImpl;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The base case of the inheritance recursion (and also the implementation
|
|
|
|
* of an empty tuple).
|
|
|
|
*/
|
|
|
|
template <std::size_t Index>
|
2015-12-14 23:01:17 +03:00
|
|
|
struct TupleImpl<Index> {
|
|
|
|
bool operator==(const TupleImpl<Index>& aOther) const { return true; }
|
2018-08-20 03:42:59 +03:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
void ForEach(const F& aFunc) {}
|
2015-12-14 23:01:17 +03:00
|
|
|
};
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* One node of the recursive inheritance hierarchy. It stores the element at
|
|
|
|
* index 'Index' of a tuple, of type 'HeadT', and inherits from the nodes
|
|
|
|
* that store the remaining elements, of types 'TailT...'.
|
|
|
|
*/
|
|
|
|
template <std::size_t Index, typename HeadT, typename... TailT>
|
|
|
|
struct TupleImpl<Index, HeadT, TailT...>
|
|
|
|
: public TupleImpl<Index + 1, TailT...> {
|
|
|
|
typedef TupleImpl<Index + 1, TailT...> Base;
|
|
|
|
|
|
|
|
// Accessors for the head and the tail.
|
|
|
|
// These are static, because the intended usage is for the caller to,
|
|
|
|
// given a tuple, obtain the type B of the base class which stores the
|
|
|
|
// element of interest, and then call B::Head(tuple) to access it.
|
|
|
|
// (Tail() is mostly for internal use, but is exposed for consistency.)
|
|
|
|
static HeadT& Head(TupleImpl& aTuple) { return aTuple.mHead; }
|
|
|
|
static const HeadT& Head(const TupleImpl& aTuple) { return aTuple.mHead; }
|
|
|
|
static Base& Tail(TupleImpl& aTuple) { return aTuple; }
|
|
|
|
static const Base& Tail(const TupleImpl& aTuple) { return aTuple; }
|
|
|
|
|
|
|
|
TupleImpl() : Base(), mHead() {}
|
|
|
|
|
|
|
|
// Construct from const references to the elements.
|
|
|
|
explicit TupleImpl(const HeadT& aHead, const TailT&... aTail)
|
|
|
|
: Base(aTail...), mHead(aHead) {}
|
|
|
|
|
|
|
|
// Construct from objects that are convertible to the elements.
|
|
|
|
// This constructor is enabled only when the argument types are actually
|
|
|
|
// convertible to the element types, otherwise it could become a better
|
|
|
|
// match for certain invocations than the copy constructor.
|
|
|
|
template <typename OtherHeadT, typename... OtherTailT,
|
|
|
|
typename = typename EnableIf<
|
|
|
|
CheckConvertibility<Group<OtherHeadT, OtherTailT...>,
|
|
|
|
Group<HeadT, TailT...>>::value>::Type>
|
|
|
|
explicit TupleImpl(OtherHeadT&& aHead, OtherTailT&&... aTail)
|
2018-06-01 19:30:30 +03:00
|
|
|
: Base(std::forward<OtherTailT>(aTail)...),
|
|
|
|
mHead(std::forward<OtherHeadT>(aHead)) {}
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
// Copy and move constructors.
|
|
|
|
// We'd like to use '= default' to implement these, but MSVC 2013's support
|
|
|
|
// for '= default' is incomplete and this doesn't work.
|
|
|
|
TupleImpl(const TupleImpl& aOther)
|
|
|
|
: Base(Tail(aOther)), mHead(Head(aOther)) {}
|
|
|
|
TupleImpl(TupleImpl&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: Base(std::move(Tail(aOther))),
|
2018-06-01 19:30:30 +03:00
|
|
|
mHead(std::forward<HeadT>(Head(aOther))) {}
|
2015-07-18 10:48:39 +03:00
|
|
|
|
|
|
|
// Assign from a tuple whose elements are convertible to the elements
|
|
|
|
// of this tuple.
|
|
|
|
template <typename... OtherElements,
|
|
|
|
typename = typename EnableIf<sizeof...(OtherElements) ==
|
|
|
|
sizeof...(TailT) + 1>::Type>
|
|
|
|
TupleImpl& operator=(const TupleImpl<Index, OtherElements...>& aOther) {
|
|
|
|
typedef TupleImpl<Index, OtherElements...> OtherT;
|
|
|
|
Head(*this) = OtherT::Head(aOther);
|
|
|
|
Tail(*this) = OtherT::Tail(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename... OtherElements,
|
|
|
|
typename = typename EnableIf<sizeof...(OtherElements) ==
|
|
|
|
sizeof...(TailT) + 1>::Type>
|
|
|
|
TupleImpl& operator=(TupleImpl<Index, OtherElements...>&& aOther) {
|
|
|
|
typedef TupleImpl<Index, OtherElements...> OtherT;
|
2018-05-30 22:15:35 +03:00
|
|
|
Head(*this) = std::move(OtherT::Head(aOther));
|
|
|
|
Tail(*this) = std::move(OtherT::Tail(aOther));
|
2015-07-18 10:48:39 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
// Copy and move assignment operators.
|
|
|
|
TupleImpl& operator=(const TupleImpl& aOther) {
|
|
|
|
Head(*this) = Head(aOther);
|
|
|
|
Tail(*this) = Tail(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
TupleImpl& operator=(TupleImpl&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
Head(*this) = std::move(Head(aOther));
|
|
|
|
Tail(*this) = std::move(Tail(aOther));
|
2015-05-22 05:33:49 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2015-12-14 23:01:17 +03:00
|
|
|
bool operator==(const TupleImpl& aOther) const {
|
|
|
|
return Head(*this) == Head(aOther) && Tail(*this) == Tail(aOther);
|
|
|
|
}
|
2018-08-20 03:42:59 +03:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
void ForEach(const F& aFunc) const& {
|
|
|
|
aFunc(Head(*this));
|
|
|
|
Tail(*this).ForEach(aFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
void ForEach(const F& aFunc) & {
|
|
|
|
aFunc(Head(*this));
|
|
|
|
Tail(*this).ForEach(aFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
void ForEach(const F& aFunc) && {
|
|
|
|
aFunc(std::move(Head(*this)));
|
|
|
|
std::move(Tail(*this)).ForEach(aFunc);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-22 05:33:49 +03:00
|
|
|
private:
|
|
|
|
HeadT mHead; // The element stored at this index in the tuple.
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace detail
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tuple is a class that stores zero or more objects, whose types are specified
|
2020-03-17 15:42:12 +03:00
|
|
|
* as template parameters. It can be thought of as a generalization of
|
|
|
|
* std::pair, (which can be thought of as a 2-tuple).
|
2015-05-22 05:33:49 +03:00
|
|
|
*
|
|
|
|
* Tuple allows index-based access to its elements (with the index having to be
|
|
|
|
* known at compile time) via the non-member function 'Get<N>(tuple)'.
|
|
|
|
*/
|
|
|
|
template <typename... Elements>
|
|
|
|
class Tuple : public detail::TupleImpl<0, Elements...> {
|
|
|
|
typedef detail::TupleImpl<0, Elements...> Impl;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-22 05:33:49 +03:00
|
|
|
public:
|
|
|
|
// The constructors and assignment operators here are simple wrappers
|
|
|
|
// around those in TupleImpl.
|
|
|
|
|
|
|
|
Tuple() : Impl() {}
|
|
|
|
explicit Tuple(const Elements&... aElements) : Impl(aElements...) {}
|
|
|
|
// Here, we can't just use 'typename... OtherElements' because MSVC will give
|
|
|
|
// a warning "C4520: multiple default constructors specified" (even if no one
|
|
|
|
// actually instantiates the constructor with an empty parameter pack -
|
|
|
|
// that's probably a bug) and we compile with warnings-as-errors.
|
|
|
|
template <typename OtherHead, typename... OtherTail,
|
|
|
|
typename = typename EnableIf<detail::CheckConvertibility<
|
|
|
|
detail::Group<OtherHead, OtherTail...>,
|
|
|
|
detail::Group<Elements...>>::value>::Type>
|
|
|
|
explicit Tuple(OtherHead&& aHead, OtherTail&&... aTail)
|
2018-06-01 19:30:30 +03:00
|
|
|
: Impl(std::forward<OtherHead>(aHead),
|
|
|
|
std::forward<OtherTail>(aTail)...) {}
|
2015-05-22 05:33:49 +03:00
|
|
|
Tuple(const Tuple& aOther) : Impl(aOther) {}
|
2018-05-30 22:15:35 +03:00
|
|
|
Tuple(Tuple&& aOther) : Impl(std::move(aOther)) {}
|
2015-05-22 05:33:49 +03:00
|
|
|
|
2015-07-18 10:48:39 +03:00
|
|
|
template <typename... OtherElements,
|
|
|
|
typename = typename EnableIf<sizeof...(OtherElements) ==
|
|
|
|
sizeof...(Elements)>::Type>
|
|
|
|
Tuple& operator=(const Tuple<OtherElements...>& aOther) {
|
|
|
|
static_cast<Impl&>(*this) = aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename... OtherElements,
|
|
|
|
typename = typename EnableIf<sizeof...(OtherElements) ==
|
|
|
|
sizeof...(Elements)>::Type>
|
|
|
|
Tuple& operator=(Tuple<OtherElements...>&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
static_cast<Impl&>(*this) = std::move(aOther);
|
2015-07-18 10:48:39 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2015-05-22 05:33:49 +03:00
|
|
|
Tuple& operator=(const Tuple& aOther) {
|
|
|
|
static_cast<Impl&>(*this) = aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Tuple& operator=(Tuple&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
static_cast<Impl&>(*this) = std::move(aOther);
|
2015-05-22 05:33:49 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2015-12-14 23:01:17 +03:00
|
|
|
bool operator==(const Tuple& aOther) const {
|
|
|
|
return static_cast<const Impl&>(*this) == static_cast<const Impl&>(aOther);
|
|
|
|
}
|
2015-05-22 05:33:49 +03:00
|
|
|
};
|
|
|
|
|
2015-07-24 01:42:00 +03:00
|
|
|
/**
|
|
|
|
* Specialization of Tuple for two elements.
|
2020-03-17 15:42:12 +03:00
|
|
|
* This is created to support construction and assignment from a CompactPair or
|
2015-07-24 01:42:00 +03:00
|
|
|
* std::pair.
|
|
|
|
*/
|
|
|
|
template <typename A, typename B>
|
|
|
|
class Tuple<A, B> : public detail::TupleImpl<0, A, B> {
|
|
|
|
typedef detail::TupleImpl<0, A, B> Impl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// The constructors and assignment operators here are simple wrappers
|
|
|
|
// around those in TupleImpl.
|
|
|
|
|
|
|
|
Tuple() : Impl() {}
|
|
|
|
explicit Tuple(const A& aA, const B& aB) : Impl(aA, aB) {}
|
|
|
|
template <typename AArg, typename BArg,
|
|
|
|
typename = typename EnableIf<detail::CheckConvertibility<
|
|
|
|
detail::Group<AArg, BArg>, detail::Group<A, B>>::value>::Type>
|
|
|
|
explicit Tuple(AArg&& aA, BArg&& aB)
|
2018-06-01 19:30:30 +03:00
|
|
|
: Impl(std::forward<AArg>(aA), std::forward<BArg>(aB)) {}
|
2015-07-24 01:42:00 +03:00
|
|
|
Tuple(const Tuple& aOther) : Impl(aOther) {}
|
2018-05-30 22:15:35 +03:00
|
|
|
Tuple(Tuple&& aOther) : Impl(std::move(aOther)) {}
|
2020-03-17 15:42:12 +03:00
|
|
|
explicit Tuple(const CompactPair<A, B>& aOther)
|
2015-07-24 01:42:00 +03:00
|
|
|
: Impl(aOther.first(), aOther.second()) {}
|
2020-03-17 15:42:12 +03:00
|
|
|
explicit Tuple(CompactPair<A, B>&& aOther)
|
2018-06-01 19:30:30 +03:00
|
|
|
: Impl(std::forward<A>(aOther.first()),
|
|
|
|
std::forward<B>(aOther.second())) {}
|
2015-07-24 01:42:00 +03:00
|
|
|
explicit Tuple(const std::pair<A, B>& aOther)
|
|
|
|
: Impl(aOther.first, aOther.second) {}
|
2018-06-01 19:30:30 +03:00
|
|
|
explicit Tuple(std::pair<A, B>&& aOther)
|
|
|
|
: Impl(std::forward<A>(aOther.first), std::forward<B>(aOther.second)) {}
|
2015-07-24 01:42:00 +03:00
|
|
|
|
|
|
|
template <typename AArg, typename BArg>
|
|
|
|
Tuple& operator=(const Tuple<AArg, BArg>& aOther) {
|
|
|
|
static_cast<Impl&>(*this) = aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename AArg, typename BArg>
|
|
|
|
Tuple& operator=(Tuple<AArg, BArg>&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
static_cast<Impl&>(*this) = std::move(aOther);
|
2015-07-24 01:42:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Tuple& operator=(const Tuple& aOther) {
|
|
|
|
static_cast<Impl&>(*this) = aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Tuple& operator=(Tuple&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
static_cast<Impl&>(*this) = std::move(aOther);
|
2015-07-24 01:42:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename AArg, typename BArg>
|
2020-03-17 15:42:12 +03:00
|
|
|
Tuple& operator=(const CompactPair<AArg, BArg>& aOther) {
|
2015-07-24 01:42:00 +03:00
|
|
|
Impl::Head(*this) = aOther.first();
|
|
|
|
Impl::Tail(*this).Head(*this) = aOther.second();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename AArg, typename BArg>
|
2020-03-17 15:42:12 +03:00
|
|
|
Tuple& operator=(CompactPair<AArg, BArg>&& aOther) {
|
2018-06-01 19:30:30 +03:00
|
|
|
Impl::Head(*this) = std::forward<AArg>(aOther.first());
|
|
|
|
Impl::Tail(*this).Head(*this) = std::forward<BArg>(aOther.second());
|
2015-07-24 01:42:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename AArg, typename BArg>
|
|
|
|
Tuple& operator=(const std::pair<AArg, BArg>& aOther) {
|
|
|
|
Impl::Head(*this) = aOther.first;
|
|
|
|
Impl::Tail(*this).Head(*this) = aOther.second;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <typename AArg, typename BArg>
|
|
|
|
Tuple& operator=(std::pair<AArg, BArg>&& aOther) {
|
2018-06-01 19:30:30 +03:00
|
|
|
Impl::Head(*this) = std::forward<AArg>(aOther.first);
|
|
|
|
Impl::Tail(*this).Head(*this) = std::forward<BArg>(aOther.second);
|
2015-07-24 01:42:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-22 05:33:49 +03:00
|
|
|
/**
|
|
|
|
* Specialization of Tuple for zero arguments.
|
|
|
|
* This is necessary because if the primary template were instantiated with
|
|
|
|
* an empty parameter pack, the 'Tuple(Elements...)' constructors would
|
|
|
|
* become illegal overloads of the default constructor.
|
|
|
|
*/
|
|
|
|
template <>
|
|
|
|
class Tuple<> {};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions for implementing Get<N>(tuple).
|
|
|
|
* These functions take a TupleImpl<Index, Elements...>, with Index being
|
|
|
|
* explicitly specified, and Elements being deduced. By passing a Tuple
|
|
|
|
* object as argument, template argument deduction will do its magic and
|
|
|
|
* cast the tuple to the base class which stores the element at Index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Const reference version.
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
auto TupleGetHelper(TupleImpl<Index, Elements...>& aTuple)
|
|
|
|
-> decltype(TupleImpl<Index, Elements...>::Head(aTuple)) {
|
|
|
|
return TupleImpl<Index, Elements...>::Head(aTuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-const reference version.
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
auto TupleGetHelper(const TupleImpl<Index, Elements...>& aTuple)
|
|
|
|
-> decltype(TupleImpl<Index, Elements...>::Head(aTuple)) {
|
|
|
|
return TupleImpl<Index, Elements...>::Head(aTuple);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace detail
|
2015-05-22 05:33:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Index-based access to an element of a tuple.
|
|
|
|
* The syntax is Get<Index>(tuple). The index is zero-based.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Tuple<int, float, char> t;
|
|
|
|
* ...
|
|
|
|
* float f = Get<1>(t);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Non-const reference version.
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
auto Get(Tuple<Elements...>& aTuple)
|
|
|
|
-> decltype(detail::TupleGetHelper<Index>(aTuple)) {
|
|
|
|
return detail::TupleGetHelper<Index>(aTuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Const reference version.
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
auto Get(const Tuple<Elements...>& aTuple)
|
|
|
|
-> decltype(detail::TupleGetHelper<Index>(aTuple)) {
|
|
|
|
return detail::TupleGetHelper<Index>(aTuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rvalue reference version.
|
|
|
|
template <std::size_t Index, typename... Elements>
|
|
|
|
auto Get(Tuple<Elements...>&& aTuple)
|
2018-05-30 22:15:35 +03:00
|
|
|
-> decltype(std::move(mozilla::Get<Index>(aTuple))) {
|
2015-05-22 05:33:49 +03:00
|
|
|
// We need a 'mozilla::' qualification here to avoid
|
|
|
|
// name lookup only finding the current function.
|
2018-05-30 22:15:35 +03:00
|
|
|
return std::move(mozilla::Get<Index>(aTuple));
|
2015-05-22 05:33:49 +03:00
|
|
|
}
|
|
|
|
|
2018-08-20 03:42:59 +03:00
|
|
|
/**
|
|
|
|
* Helpers which call a function for each member of the tuple in turn. This will
|
|
|
|
* typically be used with a lambda function with an `auto&` argument:
|
|
|
|
*
|
|
|
|
* Tuple<Foo*, Bar*, SmartPtr<Baz>> tuple{a, b, c};
|
|
|
|
*
|
|
|
|
* ForEach(tuple, [](auto& aElem) {
|
|
|
|
* aElem = nullptr;
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
inline void ForEach(const Tuple<>& aTuple, const F& aFunc) {}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
inline void ForEach(Tuple<>& aTuple, const F& aFunc) {}
|
|
|
|
|
|
|
|
template <typename F, typename... Elements>
|
|
|
|
void ForEach(const Tuple<Elements...>& aTuple, const F& aFunc) {
|
|
|
|
aTuple.ForEach(aTuple, aFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F, typename... Elements>
|
|
|
|
void ForEach(Tuple<Elements...>& aTuple, const F& aFunc) {
|
|
|
|
aTuple.ForEach(aFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F, typename... Elements>
|
|
|
|
void ForEach(Tuple<Elements...>&& aTuple, const F& aFunc) {
|
|
|
|
std::forward<Tuple<Elements...>>(aTuple).ForEach(aFunc);
|
|
|
|
}
|
|
|
|
|
2015-05-22 05:33:49 +03:00
|
|
|
/**
|
|
|
|
* A convenience function for constructing a tuple out of a sequence of
|
|
|
|
* values without specifying the type of the tuple.
|
|
|
|
* The type of the tuple is deduced from the types of its elements.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* auto tuple = MakeTuple(42, 0.5f, 'c'); // has type Tuple<int, float, char>
|
|
|
|
*/
|
|
|
|
template <typename... Elements>
|
2020-03-28 16:57:15 +03:00
|
|
|
inline Tuple<std::decay_t<Elements>...> MakeTuple(Elements&&... aElements) {
|
|
|
|
return Tuple<std::decay_t<Elements>...>(std::forward<Elements>(aElements)...);
|
2015-05-22 05:33:49 +03:00
|
|
|
}
|
|
|
|
|
2015-07-18 10:48:39 +03:00
|
|
|
/**
|
|
|
|
* A convenience function for constructing a tuple of references to a
|
|
|
|
* sequence of variables. Since assignments to the elements of the tuple
|
|
|
|
* "go through" to the referenced variables, this can be used to "unpack"
|
|
|
|
* a tuple into individual variables.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* int i;
|
|
|
|
* float f;
|
|
|
|
* char c;
|
|
|
|
* Tie(i, f, c) = FunctionThatReturnsATuple();
|
|
|
|
*/
|
|
|
|
template <typename... Elements>
|
2015-11-03 03:50:06 +03:00
|
|
|
inline Tuple<Elements&...> Tie(Elements&... aVariables) {
|
2015-07-18 10:48:39 +03:00
|
|
|
return Tuple<Elements&...>(aVariables...);
|
|
|
|
}
|
|
|
|
|
2015-05-22 05:33:49 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_Tuple_h */
|