1 // The MIT License (MIT)
2 //
3 // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6 // 	of this software and associated documentation files (the "Software"), to deal
7 // 	in the Software without restriction, including without limitation the rights
8 // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // 	copies of the Software, and to permit persons to whom the Software is
10 // 	furnished to do so, subject to the following conditions:
11 //
12 //  The above copyright notice and this permission notice shall be included in
13 // 	all copies or substantial portions of the Software.
14 //
15 // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // 	THE SOFTWARE.
22 
23 #pragma once
24 
25 
26 namespace MT
27 {
28 
29 	/// \class Static vector
30 	/// \brief A variable-size array container with fixed capacity.
31 	template<class T, size_t CAPACITY>
32 	class StaticVector
33 	{
34 		static const int32 ALIGNMENT = 16;
35 		static const int32 ALIGNMENT_MASK = (ALIGNMENT-1);
36 
37 		uint32 count;
38 
39 		byte rawMemory_[sizeof(T) * CAPACITY + ALIGNMENT];
40 
IndexToObject(int32 index)41 		inline T* IndexToObject(int32 index)
42 		{
43 			byte* alignedMemory = (byte*)( ( (uintptr_t)&rawMemory_[0] + ALIGNMENT_MASK ) & ~(uintptr_t)ALIGNMENT_MASK );
44 			T* pObjectMemory = (T*)(alignedMemory + index * sizeof(T));
45 			return pObjectMemory;
46 		}
47 
CopyCtor(T * element,const T & val)48 		inline void CopyCtor(T* element, const T & val)
49 		{
50 			new(element) T(val);
51 		}
52 
MoveCtor(T * element,T && val)53 		inline void MoveCtor(T* element, T && val)
54 		{
55 			new(element) T(std::move(val));
56 		}
57 
Dtor(T * element)58 		inline void Dtor(T* element)
59 		{
60 			MT_UNUSED(element);
61 			element->~T();
62 		}
63 
64 	public:
65 
66 		MT_NOCOPYABLE(StaticVector);
67 
StaticVector()68 		inline StaticVector()
69 			: count(0)
70 		{
71 		}
72 
73 		inline StaticVector(uint32 _count, const T & defaultElement = T())
count(_count)74 			: count(_count)
75 		{
76 			MT_ASSERT(count <= CAPACITY, "Too big size");
77 			for (uint32 i = 0; i < count; i++)
78 			{
79 				CopyCtor(Begin() + i, defaultElement);
80 			}
81 		}
82 
~StaticVector()83 		inline ~StaticVector()
84 		{
85 			for (uint32 i = 0; i < count; i++)
86 			{
87 				Dtor(Begin() + i);
88 			}
89 		}
90 
91 		inline const T &operator[]( uint32 i ) const
92 		{
93 			MT_ASSERT( i < Size(), "bad index" );
94 			return *IndexToObject(i);
95 		}
96 
97 		inline T &operator[]( uint32 i )
98 		{
99 			MT_ASSERT( i < Size(), "bad index" );
100 			return *IndexToObject(i);
101 		}
102 
PushBack(T && val)103 		inline void PushBack(T && val)
104 		{
105 			MT_ASSERT(count < CAPACITY, "Can't add element");
106 			uint32 lastElementIndex = count;
107 			count++;
108 			MoveCtor( IndexToObject(lastElementIndex), std::move(val) );
109 		}
110 
Size()111 		inline size_t Size() const
112 		{
113 			return count;
114 		}
115 
IsEmpty()116 		inline bool IsEmpty() const
117 		{
118 			return count == 0;
119 		}
120 
Begin()121 		inline T* Begin()
122 		{
123 			return IndexToObject(0);
124 		}
125 	};
126 
127 
128 
129 }
130 
131