AWS SDK for C++  1.9.106
AWS SDK for C++
EventHeader.h
Go to the documentation of this file.
1 
6 #pragma once
7 
10 #include <aws/core/utils/Array.h>
12 #include <aws/core/utils/UUID.h>
14 #include <aws/event-stream/event_stream.h>
15 #include <cassert>
16 
17 namespace Aws
18 {
19  namespace Utils
20  {
21  namespace Event
22  {
23  static const char CLASS_TAG[] = "EventHeader";
29  {
30  public:
31  enum class EventHeaderType
32  {
33  BOOL_TRUE = 0,
34  BOOL_FALSE,
35  BYTE,
36  INT16,
37  INT32,
38  INT64,
39  BYTE_BUF,
40  STRING,
41  /* 64 bit integer (millis since epoch) */
42  TIMESTAMP,
43  UUID,
44  UNKNOWN
45  };
46 
47  EventHeaderValue() : m_eventHeaderStaticValue({0}) {}
48 
49  EventHeaderValue(aws_event_stream_header_value_pair* header) :
50  m_eventHeaderType(static_cast<EventHeaderType>(header->header_value_type)),
51  m_eventHeaderStaticValue({0})
52  {
53  switch (m_eventHeaderType)
54  {
55  case EventHeaderType::BOOL_TRUE:
56  case EventHeaderType::BOOL_FALSE:
57  m_eventHeaderStaticValue.boolValue = aws_event_stream_header_value_as_bool(header) != 0;
58  break;
59  case EventHeaderType::BYTE:
60  m_eventHeaderStaticValue.byteValue = aws_event_stream_header_value_as_byte(header);
61  break;
62  case EventHeaderType::INT16:
63  m_eventHeaderStaticValue.int16Value = aws_event_stream_header_value_as_int16(header);
64  break;
65  case EventHeaderType::INT32:
66  m_eventHeaderStaticValue.int32Value = aws_event_stream_header_value_as_int32(header);
67  break;
68  case EventHeaderType::INT64:
69  m_eventHeaderStaticValue.int64Value = aws_event_stream_header_value_as_int64(header);
70  break;
71  case EventHeaderType::BYTE_BUF:
72  m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_bytebuf(header).buffer), header->header_value_len);
73  break;
74  case EventHeaderType::STRING:
75  m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_string(header).buffer), header->header_value_len);
76  break;
77  case EventHeaderType::TIMESTAMP:
78  m_eventHeaderStaticValue.timestampValue = aws_event_stream_header_value_as_timestamp(header);
79  break;
80  case EventHeaderType::UUID:
81  assert(header->header_value_len == 16u);
82  m_eventHeaderVariableLengthValue = ByteBuffer(static_cast<uint8_t*>(aws_event_stream_header_value_as_uuid(header).buffer), header->header_value_len);
83  break;
84  default:
85  AWS_LOG_ERROR(CLASS_TAG, "Encountered unknown type of header.");
86  break;
87  }
88  };
89 
91  m_eventHeaderType(EventHeaderType::STRING),
92  m_eventHeaderVariableLengthValue(reinterpret_cast<const uint8_t*>(s.data()), s.length()),
93  m_eventHeaderStaticValue({0})
94  {
95  }
96 
98  m_eventHeaderType(EventHeaderType::BYTE_BUF),
99  m_eventHeaderVariableLengthValue(bb),
100  m_eventHeaderStaticValue({0})
101  {
102  }
103 
105  m_eventHeaderType(EventHeaderType::BYTE_BUF),
106  m_eventHeaderVariableLengthValue(std::move(bb)),
107  m_eventHeaderStaticValue({0})
108  {
109  }
110 
111 
112  explicit EventHeaderValue(unsigned char byte) :
113  m_eventHeaderType(EventHeaderType::BYTE),
114  m_eventHeaderStaticValue({0})
115  {
116  m_eventHeaderStaticValue.byteValue = byte;
117  }
118 
119  explicit EventHeaderValue(bool b) :
120  m_eventHeaderType(b ? EventHeaderType::BOOL_TRUE : EventHeaderType::BOOL_FALSE),
121  m_eventHeaderStaticValue({0})
122  {
123  m_eventHeaderStaticValue.boolValue = b;
124  }
125 
126  explicit EventHeaderValue(int16_t n) :
127  m_eventHeaderType(EventHeaderType::INT16),
128  m_eventHeaderStaticValue({0})
129  {
130  m_eventHeaderStaticValue.int16Value = n;
131  }
132 
133  explicit EventHeaderValue(int32_t n) :
134  m_eventHeaderType(EventHeaderType::INT32),
135  m_eventHeaderStaticValue({0})
136  {
137  m_eventHeaderStaticValue.int32Value = n;
138  }
139 
140  explicit EventHeaderValue(int64_t n, EventHeaderType type = EventHeaderType::INT64) :
141  m_eventHeaderType(type),
142  m_eventHeaderStaticValue({0})
143  {
144  if (type == EventHeaderType::TIMESTAMP)
145  {
146  m_eventHeaderStaticValue.timestampValue = n;
147  }
148  else
149  {
150  m_eventHeaderStaticValue.int64Value = n;
151  }
152  }
153 
154  EventHeaderType GetType() const { return m_eventHeaderType; }
155 
156 
159 
164  inline bool GetEventHeaderValueAsBoolean() const
165  {
166  assert(m_eventHeaderType == EventHeaderType::BOOL_TRUE || m_eventHeaderType == EventHeaderType::BOOL_FALSE);
167  if (m_eventHeaderType != EventHeaderType::BOOL_TRUE && m_eventHeaderType != EventHeaderType::BOOL_FALSE)
168  {
169  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is TRUE or FALSE, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
170  return false;
171  }
172  return m_eventHeaderStaticValue.boolValue;
173  }
174 
179  inline uint8_t GetEventHeaderValueAsByte() const
180  {
181  assert(m_eventHeaderType == EventHeaderType::BYTE);
182  if (m_eventHeaderType != EventHeaderType::BYTE)
183  {
184  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is BYTE, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
185  return static_cast<uint8_t>(0);
186  }
187  return m_eventHeaderStaticValue.byteValue;
188  }
189 
194  inline int16_t GetEventHeaderValueAsInt16() const
195  {
196  assert(m_eventHeaderType == EventHeaderType::INT16);
197  if (m_eventHeaderType != EventHeaderType::INT16)
198  {
199  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT16, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
200  return static_cast<int16_t>(0);
201  }
202  return m_eventHeaderStaticValue.int16Value;
203  }
204 
209  inline int32_t GetEventHeaderValueAsInt32() const
210  {
211  assert(m_eventHeaderType == EventHeaderType::INT32);
212  if (m_eventHeaderType != EventHeaderType::INT32)
213  {
214  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT32, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
215  return static_cast<int32_t>(0);
216  }
217  return m_eventHeaderStaticValue.int32Value;
218  }
219 
224  inline int64_t GetEventHeaderValueAsInt64() const
225  {
226  assert(m_eventHeaderType == EventHeaderType::INT64);
227  if (m_eventHeaderType != EventHeaderType::INT64)
228  {
229  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is INT64, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
230  return static_cast<uint64_t>(0);
231  }
232  return m_eventHeaderStaticValue.int64Value;
233  }
234 
240  {
241  assert(m_eventHeaderType == EventHeaderType::BYTE_BUF);
242  if (m_eventHeaderType != EventHeaderType::BYTE_BUF)
243  {
244  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is BYTE_BUF, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
245  return ByteBuffer();
246  }
247  return m_eventHeaderVariableLengthValue;
248  }
249 
255  {
256  assert(m_eventHeaderType == EventHeaderType::STRING);
257  if (m_eventHeaderType != EventHeaderType::STRING)
258  {
259  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is STRING, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
260  return {};
261  }
262  return Aws::String(reinterpret_cast<char*>(m_eventHeaderVariableLengthValue.GetUnderlyingData()), m_eventHeaderVariableLengthValue.GetLength());
263  }
264 
269  inline int64_t GetEventHeaderValueAsTimestamp() const
270  {
271  assert(m_eventHeaderType == EventHeaderType::TIMESTAMP);
272  if (m_eventHeaderType != EventHeaderType::TIMESTAMP)
273  {
274  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is TIMESTAMP, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
275  return static_cast<int64_t>(0);
276  }
277  return m_eventHeaderStaticValue.timestampValue;
278  }
279 
285  {
286  assert(m_eventHeaderType == EventHeaderType::UUID);
287  assert(m_eventHeaderVariableLengthValue.GetLength() == 16u);
288  if (m_eventHeaderType != EventHeaderType::UUID)
289  {
290  AWS_LOGSTREAM_ERROR(CLASS_TAG, "Expected event header type is UUID, but encountered " << GetNameForEventHeaderType(m_eventHeaderType));
291  char uuid[32] = {0};
292  return Aws::Utils::UUID(uuid);
293  }
294  return Aws::Utils::UUID(m_eventHeaderVariableLengthValue.GetUnderlyingData());
295  }
296 
297  inline const ByteBuffer& GetUnderlyingBuffer() const
298  {
299  return m_eventHeaderVariableLengthValue;
300  }
301 
302  private:
303  EventHeaderType m_eventHeaderType;
304  ByteBuffer m_eventHeaderVariableLengthValue;
305  union
306  {
307  int64_t timestampValue;
308  int64_t int64Value;
309  int32_t int32Value;
310  int16_t int16Value;
311  uint8_t byteValue;
312  bool boolValue;
313  } m_eventHeaderStaticValue;
314  };
315 
316  typedef std::pair<Aws::String, EventHeaderValue> EventHeaderValuePair;
318  }
319  }
320 }
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(bool b)
Definition: EventHeader.h:119
LogMacros.h
Aws::Utils::Event::EventHeaderValueCollection
Aws::Map< Aws::String, EventHeaderValue > EventHeaderValueCollection
Definition: EventHeader.h:317
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsInt64
int64_t GetEventHeaderValueAsInt64() const
Definition: EventHeader.h:224
Aws::Utils::Event::EventHeaderValue::int64Value
int64_t int64Value
Definition: EventHeader.h:308
buffer
char * buffer
Definition: cJSON.h:174
Aws::Utils::Event::EventHeaderValue::EventHeaderType
EventHeaderType
Definition: EventHeader.h:32
AWS_CORE_API
#define AWS_CORE_API
Definition: Core_EXPORTS.h:25
AWS_LOG_ERROR
#define AWS_LOG_ERROR(tag,...)
Definition: LogMacros.h:70
AWS_LOGSTREAM_ERROR
#define AWS_LOGSTREAM_ERROR(tag, streamExpression)
Definition: LogMacros.h:137
Aws::IoTWireless::Model::Event
Event
Definition: Event.h:17
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(unsigned char byte)
Definition: EventHeader.h:112
Aws::Utils::Event::EventHeaderValue::byteValue
uint8_t byteValue
Definition: EventHeader.h:311
Aws::Utils::Event::EventHeaderValuePair
std::pair< Aws::String, EventHeaderValue > EventHeaderValuePair
Definition: EventHeader.h:316
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(int16_t n)
Definition: EventHeader.h:126
Aws::Utils::Event::EventHeaderValue::GetEventHeaderTypeForName
static EventHeaderType GetEventHeaderTypeForName(const Aws::String &name)
Aws::Utils::Event::EventHeaderValue::int16Value
int16_t int16Value
Definition: EventHeader.h:310
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsByte
uint8_t GetEventHeaderValueAsByte() const
Definition: EventHeader.h:179
Aws::Utils::Event::EventHeaderValue
Definition: EventHeader.h:29
Aws::Utils::ByteBuffer
Array< unsigned char > ByteBuffer
Definition: Array.h:222
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsInt32
int32_t GetEventHeaderValueAsInt32() const
Definition: EventHeader.h:209
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(int32_t n)
Definition: EventHeader.h:133
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(ByteBuffer &&bb)
Definition: EventHeader.h:104
b
const cJSON *const b
Definition: cJSON.h:267
Aws::Utils::Event::EventHeaderValue::int32Value
int32_t int32Value
Definition: EventHeader.h:309
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(const ByteBuffer &bb)
Definition: EventHeader.h:97
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue()
Definition: EventHeader.h:47
Aws::Utils::Event::EventHeaderValue::GetNameForEventHeaderType
static Aws::String GetNameForEventHeaderType(EventHeaderType value)
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsBytebuf
ByteBuffer GetEventHeaderValueAsBytebuf() const
Definition: EventHeader.h:239
Aws::Utils::Array< unsigned char >
AWSString.h
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsTimestamp
int64_t GetEventHeaderValueAsTimestamp() const
Definition: EventHeader.h:269
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsString
Aws::String GetEventHeaderValueAsString() const
Definition: EventHeader.h:254
Core_EXPORTS.h
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsUuid
Aws::Utils::UUID GetEventHeaderValueAsUuid() const
Definition: EventHeader.h:284
Aws::Utils::Event::EventHeaderValue::boolValue
bool boolValue
Definition: EventHeader.h:312
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(int64_t n, EventHeaderType type=EventHeaderType::INT64)
Definition: EventHeader.h:140
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(aws_event_stream_header_value_pair *header)
Definition: EventHeader.h:49
Aws
Definition: AccessManagementClient.h:15
length
char const int length
Definition: cJSON.h:174
Array.h
Aws::Utils::Event::EventHeaderValue::GetType
EventHeaderType GetType() const
Definition: EventHeader.h:154
Aws::Utils::Event::EventHeaderValue::timestampValue
int64_t timestampValue
Definition: EventHeader.h:307
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsInt16
int16_t GetEventHeaderValueAsInt16() const
Definition: EventHeader.h:194
Aws::String
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
Aws::Utils::Event::EventHeaderValue::GetUnderlyingBuffer
const ByteBuffer & GetUnderlyingBuffer() const
Definition: EventHeader.h:297
Aws::Utils::UUID
Definition: UUID.h:22
Aws::Utils::Event::EventHeaderValue::EventHeaderValue
EventHeaderValue(const Aws::String &s)
Definition: EventHeader.h:90
Aws::Utils::Event::CLASS_TAG
static const char CLASS_TAG[]
Definition: EventHeader.h:23
UUID.h
AWSMap.h
Aws::Map
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition: AWSMap.h:19
Aws::Utils::Event::EventHeaderValue::GetEventHeaderValueAsBoolean
bool GetEventHeaderValueAsBoolean() const
Definition: EventHeader.h:164