2015-06-30 20:01:00 +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 template class for tagged unions. */
|
|
|
|
|
|
|
|
#include <new>
|
2016-07-16 12:31:45 +03:00
|
|
|
#include <stdint.h>
|
2015-06-30 20:01:00 +03:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2019-04-25 17:03:40 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2017-02-14 22:23:18 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2017-01-31 02:56:05 +03:00
|
|
|
#include "mozilla/TemplateLib.h"
|
2020-03-28 17:16:19 +03:00
|
|
|
#include <type_traits>
|
2019-04-02 14:53:47 +03:00
|
|
|
#include <utility>
|
2015-06-30 20:01:00 +03:00
|
|
|
|
|
|
|
#ifndef mozilla_Variant_h
|
|
|
|
# define mozilla_Variant_h
|
|
|
|
|
2017-07-21 15:18:02 +03:00
|
|
|
namespace IPC {
|
|
|
|
template <typename T>
|
|
|
|
struct ParamTraits;
|
|
|
|
} // namespace IPC
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-10-08 19:03:11 +03:00
|
|
|
namespace ipc {
|
|
|
|
template <typename T>
|
|
|
|
struct IPDLParamTraits;
|
|
|
|
} // namespace ipc
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
template <typename... Ts>
|
|
|
|
class Variant;
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2017-05-08 02:09:21 +03:00
|
|
|
// Nth<N, types...>::Type is the Nth type (0-based) in the list of types Ts.
|
|
|
|
template <size_t N, typename... Ts>
|
|
|
|
struct Nth;
|
|
|
|
|
|
|
|
template <typename T, typename... Ts>
|
|
|
|
struct Nth<0, T, Ts...> {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t N, typename T, typename... Ts>
|
|
|
|
struct Nth<N, T, Ts...> {
|
|
|
|
using Type = typename Nth<N - 1, Ts...>::Type;
|
|
|
|
};
|
|
|
|
|
2016-02-26 01:34:12 +03:00
|
|
|
/// SelectVariantTypeHelper is used in the implementation of SelectVariantType.
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct SelectVariantTypeHelper<T> {
|
2017-05-10 06:49:38 +03:00
|
|
|
static constexpr size_t count = 0;
|
|
|
|
};
|
2016-02-26 01:34:12 +03:00
|
|
|
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper<T, T, Variants...> {
|
|
|
|
typedef T Type;
|
2017-05-10 06:49:38 +03:00
|
|
|
static constexpr size_t count =
|
|
|
|
1 + SelectVariantTypeHelper<T, Variants...>::count;
|
2016-02-26 01:34:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper<T, const T, Variants...> {
|
|
|
|
typedef const T Type;
|
2017-05-10 06:49:38 +03:00
|
|
|
static constexpr size_t count =
|
|
|
|
1 + SelectVariantTypeHelper<T, Variants...>::count;
|
2016-02-26 01:34:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper<T, const T&, Variants...> {
|
|
|
|
typedef const T& Type;
|
2017-05-10 06:49:38 +03:00
|
|
|
static constexpr size_t count =
|
|
|
|
1 + SelectVariantTypeHelper<T, Variants...>::count;
|
2016-02-26 01:34:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper<T, T&&, Variants...> {
|
|
|
|
typedef T&& Type;
|
2017-05-10 06:49:38 +03:00
|
|
|
static constexpr size_t count =
|
|
|
|
1 + SelectVariantTypeHelper<T, Variants...>::count;
|
2016-02-26 01:34:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename Head, typename... Variants>
|
|
|
|
struct SelectVariantTypeHelper<T, Head, Variants...>
|
|
|
|
: public SelectVariantTypeHelper<T, Variants...> {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SelectVariantType takes a type T and a list of variant types Variants and
|
|
|
|
* yields a type Type, selected from Variants, that can store a value of type T
|
|
|
|
* or a reference to type T. If no such type was found, Type is not defined.
|
2017-05-10 06:49:38 +03:00
|
|
|
* SelectVariantType also has a `count` member that contains the total number of
|
|
|
|
* selectable types (which will be used to check that a requested type is not
|
|
|
|
* ambiguously present twice.)
|
2016-02-26 01:34:12 +03:00
|
|
|
*/
|
|
|
|
template <typename T, typename... Variants>
|
|
|
|
struct SelectVariantType
|
|
|
|
: public SelectVariantTypeHelper<
|
2020-03-28 16:57:19 +03:00
|
|
|
std::remove_const_t<std::remove_reference_t<T>>, Variants...> {};
|
2016-02-26 01:34:12 +03:00
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
// Compute a fast, compact type that can be used to hold integral values that
|
|
|
|
// distinctly map to every type in Ts.
|
|
|
|
template <typename... Ts>
|
|
|
|
struct VariantTag {
|
|
|
|
private:
|
|
|
|
static const size_t TypeCount = sizeof...(Ts);
|
|
|
|
|
|
|
|
public:
|
2021-07-11 12:43:50 +03:00
|
|
|
using Type = std::conditional_t<
|
|
|
|
(TypeCount <= 2), bool,
|
|
|
|
std::conditional_t<(TypeCount <= size_t(UINT_FAST8_MAX)), uint_fast8_t,
|
|
|
|
size_t // stop caring past a certain
|
|
|
|
// point :-)
|
|
|
|
>>;
|
2016-07-16 12:31:45 +03:00
|
|
|
};
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
// TagHelper gets the given sentinel tag value for the given type T. This has to
|
2016-07-16 12:31:45 +03:00
|
|
|
// be split out from VariantImplementation because you can't nest a partial
|
|
|
|
// template specialization within a template class.
|
2015-06-30 20:01:00 +03:00
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename T, typename U, typename Next,
|
|
|
|
bool isMatch>
|
2015-06-30 20:01:00 +03:00
|
|
|
struct TagHelper;
|
|
|
|
|
|
|
|
// In the case where T != U, we continue recursion.
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename T, typename U, typename Next>
|
|
|
|
struct TagHelper<Tag, N, T, U, Next, false> {
|
|
|
|
static Tag tag() { return Next::template tag<U>(); }
|
2015-06-30 20:01:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// In the case where T == U, return the tag number.
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename T, typename U, typename Next>
|
|
|
|
struct TagHelper<Tag, N, T, U, Next, true> {
|
|
|
|
static Tag tag() { return Tag(N); }
|
2015-06-30 20:01:00 +03:00
|
|
|
};
|
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
// The VariantImplementation template provides the guts of mozilla::Variant. We
|
|
|
|
// create a VariantImplementation for each T in Ts... which handles
|
|
|
|
// construction, destruction, etc for when the Variant's type is T. If the
|
|
|
|
// Variant's type isn't T, it punts the request on to the next
|
|
|
|
// VariantImplementation.
|
2015-06-30 20:01:00 +03:00
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename... Ts>
|
2015-06-30 20:01:00 +03:00
|
|
|
struct VariantImplementation;
|
|
|
|
|
|
|
|
// The singly typed Variant / recursion base case.
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename T>
|
|
|
|
struct VariantImplementation<Tag, N, T> {
|
2015-06-30 20:01:00 +03:00
|
|
|
template <typename U>
|
2016-07-16 12:31:45 +03:00
|
|
|
static Tag tag() {
|
2020-03-28 16:57:20 +03:00
|
|
|
static_assert(std::is_same_v<T, U>, "mozilla::Variant: tag: bad type!");
|
2016-07-16 12:31:45 +03:00
|
|
|
return Tag(N);
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void copyConstruct(void* aLhs, const Variant& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
::new (KnownNotNull, aLhs) T(aRhs.template as<N>());
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void moveConstruct(void* aLhs, Variant&& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
::new (KnownNotNull, aLhs) T(aRhs.template extract<N>());
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void destroy(Variant& aV) {
|
2017-05-08 02:09:21 +03:00
|
|
|
aV.template as<N>().~T();
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
2015-07-19 17:32:00 +03:00
|
|
|
|
|
|
|
template <typename Variant>
|
2015-08-09 02:43:35 +03:00
|
|
|
static bool equal(const Variant& aLhs, const Variant& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
return aLhs.template as<N>() == aRhs.template as<N>();
|
2015-07-19 17:32:00 +03:00
|
|
|
}
|
2015-08-09 02:43:35 +03:00
|
|
|
|
2015-12-15 18:45:22 +03:00
|
|
|
template <typename Matcher, typename ConcreteVariant>
|
2020-12-02 03:17:46 +03:00
|
|
|
static decltype(auto) match(Matcher&& aMatcher, ConcreteVariant&& aV) {
|
2020-03-14 00:56:47 +03:00
|
|
|
if constexpr (std::is_invocable_v<Matcher, Tag,
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(std::forward<ConcreteVariant>(aV)
|
|
|
|
.template as<N>())>) {
|
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
Tag(N), std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
} else {
|
2020-12-02 03:17:46 +03:00
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
}
|
2015-08-09 02:43:35 +03:00
|
|
|
}
|
2019-04-02 14:53:51 +03:00
|
|
|
|
|
|
|
template <typename ConcreteVariant, typename Matcher>
|
2020-12-02 03:17:46 +03:00
|
|
|
static decltype(auto) matchN(ConcreteVariant&& aV, Matcher&& aMatcher) {
|
2020-03-14 00:56:47 +03:00
|
|
|
if constexpr (std::is_invocable_v<Matcher, Tag,
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(std::forward<ConcreteVariant>(aV)
|
|
|
|
.template as<N>())>) {
|
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
Tag(N), std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
} else {
|
2020-12-02 03:17:46 +03:00
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
}
|
2019-04-02 14:53:51 +03:00
|
|
|
}
|
2015-06-30 20:01:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// VariantImplementation for some variant type T.
|
2016-07-16 12:31:45 +03:00
|
|
|
template <typename Tag, size_t N, typename T, typename... Ts>
|
|
|
|
struct VariantImplementation<Tag, N, T, Ts...> {
|
2015-06-30 20:01:00 +03:00
|
|
|
// The next recursive VariantImplementation.
|
2016-07-16 12:31:45 +03:00
|
|
|
using Next = VariantImplementation<Tag, N + 1, Ts...>;
|
2015-06-30 20:01:00 +03:00
|
|
|
|
|
|
|
template <typename U>
|
2016-07-16 12:31:45 +03:00
|
|
|
static Tag tag() {
|
2020-03-28 16:57:20 +03:00
|
|
|
return TagHelper<Tag, N, T, U, Next, std::is_same_v<T, U>>::tag();
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void copyConstruct(void* aLhs, const Variant& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
if (aRhs.template is<N>()) {
|
|
|
|
::new (KnownNotNull, aLhs) T(aRhs.template as<N>());
|
2015-06-30 20:01:00 +03:00
|
|
|
} else {
|
|
|
|
Next::copyConstruct(aLhs, aRhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void moveConstruct(void* aLhs, Variant&& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
if (aRhs.template is<N>()) {
|
|
|
|
::new (KnownNotNull, aLhs) T(aRhs.template extract<N>());
|
2015-06-30 20:01:00 +03:00
|
|
|
} else {
|
2018-05-30 22:15:35 +03:00
|
|
|
Next::moveConstruct(aLhs, std::move(aRhs));
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static void destroy(Variant& aV) {
|
2017-05-08 02:09:21 +03:00
|
|
|
if (aV.template is<N>()) {
|
|
|
|
aV.template as<N>().~T();
|
2015-06-30 20:01:00 +03:00
|
|
|
} else {
|
|
|
|
Next::destroy(aV);
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 17:32:00 +03:00
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static bool equal(const Variant& aLhs, const Variant& aRhs) {
|
2017-05-08 02:09:21 +03:00
|
|
|
if (aLhs.template is<N>()) {
|
|
|
|
MOZ_ASSERT(aRhs.template is<N>());
|
|
|
|
return aLhs.template as<N>() == aRhs.template as<N>();
|
2015-07-19 17:32:00 +03:00
|
|
|
} else {
|
|
|
|
return Next::equal(aLhs, aRhs);
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 02:43:35 +03:00
|
|
|
|
2015-12-15 18:45:22 +03:00
|
|
|
template <typename Matcher, typename ConcreteVariant>
|
2020-12-02 03:17:46 +03:00
|
|
|
static decltype(auto) match(Matcher&& aMatcher, ConcreteVariant&& aV) {
|
2017-05-08 02:09:21 +03:00
|
|
|
if (aV.template is<N>()) {
|
2020-03-14 00:56:47 +03:00
|
|
|
if constexpr (std::is_invocable_v<Matcher, Tag,
|
2021-05-10 10:15:07 +03:00
|
|
|
decltype(std::forward<ConcreteVariant>(
|
|
|
|
aV)
|
|
|
|
.template as<N>())>) {
|
2020-12-02 03:17:46 +03:00
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
Tag(N), std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
} else {
|
2020-12-02 03:17:46 +03:00
|
|
|
return std::forward<Matcher>(aMatcher)(
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
}
|
2015-08-09 02:43:35 +03:00
|
|
|
} else {
|
|
|
|
// If you're seeing compilation errors here like "no matching
|
|
|
|
// function for call to 'match'" then that means that the
|
|
|
|
// Matcher doesn't exhaust all variant types. There must exist a
|
2019-04-02 14:53:47 +03:00
|
|
|
// Matcher::operator()(T&) for every variant type T.
|
2015-08-09 02:43:35 +03:00
|
|
|
//
|
2019-04-02 14:53:51 +03:00
|
|
|
// If you're seeing compilation errors here like "cannot initialize
|
|
|
|
// return object of type <...> with an rvalue of type <...>" then that
|
|
|
|
// means that the Matcher::operator()(T&) overloads are returning
|
|
|
|
// different types. They must all return the same type.
|
2020-12-02 03:17:46 +03:00
|
|
|
return Next::match(std::forward<Matcher>(aMatcher),
|
|
|
|
std::forward<ConcreteVariant>(aV));
|
2015-08-09 02:43:35 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 14:53:51 +03:00
|
|
|
|
|
|
|
template <typename ConcreteVariant, typename Mi, typename... Ms>
|
2020-12-02 03:17:46 +03:00
|
|
|
static decltype(auto) matchN(ConcreteVariant&& aV, Mi&& aMi, Ms&&... aMs) {
|
2019-04-02 14:53:51 +03:00
|
|
|
if (aV.template is<N>()) {
|
2020-03-14 00:56:47 +03:00
|
|
|
if constexpr (std::is_invocable_v<Mi, Tag,
|
2021-05-10 10:15:07 +03:00
|
|
|
decltype(std::forward<ConcreteVariant>(
|
|
|
|
aV)
|
|
|
|
.template as<N>())>) {
|
2020-12-02 03:17:46 +03:00
|
|
|
static_assert(
|
|
|
|
std::is_same_v<
|
|
|
|
decltype(std::forward<Mi>(aMi)(
|
|
|
|
Tag(N),
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>())),
|
|
|
|
decltype(Next::matchN(std::forward<ConcreteVariant>(aV),
|
|
|
|
std::forward<Ms>(aMs)...))>,
|
|
|
|
"all matchers must have the same return type");
|
|
|
|
return std::forward<Mi>(aMi)(
|
|
|
|
Tag(N), std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
} else {
|
2020-12-02 03:17:46 +03:00
|
|
|
static_assert(
|
|
|
|
std::is_same_v<
|
|
|
|
decltype(std::forward<Mi>(aMi)(
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>())),
|
|
|
|
decltype(Next::matchN(std::forward<ConcreteVariant>(aV),
|
|
|
|
std::forward<Ms>(aMs)...))>,
|
|
|
|
"all matchers must have the same return type");
|
|
|
|
return std::forward<Mi>(aMi)(
|
|
|
|
std::forward<ConcreteVariant>(aV).template as<N>());
|
2020-03-14 00:56:47 +03:00
|
|
|
}
|
2019-04-02 14:53:51 +03:00
|
|
|
} else {
|
|
|
|
// If you're seeing compilation errors here like "no matching
|
|
|
|
// function for call to 'match'" then that means that the
|
|
|
|
// Matchers don't exhaust all variant types. There must exist a
|
|
|
|
// Matcher (with its operator()(T&)) for every variant type T, in the
|
|
|
|
// exact same order.
|
2020-12-02 03:17:46 +03:00
|
|
|
return Next::matchN(std::forward<ConcreteVariant>(aV),
|
|
|
|
std::forward<Ms>(aMs)...);
|
2019-04-02 14:53:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 20:01:00 +03:00
|
|
|
};
|
|
|
|
|
2016-02-26 01:34:12 +03:00
|
|
|
/**
|
|
|
|
* AsVariantTemporary stores a value of type T to allow construction of a
|
|
|
|
* Variant value via type inference. Because T is copied and there's no
|
|
|
|
* guarantee that the copy can be elided, AsVariantTemporary is best used with
|
|
|
|
* primitive or very small types.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
struct AsVariantTemporary {
|
|
|
|
explicit AsVariantTemporary(const T& aValue) : mValue(aValue) {}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
explicit AsVariantTemporary(U&& aValue) : mValue(std::forward<U>(aValue)) {}
|
|
|
|
|
|
|
|
AsVariantTemporary(const AsVariantTemporary& aOther)
|
|
|
|
: mValue(aOther.mValue) {}
|
|
|
|
|
|
|
|
AsVariantTemporary(AsVariantTemporary&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mValue(std::move(aOther.mValue)) {}
|
2016-02-26 01:34:12 +03:00
|
|
|
|
|
|
|
AsVariantTemporary() = delete;
|
|
|
|
void operator=(const AsVariantTemporary&) = delete;
|
|
|
|
void operator=(AsVariantTemporary&&) = delete;
|
|
|
|
|
2020-03-28 16:57:19 +03:00
|
|
|
std::remove_const_t<std::remove_reference_t<T>> mValue;
|
2016-02-26 01:34:12 +03:00
|
|
|
};
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-05-08 02:26:07 +03:00
|
|
|
// Used to unambiguously specify one of the Variant's type.
|
|
|
|
template <typename T>
|
|
|
|
struct VariantType {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Used to specify one of the Variant's type by index.
|
|
|
|
template <size_t N>
|
|
|
|
struct VariantIndex {
|
|
|
|
static constexpr size_t index = N;
|
|
|
|
};
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
/**
|
|
|
|
* # mozilla::Variant
|
|
|
|
*
|
|
|
|
* A variant / tagged union / heterogenous disjoint union / sum-type template
|
|
|
|
* class. Similar in concept to (but not derived from) `boost::variant`.
|
|
|
|
*
|
|
|
|
* Sometimes, you may wish to use a C union with non-POD types. However, this is
|
|
|
|
* forbidden in C++ because it is not clear which type in the union should have
|
|
|
|
* its constructor and destructor run on creation and deletion
|
|
|
|
* respectively. This is the problem that `mozilla::Variant` solves.
|
|
|
|
*
|
|
|
|
* ## Usage
|
|
|
|
*
|
|
|
|
* A `mozilla::Variant` instance is constructed (via move or copy) from one of
|
|
|
|
* its variant types (ignoring const and references). It does *not* support
|
|
|
|
* construction from subclasses of variant types or types that coerce to one of
|
|
|
|
* the variant types.
|
|
|
|
*
|
|
|
|
* Variant<char, uint32_t> v1('a');
|
|
|
|
* Variant<UniquePtr<A>, B, C> v2(MakeUnique<A>());
|
2017-05-08 02:26:07 +03:00
|
|
|
* Variant<bool, char> v3(VariantType<char>, 0); // disambiguation needed
|
|
|
|
* Variant<int, int> v4(VariantIndex<1>, 0); // 2nd int
|
2015-06-30 20:01:00 +03:00
|
|
|
*
|
2016-02-26 01:34:12 +03:00
|
|
|
* Because specifying the full type of a Variant value is often verbose,
|
2017-05-08 02:26:07 +03:00
|
|
|
* there are two easier ways to construct values:
|
2016-02-26 01:34:12 +03:00
|
|
|
*
|
2017-05-08 02:26:07 +03:00
|
|
|
* A. AsVariant() can be used to construct a Variant value using type inference
|
|
|
|
* in contexts such as expressions or when returning values from functions.
|
|
|
|
* Because AsVariant() must copy or move the value into a temporary and this
|
|
|
|
* cannot necessarily be elided by the compiler, it's mostly appropriate only
|
|
|
|
* for use with primitive or very small types.
|
2016-02-26 01:34:12 +03:00
|
|
|
*
|
|
|
|
* Variant<char, uint32_t> Foo() { return AsVariant('x'); }
|
|
|
|
* // ...
|
|
|
|
* Variant<char, uint32_t> v1 = Foo(); // v1 holds char('x').
|
|
|
|
*
|
2017-05-08 02:26:07 +03:00
|
|
|
* B. Brace-construction with VariantType or VariantIndex; this also allows
|
|
|
|
* in-place construction with any number of arguments.
|
|
|
|
*
|
|
|
|
* struct AB { AB(int, int){...} };
|
|
|
|
* static Variant<AB, bool> foo()
|
|
|
|
* {
|
|
|
|
* return {VariantIndex<0>{}, 1, 2};
|
|
|
|
* }
|
|
|
|
* // ...
|
|
|
|
* Variant<AB, bool> v0 = Foo(); // v0 holds AB(1,2).
|
|
|
|
*
|
2015-06-30 20:01:00 +03:00
|
|
|
* All access to the contained value goes through type-safe accessors.
|
2017-05-08 02:09:21 +03:00
|
|
|
* Either the stored type, or the type index may be provided.
|
2015-06-30 20:01:00 +03:00
|
|
|
*
|
|
|
|
* void
|
|
|
|
* Foo(Variant<A, B, C> v)
|
|
|
|
* {
|
|
|
|
* if (v.is<A>()) {
|
|
|
|
* A& ref = v.as<A>();
|
|
|
|
* ...
|
2017-05-10 06:49:38 +03:00
|
|
|
* } else (v.is<1>()) { // Instead of v.is<B>.
|
2017-05-08 02:09:21 +03:00
|
|
|
* ...
|
2015-06-30 20:01:00 +03:00
|
|
|
* } else {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2017-05-10 06:49:38 +03:00
|
|
|
* In some situation, a Variant may be constructed from templated types, in
|
|
|
|
* which case it is possible that the same type could be given multiple times by
|
|
|
|
* an external developer. Or seemingly-different types could be aliases.
|
|
|
|
* In this case, repeated types can only be accessed through their index, to
|
|
|
|
* prevent ambiguous access by type.
|
|
|
|
*
|
|
|
|
* // Bad!
|
|
|
|
* template <typename T>
|
|
|
|
* struct ResultOrError
|
|
|
|
* {
|
|
|
|
* Variant<T, int> m;
|
2017-05-08 02:26:07 +03:00
|
|
|
* ResultOrError() : m(int(0)) {} // Error '0' by default
|
|
|
|
* ResultOrError(const T& r) : m(r) {}
|
2017-05-10 06:49:38 +03:00
|
|
|
* bool IsResult() const { return m.is<T>(); }
|
|
|
|
* bool IsError() const { return m.is<int>(); }
|
|
|
|
* };
|
|
|
|
* // Now instantiante with the result being an int too:
|
2017-05-08 02:26:07 +03:00
|
|
|
* ResultOrError<int> myResult(123); // Fail!
|
2017-05-10 06:49:38 +03:00
|
|
|
* // In Variant<int, int>, which 'int' are we refering to, from inside
|
|
|
|
* // ResultOrError functions?
|
|
|
|
*
|
|
|
|
* // Good!
|
|
|
|
* template <typename T>
|
|
|
|
* struct ResultOrError
|
|
|
|
* {
|
|
|
|
* Variant<T, int> m;
|
2017-05-08 02:26:07 +03:00
|
|
|
* ResultOrError() : m(VariantIndex<1>{}, 0) {} // Error '0' by default
|
|
|
|
* ResultOrError(const T& r) : m(VariantIndex<0>{}, r) {}
|
2017-05-10 06:49:38 +03:00
|
|
|
* bool IsResult() const { return m.is<0>(); } // 0 -> T
|
|
|
|
* bool IsError() const { return m.is<1>(); } // 1 -> int
|
|
|
|
* };
|
|
|
|
* // Now instantiante with the result being an int too:
|
2017-05-08 02:26:07 +03:00
|
|
|
* ResultOrError<int> myResult(123); // It now works!
|
2017-05-10 06:49:38 +03:00
|
|
|
*
|
2015-06-30 20:01:00 +03:00
|
|
|
* Attempting to use the contained value as type `T1` when the `Variant`
|
|
|
|
* instance contains a value of type `T2` causes an assertion failure.
|
|
|
|
*
|
|
|
|
* A a;
|
|
|
|
* Variant<A, B, C> v(a);
|
|
|
|
* v.as<B>(); // <--- Assertion failure!
|
|
|
|
*
|
|
|
|
* Trying to use a `Variant<Ts...>` instance as some type `U` that is not a
|
|
|
|
* member of the set of `Ts...` is a compiler error.
|
|
|
|
*
|
|
|
|
* A a;
|
|
|
|
* Variant<A, B, C> v(a);
|
|
|
|
* v.as<SomeRandomType>(); // <--- Compiler error!
|
|
|
|
*
|
|
|
|
* Additionally, you can turn a `Variant` that `is<T>` into a `T` by moving it
|
|
|
|
* out of the containing `Variant` instance with the `extract<T>` method:
|
|
|
|
*
|
|
|
|
* Variant<UniquePtr<A>, B, C> v(MakeUnique<A>());
|
|
|
|
* auto ptr = v.extract<UniquePtr<A>>();
|
|
|
|
*
|
2015-08-09 02:43:35 +03:00
|
|
|
* Finally, you can exhaustively match on the contained variant and branch into
|
2017-05-08 02:09:21 +03:00
|
|
|
* different code paths depending on which type is contained. This is preferred
|
|
|
|
* to manually checking every variant type T with is<T>() because it provides
|
2015-08-09 02:43:35 +03:00
|
|
|
* compile-time checking that you handled every type, rather than runtime
|
|
|
|
* assertion failures.
|
|
|
|
*
|
|
|
|
* // Bad!
|
|
|
|
* char* foo(Variant<A, B, C, D>& v) {
|
|
|
|
* if (v.is<A>()) {
|
|
|
|
* return ...;
|
|
|
|
* } else if (v.is<B>()) {
|
|
|
|
* return ...;
|
|
|
|
* } else {
|
|
|
|
* return doSomething(v.as<C>()); // Forgot about case D!
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2019-04-02 14:53:47 +03:00
|
|
|
* // Instead, a single function object (that can deal with all possible
|
|
|
|
* // options) may be provided:
|
2015-08-09 02:43:35 +03:00
|
|
|
* struct FooMatcher
|
|
|
|
* {
|
2015-12-15 18:45:22 +03:00
|
|
|
* // The return type of all matchers must be identical.
|
2019-04-02 14:53:47 +03:00
|
|
|
* char* operator()(A& a) { ... }
|
|
|
|
* char* operator()(B& b) { ... }
|
|
|
|
* char* operator()(C& c) { ... }
|
|
|
|
* char* operator()(D& d) { ... } // Compile-time error to forget D!
|
2015-08-09 02:43:35 +03:00
|
|
|
* }
|
|
|
|
* char* foo(Variant<A, B, C, D>& v) {
|
|
|
|
* return v.match(FooMatcher());
|
|
|
|
* }
|
|
|
|
*
|
2019-04-02 14:53:47 +03:00
|
|
|
* // In some situations, a single generic lambda may also be appropriate:
|
|
|
|
* char* foo(Variant<A, B, C, D>& v) {
|
2020-03-14 00:56:47 +03:00
|
|
|
* return v.match([](auto&) {...});
|
2019-04-02 14:53:47 +03:00
|
|
|
* }
|
|
|
|
*
|
2019-04-02 14:53:51 +03:00
|
|
|
* // Alternatively, multiple function objects may be provided, each one
|
|
|
|
* // corresponding to an option, in the same order:
|
|
|
|
* char* foo(Variant<A, B, C, D>& v) {
|
|
|
|
* return v.match([](A&) { ... },
|
|
|
|
* [](B&) { ... },
|
|
|
|
* [](C&) { ... },
|
|
|
|
* [](D&) { ... });
|
|
|
|
* }
|
|
|
|
*
|
2020-03-14 00:56:47 +03:00
|
|
|
* // In rare cases, the index of the currently-active alternative is
|
|
|
|
* // needed, it may be obtained by adding a first parameter in the matcner
|
|
|
|
* // callback, which will receive the index in its most compact type (just
|
|
|
|
* // use `size_t` if the exact type is not important), e.g.:
|
|
|
|
* char* foo(Variant<A, B, C, D>& v) {
|
|
|
|
* return v.match([](auto aIndex, auto& aAlternative) {...});
|
|
|
|
* // --OR--
|
|
|
|
* return v.match([](size_t aIndex, auto& aAlternative) {...});
|
|
|
|
* }
|
|
|
|
*
|
2015-06-30 20:01:00 +03:00
|
|
|
* ## Examples
|
|
|
|
*
|
|
|
|
* A tree is either an empty leaf, or a node with a value and two children:
|
|
|
|
*
|
|
|
|
* struct Leaf { };
|
|
|
|
*
|
|
|
|
* template<typename T>
|
|
|
|
* struct Node
|
|
|
|
* {
|
|
|
|
* T value;
|
|
|
|
* Tree<T>* left;
|
|
|
|
* Tree<T>* right;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* template<typename T>
|
|
|
|
* using Tree = Variant<Leaf, Node<T>>;
|
|
|
|
*
|
|
|
|
* A copy-on-write string is either a non-owning reference to some existing
|
|
|
|
* string, or an owning reference to our copy:
|
|
|
|
*
|
|
|
|
* class CopyOnWriteString
|
|
|
|
* {
|
|
|
|
* Variant<const char*, UniquePtr<char[]>> string;
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
* };
|
2017-01-31 02:56:05 +03:00
|
|
|
*
|
|
|
|
* Because Variant must be aligned suitable to hold any value stored within it,
|
|
|
|
* and because |alignas| requirements don't affect platform ABI with respect to
|
|
|
|
* how parameters are laid out in memory, Variant can't be used as the type of a
|
|
|
|
* function parameter. Pass Variant to functions by pointer or reference
|
|
|
|
* instead.
|
2015-06-30 20:01:00 +03:00
|
|
|
*/
|
|
|
|
template <typename... Ts>
|
2017-01-31 02:56:05 +03:00
|
|
|
class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS MOZ_NON_PARAM Variant {
|
2017-07-21 15:18:02 +03:00
|
|
|
friend struct IPC::ParamTraits<mozilla::Variant<Ts...>>;
|
2019-10-08 19:03:11 +03:00
|
|
|
friend struct mozilla::ipc::IPDLParamTraits<mozilla::Variant<Ts...>>;
|
2017-07-21 15:18:02 +03:00
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
using Tag = typename detail::VariantTag<Ts...>::Type;
|
|
|
|
using Impl = detail::VariantImplementation<Tag, 0, Ts...>;
|
2017-01-31 02:56:05 +03:00
|
|
|
|
|
|
|
static constexpr size_t RawDataAlignment = tl::Max<alignof(Ts)...>::value;
|
|
|
|
static constexpr size_t RawDataSize = tl::Max<sizeof(Ts)...>::value;
|
2015-06-30 20:01:00 +03:00
|
|
|
|
|
|
|
// Raw storage for the contained variant value.
|
2017-01-31 02:56:05 +03:00
|
|
|
alignas(RawDataAlignment) unsigned char rawData[RawDataSize];
|
2015-06-30 20:01:00 +03:00
|
|
|
|
2016-07-16 12:31:45 +03:00
|
|
|
// Each type is given a unique tag value that lets us keep track of the
|
|
|
|
// contained variant value's type.
|
|
|
|
Tag tag;
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
// Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
|
|
|
|
// -Werror compile error) to reinterpret_cast<> |rawData| to |T*|, even
|
|
|
|
// through |void*|. Placing the latter cast in these separate functions
|
|
|
|
// breaks the chain such that affected GCC versions no longer warn/error.
|
|
|
|
void* ptr() { return rawData; }
|
|
|
|
|
|
|
|
const void* ptr() const { return rawData; }
|
2015-06-30 20:01:00 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
/** Perfect forwarding construction for some variant type T. */
|
|
|
|
template <typename RefT,
|
|
|
|
// RefT captures both const& as well as && (as intended, to support
|
|
|
|
// perfect forwarding), so we have to remove those qualifiers here
|
|
|
|
// when ensuring that T is a variant of this type, and getting T's
|
|
|
|
// tag, etc.
|
2016-02-26 01:34:12 +03:00
|
|
|
typename T = typename detail::SelectVariantType<RefT, Ts...>::Type>
|
2015-06-30 20:01:00 +03:00
|
|
|
explicit Variant(RefT&& aT) : tag(Impl::template tag<T>()) {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<RefT, Ts...>::count == 1,
|
|
|
|
"Variant can only be selected by type if that type is unique");
|
2018-06-01 19:30:30 +03:00
|
|
|
::new (KnownNotNull, ptr()) T(std::forward<RefT>(aT));
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 02:26:07 +03:00
|
|
|
/**
|
|
|
|
* Perfect forwarding construction for some variant type T, by
|
|
|
|
* explicitly giving the type.
|
|
|
|
* This is necessary to construct from any number of arguments,
|
|
|
|
* or to convert from a type that is not in the Variant's type list.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
MOZ_IMPLICIT Variant(const VariantType<T>&, Args&&... aTs)
|
|
|
|
: tag(Impl::template tag<T>()) {
|
2018-06-01 19:30:30 +03:00
|
|
|
::new (KnownNotNull, ptr()) T(std::forward<Args>(aTs)...);
|
2017-05-08 02:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perfect forwarding construction for some variant type T, by
|
|
|
|
* explicitly giving the type index.
|
|
|
|
* This is necessary to construct from any number of arguments,
|
|
|
|
* or to convert from a type that is not in the Variant's type list,
|
|
|
|
* or to construct a type that is present more than once in the Variant.
|
|
|
|
*/
|
|
|
|
template <size_t N, typename... Args>
|
|
|
|
MOZ_IMPLICIT Variant(const VariantIndex<N>&, Args&&... aTs) : tag(N) {
|
|
|
|
using T = typename detail::Nth<N, Ts...>::Type;
|
2018-06-01 19:30:30 +03:00
|
|
|
::new (KnownNotNull, ptr()) T(std::forward<Args>(aTs)...);
|
2017-05-08 02:26:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 01:34:12 +03:00
|
|
|
/**
|
|
|
|
* Constructs this Variant from an AsVariantTemporary<T> such that T can be
|
|
|
|
* stored in one of the types allowable in this Variant. This is used in the
|
|
|
|
* implementation of AsVariant().
|
|
|
|
*/
|
2017-05-08 02:26:07 +03:00
|
|
|
template <typename RefT>
|
2016-02-26 01:34:12 +03:00
|
|
|
MOZ_IMPLICIT Variant(detail::AsVariantTemporary<RefT>&& aValue)
|
2017-05-08 02:26:07 +03:00
|
|
|
: tag(Impl::template tag<
|
|
|
|
typename detail::SelectVariantType<RefT, Ts...>::Type>()) {
|
|
|
|
using T = typename detail::SelectVariantType<RefT, Ts...>::Type;
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<RefT, Ts...>::count == 1,
|
|
|
|
"Variant can only be selected by type if that type is unique");
|
2018-05-30 22:15:35 +03:00
|
|
|
::new (KnownNotNull, ptr()) T(std::move(aValue.mValue));
|
2016-02-26 01:34:12 +03:00
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
/** Copy construction. */
|
2015-07-22 21:49:00 +03:00
|
|
|
Variant(const Variant& aRhs) : tag(aRhs.tag) {
|
2015-06-30 20:01:00 +03:00
|
|
|
Impl::copyConstruct(ptr(), aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Move construction. */
|
2015-07-22 21:49:00 +03:00
|
|
|
Variant(Variant&& aRhs) : tag(aRhs.tag) {
|
2018-05-30 22:15:35 +03:00
|
|
|
Impl::moveConstruct(ptr(), std::move(aRhs));
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy assignment. */
|
|
|
|
Variant& operator=(const Variant& aRhs) {
|
|
|
|
MOZ_ASSERT(&aRhs != this, "self-assign disallowed");
|
|
|
|
this->~Variant();
|
2017-02-14 22:23:18 +03:00
|
|
|
::new (KnownNotNull, this) Variant(aRhs);
|
2015-06-30 20:01:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Move assignment. */
|
|
|
|
Variant& operator=(Variant&& aRhs) {
|
|
|
|
MOZ_ASSERT(&aRhs != this, "self-assign disallowed");
|
|
|
|
this->~Variant();
|
2018-05-30 22:15:35 +03:00
|
|
|
::new (KnownNotNull, this) Variant(std::move(aRhs));
|
2015-06-30 20:01:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:34:12 +03:00
|
|
|
/** Move assignment from AsVariant(). */
|
2017-05-08 02:09:21 +03:00
|
|
|
template <typename T>
|
2016-02-26 01:34:12 +03:00
|
|
|
Variant& operator=(detail::AsVariantTemporary<T>&& aValue) {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"Variant can only be selected by type if that type is unique");
|
2016-02-26 01:34:12 +03:00
|
|
|
this->~Variant();
|
2018-05-30 22:15:35 +03:00
|
|
|
::new (KnownNotNull, this) Variant(std::move(aValue));
|
2016-02-26 01:34:12 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
~Variant() { Impl::destroy(*this); }
|
|
|
|
|
2019-10-08 19:03:11 +03:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
T& emplace(Args&&... aTs) {
|
|
|
|
Impl::destroy(*this);
|
|
|
|
tag = Impl::template tag<T>();
|
|
|
|
::new (KnownNotNull, ptr()) T(std::forward<Args>(aTs)...);
|
|
|
|
return as<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N, typename... Args>
|
|
|
|
typename detail::Nth<N, Ts...>::Type& emplace(Args&&... aTs) {
|
|
|
|
using T = typename detail::Nth<N, Ts...>::Type;
|
|
|
|
Impl::destroy(*this);
|
|
|
|
tag = N;
|
|
|
|
::new (KnownNotNull, ptr()) T(std::forward<Args>(aTs)...);
|
|
|
|
return as<N>();
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
/** Check which variant type is currently contained. */
|
|
|
|
template <typename T>
|
|
|
|
bool is() const {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"provided a type not uniquely found in this Variant's type list");
|
2015-06-30 20:01:00 +03:00
|
|
|
return Impl::template tag<T>() == tag;
|
|
|
|
}
|
|
|
|
|
2017-05-08 02:09:21 +03:00
|
|
|
template <size_t N>
|
|
|
|
bool is() const {
|
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
return N == size_t(tag);
|
|
|
|
}
|
|
|
|
|
2015-07-19 17:32:00 +03:00
|
|
|
/**
|
|
|
|
* Operator == overload that defers to the variant type's operator==
|
|
|
|
* implementation if the rhs is tagged as the same type as this one.
|
|
|
|
*/
|
|
|
|
bool operator==(const Variant& aRhs) const {
|
|
|
|
return tag == aRhs.tag && Impl::equal(*this, aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator != overload that defers to the negation of the variant type's
|
|
|
|
* operator== implementation if the rhs is tagged as the same type as this
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
bool operator!=(const Variant& aRhs) const { return !(*this == aRhs); }
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
// Accessors for working with the contained variant value.
|
|
|
|
|
2020-12-02 03:17:46 +03:00
|
|
|
/** Mutable lvalue-reference. */
|
2015-06-30 20:01:00 +03:00
|
|
|
template <typename T>
|
2020-12-02 03:17:46 +03:00
|
|
|
T& as() & {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"provided a type not uniquely found in this Variant's type list");
|
2017-01-10 09:24:49 +03:00
|
|
|
MOZ_RELEASE_ASSERT(is<T>());
|
2017-01-31 02:56:05 +03:00
|
|
|
return *static_cast<T*>(ptr());
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 02:09:21 +03:00
|
|
|
template <size_t N>
|
2020-12-02 03:17:46 +03:00
|
|
|
typename detail::Nth<N, Ts...>::Type& as() & {
|
2017-05-08 02:09:21 +03:00
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<N>());
|
|
|
|
return *static_cast<typename detail::Nth<N, Ts...>::Type*>(ptr());
|
|
|
|
}
|
|
|
|
|
2020-12-02 03:17:46 +03:00
|
|
|
/** Immutable const lvalue-reference. */
|
2015-06-30 20:01:00 +03:00
|
|
|
template <typename T>
|
2020-12-02 03:17:46 +03:00
|
|
|
const T& as() const& {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(detail::SelectVariantType<T, Ts...>::count == 1,
|
2015-06-30 20:01:00 +03:00
|
|
|
"provided a type not found in this Variant's type list");
|
2017-01-10 09:24:49 +03:00
|
|
|
MOZ_RELEASE_ASSERT(is<T>());
|
2017-01-31 02:56:05 +03:00
|
|
|
return *static_cast<const T*>(ptr());
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 02:09:21 +03:00
|
|
|
template <size_t N>
|
2020-12-02 03:17:46 +03:00
|
|
|
const typename detail::Nth<N, Ts...>::Type& as() const& {
|
2017-05-08 02:09:21 +03:00
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<N>());
|
|
|
|
return *static_cast<const typename detail::Nth<N, Ts...>::Type*>(ptr());
|
|
|
|
}
|
|
|
|
|
2020-12-02 03:17:46 +03:00
|
|
|
/** Mutable rvalue-reference. */
|
|
|
|
template <typename T>
|
|
|
|
T&& as() && {
|
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"provided a type not uniquely found in this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<T>());
|
|
|
|
return std::move(*static_cast<T*>(ptr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
typename detail::Nth<N, Ts...>::Type&& as() && {
|
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<N>());
|
|
|
|
return std::move(
|
|
|
|
*static_cast<typename detail::Nth<N, Ts...>::Type*>(ptr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Immutable const rvalue-reference. */
|
|
|
|
template <typename T>
|
|
|
|
const T&& as() const&& {
|
|
|
|
static_assert(detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"provided a type not found in this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<T>());
|
|
|
|
return std::move(*static_cast<const T*>(ptr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
const typename detail::Nth<N, Ts...>::Type&& as() const&& {
|
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<N>());
|
|
|
|
return std::move(
|
|
|
|
*static_cast<const typename detail::Nth<N, Ts...>::Type*>(ptr()));
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
/**
|
|
|
|
* Extract the contained variant value from this container into a temporary
|
|
|
|
* value. On completion, the value in the variant will be in a
|
|
|
|
* safely-destructible state, as determined by the behavior of T's move
|
|
|
|
* constructor when provided the variant's internal value.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T extract() {
|
2017-05-10 06:49:38 +03:00
|
|
|
static_assert(
|
|
|
|
detail::SelectVariantType<T, Ts...>::count == 1,
|
|
|
|
"provided a type not uniquely found in this Variant's type list");
|
2015-06-30 20:01:00 +03:00
|
|
|
MOZ_ASSERT(is<T>());
|
2018-05-30 22:15:35 +03:00
|
|
|
return T(std::move(as<T>()));
|
2015-06-30 20:01:00 +03:00
|
|
|
}
|
2015-08-09 02:43:35 +03:00
|
|
|
|
2017-05-08 02:09:21 +03:00
|
|
|
template <size_t N>
|
|
|
|
typename detail::Nth<N, Ts...>::Type extract() {
|
|
|
|
static_assert(N < sizeof...(Ts),
|
|
|
|
"provided an index outside of this Variant's type list");
|
|
|
|
MOZ_RELEASE_ASSERT(is<N>());
|
2018-05-30 22:15:35 +03:00
|
|
|
return typename detail::Nth<N, Ts...>::Type(std::move(as<N>()));
|
2017-05-08 02:09:21 +03:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:35:00 +03:00
|
|
|
// Exhaustive matching of all variant types on the contained value.
|
2015-08-09 02:43:35 +03:00
|
|
|
|
2020-12-02 03:17:46 +03:00
|
|
|
/** Match on an immutable const lvalue-reference. */
|
2015-08-09 02:43:35 +03:00
|
|
|
template <typename Matcher>
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(auto) match(Matcher&& aMatcher) const& {
|
2019-04-02 14:53:47 +03:00
|
|
|
return Impl::match(std::forward<Matcher>(aMatcher), *this);
|
2015-08-09 02:43:35 +03:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:53:51 +03:00
|
|
|
template <typename M0, typename M1, typename... Ms>
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(auto) match(M0&& aM0, M1&& aM1, Ms&&... aMs) const& {
|
|
|
|
return matchN(*this, std::forward<M0>(aM0), std::forward<M1>(aM1),
|
|
|
|
std::forward<Ms>(aMs)...);
|
2019-04-02 14:53:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 03:17:46 +03:00
|
|
|
/** Match on a mutable non-const lvalue-reference. */
|
2015-08-09 02:43:35 +03:00
|
|
|
template <typename Matcher>
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(auto) match(Matcher&& aMatcher) & {
|
2019-04-02 14:53:47 +03:00
|
|
|
return Impl::match(std::forward<Matcher>(aMatcher), *this);
|
2015-08-09 02:43:35 +03:00
|
|
|
}
|
2019-04-02 14:53:51 +03:00
|
|
|
|
|
|
|
template <typename M0, typename M1, typename... Ms>
|
2020-12-02 03:17:46 +03:00
|
|
|
decltype(auto) match(M0&& aM0, M1&& aM1, Ms&&... aMs) & {
|
|
|
|
return matchN(*this, std::forward<M0>(aM0), std::forward<M1>(aM1),
|
|
|
|
std::forward<Ms>(aMs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Match on an immutable const rvalue-reference. */
|
|
|
|
template <typename Matcher>
|
|
|
|
decltype(auto) match(Matcher&& aMatcher) const&& {
|
|
|
|
return Impl::match(std::forward<Matcher>(aMatcher), std::move(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename M0, typename M1, typename... Ms>
|
|
|
|
decltype(auto) match(M0&& aM0, M1&& aM1, Ms&&... aMs) const&& {
|
|
|
|
return matchN(std::move(*this), std::forward<M0>(aM0),
|
|
|
|
std::forward<M1>(aM1), std::forward<Ms>(aMs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Match on a mutable non-const rvalue-reference. */
|
|
|
|
template <typename Matcher>
|
|
|
|
decltype(auto) match(Matcher&& aMatcher) && {
|
|
|
|
return Impl::match(std::forward<Matcher>(aMatcher), std::move(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename M0, typename M1, typename... Ms>
|
|
|
|
decltype(auto) match(M0&& aM0, M1&& aM1, Ms&&... aMs) && {
|
|
|
|
return matchN(std::move(*this), std::forward<M0>(aM0),
|
|
|
|
std::forward<M1>(aM1), std::forward<Ms>(aMs)...);
|
2019-04-02 14:53:51 +03:00
|
|
|
}
|
2019-04-25 17:03:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Incorporate the current variant's tag into hashValue.
|
|
|
|
* Note that this does not hash the actual contents; you must take
|
|
|
|
* care of that yourself, perhaps by using a match.
|
|
|
|
*/
|
2019-05-01 15:13:23 +03:00
|
|
|
mozilla::HashNumber addTagToHash(mozilla::HashNumber hashValue) const {
|
2019-04-25 17:03:40 +03:00
|
|
|
return mozilla::AddToHash(hashValue, tag);
|
|
|
|
}
|
2020-12-02 03:17:46 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename ConcreteVariant, typename M0, typename M1, typename... Ms>
|
|
|
|
static decltype(auto) matchN(ConcreteVariant&& aVariant, M0&& aM0, M1&& aM1,
|
|
|
|
Ms&&... aMs) {
|
|
|
|
static_assert(
|
|
|
|
2 + sizeof...(Ms) == sizeof...(Ts),
|
|
|
|
"Variant<T...>::match() takes either one callable argument that "
|
|
|
|
"accepts every type T; or one for each type T, in order");
|
|
|
|
return Impl::matchN(std::forward<ConcreteVariant>(aVariant),
|
|
|
|
std::forward<M0>(aM0), std::forward<M1>(aM1),
|
|
|
|
std::forward<Ms>(aMs)...);
|
|
|
|
}
|
2015-06-30 20:01:00 +03:00
|
|
|
};
|
|
|
|
|
2016-02-26 01:34:12 +03:00
|
|
|
/*
|
|
|
|
* AsVariant() is used to construct a Variant<T,...> value containing the
|
|
|
|
* provided T value using type inference. It can be used to construct Variant
|
|
|
|
* values in expressions or return them from functions without specifying the
|
|
|
|
* entire Variant type.
|
|
|
|
*
|
|
|
|
* Because AsVariant() must copy or move the value into a temporary and this
|
|
|
|
* cannot necessarily be elided by the compiler, it's mostly appropriate only
|
|
|
|
* for use with primitive or very small types.
|
|
|
|
*
|
|
|
|
* AsVariant() returns a AsVariantTemporary value which is implicitly
|
|
|
|
* convertible to any Variant that can hold a value of type T.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
detail::AsVariantTemporary<T> AsVariant(T&& aValue) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return detail::AsVariantTemporary<T>(std::forward<T>(aValue));
|
2016-02-26 01:34:12 +03:00
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:00 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_Variant_h */
|