AWS SDK for C++  0.14.3
AWS SDK for C++
Array.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
19 
22 #include <memory>
23 #include <cassert>
24 #include <cstring>
25 #include <algorithm>
26 
27 #ifdef _WIN32
28 
29 #include <iterator>
30 
31 #endif // _WIN32
32 
33 namespace Aws
34 {
35  namespace Utils
36  {
37  static const char* ARRAY_ALLOCATION_TAG = "Aws::Array";
38 
42  template<typename T>
43  class Array
44  {
45 
46  public:
50  Array(size_t arraySize = 0) :
51  m_size(arraySize),
52  m_data(arraySize > 0 ? Aws::MakeUniqueArray<T>(arraySize, ARRAY_ALLOCATION_TAG) : nullptr)
53  {
54  }
55 
59  Array(const T* arrayToCopy, size_t arraySize) :
60  m_size(arraySize),
61  m_data(nullptr)
62  {
63  if (arrayToCopy != nullptr && m_size > 0)
64  {
65  m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
66 
67 #ifdef _WIN32
68  std::copy(arrayToCopy, arrayToCopy + arraySize, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
69 #else
70  std::copy(arrayToCopy, arrayToCopy + arraySize, m_data.get());
71 #endif // MSVC
72  }
73  }
74 
79  {
80  size_t totalSize = 0;
81  for(auto& array : toMerge)
82  {
83  totalSize += array->m_size;
84  }
85 
86  m_size = totalSize;
87  m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
88 
89  size_t location = 0;
90  for(auto& arr : toMerge)
91  {
92  if(arr->m_size > 0 && arr->m_data)
93  {
94  size_t arraySize = arr->m_size;
95 #ifdef _WIN32
96  std::copy(arr->m_data.get(), arr->m_data.get() + arraySize, stdext::checked_array_iterator< T * >(m_data.get() + location, m_size));
97 #else
98  std::copy(arr->m_data.get(), arr->m_data.get() + arraySize, m_data.get() + location);
99 #endif // MSVC
100  location += arraySize;
101  }
102  }
103  }
104 
105  Array(const Array& other)
106  {
107  m_size = other.m_size;
108  m_data = nullptr;
109 
110  if (m_size > 0)
111  {
112  m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
113 
114 #ifdef _WIN32
115  std::copy(other.m_data.get(), other.m_data.get() + other.m_size, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
116 #else
117  std::copy(other.m_data.get(), other.m_data.get() + other.m_size, m_data.get());
118 #endif // MSVC
119  }
120  }
121 
122  //move c_tor
123  Array(Array&& other) :
124  m_size(other.m_size),
125  m_data(std::move(other.m_data))
126  {
127  other.m_size = 0;
128  other.m_data = nullptr;
129  }
130 
131  virtual ~Array() = default;
132 
133  Array& operator=(const Array& other)
134  {
135  if (this == &other)
136  {
137  return *this;
138  }
139 
140  m_size = other.m_size;
141  m_data = nullptr;
142 
143  if (m_size > 0)
144  {
145  m_data.reset(Aws::NewArray<T>(m_size, ARRAY_ALLOCATION_TAG));
146 
147 #ifdef _WIN32
148  std::copy(other.m_data.get(), other.m_data.get() + other.m_size, stdext::checked_array_iterator< T * >(m_data.get(), m_size));
149 #else
150  std::copy(other.m_data.get(), other.m_data.get() + other.m_size, m_data.get());
151 #endif // MSVC
152  }
153 
154  return *this;
155  }
156 
158  {
159  m_size = other.m_size;
160  m_data = std::move(other.m_data);
161 
162  return *this;
163  }
164 
165  bool operator==(const Array& other) const
166  {
167  if (this == &other)
168  return true;
169 
170  if (m_size == 0 && other.m_size == 0)
171  {
172  return true;
173  }
174 
175  if (m_size == other.m_size && m_data && other.m_data)
176  {
177  for (unsigned i = 0; i < m_size; ++i)
178  {
179  if (m_data.get()[i] != other.m_data.get()[i])
180  return false;
181  }
182 
183  return true;
184  }
185 
186  return false;
187  }
188 
189  bool operator!=(const Array& other) const
190  {
191  return !(*this == other);
192  }
193 
194  T const& GetItem(size_t index) const
195  {
196  assert(index < m_size);
197  return m_data.get()[index];
198  }
199 
200  T& GetItem(size_t index)
201  {
202  assert(index < m_size);
203  return m_data.get()[index];
204  }
205 
206  T& operator[](size_t index)
207  {
208  return GetItem(index);
209  }
210 
211  T const& operator[](size_t index) const
212  {
213  return GetItem(index);
214  }
215 
216  inline size_t GetLength() const
217  {
218  return m_size;
219  }
220 
221  inline T* GetUnderlyingData() const
222  {
223  return m_data.get();
224  }
225 
226  protected:
227  size_t m_size;
228 
230  };
231 
233 
238  class AWS_CORE_API CryptoBuffer : public ByteBuffer
239  {
240  public:
241  CryptoBuffer(size_t arraySize = 0) : ByteBuffer(arraySize) {}
242  CryptoBuffer(const unsigned char* arrayToCopy, size_t arraySize) : ByteBuffer(arrayToCopy, arraySize) {}
243  CryptoBuffer(Aws::Vector<ByteBuffer*>&& toMerge) : ByteBuffer(std::move(toMerge)) {}
244  CryptoBuffer(const ByteBuffer& other) : ByteBuffer(other) {}
245  CryptoBuffer(const CryptoBuffer& other) : ByteBuffer(other) {}
246  CryptoBuffer(CryptoBuffer&& other) : ByteBuffer(std::move(other)) {}
247  CryptoBuffer& operator=(const CryptoBuffer&) = default;
248  CryptoBuffer& operator=(CryptoBuffer&& other) { ByteBuffer::operator=(std::move(other)); return *this; }
249  bool operator==(const CryptoBuffer& other) const { return ByteBuffer::operator==(other); }
250  bool operator!=(const CryptoBuffer& other) const { return ByteBuffer::operator!=(other); }
251 
252  ~CryptoBuffer() { Zero(); }
253 
254  Array<CryptoBuffer> Slice(size_t sizeOfSlice) const;
255  CryptoBuffer& operator^(const CryptoBuffer& operand);
256  void Zero();
257  };
258 
259  } // namespace Utils
260 } // namespace Aws
virtual ~Array()=default
Array(const T *arrayToCopy, size_t arraySize)
Definition: Array.h:59
T const & operator[](size_t index) const
Definition: Array.h:211
size_t GetLength() const
Definition: Array.h:216
T * GetUnderlyingData() const
Definition: Array.h:221
CryptoBuffer(CryptoBuffer &&other)
Definition: Array.h:246
UniqueArrayPtr< T > MakeUniqueArray(std::size_t amount, const char *allocationTag, ArgTypes &&...args)
Definition: AWSMemory.h:239
STL namespace.
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:27
bool operator!=(const CryptoBuffer &other) const
Definition: Array.h:250
bool operator!=(const Array &other) const
Definition: Array.h:189
static const char * ARRAY_ALLOCATION_TAG
Definition: Array.h:37
Array(size_t arraySize=0)
Definition: Array.h:50
CryptoBuffer(Aws::Vector< ByteBuffer * > &&toMerge)
Definition: Array.h:243
T & GetItem(size_t index)
Definition: Array.h:200
Array< unsigned char > ByteBuffer
Definition: Array.h:232
Array(Array &&other)
Definition: Array.h:123
CryptoBuffer & operator=(CryptoBuffer &&other)
Definition: Array.h:248
std::unique_ptr< T, ArrayDeleter< T > > UniqueArrayPtr
Definition: AWSMemory.h:232
CryptoBuffer(const CryptoBuffer &other)
Definition: Array.h:245
CryptoBuffer(const ByteBuffer &other)
Definition: Array.h:244
size_t m_size
Definition: Array.h:227
Array & operator=(Array &&other)
Definition: Array.h:157
Array(Aws::Vector< Array * > &&toMerge)
Definition: Array.h:78
bool operator==(const CryptoBuffer &other) const
Definition: Array.h:249
T const & GetItem(size_t index) const
Definition: Array.h:194
CryptoBuffer(size_t arraySize=0)
Definition: Array.h:241
Array & operator=(const Array &other)
Definition: Array.h:133
Array(const Array &other)
Definition: Array.h:105
CryptoBuffer(const unsigned char *arrayToCopy, size_t arraySize)
Definition: Array.h:242
bool operator==(const Array &other) const
Definition: Array.h:165
#define AWS_CORE_API
Definition: Core_EXPORTS.h:35
Aws::UniqueArrayPtr< T > m_data
Definition: Array.h:229
T & operator[](size_t index)
Definition: Array.h:206
JSON (JavaScript Object Notation).