• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <array>
17 #include <cstddef>
18 #include <type_traits>
19 #include <utility>
20 
21 namespace pw {
22 namespace containers {
23 namespace impl {
24 
25 template <typename T, size_t kSize, size_t... kIndices>
CopyArray(const T (& values)[kSize],std::index_sequence<kIndices...>)26 constexpr std::array<std::remove_cv_t<T>, kSize> CopyArray(
27     const T (&values)[kSize], std::index_sequence<kIndices...>) {
28   return {{values[kIndices]...}};
29 }
30 
31 template <typename T, size_t kSize, size_t... kIndices>
32 constexpr std::array<std::remove_cv_t<T>, kSize> MoveArray(
33     T (&&values)[kSize], std::index_sequence<kIndices...>) {
34   return {{std::move(values[kIndices])...}};
35 }
36 
37 }  // namespace impl
38 
39 // pw::containers::to_array is C++14-compatible implementation of C++20's
40 // std::to_array.
41 template <typename T, size_t kSize>
to_array(T (& values)[kSize])42 constexpr std::array<std::remove_cv_t<T>, kSize> to_array(T (&values)[kSize]) {
43   return impl::CopyArray(values, std::make_index_sequence<kSize>{});
44 }
45 
46 template <typename T, size_t kSize>
47 constexpr std::array<std::remove_cv_t<T>, kSize> to_array(T (&&values)[kSize]) {
48   return impl::MoveArray(std::move(values), std::make_index_sequence<kSize>{});
49 }
50 
51 }  // namespace containers
52 }  // namespace pw
53