|
3 | 3 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
4 | 4 | * SPDX-License-Identifier: Apache-2.0.
|
5 | 5 | */
|
| 6 | +#include <aws/common/common.h> |
6 | 7 | #include <aws/crt/Exports.h>
|
7 | 8 | #include <aws/crt/Optional.h>
|
8 | 9 | #include <aws/crt/StlAllocator.h>
|
9 |
| - |
10 |
| -#include <aws/common/common.h> |
| 10 | +#include <aws/crt/StringView.h> |
11 | 11 | #include <aws/io/socket.h>
|
12 | 12 | #include <aws/mqtt/mqtt.h>
|
13 | 13 | #include <functional>
|
@@ -70,6 +70,75 @@ namespace Aws
|
70 | 70 | AWS_CRT_CPP_API Vector<uint8_t> Base64Decode(const String &decode);
|
71 | 71 | AWS_CRT_CPP_API String Base64Encode(const Vector<uint8_t> &encode);
|
72 | 72 |
|
| 73 | + template <typename RawType, typename TargetType> using TypeConvertor = std::function<TargetType(RawType)>; |
| 74 | + |
| 75 | + /** |
| 76 | + * Template function to convert an aws_array_list of RawType to a C++ like Vector of TargetType. |
| 77 | + * A conversion function should be provided to do the type conversion |
| 78 | + */ |
| 79 | + template <typename RawType, typename TargetType> |
| 80 | + AWS_CRT_CPP_API Vector<TargetType> ArrayListToVector( |
| 81 | + const aws_array_list *array, |
| 82 | + TypeConvertor<RawType, TargetType> conv) |
| 83 | + { |
| 84 | + Vector<TargetType> v; |
| 85 | + size_t cnt = aws_array_list_length(array); |
| 86 | + for (size_t i = 0; i < cnt; i++) |
| 87 | + { |
| 88 | + RawType t; |
| 89 | + aws_array_list_get_at(array, &t, i); |
| 90 | + v.emplace_back(conv(t)); |
| 91 | + } |
| 92 | + return v; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Template function to convert an aws_array_list of RawType to a C++ like Vector of TargetType. |
| 97 | + * This template assumes a direct constructor: TargetType(RawType) is available |
| 98 | + */ |
| 99 | + template <typename RawType, typename TargetType> |
| 100 | + AWS_CRT_CPP_API Vector<TargetType> ArrayListToVector(const aws_array_list *array) |
| 101 | + { |
| 102 | + Vector<TargetType> v; |
| 103 | + size_t cnt = aws_array_list_length(array); |
| 104 | + for (size_t i = 0; i < cnt; i++) |
| 105 | + { |
| 106 | + RawType t; |
| 107 | + aws_array_list_get_at(array, &t, i); |
| 108 | + v.emplace_back(TargetType(t)); |
| 109 | + } |
| 110 | + return v; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Template function to convert an aws_array_list of Type to a C++ like Vector of Type. |
| 115 | + */ |
| 116 | + template <typename Type> AWS_CRT_CPP_API Vector<Type> ArrayListToVector(const aws_array_list *array) |
| 117 | + { |
| 118 | + Vector<Type> v; |
| 119 | + size_t cnt = aws_array_list_length(array); |
| 120 | + for (size_t i = 0; i < cnt; i++) |
| 121 | + { |
| 122 | + Type t; |
| 123 | + aws_array_list_get_at(array, &t, i); |
| 124 | + v.emplace_back(t); |
| 125 | + } |
| 126 | + return v; |
| 127 | + } |
| 128 | + |
| 129 | + AWS_CRT_CPP_API inline StringView ByteCursorToStringView(const ByteCursor &bc) |
| 130 | + { |
| 131 | + return StringView(reinterpret_cast<char *>(bc.ptr), bc.len); |
| 132 | + } |
| 133 | + |
| 134 | + AWS_CRT_CPP_API inline ByteCursor StringViewToByteCursor(const StringView &sv) |
| 135 | + { |
| 136 | + ByteCursor bc; |
| 137 | + bc.ptr = (uint8_t *)(sv.data()); |
| 138 | + bc.len = sv.size(); |
| 139 | + return bc; |
| 140 | + } |
| 141 | + |
73 | 142 | template <typename T> void Delete(T *t, Allocator *allocator)
|
74 | 143 | {
|
75 | 144 | t->~T();
|
|
0 commit comments