summaryrefslogtreecommitdiff
path: root/libsanitizer/sanitizer_common/sanitizer_type_traits.h
blob: 06a44d1b5c7af525861cfca276ae79bb8a88da84 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//===-- sanitizer_type_traits.h ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implements a subset of C++ type traits. This is so we can avoid depending
// on system C++ headers.
//
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_TYPE_TRAITS_H
#define SANITIZER_TYPE_TRAITS_H

#include "sanitizer_common/sanitizer_internal_defs.h"

namespace __sanitizer {

struct true_type {
  static const bool value = true;
};

struct false_type {
  static const bool value = false;
};

// is_same<T, U>
//
// Type trait to compare if types are the same.
// E.g.
//
// ```
// is_same<int,int>::value - True
// is_same<int,char>::value - False
// ```
template <typename T, typename U>
struct is_same : public false_type {};

template <typename T>
struct is_same<T, T> : public true_type {};

// conditional<B, T, F>
//
// Defines type as T if B is true or as F otherwise.
// E.g. the following is true
//
// ```
// is_same<int, conditional<true, int, double>::type>::value
// is_same<double, conditional<false, int, double>::type>::value
// ```
template <bool B, class T, class F>
struct conditional {
  using type = T;
};

template <class T, class F>
struct conditional<false, T, F> {
  using type = F;
};

template <class T>
struct remove_reference {
  using type = T;
};
template <class T>
struct remove_reference<T&> {
  using type = T;
};
template <class T>
struct remove_reference<T&&> {
  using type = T;
};

template <class T>
WARN_UNUSED_RESULT inline typename remove_reference<T>::type&& move(T&& t) {
  return static_cast<typename remove_reference<T>::type&&>(t);
}

template <class T>
WARN_UNUSED_RESULT inline constexpr T&& forward(
    typename remove_reference<T>::type& t) {
  return static_cast<T&&>(t);
}

template <class T>
WARN_UNUSED_RESULT inline constexpr T&& forward(
    typename remove_reference<T>::type&& t) {
  return static_cast<T&&>(t);
}

template <class T, T v>
struct integral_constant {
  static constexpr const T value = v;
  typedef T value_type;
  typedef integral_constant type;
  constexpr operator value_type() const { return value; }
  constexpr value_type operator()() const { return value; }
};

#ifndef __has_builtin
#  define __has_builtin(x) 0
#endif

#if __has_builtin(__is_trivially_destructible)

template <class T>
struct is_trivially_destructible
    : public integral_constant<bool, __is_trivially_destructible(T)> {};

#elif __has_builtin(__has_trivial_destructor)

template <class T>
struct is_trivially_destructible
    : public integral_constant<bool, __has_trivial_destructor(T)> {};

#else

template <class T>
struct is_trivially_destructible
    : public integral_constant<bool, /* less efficient fallback */ false> {};

#endif

#if __has_builtin(__is_trivially_copyable)

template <class T>
struct is_trivially_copyable
    : public integral_constant<bool, __is_trivially_copyable(T)> {};

#else

template <class T>
struct is_trivially_copyable
    : public integral_constant<bool, /* less efficient fallback */ false> {};

#endif

}  // namespace __sanitizer

#endif