SectionPairRow.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2023-02-23
  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. SectionPairRow::SectionPairRow(const section_pair_identifier &_key, const SectionPairRowEnumType &_type) : Class("SectionPairRow")
  36. {
  37. this->_setKey(_key);
  38. this->_initValue(_type);
  39. }
  40. SectionPairRow::~SectionPairRow() noexcept = default;
  41. section_pair_row_value SectionPairRow::getKey()
  42. {
  43. return this->key;
  44. }
  45. shared_ptr<SectionPairRowValue> SectionPairRow::getValue()
  46. {
  47. return this->value;
  48. }
  49. bool SectionPairRow::isList()
  50. {
  51. return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
  52. }
  53. bool SectionPairRow::isSingleValue()
  54. {
  55. return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
  56. }
  57. byte_field SectionPairRow::marshal()
  58. {
  59. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  60. return this->serializable->marshal();
  61. }
  62. void SectionPairRow::reserveNewLine(const string &_reservedNewLine)
  63. {
  64. this->reservedNewLine = _reservedNewLine;
  65. }
  66. void SectionPairRow::setKey(const section_pair_identifier &_key)
  67. {
  68. this->_setKey(_key);
  69. }
  70. void SectionPairRow::unmarshal(const byte_field &_data)
  71. {
  72. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  73. this->serializable->unmarshal(_data);
  74. }
  75. void SectionPairRow::_createSerializable()
  76. {
  77. SerializableSectionPairParameter parameter{};
  78. parameter.setValue(shared_from_this());
  79. if (!this->reservedNewLine.empty())
  80. {
  81. parameter.setNewLine(this->reservedNewLine);
  82. }
  83. this->serializable = make_shared<SerializableSectionPairRow>(parameter);
  84. }
  85. void SectionPairRow::_initValue(const SectionPairRowEnumType &_type)
  86. {
  87. switch (_type)
  88. {
  89. case SECTION_PAIR_ROW_NOT_IMPLEMENTED:
  90. {
  91. throw IllegalArgumentException{this->getClassName() + ": default row enum type can not be set!"};
  92. }
  93. case SECTION_PAIR_ROW_LIST_VALUE:
  94. {
  95. this->value = make_shared<SectionPairRowListValue>();
  96. }
  97. break;
  98. case SECTION_PAIR_ROW_SINGLE_VALUE:
  99. {
  100. this->value = make_shared<SectionPairRowSingleValue>("empty");
  101. }
  102. break;
  103. }
  104. }
  105. void SectionPairRow::_setKey(const section_pair_identifier &_key)
  106. {
  107. EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
  108. SectionPairIdentifierArgumentEvaluator(_key).evaluate();
  109. this->key = _key;
  110. }