SectionPairDocument.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. section_pair_document_section_list_element SectionPairDocument::get(const section_pair_identifier &_sectionId)
  63. {
  64. return this->_get(_sectionId);
  65. }
  66. size_t SectionPairDocument::getAmountOfSections()
  67. {
  68. return this->sections.size();
  69. }
  70. string SectionPairDocument::getHeader()
  71. {
  72. return this->header;
  73. }
  74. section_pair_document_section_list SectionPairDocument::getSectionList()
  75. {
  76. return this->sections;
  77. }
  78. bool SectionPairDocument::hasSection(const section_pair_identifier &_sectionId)
  79. {
  80. return this->_hasSection(_sectionId);
  81. }
  82. byte_field SectionPairDocument::marshal()
  83. {
  84. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  85. return this->serializable->marshal();
  86. }
  87. void SectionPairDocument::reserveNewLine(const string &_reservedNewLine)
  88. {
  89. this->reservedNewLine = _reservedNewLine;
  90. }
  91. void SectionPairDocument::unmarshal(const byte_field &_data)
  92. {
  93. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  94. this->serializable->unmarshal(_data);
  95. }
  96. void SectionPairDocument::_checkSectionExistence(const section_pair_identifier &_sectionId)
  97. {
  98. if (this->_hasSection(_sectionId))
  99. {
  100. string message = this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!";
  101. throw IllegalArgumentException{SectionPairMessageFormatter::getFormattedMessage(message)};
  102. }
  103. }
  104. void SectionPairDocument::_createSerializable()
  105. {
  106. SerializableSectionPairParameter parameter{};
  107. parameter.setValue(shared_from_this());
  108. if (!this->reservedNewLine.empty())
  109. {
  110. parameter.setNewLine(this->reservedNewLine);
  111. }
  112. this->serializable = make_shared<SerializableSectionPairDocument>(parameter);
  113. }
  114. section_pair_document_section_list_element SectionPairDocument::_get(const section_pair_identifier &_sectionId)
  115. {
  116. section_pair_document_section_list_element element{};
  117. for (const auto &_section : this->sections)
  118. {
  119. if (_section->getSectionId() == _sectionId)
  120. {
  121. element = _section;
  122. break;
  123. }
  124. }
  125. return element;
  126. }
  127. bool SectionPairDocument::_hasSection(const section_pair_identifier &_identifier)
  128. {
  129. bool sectionExists{};
  130. for (const auto &_section : this->sections)
  131. {
  132. if (_section->getSectionId() == _identifier)
  133. {
  134. sectionExists = true;
  135. break;
  136. }
  137. }
  138. return sectionExists;
  139. }