SectionPairRow.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-13
  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() = 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::setKey(const ls::std::io::section_pair_identifier &_key)
  45. {
  46. this->_setKey(_key);
  47. }
  48. void ls::std::io::SectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
  49. {
  50. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  51. this->serializable->unmarshal(_data);
  52. }
  53. void ls::std::io::SectionPairRow::_createSerializable()
  54. {
  55. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairRow>(shared_from_this());
  56. }
  57. void ls::std::io::SectionPairRow::_initValue(const ls::std::io::SectionPairRowEnumType &_type)
  58. {
  59. switch (_type)
  60. {
  61. case SECTION_PAIR_ROW_NOT_IMPLEMENTED:
  62. {
  63. throw ls::std::core::IllegalArgumentException{this->getClassName() + ": default row enum type can not be set!"};
  64. }
  65. case SECTION_PAIR_ROW_LIST_VALUE:
  66. {
  67. this->value = ::std::make_shared<ls::std::io::SectionPairRowListValue>();
  68. }
  69. break;
  70. case SECTION_PAIR_ROW_SINGLE_VALUE:
  71. {
  72. this->value = ::std::make_shared<ls::std::io::SectionPairRowSingleValue>("empty");
  73. }
  74. break;
  75. }
  76. }
  77. void ls::std::io::SectionPairRow::_setKey(const ls::std::io::section_pair_identifier &_key)
  78. {
  79. ls::std::core::EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
  80. ls::std::io::SectionPairIdentifierArgumentEvaluator(_key, this->getClassName() + ": section pair key identifier \"" + _key + "\" contains invalid characters!").evaluate();
  81. this->key = _key;
  82. }