SectionPairDocument.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-22
  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. ls::std::io::SectionPairDocument::SectionPairDocument() : ls::std::core::Class("SectionPairDocument")
  17. {}
  18. ls::std::io::SectionPairDocument::~SectionPairDocument() = default;
  19. void ls::std::io::SectionPairDocument::add(const section_pair_document_section_list_element &_section)
  20. {
  21. ::std::string message = this->getClassName() + ": add section attempt failed, since \"_section\" argument is null!";
  22. ls::std::core::NullPointerArgumentEvaluator(::std::reinterpret_pointer_cast<void>(_section), message).evaluate();
  23. this->_checkSectionExistence(_section->getSectionId());
  24. this->sections.push_back(_section);
  25. }
  26. void ls::std::io::SectionPairDocument::clear()
  27. {
  28. this->sections.clear();
  29. }
  30. ls::std::io::section_pair_document_section_list_element ls::std::io::SectionPairDocument::get(size_t _index)
  31. {
  32. ls::std::io::section_pair_document_section_list_element element{};
  33. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
  34. size_t index{};
  35. for (const auto &_element : this->sections)
  36. {
  37. if (_index == index)
  38. {
  39. element = _element;
  40. break;
  41. }
  42. ++index;
  43. }
  44. return element;
  45. }
  46. size_t ls::std::io::SectionPairDocument::getAmountOfSections()
  47. {
  48. return this->sections.size();
  49. }
  50. ::std::string ls::std::io::SectionPairDocument::getHeader()
  51. {
  52. return this->header;
  53. }
  54. ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
  55. {
  56. return this->sections;
  57. }
  58. ls::std::core::type::byte_field ls::std::io::SectionPairDocument::marshal()
  59. {
  60. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  61. return this->serializable->marshal();
  62. }
  63. void ls::std::io::SectionPairDocument::reserveNewLine(const ::std::string &_reservedNewLine)
  64. {
  65. this->reservedNewLine = _reservedNewLine;
  66. }
  67. void ls::std::io::SectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
  68. {
  69. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  70. this->serializable->unmarshal(_data);
  71. }
  72. void ls::std::io::SectionPairDocument::_checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId)
  73. {
  74. if (this->_hasSection(_sectionId))
  75. {
  76. ::std::string message = this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!";
  77. throw ls::std::core::IllegalArgumentException{ls::std::io::SectionPairMessageFormatter::getFormattedMessage(message)};
  78. }
  79. }
  80. void ls::std::io::SectionPairDocument::_createSerializable()
  81. {
  82. ls::std::io::SerializableSectionPairParameter parameter{};
  83. parameter.setValue(shared_from_this());
  84. if (!this->reservedNewLine.empty())
  85. {
  86. parameter.setNewLine(this->reservedNewLine);
  87. }
  88. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairDocument>(parameter);
  89. }
  90. bool ls::std::io::SectionPairDocument::_hasSection(const ls::std::io::section_pair_identifier &_identifier)
  91. {
  92. bool sectionExists{};
  93. for (const auto &_section : this->sections)
  94. {
  95. if (_section->getSectionId() == _identifier)
  96. {
  97. sectionExists = true;
  98. break;
  99. }
  100. }
  101. return sectionExists;
  102. }