• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	 * MSVC v140 chokes on this if it is a raw variadic template list.
55 	 * By providing a single argument lead for type deduction it seems to fix
56 	 * things. Marking constructor as explicit since this effectively becomes
57 	 * a single argument constructor.
58 	 *//*--------------------------------------------------------------------*/
59 	template<typename _arg_t, typename... _args_t>
ConstexprVector(const _arg_t & arg1,const _args_t &...args)60 	inline constexpr explicit ConstexprVector(const _arg_t& arg1, const _args_t&... args) noexcept :
61 		values{arg1, args...},
62 		count{sizeof...(_args_t) + 1}
63 	{
64 		static_assert((sizeof...(_args_t) + 1) <= CAPACITY, "Not enough capacity to store values");
65 	}
66 
at(size_type pos) const67 	inline constexpr const_reference at(size_type pos) const noexcept { return values[pos]; }
operator [](size_type pos) const68 	inline constexpr const_reference operator[](size_type pos) const noexcept { return values[pos]; }
front() const69 	inline constexpr const_reference front() const noexcept { return values[0]; }
back() const70 	inline constexpr const_reference back() const noexcept { return values[count - 1];	}
data() const71 	inline constexpr const_pointer data() const noexcept { return values; }
begin() const72 	inline constexpr const_iterator begin() const noexcept { return &values[0]; }
cbegin() const73 	inline constexpr const_iterator cbegin() const noexcept { return &values[0]; }
end() const74 	inline constexpr const_iterator end() const noexcept { return &values[count]; }
cend() const75 	inline constexpr const_iterator cend() const noexcept { return &values[count]; }
empty() const76 	inline constexpr bool empty() const noexcept { return count == 0; }
size() const77 	inline constexpr size_type size() const noexcept { return count; }
max_size() const78 	inline constexpr size_type max_size() const noexcept { return CAPACITY; }
79 
80 private:
81 	value_type values[CAPACITY];
82 	size_type count;
83 };
84 
85 } // namespace vkt
86 
87 #endif // _VKTCONSTEXPRVECTORUTIL_HPP
88