AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
EventHeader.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9#include <aws/core/utils/Array.h>
10#include <aws/core/utils/DateTime.h>
11#include <aws/core/utils/StringUtils.h>
12#include <aws/core/utils/UUID.h>
13#include <aws/core/utils/logging/LogMacros.h>
14#include <aws/core/utils/memory/stl/AWSMap.h>
15#include <aws/core/utils/memory/stl/AWSString.h>
16#include <aws/event-stream/event_stream.h>
17#include <cassert>
18
19namespace Aws
20{
21 namespace Utils
22 {
23 namespace Event
24 {
25 static const char CLASS_TAG[] = "EventHeader";
30 class AWS_CORE_API EventHeaderValue
31 {
32 public:
33 enum class EventHeaderType
34 {
35 BOOL_TRUE = 0,
36 BOOL_FALSE,
37 BYTE,
38 INT16,
39 INT32,
40 INT64,
41 BYTE_BUF,
42 STRING,
43 /* 64 bit integer (millis since epoch) */
44 TIMESTAMP,
45 UUID,
46 UNKNOWN
47 };
48
49 EventHeaderValue() : m_eventHeaderType(EventHeaderType::UNKNOWN), m_eventHeaderStaticValue({0}) {}
50
51 EventHeaderValue(aws_event_stream_header_value_pair* header) :
52 m_eventHeaderType(static_cast<EventHeaderType>(header->header_value_type)),
53 m_eventHeaderStaticValue({0})
54 {
55 switch (m_eventHeaderType)
56 {
57 case EventHeaderType::BOOL_TRUE:
58 case EventHeaderType::BOOL_FALSE:
59 m_eventHeaderStaticValue.boolValue = aws_event_stream_header_value_as_bool(header) != 0;
60 break;
61 case EventHeaderType::BYTE:
62 m_eventHeaderStaticValue.byteValue = aws_event_stream_header_value_as_byte(header);
63 break;
64 case EventHeaderType::INT16:
65 m_eventHeaderStaticValue.int16Value = aws_event_stream_header_value_as_int16(header);
66 break;
67 case EventHeaderType::INT32:
68 m_eventHeaderStaticValue.int32Value = aws_event_stream_header_value_as_int32(header);
69 break;
70 case EventHeaderType::INT64:
71 m_eventHeaderStaticValue.int64Value = aws_event_stream_header_value_as_int64(header);
72 break;
73 case EventHeaderType::BYTE_BUF:
74 m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_bytebuf(header).buffer), header->header_value_len);
75 break;
76 case EventHeaderType::STRING:
77 m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_string(header).buffer), header->header_value_len);
78 break;
79 case EventHeaderType::TIMESTAMP:
80 m_eventHeaderStaticValue.timestampValue = aws_event_stream_header_value_as_timestamp(header);
81 break;
82 case EventHeaderType::UUID:
83 assert(header->header_value_len == 16u);
84 m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_uuid(header).buffer), header->header_value_len);
85 break;
86 default:
87 AWS_LOG_ERROR(CLASS_TAG, "Encountered unknown type of header.");
88 break;
89 }
90 };
91
93 m_eventHeaderType(EventHeaderType::STRING),
94 m_eventHeaderVariableLengthValue(reinterpret_cast<const uint8_t*>(s.data()), s.length()),
95 m_eventHeaderStaticValue({0})
96 {
97 }
98
100 m_eventHeaderType(EventHeaderType::BYTE_BUF),
101 m_eventHeaderVariableLengthValue(bb),
102 m_eventHeaderStaticValue({0})
103 {
104 }
105
107 m_eventHeaderType(EventHeaderType::BYTE_BUF),
108 m_eventHeaderVariableLengthValue(std::move(bb)),
109 m_eventHeaderStaticValue({0})
110 {
111 }
112
113
114 explicit EventHeaderValue(unsigned char byte) :
115 m_eventHeaderType(EventHeaderType::BYTE),
116 m_eventHeaderStaticValue({0})
117 {
118 m_eventHeaderStaticValue.byteValue = byte;
119 }
120
121 explicit EventHeaderValue(bool b) :
122 m_eventHeaderType(b ? EventHeaderType::BOOL_TRUE : EventHeaderType::BOOL_FALSE),
123 m_eventHeaderStaticValue({0})
124 {
125 m_eventHeaderStaticValue.boolValue = b;
126 }
127
128 explicit EventHeaderValue(int16_t n) :
129 m_eventHeaderType(EventHeaderType::INT16),
130 m_eventHeaderStaticValue({0})
131 {
132 m_eventHeaderStaticValue.int16Value = n;
133 }
134
135 explicit EventHeaderValue(int32_t n) :
136 m_eventHeaderType(EventHeaderType::INT32),
137 m_eventHeaderStaticValue({0})
138 {
139 m_eventHeaderStaticValue.int32Value = n;
140 }
141
142 explicit EventHeaderValue(int64_t n, EventHeaderType type = EventHeaderType::INT64) :
143 m_eventHeaderType(type),
144 m_eventHeaderStaticValue({0})
145 {
146 if (type == EventHeaderType::TIMESTAMP)
147 {
148 m_eventHeaderStaticValue.timestampValue = n;
149 }
150 else
151 {
152 m_eventHeaderStaticValue.int64Value = n;
153 }
154 }
155
156 EventHeaderType GetType() const { return m_eventHeaderType; }
157
158
161
167 {
168 assert(m_eventHeaderType == EventHeaderType::BOOL_TRUE || m_eventHeaderType == EventHeaderType::BOOL_FALSE);
169 if (m_eventHeaderType != EventHeaderType::BOOL_TRUE && m_eventHeaderType != EventHeaderType::BOOL_FALSE)
170 {
171 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is TRUE or FALSE, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
172 return false;
173 }
174 return m_eventHeaderStaticValue.boolValue;
175 }
176
181 inline uint8_t GetEventHeaderValueAsByte() const
182 {
183 assert(m_eventHeaderType == EventHeaderType::BYTE);
184 if (m_eventHeaderType != EventHeaderType::BYTE)
185 {
186 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is BYTE, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
187 return static_cast<uint8_t>(0);
188 }
189 return m_eventHeaderStaticValue.byteValue;
190 }
191
196 inline int16_t GetEventHeaderValueAsInt16() const
197 {
198 assert(m_eventHeaderType == EventHeaderType::INT16);
199 if (m_eventHeaderType != EventHeaderType::INT16)
200 {
201 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT16, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
202 return static_cast<int16_t>(0);
203 }
204 return m_eventHeaderStaticValue.int16Value;
205 }
206
211 inline int32_t GetEventHeaderValueAsInt32() const
212 {
213 assert(m_eventHeaderType == EventHeaderType::INT32);
214 if (m_eventHeaderType != EventHeaderType::INT32)
215 {
216 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT32, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
217 return static_cast<int32_t>(0);
218 }
219 return m_eventHeaderStaticValue.int32Value;
220 }
221
226 inline int64_t GetEventHeaderValueAsInt64() const
227 {
228 assert(m_eventHeaderType == EventHeaderType::INT64);
229 if (m_eventHeaderType != EventHeaderType::INT64)
230 {
231 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT64, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
232 return static_cast<uint64_t>(0);
233 }
234 return m_eventHeaderStaticValue.int64Value;
235 }
236
242 {
243 assert(m_eventHeaderType == EventHeaderType::BYTE_BUF);
244 if (m_eventHeaderType != EventHeaderType::BYTE_BUF)
245 {
246 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is BYTE_BUF, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
247 return ByteBuffer();
248 }
249 return m_eventHeaderVariableLengthValue;
250 }
251
257 {
258 assert(m_eventHeaderType == EventHeaderType::STRING);
259 if (m_eventHeaderType != EventHeaderType::STRING)
260 {
261 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is STRING, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
262 return {};
263 }
264 return Aws::String(reinterpret_cast<char*>(m_eventHeaderVariableLengthValue.GetUnderlyingData()), m_eventHeaderVariableLengthValue.GetLength());
265 }
266
271 inline int64_t GetEventHeaderValueAsTimestamp() const
272 {
273 assert(m_eventHeaderType == EventHeaderType::TIMESTAMP);
274 if (m_eventHeaderType != EventHeaderType::TIMESTAMP)
275 {
276 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is TIMESTAMP, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
277 return static_cast<int64_t>(0);
278 }
279 return m_eventHeaderStaticValue.timestampValue;
280 }
281
287 {
288 assert(m_eventHeaderType == EventHeaderType::UUID);
289 assert(m_eventHeaderVariableLengthValue.GetLength() == 16u);
290 if (m_eventHeaderType != EventHeaderType::UUID)
291 {
292 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is UUID, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
293 char uuid[32] = {0};
294 return Aws::Utils::UUID(uuid);
295 }
296 return Aws::Utils::UUID(m_eventHeaderVariableLengthValue.GetUnderlyingData());
297 }
298
299 inline const ByteBuffer& GetUnderlyingBuffer() const
300 {
301 return m_eventHeaderVariableLengthValue;
302 }
303
304 inline Aws::String ToString() const
305 {
306 switch (m_eventHeaderType)
307 {
308 case EventHeaderType::BOOL_TRUE:
309 case EventHeaderType::BOOL_FALSE:
310 return Utils::StringUtils::to_string(GetEventHeaderValueAsBoolean());
311 case EventHeaderType::BYTE:
312 return Utils::StringUtils::to_string(GetEventHeaderValueAsByte());
313 case EventHeaderType::INT16:
314 return Utils::StringUtils::to_string(GetEventHeaderValueAsInt16());
315 case EventHeaderType::INT32:
316 return Utils::StringUtils::to_string(GetEventHeaderValueAsInt32());
317 case EventHeaderType::INT64:
318 return Utils::StringUtils::to_string(GetEventHeaderValueAsInt64());
319 case EventHeaderType::BYTE_BUF:
320 return Aws::String(reinterpret_cast<char*>(GetEventHeaderValueAsBytebuf().GetUnderlyingData()), GetEventHeaderValueAsBytebuf().GetLength());
321 case EventHeaderType::STRING:
322 return GetEventHeaderValueAsString();
323 case EventHeaderType::TIMESTAMP:
324 return Aws::Utils::DateTime(GetEventHeaderValueAsTimestamp()).ToGmtString(Aws::Utils::DateFormat::RFC822);
325 case EventHeaderType::UUID:
326 return GetEventHeaderValueAsUuid();
327 case EventHeaderType::UNKNOWN:
328 default:
329 AWS_LOGSTREAM_ERROR(CLASS_TAG, "Cannot transform EventHeader value to string: type is unknown");
330 return {};
331 }
332 }
333
334 private:
335 EventHeaderType m_eventHeaderType;
336 ByteBuffer m_eventHeaderVariableLengthValue;
337 union
338 {
340 int64_t int64Value;
341 int32_t int32Value;
342 int16_t int16Value;
343 uint8_t byteValue;
345 } m_eventHeaderStaticValue;
346 };
347
348 typedef std::pair<Aws::String, EventHeaderValue> EventHeaderValuePair;
350 }
351 }
352}
Aws::String ToGmtString(DateFormat format) const
EventHeaderValue(int64_t n, EventHeaderType type=EventHeaderType::INT64)
static Aws::String GetNameForEventHeaderType(EventHeaderType value)
Aws::String GetEventHeaderValueAsString() const
EventHeaderType GetType() const
EventHeaderValue(aws_event_stream_header_value_pair *header)
Definition EventHeader.h:51
const ByteBuffer & GetUnderlyingBuffer() const
ByteBuffer GetEventHeaderValueAsBytebuf() const
static EventHeaderType GetEventHeaderTypeForName(const Aws::String &name)
int64_t GetEventHeaderValueAsTimestamp() const
EventHeaderValue(unsigned char byte)
EventHeaderValue(const ByteBuffer &bb)
Definition EventHeader.h:99
Aws::Utils::UUID GetEventHeaderValueAsUuid() const
EventHeaderValue(const Aws::String &s)
Definition EventHeader.h:92
Aws::Map< Aws::String, EventHeaderValue > EventHeaderValueCollection
std::pair< Aws::String, EventHeaderValue > EventHeaderValuePair
static const char CLASS_TAG[]
Definition EventHeader.h:25
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition AWSMap.h:20
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97