AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
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);
62 
63  AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept;
67  AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept;
68 
69  AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(const String &decode) noexcept;
70  AWS_CRT_CPP_API String Base64Encode(const Vector<uint8_t> &encode) noexcept;
71 
72  template <typename RawType, typename TargetType> using TypeConvertor = std::function<TargetType(RawType)>;
73 
78  template <typename RawType, typename TargetType>
80  {
82  size_t cnt = aws_array_list_length(array);
83  for (size_t i = 0; i < cnt; i++)
84  {
85  RawType t;
86  aws_array_list_get_at(array, &t, i);
87  v.emplace_back(conv(t));
88  }
89  return v;
90  }
91 
96  template <typename RawType, typename TargetType>
97  Vector<TargetType> ArrayListToVector(const aws_array_list *array)
98  {
100  size_t cnt = aws_array_list_length(array);
101  for (size_t i = 0; i < cnt; i++)
102  {
103  RawType t;
104  aws_array_list_get_at(array, &t, i);
105  v.emplace_back(TargetType(t));
106  }
107  return v;
108  }
109 
113  template <typename Type> Vector<Type> ArrayListToVector(const aws_array_list *array)
114  {
115  Vector<Type> v;
116  size_t cnt = aws_array_list_length(array);
117  for (size_t i = 0; i < cnt; i++)
118  {
119  Type t;
120  aws_array_list_get_at(array, &t, i);
121  v.emplace_back(t);
122  }
123  return v;
124  }
125 
127  {
128  return StringView(reinterpret_cast<char *>(bc.ptr), bc.len);
129  }
130 
132  {
133  ByteCursor bc;
134  bc.ptr = (uint8_t *)(sv.data());
135  bc.len = sv.size();
136  return bc;
137  }
138 
139  template <typename T> void Delete(T *t, Allocator *allocator)
140  {
141  t->~T();
142  aws_mem_release(allocator, t);
143  }
144 
145  template <typename T, typename... Args> T *New(Allocator *allocator, Args &&...args)
146  {
147  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
148  if (!t)
149  return nullptr;
150  return new (t) T(std::forward<Args>(args)...);
151  }
152 
153  template <typename T, typename... Args> std::shared_ptr<T> MakeShared(Allocator *allocator, Args &&...args)
154  {
155  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
156  if (!t)
157  return nullptr;
158  new (t) T(std::forward<Args>(args)...);
159 
160  return std::shared_ptr<T>(t, [allocator](T *obj) { Delete(obj, allocator); });
161  }
162 
163  template <typename T> using ScopedResource = std::unique_ptr<T, std::function<void(T *)>>;
164 
165  } // namespace Crt
166 } // namespace Aws
Aws::Crt::Base64Encode
AWS_CRT_CPP_API String Base64Encode(const Vector< uint8_t > &encode) noexcept
Definition: Types.cpp:95
Aws::Crt::ByteCursorFromCString
AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept
Definition: Types.cpp:48
Aws::Crt::ByteCursorFromStringView
AWS_CRT_CPP_API ByteCursor ByteCursorFromStringView(const Crt::StringView &str) noexcept
Definition: Types.cpp:58
Aws::Crt::ScopedResource
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition: Types.h:163
Aws::Crt::ByteCursorToStringView
AWS_CRT_CPP_API StringView ByteCursorToStringView(const ByteCursor &bc)
Definition: Types.h:126
Aws::Crt::StringStream
std::basic_stringstream< char, std::char_traits< char >, StlAllocator< char > > StringStream
Definition: Types.h:46
Aws::Crt::Base64Decode
AWS_CRT_CPP_API Vector< uint8_t > Base64Decode(const String &decode) noexcept
Definition: Types.cpp:73
Aws::Crt::Io::IStream
std::basic_istream< char, std::char_traits< char > > IStream
Definition: Types.h:35
StlAllocator.h
Aws::Crt::List
std::list< T, StlAllocator< T > > List
Definition: Types.h:54
Aws::Crt::StringViewToByteCursor
AWS_CRT_CPP_API ByteCursor StringViewToByteCursor(const StringView &sv)
Definition: Types.h:131
Aws::Crt::Mqtt::QOS
aws_mqtt_qos QOS
Definition: Types.h:40
StringView.h
Aws::Crt::Map
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> Map
Definition: Types.h:47
Aws::Crt::TypeConvertor
std::function< TargetType(RawType)> TypeConvertor
Definition: Types.h:72
Aws::Crt::ByteBufFromCString
AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept
Definition: Types.cpp:13
Aws::Crt::ByteBufFromArray
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept
Definition: Types.cpp:23
Aws::Crt::Delete
void Delete(T *t, Allocator *allocator)
Definition: Types.h:139
Optional.h
Aws::Crt::New
T * New(Allocator *allocator, Args &&...args)
Definition: Types.h:145
Aws::Crt::MakeShared
std::shared_ptr< T > MakeShared(Allocator *allocator, Args &&...args)
Definition: Types.h:153
Aws::Crt::ArrayListToVector
Vector< TargetType > ArrayListToVector(const aws_array_list *array, TypeConvertor< RawType, TargetType > conv)
Definition: Types.h:79
Aws::Crt::ByteBufNewCopy
AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len)
Definition: Types.cpp:28
Aws::Crt::basic_string_view::data
constexpr const_pointer data() const noexcept
Definition: StringView.h:127
Aws::Crt::ByteBufFromEmptyArray
AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:18
Aws::Crt::Mqtt::ReturnCode
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:41
Aws::Crt::StringView
string_view StringView
Definition: StringView.h:846
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:53
Aws::Crt::ByteBufInit
AWS_CRT_CPP_API ByteBuf ByteBufInit(Allocator *alloc, size_t len)
Definition: Types.cpp:36
Aws::Crt::basic_string_view
Definition: StringView.h:33
Aws::Crt::ByteBufDelete
AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &)
Definition: Types.cpp:43
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::ByteCursorFromString
AWS_CRT_CPP_API ByteCursor ByteCursorFromString(const Crt::String &str) noexcept
Definition: Types.cpp:53
Exports.h
Aws::Crt::ByteCursorFromArray
AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:68
Aws::Crt::MultiMap
std::multimap< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> MultiMap
Definition: Types.h:52
Aws::Crt::StlAllocator
Definition: StlAllocator.h:21
Aws::Crt::ByteCursorFromByteBuf
AWS_CRT_CPP_API ByteCursor ByteCursorFromByteBuf(const ByteBuf &) noexcept
Definition: Types.cpp:63
Aws::Crt::basic_string_view::size
constexpr size_type size() const noexcept
Definition: StringView.h:93
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
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:50