SectionPairDocument.cpp 3.6 KB

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