SectionPairDocument.cpp 4.7 KB

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