aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Types.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/common/common.h>
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/Optional.h>
9 #include <aws/crt/StlAllocator.h>
10 #include <aws/crt/StringView.h>
11 #include <aws/io/socket.h>
12 #include <aws/mqtt/mqtt.h>
13 #include <functional>
14 #include <list>
15 #include <map>
16 #include <sstream>
17 #include <string>
18 #include <unordered_map>
19 #include <utility>
20 #include <vector>
21 
22 struct aws_byte_buf;
23 struct aws_byte_cursor;
24 struct aws_socket_options;
25 
26 namespace Aws
27 {
28  namespace Crt
29  {
30  using ByteBuf = aws_byte_buf;
31  using ByteCursor = aws_byte_cursor;
32 
33  namespace Io
34  {
35  using IStream = std::basic_istream<char, std::char_traits<char>>;
36  } // namespace Io
37 
38  namespace Mqtt
39  {
40  using QOS = aws_mqtt_qos;
41  using ReturnCode = aws_mqtt_connect_return_code;
42  } // namespace Mqtt
43 
44  template <typename T> class StlAllocator;
45  using String = std::basic_string<char, std::char_traits<char>, StlAllocator<char>>;
46  using StringStream = std::basic_stringstream<char, std::char_traits<char>, StlAllocator<char>>;
47  template <typename K, typename V> using Map = std::map<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
48  template <typename K, typename V>
49  using UnorderedMap =
50  std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, StlAllocator<std::pair<const K, V>>>;
51  template <typename K, typename V>
52  using MultiMap = std::multimap<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
53  template <typename T> using Vector = std::vector<T, StlAllocator<T>>;
54  template <typename T> using List = std::list<T, StlAllocator<T>>;
55 
56  AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept;
57  AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept;
58  AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept;
59  AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len);
60  AWS_CRT_CPP_API ByteBuf ByteBufInit(Allocator *alloc, size_t len);
61  AWS_CRT_CPP_API bool ByteBufInitFromFile(ByteBuf &buf, Allocator *alloc, const char *filename);
63 
64  AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept;
68  AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept;
69 
70  AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(const String &decode) noexcept;
71  AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(ByteCursor decode) noexcept;
72  AWS_CRT_CPP_API size_t Base64DecodedLength(ByteCursor decode) noexcept;
73  AWS_CRT_CPP_API String Base64Encode(const Vector<uint8_t> &encode) noexcept;
75  AWS_CRT_CPP_API size_t Base64EncodedLength(ByteCursor encode) noexcept;
76 
77  template <typename RawType, typename TargetType> using TypeConvertor = std::function<TargetType(RawType)>;
78 
83  template <typename RawType, typename TargetType>
84  Vector<TargetType> ArrayListToVector(const aws_array_list *array, TypeConvertor<RawType, TargetType> conv)
85  {
87  size_t cnt = aws_array_list_length(array);
88  for (size_t i = 0; i < cnt; i++)
89  {
90  RawType t;
91  aws_array_list_get_at(array, &t, i);
92  v.emplace_back(conv(t));
93  }
94  return v;
95  }
96 
101  template <typename RawType, typename TargetType>
102  Vector<TargetType> ArrayListToVector(const aws_array_list *array)
103  {
105  size_t cnt = aws_array_list_length(array);
106  for (size_t i = 0; i < cnt; i++)
107  {
108  RawType t;
109  aws_array_list_get_at(array, &t, i);
110  v.emplace_back(TargetType(t));
111  }
112  return v;
113  }
114 
118  template <typename Type> Vector<Type> ArrayListToVector(const aws_array_list *array)
119  {
120  Vector<Type> v;
121  size_t cnt = aws_array_list_length(array);
122  for (size_t i = 0; i < cnt; i++)
123  {
124  Type t;
125  aws_array_list_get_at(array, &t, i);
126  v.emplace_back(t);
127  }
128  return v;
129  }
130 
132  {
133  return StringView(reinterpret_cast<char *>(bc.ptr), bc.len);
134  }
135 
137  {
138  ByteCursor bc;
139  bc.ptr = (uint8_t *)(sv.data());
140  bc.len = sv.size();
141  return bc;
142  }
143 
144  template <typename T> void Delete(T *t, Allocator *allocator)
145  {
146  t->~T();
147  aws_mem_release(allocator, t);
148  }
149 
150  template <typename T, typename... Args> T *New(Allocator *allocator, Args &&...args)
151  {
152  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
153  if (!t)
154  return nullptr;
155  return new (t) T(std::forward<Args>(args)...);
156  }
157 
158  template <typename T, typename... Args> std::shared_ptr<T> MakeShared(Allocator *allocator, Args &&...args)
159  {
160  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
161  if (!t)
162  return nullptr;
163  new (t) T(std::forward<Args>(args)...);
164 
165  return std::shared_ptr<T>(t, [allocator](T *obj) { Delete(obj, allocator); });
166  }
167 
168  template <typename T> using ScopedResource = std::unique_ptr<T, std::function<void(T *)>>;
169 
170  template <
171  typename Derived,
172  typename Base,
173  typename std::enable_if<std::is_base_of<Base, Derived>::value, bool>::type = true>
175  {
176  const auto &deleter = derived.get_deleter();
177  return ScopedResource<Base>(
178  derived.release(), [deleter](Base *base) { deleter(static_cast<Derived *>(base)); });
179  }
180 
181  template <
182  typename Derived,
183  typename Base,
184  typename std::enable_if<!std::is_base_of<Base, Derived>::value, bool>::type = true>
185  ScopedResource<Base> SafeSuperCast(ScopedResource<Derived> derived)
186  {
187  (void)derived;
188  static_assert(std::is_base_of<Base, Derived>::value, "Base must be a base class of Derived");
189  return nullptr;
190  }
191 
192  template <
193  typename Base,
194  typename Derived,
195  typename std::enable_if<std::is_base_of<Base, Derived>::value, bool>::type = true>
197  {
198  return ScopedResource<Derived>(static_cast<Derived *>(base.release()), base.get_deleter());
199  }
200 
201  template <
202  typename Base,
203  typename Derived,
204  typename std::enable_if<!std::is_base_of<Base, Derived>::value, bool>::type = true>
205  ScopedResource<Derived> SafeSubCast(ScopedResource<Base> base)
206  {
207  (void)base;
208  static_assert(std::is_base_of<Base, Derived>::value, "Base must be a base class of Derived");
209  return nullptr;
210  }
211 
212  } // namespace Crt
213 } // namespace Aws
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept
Definition: Types.cpp:23
Vector< TargetType > ArrayListToVector(const aws_array_list *array, TypeConvertor< RawType, TargetType > conv)
Definition: Types.h:84
std::multimap< K, V, std::less< K >, StlAllocator< std::pair< const K, V >>> MultiMap
Definition: Types.h:52
AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len)
Definition: Types.cpp:28
T * New(Allocator *allocator, Args &&...args)
Definition: Types.h:150
ScopedResource< Base > SafeSuperCast(ScopedResource< Derived > derived)
Definition: Types.h:174
std::shared_ptr< T > MakeShared(Allocator *allocator, Args &&...args)
Definition: Types.h:158
#define AWS_CRT_CPP_API
Definition: Exports.h:36
AWS_CRT_CPP_API ByteBuf ByteBufInit(Allocator *alloc, size_t len)
Definition: Types.cpp:36
AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:18
Definition: StringView.h:861
constexpr const_pointer data() const noexcept
Definition: StringView.h:127
string_view StringView
Definition: StringView.h:856
aws_byte_buf ByteBuf
Definition: Types.h:30
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:41
AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &)
Definition: Types.cpp:48
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
std::basic_istream< char, std::char_traits< char >> IStream
Definition: Types.h:35
ScopedResource< Derived > SafeSubCast(ScopedResource< Base > base)
Definition: Types.h:196
AWS_CRT_CPP_API size_t Base64DecodedLength(ByteCursor decode) noexcept
Definition: Types.cpp:104
aws_allocator Allocator
Definition: Allocator.h:14
AWS_CRT_CPP_API ByteCursor ByteCursorFromString(const Crt::String &str) noexcept
Definition: Types.cpp:58
aws_byte_cursor ByteCursor
Definition: Types.h:31
Definition: StlAllocator.h:20
constexpr size_type size() const noexcept
Definition: StringView.h:93
AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:73
std::basic_stringstream< char, std::char_traits< char >, StlAllocator< char >> StringStream
Definition: Types.h:46
AWS_CRT_CPP_API String Base64Encode(const Vector< uint8_t > &encode) noexcept
Definition: Types.cpp:111
AWS_CRT_CPP_API ByteCursor ByteCursorFromByteBuf(const ByteBuf &) noexcept
Definition: Types.cpp:68
AWS_CRT_CPP_API size_t Base64EncodedLength(ByteCursor encode) noexcept
Definition: Types.cpp:138
AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept
Definition: Types.cpp:53
AWS_CRT_CPP_API ByteCursor ByteCursorFromStringView(const Crt::StringView &str) noexcept
Definition: Types.cpp:63
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V >>> Map
Definition: Types.h:47
AWS_CRT_CPP_API StringView ByteCursorToStringView(const ByteCursor &bc)
Definition: Types.h:131
AWS_CRT_CPP_API Vector< uint8_t > Base64Decode(const String &decode) noexcept
Definition: Types.cpp:78
std::vector< T, StlAllocator< T >> Vector
Definition: Types.h:53
aws_mqtt_qos QOS
Definition: Types.h:40
std::list< T, StlAllocator< T >> List
Definition: Types.h:54
AWS_CRT_CPP_API bool ByteBufInitFromFile(ByteBuf &buf, Allocator *alloc, const char *filename)
Definition: Types.cpp:43
std::unique_ptr< T, std::function< void(T *)>> ScopedResource
Definition: Types.h:168
AWS_CRT_CPP_API ByteCursor StringViewToByteCursor(const StringView &sv)
Definition: Types.h:136
Definition: StringView.h:32
void Delete(T *t, Allocator *allocator)
Definition: Types.h:144
std::unordered_map< K, V, std::hash< K >, std::equal_to< K >, StlAllocator< std::pair< const K, V >>> UnorderedMap
Definition: Types.h:50
Definition: Allocator.h:10
std::function< TargetType(RawType)> TypeConvertor
Definition: Types.h:77
AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept
Definition: Types.cpp:13