SectionPairDocument.cpp 4.8 KB

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