SectionPairDocument.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-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/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. ls::std::io::section_pair_document_section_list_element ls::std::io::SectionPairDocument::get(size_t _index)
  26. {
  27. ls::std::io::section_pair_document_section_list_element element{};
  28. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
  29. size_t index{};
  30. for (const auto &_element : this->sections)
  31. {
  32. if (_index == index)
  33. {
  34. element = _element;
  35. break;
  36. }
  37. ++index;
  38. }
  39. return element;
  40. }
  41. size_t ls::std::io::SectionPairDocument::getAmountOfSections()
  42. {
  43. return this->sections.size();
  44. }
  45. ::std::string ls::std::io::SectionPairDocument::getHeader()
  46. {
  47. return this->header;
  48. }
  49. ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
  50. {
  51. return this->sections;
  52. }
  53. ls::std::core::type::byte_field ls::std::io::SectionPairDocument::marshal()
  54. {
  55. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  56. return this->serializable->marshal();
  57. }
  58. void ls::std::io::SectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
  59. {
  60. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  61. this->serializable->unmarshal(_data);
  62. }
  63. void ls::std::io::SectionPairDocument::_checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId)
  64. {
  65. if (this->_hasSection(_sectionId))
  66. {
  67. throw ls::std::core::IllegalArgumentException{this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!"};
  68. }
  69. }
  70. void ls::std::io::SectionPairDocument::_createSerializable()
  71. {
  72. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairDocument>(shared_from_this());
  73. }
  74. bool ls::std::io::SectionPairDocument::_hasSection(const ls::std::io::section_pair_identifier &_identifier)
  75. {
  76. bool sectionExists{};
  77. for (const auto &_section : this->sections)
  78. {
  79. if (_section->getSectionId() == _identifier)
  80. {
  81. sectionExists = true;
  82. break;
  83. }
  84. }
  85. return sectionExists;
  86. }