EII Message Bus C Reference
msg_envelope.hpp
Go to the documentation of this file.
1 // Copyright (c) 2021 Intel Corporation.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20 
21 
27 #ifndef _EII_MSGENVELOPE_HPP_
28 #define _EII_MSGENVELOPE_HPP_
29 
30 #include <stdint.h>
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 #include <typeinfo>
35 #include <exception>
37 
38 
39 namespace eii {
40 namespace msgbus {
41 
46 class MsgbusException : public std::exception {
47  private:
48  // EII Message Bus C return value
49  msgbus_ret_t m_ret;
50 
51  // Extra message with the exception providing additional context
52  std::string m_msg;
53 
54  public:
61  MsgbusException(msgbus_ret_t ret, const char* msg);
62 
68  const char * what() const noexcept override {
69  return m_msg.c_str();
70  }
71 
78 };
79 
80 // Forward declarations
81 class MsgEnvelopeObject;
82 class MsgEnvelopeList;
83 class MsgEnvelope;
84 
89  private:
90  // msg_envelope_elem_body_t object
92 
97 
101  MsgEnvelopeElement& operator=(const MsgEnvelopeElement& src);
102 
111 
112  public:
113  // Declaring MsgEnvelope as friend so that it can access
114  // private constructors
115  friend MsgEnvelope;
116  friend MsgEnvelopeList;
117  friend MsgEnvelopeObject;
118 
126  int64_t to_int();
127 
135  double to_float();
136 
144  const char* to_string();
145 
153  bool to_bool();
154 
163 
172 
178  bool is_none();
179 
186 
192  const char* get_type_str();
193 
198 };
199 
203 class MsgEnvelope {
204  private:
205  // msg_envelope_t object
206  msg_envelope_t* m_msgenv;
207 
211  MsgEnvelope(const MsgEnvelope& src);
212 
216  MsgEnvelope& operator=(const MsgEnvelope& src);
217 
218  public:
227 
235  MsgEnvelope(msg_envelope_t* msgenv);
236 
245  void put_integer(const std::string key, int64_t value);
246 
255  void put_float(const std::string key, double value);
256 
265  void put_string(const std::string key, const std::string value);
266 
275  void put_bool(const std::string key, bool value);
276 
285  void put_array(const std::string key, MsgEnvelopeList* value);
286 
297  template<typename T>
298  void put_vector(const std::string key,
299  std::vector<T> const& value) {
300  if (m_msgenv == NULL) {
301  throw MsgbusException(MSG_ERR_UNKNOWN,
302  "Message Envelope already consumed, "
303  "unable to add to message envelope");
304  }
306  if (arr == NULL) {
307  throw MsgbusException(MSG_ERR_NO_MEMORY,
308  "Failed to create underlying msg envelope array");
309  }
310  msgbus_ret_t ret;
311  for (int i = 0; i < value.size(); i++) {
312  try {
313  if (typeid(value).name() == typeid(std::vector<int>).name()) {
314  ret = msgbus_msg_envelope_elem_array_add_integer(arr, value[i]);
315  if (ret != MSG_SUCCESS) {
316  throw MsgbusException(ret, "Failed to add integer "
317  "into msg envelope array");
318  }
319  } else if (typeid(value).name() == typeid(std::vector<double>).name()) {
320  ret = msgbus_msg_envelope_elem_array_add_float(arr, value[i]);
321  if (ret != MSG_SUCCESS) {
322  throw MsgbusException(ret, "Failed to add integer "
323  "into msg envelope array");
324  }
325  } else if (typeid(value).name() == typeid(std::vector<bool>).name()) {
326  ret = msgbus_msg_envelope_elem_array_add_bool(arr, value[i]);
327  if (ret != MSG_SUCCESS) {
328  throw MsgbusException(ret, "Failed to add integer "
329  "into msg envelope array");
330  }
331  } else {
332  throw MsgbusException(MSG_ERR_UNKNOWN, "Vector type not supported");
333  }
334  } catch (const std::exception& e) {
335  throw MsgbusException(MSG_ERR_UNKNOWN, e.what());
336  }
337  }
338 
339  if (m_msgenv == NULL) {
340  throw MsgbusException(MSG_ERR_UNKNOWN,
341  "Message Envelope already consumed, "
342  "unable to add to message envelope");
343  }
344  ret = msgbus_msg_envelope_put(m_msgenv, key.c_str(), arr);
345  if (ret != MSG_SUCCESS) {
346  throw MsgbusException(ret, "Failed to add array into message envelope");
347  }
348  }
349 
358  void put_object(const std::string key, MsgEnvelopeObject* value);
359 
368  void put_blob(char* value, size_t size);
369 
377  void remove(const std::string key);
378 
388  int64_t get_int(const std::string key);
389 
399  double get_float(const std::string key);
400 
410  const char* get_string(const std::string key);
411 
421  bool get_bool(const std::string key);
422 
430  char* get_blob(const std::string key);
431 
442  MsgEnvelopeObject* get_object(const std::string key);
443 
454  MsgEnvelopeList* get_array(const std::string key);
455 
466  MsgEnvelopeElement* get_msg_envelope_element(const std::string key);
467 
474 
478  ~MsgEnvelope();
479 };
480 
485  private:
486  // msg_envelope_elem_body_t object
488 
489  // bool whether the MsgEnvelopeList owns m_arr
490  bool m_owns_array;
491 
495  MsgEnvelopeList(const MsgEnvelopeList& src);
496 
500  MsgEnvelopeList& operator=(const MsgEnvelopeList& src);
501 
511  MsgEnvelopeList(msg_envelope_elem_body_t* arr, bool owns_array);
512 
513  public:
514  // Declaring MsgEnvelope as friend so that it can access
515  // private constructors
516  friend MsgEnvelope;
517 
524  MsgEnvelopeList();
525 
533  void put_integer(int64_t value);
534 
542  void put_float(double value);
543 
551  void put_string(const std::string value);
552 
560  void put_bool(bool value);
561 
570  void put_object(MsgEnvelopeObject* value);
571 
579  void remove_at(int64_t index);
580 
590  int64_t get_int(int64_t index);
591 
601  double get_float(int64_t index);
602 
612  const char* get_string(int64_t index);
613 
623  bool get_bool(int64_t index);
624 
636 
648 
655 
660 };
661 
663  private:
664  // msg_envelope_elem_body_t object
665  msg_envelope_elem_body_t* m_msgenvobj;
666 
667  // bool whether the MsgEnvelopeObject object owns m_msgenvobj
668  bool m_owns_object;
669 
674 
678  MsgEnvelopeObject& operator=(const MsgEnvelopeObject& src);
679 
689  MsgEnvelopeObject(msg_envelope_elem_body_t* obj, bool owns_object);
690 
691  public:
692  // Declaring MsgEnvelope as friend so that it can access
693  // private constructors
694  friend MsgEnvelope;
695 
700 
709  void put_integer(const std::string key, int64_t value);
710 
719  void put_float(const std::string key, double value);
720 
729  void put_string(const std::string key, const std::string value);
730 
739  void put_bool(const std::string key, bool value);
740 
748  void remove(const std::string key);
749 
759  int64_t get_int(const std::string key);
760 
770  double get_float(const std::string key);
771 
781  const char* get_string(const std::string key);
782 
792  bool get_bool(const std::string key);
793 
804  MsgEnvelopeElement* get_msg_envelope_element(const std::string key);
805 
812 
817 };
818 
819 } // namespace msgbus
820 } // namespace eii
821 
830 
831 #endif // _EII_MSGENVELOPE_HPP_
eii::msgbus::MsgEnvelope::get_bool
bool get_bool(const std::string key)
eii::msgbus::MsgEnvelopeObject::get_msg_envelope_element
MsgEnvelopeElement * get_msg_envelope_element(const std::string key)
eii::msgbus::MsgEnvelopeList::MsgEnvelopeList
MsgEnvelopeList()
eii::msgbus::MsgEnvelopeElement::to_int
int64_t to_int()
eii::msgbus::MsgEnvelopeElement::get_type_str
const char * get_type_str()
eii::msgbus::MsgEnvelopeElement::to_float
double to_float()
eii::msgbus::MsgEnvelopeObject::remove
void remove(const std::string key)
eii::msgbus::MsgEnvelopeList::get_msg_envelope_array
msg_envelope_elem_body_t * get_msg_envelope_array()
eii::msgbus::MsgEnvelopeElement::is_none
bool is_none()
eii::msgbus::MsgbusException::what
const char * what() const noexcept override
Definition: msg_envelope.hpp:68
eii::msgbus::MsgEnvelope::get_float
double get_float(const std::string key)
eii::msgbus::MsgEnvelopeObject::get_int
int64_t get_int(const std::string key)
eii::msgbus::MsgEnvelopeObject::get_float
double get_float(const std::string key)
eii::msgbus::MsgEnvelopeObject::put_integer
void put_integer(const std::string key, int64_t value)
eii::msgbus::MsgEnvelopeList::remove_at
void remove_at(int64_t index)
msg_envelope.h
Messaging envelope abstraction.
eii::msgbus::MsgEnvelopeObject::put_float
void put_float(const std::string key, double value)
eii::msgbus::MsgEnvelopeObject::put_bool
void put_bool(const std::string key, bool value)
eii::msgbus::MsgEnvelopeList::~MsgEnvelopeList
~MsgEnvelopeList()
eii::msgbus::MsgEnvelope::get_msg_envelope_element
MsgEnvelopeElement * get_msg_envelope_element(const std::string key)
eii::msgbus::MsgEnvelopeList::get_msg_envelope_object
MsgEnvelopeObject * get_msg_envelope_object(int64_t index)
msg_envelope_elem_body_t
Definition: msg_envelope.h:93
eii::msgbus::MsgEnvelope::~MsgEnvelope
~MsgEnvelope()
eii::msgbus::MsgEnvelope
Definition: msg_envelope.hpp:203
eii::msgbus::MsgEnvelopeList::get_bool
bool get_bool(int64_t index)
eii::msgbus::MsgEnvelopeElement
Definition: msg_envelope.hpp:88
eii::msgbus::MsgbusException::MsgbusException
MsgbusException(msgbus_ret_t ret, const char *msg)
eii::msgbus::MsgEnvelope::get_string
const char * get_string(const std::string key)
eii::msgbus::MsgbusException::get_msgbus_ret
msgbus_ret_t get_msgbus_ret()
eii::msgbus::MsgEnvelopeElement::to_array
MsgEnvelopeList * to_array()
eii::msgbus::MsgEnvelopeElement::to_string
const char * to_string()
eii::msgbus::MsgEnvelope::get_blob
char * get_blob(const std::string key)
eii::msgbus::MsgEnvelopeList::get_float
double get_float(int64_t index)
eii::msgbus::MsgEnvelopeList::get_string
const char * get_string(int64_t index)
msgbus_ret_t
msgbus_ret_t
Definition: msgbusret.h:36
eii::msgbus::MsgEnvelopeObject::get_bool
bool get_bool(const std::string key)
msg_envelope_t
Definition: msg_envelope.h:111
eii::msgbus::MsgEnvelope::put_integer
void put_integer(const std::string key, int64_t value)
msgbus_msg_envelope_put
msgbus_ret_t msgbus_msg_envelope_put(msg_envelope_t *env, const char *key, msg_envelope_elem_body_t *data)
msg_envelope_data_type_t
msg_envelope_data_type_t
Definition: msg_envelope.h:57
eii::msgbus::MsgEnvelopeList::get_msg_envelope_element
MsgEnvelopeElement * get_msg_envelope_element(int64_t index)
eii::msgbus::MsgEnvelopeObject::put_string
void put_string(const std::string key, const std::string value)
eii::msgbus::MsgEnvelopeObject
Definition: msg_envelope.hpp:662
eii::msgbus::MsgEnvelope::get_msg_envelope
msg_envelope_t * get_msg_envelope()
eii::msgbus::MsgEnvelopeList::put_float
void put_float(double value)
eii::msgbus::MsgEnvelope::get_object
MsgEnvelopeObject * get_object(const std::string key)
eii::msgbus::MsgEnvelopeList::put_object
void put_object(MsgEnvelopeObject *value)
eii::msgbus::MsgEnvelopeList
Definition: msg_envelope.hpp:484
msgbus_msg_envelope_elem_array_add_float
msgbus_ret_t msgbus_msg_envelope_elem_array_add_float(msg_envelope_elem_body_t *arr, double value)
eii::msgbus::MsgEnvelopeList::put_string
void put_string(const std::string value)
eii::msgbus::MsgEnvelope::get_int
int64_t get_int(const std::string key)
eii::msgbus::MsgbusException
Definition: msg_envelope.hpp:46
eii::msgbus::MsgEnvelopeList::put_bool
void put_bool(bool value)
eii::msgbus::MsgEnvelopeElement::to_object
MsgEnvelopeObject * to_object()
eii::msgbus::MsgEnvelope::put_float
void put_float(const std::string key, double value)
eii::msgbus::MsgEnvelope::put_vector
void put_vector(const std::string key, std::vector< T > const &value)
Definition: msg_envelope.hpp:298
eii::msgbus::MsgEnvelope::put_array
void put_array(const std::string key, MsgEnvelopeList *value)
eii::msgbus::MsgEnvelope::put_blob
void put_blob(char *value, size_t size)
msgbus_msg_envelope_elem_array_add_integer
msgbus_ret_t msgbus_msg_envelope_elem_array_add_integer(msg_envelope_elem_body_t *arr, int64_t value)
content_type_t
content_type_t
Definition: msg_envelope.h:43
eii::msgbus::MsgEnvelope::put_bool
void put_bool(const std::string key, bool value)
eii::msgbus::MsgEnvelope::put_string
void put_string(const std::string key, const std::string value)
msgbus_msg_envelope_elem_type_str
const char * msgbus_msg_envelope_elem_type_str(msg_envelope_data_type_t dt)
msgbus_msg_envelope_elem_array_add_bool
msgbus_ret_t msgbus_msg_envelope_elem_array_add_bool(msg_envelope_elem_body_t *arr, bool value)
eii::msgbus::MsgEnvelopeElement::get_type
msg_envelope_data_type_t get_type()
eii::msgbus::MsgEnvelopeElement::~MsgEnvelopeElement
~MsgEnvelopeElement()
eii::msgbus::MsgEnvelopeObject::get_string
const char * get_string(const std::string key)
msgbus_msg_envelope_new_array
msg_envelope_elem_body_t * msgbus_msg_envelope_new_array()
eii::msgbus::MsgEnvelopeObject::~MsgEnvelopeObject
~MsgEnvelopeObject()
eii::msgbus::MsgEnvelope::remove
void remove(const std::string key)
eii::msgbus::MsgEnvelopeList::get_int
int64_t get_int(int64_t index)
eii::msgbus::MsgEnvelopeObject::get_msg_envelope_object
msg_envelope_elem_body_t * get_msg_envelope_object()
eii::msgbus::MsgEnvelopeElement::to_bool
bool to_bool()
eii::msgbus::MsgEnvelope::put_object
void put_object(const std::string key, MsgEnvelopeObject *value)
eii::msgbus::MsgEnvelope::get_array
MsgEnvelopeList * get_array(const std::string key)
eii::msgbus::MsgEnvelopeList::put_integer
void put_integer(int64_t value)
eii::msgbus::MsgEnvelopeObject::MsgEnvelopeObject
MsgEnvelopeObject()