SectionPairRow.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2023-05-18
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  11. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  12. #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
  13. #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
  14. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  15. #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
  16. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
  17. using ls::std::core::Class;
  18. using ls::std::core::ConditionalFunctionExecutor;
  19. using ls::std::core::EmptyStringArgumentEvaluator;
  20. using ls::std::core::IllegalArgumentException;
  21. using ls::std::core::type::byte_field;
  22. using ls::std::io::section_pair_identifier;
  23. using ls::std::io::section_pair_row_value;
  24. using ls::std::io::SectionPairIdentifierArgumentEvaluator;
  25. using ls::std::io::SectionPairRow;
  26. using ls::std::io::SectionPairRowEnumType;
  27. using ls::std::io::SectionPairRowListValue;
  28. using ls::std::io::SectionPairRowSingleValue;
  29. using ls::std::io::SectionPairRowValue;
  30. using ls::std::io::SerializableSectionPairParameter;
  31. using ls::std::io::SerializableSectionPairRow;
  32. using std::make_shared;
  33. using std::shared_ptr;
  34. using std::string;
  35. using std::string_view;
  36. SectionPairRow::SectionPairRow(const section_pair_identifier &_key, const SectionPairRowEnumType &_type) : Class("SectionPairRow")
  37. {
  38. this->_setKey(_key);
  39. this->_initValue(_type);
  40. }
  41. SectionPairRow::~SectionPairRow() noexcept = default;
  42. section_pair_row_value SectionPairRow::getKey() const
  43. {
  44. return this->key;
  45. }
  46. shared_ptr<SectionPairRowValue> SectionPairRow::getValue() const
  47. {
  48. return this->value;
  49. }
  50. bool SectionPairRow::isList() const
  51. {
  52. return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
  53. }
  54. bool SectionPairRow::isSingleValue() const
  55. {
  56. return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
  57. }
  58. byte_field SectionPairRow::marshal()
  59. {
  60. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  61. return this->serializable->marshal();
  62. }
  63. void SectionPairRow::reserveNewLine(string_view _reservedNewLine)
  64. {
  65. this->reservedNewLine = _reservedNewLine;
  66. }
  67. void SectionPairRow::setKey(const section_pair_identifier &_key)
  68. {
  69. this->_setKey(_key);
  70. }
  71. void SectionPairRow::unmarshal(const byte_field &_data)
  72. {
  73. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  74. this->serializable->unmarshal(_data);
  75. }
  76. void SectionPairRow::_createSerializable()
  77. {
  78. SerializableSectionPairParameter parameter{};
  79. parameter.setValue(shared_from_this());
  80. if (!this->reservedNewLine.empty())
  81. {
  82. parameter.setNewLine(this->reservedNewLine);
  83. }
  84. this->serializable = make_shared<SerializableSectionPairRow>(parameter);
  85. }
  86. void SectionPairRow::_initValue(const SectionPairRowEnumType &_type)
  87. {
  88. switch (_type)
  89. {
  90. case SectionPairRowEnumType::SECTION_PAIR_ROW_NOT_IMPLEMENTED:
  91. {
  92. throw IllegalArgumentException{this->getClassName() + ": default row enum type can not be set!"};
  93. }
  94. case SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE:
  95. {
  96. this->value = make_shared<SectionPairRowListValue>();
  97. }
  98. break;
  99. case SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE:
  100. {
  101. this->value = make_shared<SectionPairRowSingleValue>("empty");
  102. }
  103. break;
  104. }
  105. }
  106. void SectionPairRow::_setKey(const section_pair_identifier &_key)
  107. {
  108. EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
  109. SectionPairIdentifierArgumentEvaluator(_key).evaluate();
  110. this->key = _key;
  111. }