Event.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2021-05-24
  7. *
  8. * */
  9. #include <ls_std/event/Event.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. ls_std::Event::Event(const ls_std::event_id &_id) : ls_std::Class("Event")
  12. {
  13. this->_assignId(_id);
  14. }
  15. ls_std::byte_field ls_std::Event::marshal()
  16. {
  17. ls_std::byte_field data{};
  18. if (this->serializable != nullptr)
  19. {
  20. data = this->serializable->marshal();
  21. }
  22. return data;
  23. }
  24. void ls_std::Event::unmarshal(const ls_std::byte_field &_data)
  25. {
  26. if (this->serializable != nullptr)
  27. {
  28. this->serializable->unmarshal(_data);
  29. }
  30. }
  31. bool ls_std::Event::addParameter(const ls_std::event_parameter &_eventParameter)
  32. {
  33. bool wasAdded{};
  34. if (!this->_hasParameter(_eventParameter.first))
  35. {
  36. wasAdded = this->parameterList.insert(_eventParameter).second;
  37. }
  38. return wasAdded;
  39. }
  40. void ls_std::Event::clearParameterList()
  41. {
  42. this->parameterList.clear();
  43. }
  44. ls_std::event_id ls_std::Event::getId()
  45. {
  46. return this->id;
  47. }
  48. ls_std::event_parameter_list ls_std::Event::getParameterList()
  49. {
  50. return this->parameterList;
  51. }
  52. bool ls_std::Event::removeParameter(const ls_std::event_parameter_id &_id)
  53. {
  54. return this->parameterList.erase(_id) == 1;
  55. }
  56. void ls_std::Event::setId(const ls_std::event_id &_id)
  57. {
  58. this->_assignId(_id);
  59. }
  60. void ls_std::Event::setSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
  61. {
  62. this->_assignSerializable(_serializable);
  63. }
  64. void ls_std::Event::_assignId(const ls_std::event_id &_id)
  65. {
  66. if (_id.empty())
  67. {
  68. throw ls_std::IllegalArgumentException{};
  69. }
  70. this->id = _id;
  71. }
  72. void ls_std::Event::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
  73. {
  74. if (_serializable == nullptr)
  75. {
  76. throw ls_std::IllegalArgumentException{};
  77. }
  78. this->serializable = _serializable;
  79. }
  80. bool ls_std::Event::_hasParameter(const ls_std::event_id &_id)
  81. {
  82. return this->parameterList.find(_id) != this->parameterList.end();
  83. }