// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. #pragma once #include #include #include #include namespace wpi { struct empty_array_t {}; constexpr empty_array_t empty_array; /** * This class is a wrapper around std::array that does compile time size * checking. * * std::array's implicit constructor can lead result in uninitialized elements * if the number of arguments doesn't match the std::array size. */ template class array : public std::array { public: explicit array(empty_array_t) {} template array(T arg, Ts&&... args) // NOLINT : std::array{arg, std::forward(args)...} { static_assert(1 + sizeof...(args) == N, "Dimension mismatch"); } array(const array&) = default; array& operator=(const array&) = default; array(array&&) = default; array& operator=(array&&) = default; array(const std::array& rhs) { // NOLINT *static_cast*>(this) = rhs; } array& operator=(const std::array& rhs) { *static_cast*>(this) = rhs; return *this; } array(std::array&& rhs) { // NOLINT *static_cast*>(this) = rhs; } array& operator=(std::array&& rhs) { *static_cast*>(this) = rhs; return *this; } }; template array(T, Ts...) -> array && ...), T>, 1 + sizeof...(Ts)>; } // namespace wpi template constexpr T& get(wpi::array& arr) noexcept { static_assert(I < N, "array index is within bounds"); return std::get(static_cast>(arr)); } template constexpr T&& get(wpi::array&& arr) noexcept { static_assert(I < N, "array index is within bounds"); return std::move(std::get(arr)); } template constexpr const T& get(const wpi::array& arr) noexcept { static_assert(I < N, "array index is within bounds"); return std::get(static_cast>(arr)); } template constexpr const T&& get(const wpi::array&& arr) noexcept { static_assert(I < N, "array index is within bounds"); return std::move(std::get(arr)); } // Enables structured bindings namespace std { // NOLINT // Partial specialization for wpi::array template struct tuple_size> : public integral_constant {}; // Partial specialization for wpi::array template struct tuple_element> { static_assert(I < N, "index is out of bounds"); using type = T; }; } // namespace std