AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/crt/Types.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

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>
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
22struct aws_allocator;
23struct aws_byte_buf;
24struct aws_byte_cursor;
25struct aws_socket_options;
26
27namespace Aws
28{
29 namespace Crt
30 {
31 using Allocator = aws_allocator;
32 using ByteBuf = aws_byte_buf;
33 using ByteCursor = aws_byte_cursor;
34
35 namespace Io
36 {
37 using IStream = std::basic_istream<char, std::char_traits<char>>;
38 } // namespace Io
39
40 namespace Mqtt
41 {
42 using QOS = aws_mqtt_qos;
43 using ReturnCode = aws_mqtt_connect_return_code;
44 } // namespace Mqtt
45
46 template <typename T> class StlAllocator;
47 using String = std::basic_string<char, std::char_traits<char>, StlAllocator<char>>;
48 using StringStream = std::basic_stringstream<char, std::char_traits<char>, StlAllocator<char>>;
49 template <typename K, typename V> using Map = std::map<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
50 template <typename K, typename V>
51 using UnorderedMap =
52 std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, StlAllocator<std::pair<const K, V>>>;
53 template <typename K, typename V>
54 using MultiMap = std::multimap<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
55 template <typename T> using Vector = std::vector<T, StlAllocator<T>>;
56 template <typename T> using List = std::list<T, StlAllocator<T>>;
57
58 AWS_CRT_CPP_API Allocator *DefaultAllocator() noexcept;
59 AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept;
60 AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept;
61 AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept;
62 AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len);
63 AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &);
64
65 AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept;
66 AWS_CRT_CPP_API ByteCursor ByteCursorFromString(const Crt::String &str) noexcept;
67 AWS_CRT_CPP_API ByteCursor ByteCursorFromStringView(const Crt::StringView &str) noexcept;
68 AWS_CRT_CPP_API ByteCursor ByteCursorFromByteBuf(const ByteBuf &) noexcept;
69 AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept;
70
71 AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(const String &decode);
72 AWS_CRT_CPP_API String Base64Encode(const Vector<uint8_t> &encode);
73
74 template <typename RawType, typename TargetType> using TypeConvertor = std::function<TargetType(RawType)>;
75
80 template <typename RawType, typename TargetType>
81 Vector<TargetType> ArrayListToVector(const aws_array_list *array, TypeConvertor<RawType, TargetType> conv)
82 {
83 Vector<TargetType> v;
84 size_t cnt = aws_array_list_length(array);
85 for (size_t i = 0; i < cnt; i++)
86 {
87 RawType t;
88 aws_array_list_get_at(array, &t, i);
89 v.emplace_back(conv(t));
90 }
91 return v;
92 }
93
98 template <typename RawType, typename TargetType>
99 Vector<TargetType> ArrayListToVector(const aws_array_list *array)
100 {
101 Vector<TargetType> v;
102 size_t cnt = aws_array_list_length(array);
103 for (size_t i = 0; i < cnt; i++)
104 {
105 RawType t;
106 aws_array_list_get_at(array, &t, i);
107 v.emplace_back(TargetType(t));
108 }
109 return v;
110 }
111
115 template <typename Type> Vector<Type> ArrayListToVector(const aws_array_list *array)
116 {
117 Vector<Type> v;
118 size_t cnt = aws_array_list_length(array);
119 for (size_t i = 0; i < cnt; i++)
120 {
121 Type t;
122 aws_array_list_get_at(array, &t, i);
123 v.emplace_back(t);
124 }
125 return v;
126 }
127
128 AWS_CRT_CPP_API inline StringView ByteCursorToStringView(const ByteCursor &bc)
129 {
130 return StringView(reinterpret_cast<char *>(bc.ptr), bc.len);
131 }
132
133 AWS_CRT_CPP_API inline ByteCursor StringViewToByteCursor(const StringView &sv)
134 {
135 ByteCursor bc;
136 bc.ptr = (uint8_t *)(sv.data());
137 bc.len = sv.size();
138 return bc;
139 }
140
141 template <typename T> void Delete(T *t, Allocator *allocator)
142 {
143 t->~T();
144 aws_mem_release(allocator, t);
145 }
146
147 template <typename T, typename... Args> T *New(Allocator *allocator, Args &&... args)
148 {
149 T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
150 if (!t)
151 return nullptr;
152 return new (t) T(std::forward<Args>(args)...);
153 }
154
155 template <typename T, typename... Args> std::shared_ptr<T> MakeShared(Allocator *allocator, Args &&... args)
156 {
157 T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
158 if (!t)
159 return nullptr;
160 new (t) T(std::forward<Args>(args)...);
161
162 return std::shared_ptr<T>(t, [allocator](T *obj) { Delete(obj, allocator); });
163 }
164
165 template <typename T> using ScopedResource = std::unique_ptr<T, std::function<void(T *)>>;
166
167 } // namespace Crt
168} // namespace Aws
Optional.h
StlAllocator.h
StringView.h
Aws::Crt::StlAllocator
Definition: StlAllocator.h:25
Aws::Crt::basic_string_view
Definition: StringView.h:32
Aws::Crt::basic_string_view::size
constexpr size_type size() const noexcept
Definition: StringView.h:92
Aws::Crt::basic_string_view::data
constexpr const_pointer data() const noexcept
Definition: StringView.h:126
Exports.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Crt::Io::IStream
std::basic_istream< char, std::char_traits< char > > IStream
Definition: Types.h:37
Aws::Crt::Mqtt::QOS
aws_mqtt_qos QOS
Definition: Types.h:42
Aws::Crt::Mqtt::ReturnCode
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:43
Aws::Crt::ByteCursorFromStringView
AWS_CRT_CPP_API ByteCursor ByteCursorFromStringView(const Crt::StringView &str) noexcept
Definition: Types.cpp:44
Aws::Crt::StringViewToByteCursor
AWS_CRT_CPP_API ByteCursor StringViewToByteCursor(const StringView &sv)
Definition: Types.h:133
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:33
Aws::Crt::ByteBufNewCopy
AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len)
Definition: Types.cpp:27
Aws::Crt::Map
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V > > > Map
Definition: Types.h:49
Aws::Crt::ByteCursorFromCString
AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept
Definition: Types.cpp:37
Aws::Crt::ArrayListToVector
Vector< TargetType > ArrayListToVector(const aws_array_list *array, TypeConvertor< RawType, TargetType > conv)
Definition: Types.h:81
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:17
Aws::Crt::New
T * New(Allocator *allocator, Args &&... args)
Definition: Types.h:147
Aws::Crt::StringStream
std::basic_stringstream< char, std::char_traits< char >, StlAllocator< char > > StringStream
Definition: Types.h:48
Aws::Crt::ByteCursorFromArray
AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:51
Aws::Crt::Base64Encode
AWS_CRT_CPP_API String Base64Encode(const Vector< uint8_t > &encode)
Definition: Types.cpp:77
Aws::Crt::Delete
void Delete(T *t, Allocator *allocator)
Definition: Types.h:141
Aws::Crt::UnorderedMap
std::unordered_map< K, V, std::hash< K >, std::equal_to< K >, StlAllocator< std::pair< const K, V > > > UnorderedMap
Definition: Types.h:52
Aws::Crt::List
std::list< T, StlAllocator< T > > List
Definition: Types.h:56
Aws::Crt::DefaultAllocator
AWS_CRT_CPP_API Allocator * DefaultAllocator() noexcept
Definition: Types.cpp:13
Aws::Crt::ByteBufFromEmptyArray
AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:17
Aws::Crt::TypeConvertor
std::function< TargetType(RawType)> TypeConvertor
Definition: Types.h:74
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:47
Aws::Crt::ByteCursorToStringView
AWS_CRT_CPP_API StringView ByteCursorToStringView(const ByteCursor &bc)
Definition: Types.h:128
Aws::Crt::StringView
string_view StringView
Definition: StringView.h:845
Aws::Crt::ByteCursorFromString
AWS_CRT_CPP_API ByteCursor ByteCursorFromString(const Crt::String &str) noexcept
Definition: Types.cpp:39
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:32
Aws::Crt::ScopedResource
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition: Types.h:165
Aws::Crt::MakeShared
std::shared_ptr< T > MakeShared(Allocator *allocator, Args &&... args)
Definition: Types.h:155
Aws::Crt::ByteBufFromCString
AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept
Definition: Types.cpp:15
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:55
Aws::Crt::Base64Decode
AWS_CRT_CPP_API Vector< uint8_t > Base64Decode(const String &decode)
Definition: Types.cpp:56
Aws::Crt::ByteBufDelete
AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &)
Definition: Types.cpp:35
Aws::Crt::ByteCursorFromByteBuf
AWS_CRT_CPP_API ByteCursor ByteCursorFromByteBuf(const ByteBuf &) noexcept
Definition: Types.cpp:49
Aws::Crt::ByteBufFromArray
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept
Definition: Types.cpp:22
Aws::Crt::MultiMap
std::multimap< K, V, std::less< K >, StlAllocator< std::pair< const K, V > > > MultiMap
Definition: Types.h:54
Aws
Definition: Api.h:17
std
Definition: StringView.h:851