SectionPairRow.cpp 4.0 KB

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