aws-crt-cpp
Types.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 #include <aws/crt/Exports.h>
17 #include <aws/crt/Optional.h>
18 #include <aws/crt/StlAllocator.h>
19 
20 #include <aws/common/common.h>
21 #include <aws/io/socket.h>
22 #include <aws/mqtt/mqtt.h>
23 
24 #include <functional>
25 #include <list>
26 #include <map>
27 #include <sstream>
28 #include <string>
29 #include <unordered_map>
30 #include <utility>
31 #include <vector>
32 
33 struct aws_allocator;
34 struct aws_byte_buf;
35 struct aws_byte_cursor;
36 struct aws_socket_options;
37 
38 namespace Aws
39 {
40  namespace Crt
41  {
42  using Allocator = aws_allocator;
43  using ByteBuf = aws_byte_buf;
44  using ByteCursor = aws_byte_cursor;
45 
46  namespace Io
47  {
48  using IStream = std::basic_istream<char, std::char_traits<char>>;
49  } // namespace Io
50 
51  namespace Mqtt
52  {
53  using QOS = aws_mqtt_qos;
54  using ReturnCode = aws_mqtt_connect_return_code;
55  } // namespace Mqtt
56 
57  template <typename T> class StlAllocator;
58  using String = std::basic_string<char, std::char_traits<char>, StlAllocator<char>>;
59  using StringStream = std::basic_stringstream<char, std::char_traits<char>, StlAllocator<char>>;
60  template <typename K, typename V> using Map = std::map<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
61  template <typename K, typename V>
62  using UnorderedMap =
63  std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, StlAllocator<std::pair<const K, V>>>;
64  template <typename K, typename V>
65  using MultiMap = std::multimap<K, V, std::less<K>, StlAllocator<std::pair<const K, V>>>;
66  template <typename T> using Vector = std::vector<T, StlAllocator<T>>;
67  template <typename T> using List = std::list<T, StlAllocator<T>>;
68 
70  AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept;
71  AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept;
72  AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept;
73  AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len);
75 
76  AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept;
78  AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept;
79 
80  AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(const String &decode);
81  AWS_CRT_CPP_API String Base64Encode(const Vector<uint8_t> &encode);
82 
83  template <typename T> void Delete(T *t, Allocator *allocator)
84  {
85  t->~T();
86  aws_mem_release(allocator, t);
87  }
88 
89  template <typename T, typename... Args> T *New(Allocator *allocator, Args &&... args)
90  {
91  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
92  if (!t)
93  return nullptr;
94  return new (t) T(std::forward<Args>(args)...);
95  }
96 
97  template <typename T, typename... Args> std::shared_ptr<T> MakeShared(Allocator *allocator, Args &&... args)
98  {
99  T *t = reinterpret_cast<T *>(aws_mem_acquire(allocator, sizeof(T)));
100  if (!t)
101  return nullptr;
102  new (t) T(std::forward<Args>(args)...);
103 
104  return std::shared_ptr<T>(t, [allocator](T *obj) { Delete(obj, allocator); });
105  }
106 
107  template <typename T> using ScopedResource = std::unique_ptr<T, std::function<void(T *)>>;
108 
109  } // namespace Crt
110 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::Crt::MakeShared
std::shared_ptr< T > MakeShared(Allocator *allocator, Args &&... args)
Definition: Types.h:97
Optional.h
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:66
Aws::Crt::Base64Decode
AWS_CRT_CPP_API Vector< uint8_t > Base64Decode(const String &decode)
Definition: Types.cpp:56
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
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:63
Aws::Crt::ByteCursorFromArray
AWS_CRT_CPP_API ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:51
Aws::Crt::ByteBufNewCopy
AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len)
Definition: Types.cpp:37
Aws::Crt::ByteCursorFromByteBuf
AWS_CRT_CPP_API ByteCursor ByteCursorFromByteBuf(const ByteBuf &) noexcept
Definition: Types.cpp:49
Aws::Crt::Io::IStream
std::basic_istream< char, std::char_traits< char > > IStream
Definition: Types.h:48
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:44
Aws::Crt::ByteBufFromCString
AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char *str) noexcept
Definition: Types.cpp:25
Aws::Crt::StringStream
std::basic_stringstream< char, std::char_traits< char >, StlAllocator< char > > StringStream
Definition: Types.h:59
Aws::Crt::ScopedResource
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition: Types.h:107
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:43
Aws::Crt::ByteBufFromArray
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept
Definition: Types.cpp:32
Aws::Crt::Mqtt::ReturnCode
aws_mqtt_connect_return_code ReturnCode
Definition: Types.h:54
Aws::Crt::List
std::list< T, StlAllocator< T > > List
Definition: Types.h:67
Aws::Crt::New
T * New(Allocator *allocator, Args &&... args)
Definition: Types.h:89
Aws::Crt::DefaultAllocator
AWS_CRT_CPP_API Allocator * DefaultAllocator() noexcept
Definition: Types.cpp:23
Aws::len
char const int len
Definition: cJSON.cpp:1052
Aws::Crt::ByteCursorFromCString
AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept
Definition: Types.cpp:47
Aws
Definition: Api.h:25
Exports.h
Aws::Crt::MultiMap
std::multimap< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> MultiMap
Definition: Types.h:65
StlAllocator.h
Aws::Crt::ByteBufDelete
AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &)
Definition: Types.cpp:45
Aws::Crt::StlAllocator
Definition: StlAllocator.h:29
Aws::Crt::Base64Encode
AWS_CRT_CPP_API String Base64Encode(const Vector< uint8_t > &encode)
Definition: Types.cpp:77
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:58
Aws::Crt::ByteBufFromEmptyArray
AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept
Definition: Types.cpp:27
Aws::Crt::Mqtt::QOS
aws_mqtt_qos QOS
Definition: Types.h:53
Aws::Crt::Map
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> Map
Definition: Types.h:60
Aws::Crt::Delete
void Delete(T *t, Allocator *allocator)
Definition: Types.h:83