1 #ifndef _VKTCONSTEXPRVECTORUTIL_HPP 2 #define _VKTCONSTEXPRVECTORUTIL_HPP 3 /*------------------------------------------------------------------------ 4 * Vulkan CTS Framework 5 * ------------------------ 6 * 7 * Copyright (c) 2020 The Khronos Group Inc. 8 * Copyright (c) 2020 Advanced Micro Devices, Inc. 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 * 22 */ 23 /*! 24 * \file 25 * \brief Compile time friendly dynamic sized array with maximum capacity 26 */ 27 /*--------------------------------------------------------------------*/ 28 29 #include <cstddef> 30 #include <array> 31 32 namespace vkt 33 { 34 /*--------------------------------------------------------------------*//*! 35 * \brief Constexpr compatable vector with checked maximum capacity 36 * 37 * \note Unlike std::array, size() and max_size() are different values 38 * This makes behaviour more similar to that of std::vector. 39 *//*--------------------------------------------------------------------*/ 40 template <typename _t, size_t CAPACITY> 41 class ConstexprVector 42 { 43 public: 44 using value_type = _t; 45 using size_type = ::std::size_t; 46 using difference_type = ::std::ptrdiff_t; 47 using const_reference = const value_type &; 48 using const_pointer = const value_type *; 49 using const_iterator = const value_type *; 50 ConstexprVector()51 inline constexpr ConstexprVector() noexcept : values{}, count{0} 52 { 53 } 54 55 /*--------------------------------------------------------------------*//*! 56 * MSVC v140 chokes on this if it is a raw variadic template list. 57 * By providing a single argument lead for type deduction it seems to fix 58 * things. Marking constructor as explicit since this effectively becomes 59 * a single argument constructor. 60 *//*--------------------------------------------------------------------*/ 61 template <typename _arg_t, typename... _args_t> ConstexprVector(const _arg_t & arg1,const _args_t &...args)62 inline constexpr explicit ConstexprVector(const _arg_t &arg1, const _args_t &...args) noexcept 63 : values{arg1, args...} 64 , count{sizeof...(_args_t) + 1} 65 { 66 static_assert((sizeof...(_args_t) + 1) <= CAPACITY, "Not enough capacity to store values"); 67 } 68 at(size_type pos) const69 inline constexpr const_reference at(size_type pos) const noexcept 70 { 71 return values[pos]; 72 } operator [](size_type pos) const73 inline constexpr const_reference operator[](size_type pos) const noexcept 74 { 75 return values[pos]; 76 } front() const77 inline constexpr const_reference front() const noexcept 78 { 79 return values[0]; 80 } back() const81 inline constexpr const_reference back() const noexcept 82 { 83 return values[count - 1]; 84 } data() const85 inline constexpr const_pointer data() const noexcept 86 { 87 return values; 88 } begin() const89 inline constexpr const_iterator begin() const noexcept 90 { 91 return &values[0]; 92 } cbegin() const93 inline constexpr const_iterator cbegin() const noexcept 94 { 95 return &values[0]; 96 } end() const97 inline constexpr const_iterator end() const noexcept 98 { 99 return &values[count]; 100 } cend() const101 inline constexpr const_iterator cend() const noexcept 102 { 103 return &values[count]; 104 } empty() const105 inline constexpr bool empty() const noexcept 106 { 107 return count == 0; 108 } size() const109 inline constexpr size_type size() const noexcept 110 { 111 return count; 112 } max_size() const113 inline constexpr size_type max_size() const noexcept 114 { 115 return CAPACITY; 116 } 117 118 private: 119 value_type values[CAPACITY]; 120 size_type count; 121 }; 122 123 } // namespace vkt 124 125 #endif // _VKTCONSTEXPRVECTORUTIL_HPP 126