Browse Source

Add flyweight methods to section pair model classes

This enables section pair model classes to be reused for serialization.
Patrick-Christopher Mattulat 2 years ago
parent
commit
e9d88a04e1

+ 1 - 0
include/ls-std/io/section-pair/model/SectionPairDocument.hpp

@@ -27,6 +27,7 @@ namespace ls::std::io
       ~SectionPairDocument() override;
 
       void add(const section_pair_document_section_list_element &_section);
+      void clear();
       [[nodiscard]] section_pair_document_section_list_element get(size_t _index);
       [[nodiscard]] size_t getAmountOfSections();
       [[nodiscard]] ::std::string getHeader();

+ 2 - 1
include/ls-std/io/section-pair/model/SectionPairRowListValue.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-13
+* Changed:         2023-02-16
 *
 * */
 
@@ -25,6 +25,7 @@ namespace ls::std::io
       ~SectionPairRowListValue() override;
 
       void add(const ls::std::io::section_pair_row_value &_value);
+      void clear();
       [[nodiscard]] ls::std::io::section_pair_row_value get(size_t _index);
       [[nodiscard]] ::std::list<ls::std::io::section_pair_row_value> getList();
       [[nodiscard]] size_t getSize();

+ 4 - 4
include/ls-std/io/section-pair/model/SectionPairRowValue.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-11
+* Changed:         2023-02-16
 *
 * */
 
@@ -23,9 +23,9 @@ namespace ls::std::io
       explicit SectionPairRowValue(const ls::std::io::SectionPairRowEnumType &_type);
       ~SectionPairRowValue() override;
 
-    virtual ls::std::io::SectionPairRowEnumType getType() = 0;
-    [[nodiscard]] ls::std::core::type::byte_field marshal() override;
-    void unmarshal(const ls::std::core::type::byte_field &_data) override;
+      virtual ls::std::io::SectionPairRowEnumType getType() = 0;
+      [[nodiscard]] ls::std::core::type::byte_field marshal() override;
+      void unmarshal(const ls::std::core::type::byte_field &_data) override;
 
     protected:
 

+ 2 - 1
include/ls-std/io/section-pair/model/SectionPairSection.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-13
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -28,6 +28,7 @@ namespace ls::std::io
       ~SectionPairSection() override;
 
       void add(const section_pair_row_list_element &_row);
+      void clear();
       [[nodiscard]] section_pair_row_list_element get(size_t _index);
       [[nodiscard]] ls::std::io::section_pair_row_list getList();
       [[nodiscard]] size_t getRowAmount();

+ 5 - 0
source/ls-std/io/section-pair/model/SectionPairDocument.cpp

@@ -27,6 +27,11 @@ void ls::std::io::SectionPairDocument::add(const section_pair_document_section_l
   this->sections.push_back(_section);
 }
 
+void ls::std::io::SectionPairDocument::clear()
+{
+  this->sections.clear();
+}
+
 ls::std::io::section_pair_document_section_list_element ls::std::io::SectionPairDocument::get(size_t _index)
 {
   ls::std::io::section_pair_document_section_list_element element{};

+ 6 - 1
source/ls-std/io/section-pair/model/SectionPairRowListValue.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-13
+* Changed:         2023-02-16
 *
 * */
 
@@ -27,6 +27,11 @@ void ls::std::io::SectionPairRowListValue::add(const ls::std::io::section_pair_r
   this->values.push_back(_value);
 }
 
+void ls::std::io::SectionPairRowListValue::clear()
+{
+  this->values.clear();
+}
+
 ls::std::io::section_pair_row_value ls::std::io::SectionPairRowListValue::get(size_t _index)
 {
   ls::std::core::IndexOutOfBoundsEvaluator(_index, this->values.size()).evaluate();

+ 6 - 1
source/ls-std/io/section-pair/model/SectionPairSection.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-13
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -30,6 +30,11 @@ void ls::std::io::SectionPairSection::add(const section_pair_row_list_element &_
   this->rows.push_back(_row);
 }
 
+void ls::std::io::SectionPairSection::clear()
+{
+  this->rows.clear();
+}
+
 ls::std::io::section_pair_row_list_element ls::std::io::SectionPairSection::get(size_t _index)
 {
   ls::std::core::IndexOutOfBoundsEvaluator{_index, this->rows.size()}.evaluate();

+ 10 - 0
test/cases/io/section-pair/model/SectionPairDocumentTest.cpp

@@ -63,6 +63,16 @@ namespace
         IllegalArgumentException);
   }
 
+  TEST_F(SectionPairDocumentTest, clear)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    document->add(make_shared<SectionPairSection>("general"));
+
+    ASSERT_EQ(1, document->getAmountOfSections());
+    document->clear();
+    ASSERT_TRUE(document->getSectionList().empty());
+  }
+
   TEST_F(SectionPairDocumentTest, get)
   {
     shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();

+ 11 - 1
test/cases/io/section-pair/model/SectionPairRowListValueTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-13
+* Changed:         2023-02-16
 *
 * */
 
@@ -77,6 +77,16 @@ namespace
         IllegalArgumentException);
   }
 
+  TEST_F(SectionPairRowListValueTest, clear)
+  {
+    SectionPairRowListValue list{};
+    list.add("Music");
+
+    ASSERT_EQ(1, list.getSize());
+    list.clear();
+    ASSERT_TRUE(list.getList().empty());
+  }
+
   TEST_F(SectionPairRowListValueTest, getClassName)
   {
     ASSERT_STREQ("SectionPairRowListValue", SectionPairRowListValue{}.getClassName().c_str());

+ 16 - 6
test/cases/io/section-pair/model/SectionPairSectionTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-13
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -66,11 +66,6 @@ namespace
         IllegalArgumentException);
   }
 
-  TEST_F(SectionPairSectionTest, getClassName)
-  {
-    ASSERT_STREQ("SectionPairSection", SectionPairSection{"general"}.getClassName().c_str());
-  }
-
   TEST_F(SectionPairSectionTest, add)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
@@ -98,6 +93,16 @@ namespace
         IllegalArgumentException);
   }
 
+  TEST_F(SectionPairSectionTest, clear)
+  {
+    shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
+    section->add(make_shared<SectionPairRow>("color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE));
+
+    ASSERT_EQ(1, section->getRowAmount());
+    section->clear();
+    ASSERT_TRUE(section->getList().empty());
+  }
+
   TEST_F(SectionPairSectionTest, get)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");
@@ -130,6 +135,11 @@ namespace
     ASSERT_EQ(0, section->getRowAmount());
   }
 
+  TEST_F(SectionPairSectionTest, getClassName)
+  {
+    ASSERT_STREQ("SectionPairSection", SectionPairSection{"general"}.getClassName().c_str());
+  }
+
   TEST_F(SectionPairSectionTest, getList)
   {
     shared_ptr<SectionPairSection> section = make_shared<SectionPairSection>("general");