AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
Array.h
Go to the documentation of this file.
1
6#pragma once
7
9
12#include <memory>
13#include <cassert>
14#include <cstring>
15#include <algorithm>
16
17#ifdef _WIN32
18
19#include <iterator>
20
21#endif // _WIN32
22
23namespace Aws
24{
25 namespace Utils
26 {
27 static const char* ARRAY_ALLOCATION_TAG = "Aws::Array";
28
32 template<typename T>
33 class Array
34 {
35
36 public:
40 Array(size_t arraySize = 0) :
41 m_size(arraySize),
42 m_data(arraySize > 0 ? Aws::MakeUniqueArray<T>(arraySize, ARRAY_ALLOCATION_TAG) : nullptr)
43 {
44 }
45
49 Array(const T* arrayToCopy, size_t arraySize) :
50 m_size(arraySize),
51 m_data(nullptr)
52 {
53 if (arrayToCopy != nullptr && m_size > 0)
54 {
55 m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
56
57#ifdef _WIN32
58 std::copy(arrayToCopy, arrayToCopy + arraySize, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
59#else
60 std::copy(arrayToCopy, arrayToCopy + arraySize, m_data.get());
61#endif // MSVC
62 }
63 }
64
69 {
70 size_t totalSize = 0;
71 for(auto& array : toMerge)
72 {
73 totalSize += array->m_size;
74 }
75
76 m_size = totalSize;
77 m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
78
79 size_t location = 0;
80 for(auto& arr : toMerge)
81 {
82 if(arr->m_size > 0 && arr->m_data)
83 {
84 size_t arraySize = arr->m_size;
85#ifdef _WIN32
86 std::copy(arr->m_data.get(), arr->m_data.get() + arraySize, stdext::checked_array_iterator< T * >(m_data.get() + location, m_size));
87#else
88 std::copy(arr->m_data.get(), arr->m_data.get() + arraySize, m_data.get() + location);
89#endif // MSVC
90 location += arraySize;
91 }
92 }
93 }
94
95 Array(const Array& other)
96 {
97 m_size = other.m_size;
98 m_data = nullptr;
99
100 if (m_size > 0)
101 {
102 m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
103
104#ifdef _WIN32
105 std::copy(other.m_data.get(), other.m_data.get() + other.m_size, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
106#else
107 std::copy(other.m_data.get(), other.m_data.get() + other.m_size, m_data.get());
108#endif // MSVC
109 }
110 }
111
112 //move c_tor
113 Array(Array&& other) :
114 m_size(other.m_size),
115 m_data(std::move(other.m_data))
116 {
117 other.m_size = 0;
118 other.m_data = nullptr;
119 }
120
121 virtual ~Array() = default;
122
123 Array& operator=(const Array& other)
124 {
125 if (this == &other)
126 {
127 return *this;
128 }
129
130 m_size = other.m_size;
131 m_data = nullptr;
132
133 if (m_size > 0)
134 {
135 m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
136
137#ifdef _WIN32
138 std::copy(other.m_data.get(), other.m_data.get() + other.m_size, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
139#else
140 std::copy(other.m_data.get(), other.m_data.get() + other.m_size, m_data.get());
141#endif // MSVC
142 }
143
144 return *this;
145 }
146
148 {
149 m_size = other.m_size;
150 m_data = std::move(other.m_data);
151
152 return *this;
153 }
154
155 bool operator==(const Array& other) const
156 {
157 if (this == &other)
158 return true;
159
160 if (m_size == 0 && other.m_size == 0)
161 {
162 return true;
163 }
164
165 if (m_size == other.m_size && m_data && other.m_data)
166 {
167 for (unsigned i = 0; i < m_size; ++i)
168 {
169 if (m_data.get()[i] != other.m_data.get()[i])
170 return false;
171 }
172
173 return true;
174 }
175
176 return false;
177 }
178
179 bool operator!=(const Array& other) const
180 {
181 return !(*this == other);
182 }
183
184 T const& GetItem(size_t index) const
185 {
186 assert(index < m_size);
187 return m_data.get()[index];
188 }
189
190 T& GetItem(size_t index)
191 {
192 assert(index < m_size);
193 return m_data.get()[index];
194 }
195
196 T& operator[](size_t index)
197 {
198 return GetItem(index);
199 }
200
201 T const& operator[](size_t index) const
202 {
203 return GetItem(index);
204 }
205
206 inline size_t GetLength() const
207 {
208 return m_size;
209 }
210
211 inline T* GetUnderlyingData() const
212 {
213 return m_data.get();
214 }
215
216 protected:
217 size_t m_size;
218
220 };
221
223
229 {
230 public:
231 CryptoBuffer(size_t arraySize = 0) : ByteBuffer(arraySize) {}
232 CryptoBuffer(const unsigned char* arrayToCopy, size_t arraySize) : ByteBuffer(arrayToCopy, arraySize) {}
233 CryptoBuffer(Aws::Vector<ByteBuffer*>&& toMerge) : ByteBuffer(std::move(toMerge)) {}
234 CryptoBuffer(const ByteBuffer& other) : ByteBuffer(other) {}
235 CryptoBuffer(const CryptoBuffer& other) : ByteBuffer(other) {}
236 CryptoBuffer(CryptoBuffer&& other) : ByteBuffer(std::move(other)) {}
238 CryptoBuffer& operator=(CryptoBuffer&& other) { ByteBuffer::operator=(std::move(other)); return *this; }
239 bool operator==(const CryptoBuffer& other) const { return ByteBuffer::operator==(other); }
240 bool operator!=(const CryptoBuffer& other) const { return ByteBuffer::operator!=(other); }
241
242 ~CryptoBuffer() { Zero(); }
243
244 Array<CryptoBuffer> Slice(size_t sizeOfSlice) const;
246 void Zero();
247 };
248
249 } // namespace Utils
250} // namespace Aws
#define AWS_CORE_API
Definition: Core_EXPORTS.h:26
int index
Definition: cJSON.h:181
T & GetItem(size_t index)
Definition: Array.h:190
Array(Array &&other)
Definition: Array.h:113
Array(const Array &other)
Definition: Array.h:95
virtual ~Array()=default
Array & operator=(const Array &other)
Definition: Array.h:123
size_t m_size
Definition: Array.h:217
T * GetUnderlyingData() const
Definition: Array.h:211
Array & operator=(Array &&other)
Definition: Array.h:147
Aws::UniqueArrayPtr< T > m_data
Definition: Array.h:219
size_t GetLength() const
Definition: Array.h:206
bool operator==(const Array &other) const
Definition: Array.h:155
Array(const T *arrayToCopy, size_t arraySize)
Definition: Array.h:49
bool operator!=(const Array &other) const
Definition: Array.h:179
Array(size_t arraySize=0)
Definition: Array.h:40
T & operator[](size_t index)
Definition: Array.h:196
Array(Aws::Vector< Array * > &&toMerge)
Definition: Array.h:68
T const & GetItem(size_t index) const
Definition: Array.h:184
T const & operator[](size_t index) const
Definition: Array.h:201
bool operator==(const CryptoBuffer &other) const
Definition: Array.h:239
CryptoBuffer(const unsigned char *arrayToCopy, size_t arraySize)
Definition: Array.h:232
CryptoBuffer(CryptoBuffer &&other)
Definition: Array.h:236
CryptoBuffer(const ByteBuffer &other)
Definition: Array.h:234
CryptoBuffer(const CryptoBuffer &other)
Definition: Array.h:235
CryptoBuffer(Aws::Vector< ByteBuffer * > &&toMerge)
Definition: Array.h:233
Array< CryptoBuffer > Slice(size_t sizeOfSlice) const
CryptoBuffer & operator=(const CryptoBuffer &)=default
CryptoBuffer & operator=(CryptoBuffer &&other)
Definition: Array.h:238
bool operator!=(const CryptoBuffer &other) const
Definition: Array.h:240
CryptoBuffer & operator^(const CryptoBuffer &operand)
CryptoBuffer(size_t arraySize=0)
Definition: Array.h:231
Array< unsigned char > ByteBuffer
Definition: Array.h:222
static const char * ARRAY_ALLOCATION_TAG
Definition: Array.h:27
std::unique_ptr< T, ArrayDeleter< T > > UniqueArrayPtr
Definition: AWSMemory.h:333
UniqueArrayPtr< T > MakeUniqueArray(std::size_t amount, const char *allocationTag, ArgTypes &&... args)
Definition: AWSMemory.h:340
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17