aboutsummaryrefslogtreecommitdiff
path: root/third_party/fuchsia/repo/sdk/lib/stdcompat/include/lib/stdcompat/internal/constructors.h
blob: ba592ac7bdb069fc3a20fe95913b49bd762bc97a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIB_STDCOMPAT_INTERNAL_CONSTRUCTORS_H_
#define LIB_STDCOMPAT_INTERNAL_CONSTRUCTORS_H_

#include <cstddef>
#include <type_traits>
#include <utility>

namespace cpp17 {
namespace internal {

// Mixin that implicitly deletes the subclass default constructor when type T
// is not default constructible.
template <typename T, bool = std::is_default_constructible<T>::value>
struct modulate_default_constructor {};
template <typename T>
struct modulate_default_constructor<T, false> {
  constexpr modulate_default_constructor() = delete;
};

// Mixin that implicitly deletes the subclass copy constructor when type T is
// not copy constructible.
template <std::size_t Index, typename T, bool = std::is_copy_constructible<T>::value>
struct modulate_copy_constructor {};
template <std::size_t Index, typename T>
struct modulate_copy_constructor<Index, T, false> {
  constexpr modulate_copy_constructor() = default;
  constexpr modulate_copy_constructor(const modulate_copy_constructor&) = delete;
  constexpr modulate_copy_constructor& operator=(const modulate_copy_constructor&) = default;
  constexpr modulate_copy_constructor(modulate_copy_constructor&&) = default;
  constexpr modulate_copy_constructor& operator=(modulate_copy_constructor&&) = default;
};

// Mixin that implicitly deletes the subclass copy assignment operator when type
// T is not copy assignable.
template <std::size_t Index, typename T, bool = std::is_copy_assignable<T>::value>
struct modulate_copy_assignment {};
template <std::size_t Index, typename T>
struct modulate_copy_assignment<Index, T, false> {
  constexpr modulate_copy_assignment() = default;
  constexpr modulate_copy_assignment(const modulate_copy_assignment&) = default;
  constexpr modulate_copy_assignment& operator=(const modulate_copy_assignment&) = delete;
  constexpr modulate_copy_assignment(modulate_copy_assignment&&) = default;
  constexpr modulate_copy_assignment& operator=(modulate_copy_assignment&&) = default;
};

// Mixin that implicitly deletes the subclass move constructor when type T is
// not move constructible.
template <std::size_t Index, typename T, bool = std::is_move_constructible<T>::value>
struct modulate_move_constructor {};
template <std::size_t Index, typename T>
struct modulate_move_constructor<Index, T, false> {
  constexpr modulate_move_constructor() = default;
  constexpr modulate_move_constructor(const modulate_move_constructor&) = default;
  constexpr modulate_move_constructor& operator=(const modulate_move_constructor&) = default;
  constexpr modulate_move_constructor(modulate_move_constructor&&) = delete;
  constexpr modulate_move_constructor& operator=(modulate_move_constructor&&) = default;
};

// Mixin that implicitly deletes the subclass move assignment operator when type
// T is not move assignable.
template <std::size_t Index, typename T, bool = std::is_move_assignable<T>::value>
struct modulate_move_assignment {};
template <std::size_t Index, typename T>
struct modulate_move_assignment<Index, T, false> {
  constexpr modulate_move_assignment() = default;
  constexpr modulate_move_assignment(const modulate_move_assignment&) = default;
  constexpr modulate_move_assignment& operator=(const modulate_move_assignment&) = default;
  constexpr modulate_move_assignment(modulate_move_assignment&&) = default;
  constexpr modulate_move_assignment& operator=(modulate_move_assignment&&) = delete;
};

// Utility that takes an index sequence and an equally sized parameter pack and
// mixes in each of the above copy/move construction/assignment modulators for
// each type in Ts. The indices are used to avoid duplicate direct base errors
// by ensuring that each mixin type is unique, even when there are duplicate
// types within the parameter pack Ts.
template <typename IndexSequence, typename... Ts>
struct modulate_copy_and_move_index;

template <std::size_t... Is, typename... Ts>
struct modulate_copy_and_move_index<std::index_sequence<Is...>, Ts...>
    : modulate_copy_constructor<Is, Ts>...,
      modulate_copy_assignment<Is, Ts>...,
      modulate_move_constructor<Is, Ts>...,
      modulate_move_assignment<Is, Ts>... {};

// Mixin that modulates the subclass copy/move constructors and assignment
// operators based on the copy/move characteristics of each type in Ts.
template <typename... Ts>
struct modulate_copy_and_move
    : modulate_copy_and_move_index<std::index_sequence_for<Ts...>, Ts...> {};

}  // namespace internal
}  // namespace cpp17

#endif  // LIB_STDCOMPAT_INTERNAL_CONSTRUCTORS_H_