SectionPairRow.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-22
  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. ls::std::io::SectionPairRow::SectionPairRow(const ls::std::io::section_pair_identifier &_key, const ls::std::io::SectionPairRowEnumType &_type) : ls::std::core::Class("SectionPairRow")
  18. {
  19. this->_setKey(_key);
  20. this->_initValue(_type);
  21. }
  22. ls::std::io::SectionPairRow::~SectionPairRow() noexcept = default;
  23. ls::std::io::section_pair_row_value ls::std::io::SectionPairRow::getKey()
  24. {
  25. return this->key;
  26. }
  27. ::std::shared_ptr<ls::std::io::SectionPairRowValue> ls::std::io::SectionPairRow::getValue()
  28. {
  29. return this->value;
  30. }
  31. bool ls::std::io::SectionPairRow::isList()
  32. {
  33. return this->value->getType() == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
  34. }
  35. bool ls::std::io::SectionPairRow::isSingleValue()
  36. {
  37. return this->value->getType() == ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
  38. }
  39. ls::std::core::type::byte_field ls::std::io::SectionPairRow::marshal()
  40. {
  41. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  42. return this->serializable->marshal();
  43. }
  44. void ls::std::io::SectionPairRow::reserveNewLine(const ::std::string &_reservedNewLine)
  45. {
  46. this->reservedNewLine = _reservedNewLine;
  47. }
  48. void ls::std::io::SectionPairRow::setKey(const ls::std::io::section_pair_identifier &_key)
  49. {
  50. this->_setKey(_key);
  51. }
  52. void ls::std::io::SectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
  53. {
  54. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  55. this->serializable->unmarshal(_data);
  56. }
  57. void ls::std::io::SectionPairRow::_createSerializable()
  58. {
  59. ls::std::io::SerializableSectionPairParameter parameter{};
  60. parameter.setValue(shared_from_this());
  61. if (!this->reservedNewLine.empty())
  62. {
  63. parameter.setNewLine(this->reservedNewLine);
  64. }
  65. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairRow>(parameter);
  66. }
  67. void ls::std::io::SectionPairRow::_initValue(const ls::std::io::SectionPairRowEnumType &_type)
  68. {
  69. switch (_type)
  70. {
  71. case SECTION_PAIR_ROW_NOT_IMPLEMENTED:
  72. {
  73. throw ls::std::core::IllegalArgumentException{this->getClassName() + ": default row enum type can not be set!"};
  74. }
  75. case SECTION_PAIR_ROW_LIST_VALUE:
  76. {
  77. this->value = ::std::make_shared<ls::std::io::SectionPairRowListValue>();
  78. }
  79. break;
  80. case SECTION_PAIR_ROW_SINGLE_VALUE:
  81. {
  82. this->value = ::std::make_shared<ls::std::io::SectionPairRowSingleValue>("empty");
  83. }
  84. break;
  85. }
  86. }
  87. void ls::std::io::SectionPairRow::_setKey(const ls::std::io::section_pair_identifier &_key)
  88. {
  89. ls::std::core::EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
  90. ls::std::io::SectionPairIdentifierArgumentEvaluator(_key).evaluate();
  91. this->key = _key;
  92. }