SectionPairDocument.cpp 4.7 KB

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