1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NVRAM_MESSAGES_TYPE_TRAITS_H_
18#define NVRAM_MESSAGES_TYPE_TRAITS_H_
19
20extern "C" {
21#include <stddef.h>
22}
23
24// A subset of utilities similar to what is available in <type_traits>. We have
25// our own standalone version here since not all target platforms have a full
26// C++ standard library.
27
28namespace nvram {
29
30template <typename T>
31struct remove_const {
32  using Type = T;
33};
34template <typename T>
35struct remove_const<const T> {
36  using Type = T;
37};
38
39template <typename T>
40struct remove_reference {
41  using Type = T;
42};
43template <typename T>
44struct remove_reference<T&> {
45  using Type = T;
46};
47template <typename T>
48struct remove_reference<T&&> {
49  using Type = T;
50};
51
52template <bool value, typename T = void>
53struct enable_if {};
54
55template <typename T>
56struct enable_if<true, T> {
57  using Type = T;
58};
59
60template <typename T, T const_value>
61struct integral_constant {
62  static constexpr T value = const_value;
63};
64
65using true_type = integral_constant<bool, true>;
66using false_type = integral_constant<bool, false>;
67
68template <typename T>
69T declval();
70
71template <size_t... index>
72struct index_sequence {};
73
74template <size_t size, size_t... indices>
75struct make_index_sequence_builder {
76  using Type = typename make_index_sequence_builder<size - 1,
77                                                    size - 1,
78                                                    indices...>::Type;
79};
80
81template <size_t... indices>
82struct make_index_sequence_builder<0, indices...> {
83  using Type = index_sequence<indices...>;
84};
85
86template <size_t size>
87constexpr typename make_index_sequence_builder<size>::Type
88make_index_sequence(){
89  return typename make_index_sequence_builder<size>::Type();
90};
91
92}  // namespace nvram
93
94#endif  // NVRAM_MESSAGES_TYPE_TRAITS_H_
95