AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
Array.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9
10#include <aws/core/utils/memory/AWSMemory.h>
11#include <aws/core/utils/memory/stl/AWSVector.h>
12#include <aws/core/utils/memory/stl/AWSString.h>
13#include <aws/crt/Types.h>
14#include <memory>
15#include <cassert>
16#include <cstring>
17#include <algorithm>
18
19#ifdef _WIN32
20
21#include <iterator>
22
23#endif // _WIN32
24
25namespace Aws
26{
27 namespace Utils
28 {
29 static const char* ARRAY_ALLOCATION_TAG = "Aws::Array";
30
34 template<typename T>
35 class Array
36 {
37
38 public:
42 Array(size_t arraySize = 0) :
43 m_capacity(arraySize),
44 m_length(arraySize),
45 m_data(arraySize > 0 ? Aws::MakeUniqueArray<T>(arraySize, ARRAY_ALLOCATION_TAG) : nullptr)
46 {
47 }
48
52 Array(const T* arrayToCopy, size_t arraySize) :
53 m_capacity(arraySize),
54 m_length(arraySize),
55 m_data(nullptr)
56 {
57 if (arrayToCopy != nullptr && m_capacity > 0)
58 {
59 m_data.reset(Aws::NewArray<T>(m_capacity, ARRAY_ALLOCATION_TAG));
60 std::copy(arrayToCopy, arrayToCopy + arraySize, m_data.get());
61 }
62 }
63
67 Array(size_t capacity,
68 size_t length,
70 : m_capacity(capacity),
71 m_length(length),
72 m_data(std::move(data))
73 {
74 }
75
80 {
81 size_t totalSize = 0;
82 for(auto& array : toMerge)
83 {
84 totalSize += array->m_length;
85 }
86
87 m_capacity = totalSize;
88 m_data.reset(Aws::NewArray<T>(m_capacity, ARRAY_ALLOCATION_TAG));
89
90 size_t location = 0;
91 for(auto& arr : toMerge)
92 {
93 if(arr->m_capacity > 0 && arr->m_data)
94 {
95 size_t arraySize = arr->m_length;
96 std::copy(arr->m_data.get(), arr->m_data.get() + arraySize, m_data.get() + location);
97 location += arraySize;
98 }
99 }
100 m_length = location;
101 }
102
103 Array(const Array& other)
104 {
105 m_capacity = other.m_capacity;
106 m_length = other.m_length;
107 m_data = nullptr;
108
109 if (m_capacity > 0)
110 {
111 m_data.reset(Aws::NewArray<T>(m_capacity, ARRAY_ALLOCATION_TAG));
112 std::copy(other.m_data.get(), other.m_data.get() + other.m_capacity, m_data.get());
113 }
114 }
115
116 //move c_tor
117 Array(Array&& other) noexcept:
118 m_capacity(other.m_capacity),
119 m_length(other.m_length),
120 m_data(std::move(other.m_data))
121 {
122 other.m_capacity = 0;
123 other.m_data = nullptr;
124 }
125
126 virtual ~Array() = default;
127
128 Array& operator=(const Array& other)
129 {
130 if (this == &other)
131 {
132 return *this;
133 }
134
135 m_capacity = other.m_capacity;
136 m_length = other.m_length;
137 m_data = nullptr;
138
139 if (m_capacity > 0)
140 {
141 m_data.reset(Aws::NewArray<T>(m_capacity, ARRAY_ALLOCATION_TAG));
142 std::copy(other.m_data.get(), other.m_data.get() + other.m_length, m_data.get());
143 }
144
145 return *this;
146 }
147
148 Array& operator=(Array&& other) noexcept
149 {
150 m_capacity = other.m_capacity;
151 m_length = other.m_length;
152 m_data = std::move(other.m_data);
153
154 return *this;
155 }
156
157 Array(const Aws::String& string):
158 m_capacity(string.capacity()),
159 m_length(string.length()),
160 m_data(nullptr)
161 {
162 m_data.reset(Aws::NewArray<unsigned char>(m_capacity, ARRAY_ALLOCATION_TAG));
163 std::copy(string.c_str(), string.c_str() + string.length(), m_data.get());
164 }
165
166 bool operator==(const Array& other) const
167 {
168 if (this == &other)
169 return true;
170
171 if (m_capacity == 0 && other.m_capacity == 0)
172 {
173 return true;
174 }
175
176 if (m_length != other.m_length)
177 {
178 return false;
179 }
180
181 if (m_length == other.m_length && m_capacity == other.m_capacity && m_data && other.m_data)
182 {
183 for (unsigned i = 0; i < m_length; ++i)
184 {
185 if (m_data.get()[i] != other.m_data.get()[i])
186 return false;
187 }
188
189 return true;
190 }
191
192 return false;
193 }
194
195 bool operator!=(const Array& other) const
196 {
197 return !(*this == other);
198 }
199
200 T const& GetItem(size_t index) const
201 {
202 assert(index < m_length);
203 return m_data.get()[index];
204 }
205
206 T& GetItem(size_t index)
207 {
208 assert(index < m_length);
209 return m_data.get()[index];
210 }
211
212 T& operator[](size_t index)
213 {
214 return GetItem(index);
215 }
216
217 T const& operator[](size_t index) const
218 {
219 return GetItem(index);
220 }
221
222 inline size_t GetLength() const
223 {
224 return m_length;
225 }
226
227 inline size_t GetSize() const
228 {
229 return m_capacity;
230 }
231
232 inline T* GetUnderlyingData() const
233 {
234 return m_data.get();
235 }
236
237 inline void SetLength(size_t len)
238 {
239 m_length = len;
240 }
241
242 protected:
243 size_t m_capacity = 0;
244 size_t m_length = 0;
246 };
247
249
254 class AWS_CORE_API CryptoBuffer : public ByteBuffer
255 {
256 public:
257 CryptoBuffer(size_t arraySize = 0) : ByteBuffer(arraySize) {}
258 CryptoBuffer(const unsigned char* arrayToCopy, size_t arraySize) : ByteBuffer(arrayToCopy, arraySize) {}
259 CryptoBuffer(Aws::Vector<ByteBuffer*>&& toMerge) : ByteBuffer(std::move(toMerge)) {}
260 CryptoBuffer(const ByteBuffer& other) : ByteBuffer(other) {}
261 CryptoBuffer(const CryptoBuffer& other) : ByteBuffer(other) {}
262 CryptoBuffer(CryptoBuffer&& other) : ByteBuffer(std::move(other)) {}
263 CryptoBuffer& operator=(const CryptoBuffer& other) { Zero(); ByteBuffer::operator=(other); return *this; }
264 CryptoBuffer& operator=(CryptoBuffer&& other) { Zero(); ByteBuffer::operator=(std::move(other)); return *this; }
265
266 CryptoBuffer(Crt::ByteBuf&& other) noexcept : ByteBuffer(
267 other.len,
268 other.len,
269 nullptr)
270 {
271 // Crt::ByteBuf must be allocated using SDK not CRT allocator
272 assert(get_aws_allocator() == other.allocator);
273 m_data.reset(other.buffer);
274 other.capacity = 0;
275 other.len = 0;
276 other.allocator = nullptr;
277 other.buffer = nullptr;
278 }
279
280 CryptoBuffer& operator=(Crt::ByteBuf&& other) noexcept
281 {
282 // Crt::ByteBuf must be allocated using SDK not CRT allocator
283 assert(get_aws_allocator() == other.allocator);
284 m_capacity = other.len;
285 m_length = other.len;
286 m_data.reset(other.buffer);
287 other.capacity = 0;
288 other.len = 0;
289 other.allocator = nullptr;
290 other.buffer = nullptr;
291 return *this;
292 }
293
294 bool operator==(const CryptoBuffer& other) const { return ByteBuffer::operator==(other); }
295 bool operator!=(const CryptoBuffer& other) const { return ByteBuffer::operator!=(other); }
296
297 ~CryptoBuffer() override { Zero(); }
298
299 Array<CryptoBuffer> Slice(size_t sizeOfSlice) const;
301 void Zero();
302 };
303
304 } // namespace Utils
305} // namespace Aws
T & GetItem(size_t index)
Definition Array.h:206
Array(const Array &other)
Definition Array.h:103
virtual ~Array()=default
size_t m_length
Definition Array.h:244
Array & operator=(const Array &other)
Definition Array.h:128
Array(const Aws::String &string)
Definition Array.h:157
T * GetUnderlyingData() const
Definition Array.h:232
Aws::UniqueArrayPtr< T > m_data
Definition Array.h:245
size_t GetLength() const
Definition Array.h:222
bool operator==(const Array &other) const
Definition Array.h:166
Array(const T *arrayToCopy, size_t arraySize)
Definition Array.h:52
bool operator!=(const Array &other) const
Definition Array.h:195
Array(size_t arraySize=0)
Definition Array.h:42
void SetLength(size_t len)
Definition Array.h:237
T & operator[](size_t index)
Definition Array.h:212
Array(Aws::Vector< Array * > &&toMerge)
Definition Array.h:79
Array(size_t capacity, size_t length, UniqueArrayPtr< T > data)
Definition Array.h:67
Array & operator=(Array &&other) noexcept
Definition Array.h:148
Array(Array &&other) noexcept
Definition Array.h:117
T const & GetItem(size_t index) const
Definition Array.h:200
T const & operator[](size_t index) const
Definition Array.h:217
size_t m_capacity
Definition Array.h:243
size_t GetSize() const
Definition Array.h:227
~CryptoBuffer() override
Definition Array.h:297
bool operator==(const CryptoBuffer &other) const
Definition Array.h:294
CryptoBuffer(const unsigned char *arrayToCopy, size_t arraySize)
Definition Array.h:258
CryptoBuffer(CryptoBuffer &&other)
Definition Array.h:262
CryptoBuffer(const ByteBuffer &other)
Definition Array.h:260
CryptoBuffer(const CryptoBuffer &other)
Definition Array.h:261
CryptoBuffer & operator=(const CryptoBuffer &other)
Definition Array.h:263
CryptoBuffer(Aws::Vector< ByteBuffer * > &&toMerge)
Definition Array.h:259
Array< CryptoBuffer > Slice(size_t sizeOfSlice) const
CryptoBuffer & operator=(CryptoBuffer &&other)
Definition Array.h:264
CryptoBuffer & operator=(Crt::ByteBuf &&other) noexcept
Definition Array.h:280
bool operator!=(const CryptoBuffer &other) const
Definition Array.h:295
CryptoBuffer & operator^(const CryptoBuffer &operand)
CryptoBuffer(size_t arraySize=0)
Definition Array.h:257
CryptoBuffer(Crt::ByteBuf &&other) noexcept
Definition Array.h:266
Array< unsigned char > ByteBuffer
Definition Array.h:248
static const char * ARRAY_ALLOCATION_TAG
Definition Array.h:29
std::unique_ptr< T, ArrayDeleter< T > > UniqueArrayPtr
Definition AWSMemory.h:338
UniqueArrayPtr< T > MakeUniqueArray(std::size_t amount, const char *allocationTag, ArgTypes &&... args)
Definition AWSMemory.h:345
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97
std::vector< T, Aws::Allocator< T > > Vector
Definition AWSVector.h:17
AWS_CORE_API aws_allocator * get_aws_allocator()