SectionPairDocument.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-15
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
  11. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  12. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  13. #include <ls-std/io/section-pair/SectionPairMessageFormatter.hpp>
  14. #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
  15. #include <ls-std/io/section-pair/serialization/SerializableSectionPairDocument.hpp>
  16. using ls::std::core::Class;
  17. using ls::std::core::ConditionalFunctionExecutor;
  18. using ls::std::core::IllegalArgumentException;
  19. using ls::std::core::IndexOutOfBoundsEvaluator;
  20. using ls::std::core::NullPointerArgumentEvaluator;
  21. using ls::std::core::type::byte_field;
  22. using ls::std::io::section_pair_document_section_list;
  23. using ls::std::io::section_pair_document_section_list_element;
  24. using ls::std::io::section_pair_identifier;
  25. using ls::std::io::SectionPairDocument;
  26. using ls::std::io::SectionPairMessageFormatter;
  27. using ls::std::io::SerializableSectionPairDocument;
  28. using ls::std::io::SerializableSectionPairParameter;
  29. using std::make_shared;
  30. using std::reinterpret_pointer_cast;
  31. using std::string;
  32. SectionPairDocument::SectionPairDocument() : Class("SectionPairDocument")
  33. {}
  34. SectionPairDocument::~SectionPairDocument() noexcept = default;
  35. void SectionPairDocument::add(const section_pair_document_section_list_element &_section)
  36. {
  37. string message = this->getClassName() + ": add section attempt failed, since \"_section\" argument is null!";
  38. NullPointerArgumentEvaluator(reinterpret_pointer_cast<void>(_section), message).evaluate();
  39. this->_checkSectionExistence(_section->getSectionId());
  40. this->sections.push_back(_section);
  41. }
  42. void SectionPairDocument::clear()
  43. {
  44. this->sections.clear();
  45. }
  46. section_pair_document_section_list_element SectionPairDocument::get(size_t _index)
  47. {
  48. section_pair_document_section_list_element element{};
  49. IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
  50. size_t index{};
  51. for (const auto &_element : this->sections)
  52. {
  53. if (_index == index)
  54. {
  55. element = _element;
  56. break;
  57. }
  58. ++index;
  59. }
  60. return element;
  61. }
  62. size_t SectionPairDocument::getAmountOfSections()
  63. {
  64. return this->sections.size();
  65. }
  66. string SectionPairDocument::getHeader()
  67. {
  68. return this->header;
  69. }
  70. section_pair_document_section_list SectionPairDocument::getSectionList()
  71. {
  72. return this->sections;
  73. }
  74. byte_field SectionPairDocument::marshal()
  75. {
  76. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  77. return this->serializable->marshal();
  78. }
  79. void SectionPairDocument::reserveNewLine(const string &_reservedNewLine)
  80. {
  81. this->reservedNewLine = _reservedNewLine;
  82. }
  83. void SectionPairDocument::unmarshal(const byte_field &_data)
  84. {
  85. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  86. this->serializable->unmarshal(_data);
  87. }
  88. void SectionPairDocument::_checkSectionExistence(const section_pair_identifier &_sectionId)
  89. {
  90. if (this->_hasSection(_sectionId))
  91. {
  92. string message = this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!";
  93. throw IllegalArgumentException{SectionPairMessageFormatter::getFormattedMessage(message)};
  94. }
  95. }
  96. void SectionPairDocument::_createSerializable()
  97. {
  98. SerializableSectionPairParameter parameter{};
  99. parameter.setValue(shared_from_this());
  100. if (!this->reservedNewLine.empty())
  101. {
  102. parameter.setNewLine(this->reservedNewLine);
  103. }
  104. this->serializable = make_shared<SerializableSectionPairDocument>(parameter);
  105. }
  106. bool SectionPairDocument::_hasSection(const section_pair_identifier &_identifier)
  107. {
  108. bool sectionExists{};
  109. for (const auto &_section : this->sections)
  110. {
  111. if (_section->getSectionId() == _identifier)
  112. {
  113. sectionExists = true;
  114. break;
  115. }
  116. }
  117. return sectionExists;
  118. }