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