Browse Source

Add auto serializable creation for SectionPairRowListValue class

Patrick-Christopher Mattulat 2 years ago
parent
commit
9396ecd481

+ 4 - 3
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-11
+* Changed:         2023-02-13
 *
 * */
 
@@ -17,7 +17,7 @@
 
 namespace ls::std::io
 {
-  class SectionPairRowListValue : public ls::std::core::Class, public ls::std::io::SectionPairRowValue
+  class SectionPairRowListValue : public ::std::enable_shared_from_this<SectionPairRowListValue>, public ls::std::core::Class, public ls::std::io::SectionPairRowValue
   {
     public:
 
@@ -30,13 +30,14 @@ namespace ls::std::io
       [[nodiscard]] size_t getSize();
       [[nodiscard]] ls::std::io::SectionPairRowEnumType getType() override;
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
-      void setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable>& _serializable);
       void unmarshal(const ls::std::core::type::byte_field &_data) override;
 
     private:
 
       ::std::shared_ptr<ls::std::core::interface_type::ISerializable> serializable{};
       ::std::list<ls::std::io::section_pair_row_value> values{};
+
+      void _createSerializable();
   };
 }
 

+ 6 - 6
include/ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp

@@ -3,15 +3,15 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-11
-* Changed:         2023-02-11
+* Changed:         2023-02-13
 *
 * */
 
 #ifndef LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_LIST_VALUE_HPP
 #define LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_LIST_VALUE_HPP
 
+#include <ls-std/core/Class.hpp>
 #include <ls-std/core/interface/ISerializable.hpp>
-#include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 #include <memory>
 
@@ -21,19 +21,19 @@ namespace ls::std::io
   {
     public:
 
-      explicit SerializableSectionPairRowListValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value);
+      explicit SerializableSectionPairRowListValue(const ::std::shared_ptr<ls::std::core::Class> &_value);
       ~SerializableSectionPairRowListValue() override;
 
-      [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairRowListValue> getValue();
+      [[nodiscard]] ::std::shared_ptr<ls::std::core::Class> getValue();
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
       void unmarshal(const ls::std::core::type::byte_field &_data) override;
 
     private:
 
-      ::std::shared_ptr<ls::std::io::SectionPairRowListValue> value{};
+      ::std::shared_ptr<ls::std::core::Class> value{};
 
       [[nodiscard]] static ::std::string _getLine(::std::string::size_type _position, const ls::std::core::type::byte_field &_searchText);
-      void _setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value);
+      void _setValue(const ::std::shared_ptr<ls::std::core::Class> &_value);
       static void _updateSearchText(::std::string::size_type _position, ls::std::core::type::byte_field &_searchText);
   };
 }

+ 9 - 12
source/ls-std/io/section-pair/model/SectionPairRowListValue.cpp

@@ -3,16 +3,16 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-11
+* Changed:         2023-02-13
 *
 * */
 
+#include <ls-std/core/ConditionalFunctionExecutor.hpp>
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
-#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
-#include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairRowValueArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
+#include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
 #include <string>
 
 ls::std::io::SectionPairRowListValue::SectionPairRowListValue() : ls::std::core::Class("SectionPairRowListValue"), ls::std::io::SectionPairRowValue(ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE)
@@ -64,20 +64,17 @@ ls::std::io::SectionPairRowEnumType ls::std::io::SectionPairRowListValue::getTyp
 
 ls::std::core::type::byte_field ls::std::io::SectionPairRowListValue::marshal()
 {
-  ::std::string message = "member \"serializable\" for marshal attempt is null!";
-  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable)}.evaluate();
+  ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
   return this->serializable->marshal();
 }
 
-void ls::std::io::SectionPairRowListValue::setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable> &_serializable)
+void ls::std::io::SectionPairRowListValue::unmarshal(const ls::std::core::type::byte_field &_data)
 {
-  ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_serializable)}.evaluate();
-  this->serializable = _serializable;
+  ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
+  this->serializable->unmarshal(_data);
 }
 
-void ls::std::io::SectionPairRowListValue::unmarshal(const ls::std::core::type::byte_field &_data)
+void ls::std::io::SectionPairRowListValue::_createSerializable()
 {
-  ::std::string message = "member \"serializable\" for unmarshal attempt is null!";
-  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable)}.evaluate();
-  this->serializable->unmarshal(_data);
+  this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairRowListValue>(shared_from_this());
 }

+ 7 - 6
source/ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.cpp

@@ -3,23 +3,24 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-11
-* Changed:         2023-02-11
+* Changed:         2023-02-13
 *
 * */
 
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
 #include <ls-std/io/NewLine.hpp>
+#include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
 
-ls::std::io::SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value)
+ls::std::io::SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   this->_setValue(_value);
 }
 
 ls::std::io::SerializableSectionPairRowListValue::~SerializableSectionPairRowListValue() = default;
 
-::std::shared_ptr<ls::std::io::SectionPairRowListValue> ls::std::io::SerializableSectionPairRowListValue::getValue()
+::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRowListValue::getValue()
 {
   return this->value;
 }
@@ -28,7 +29,7 @@ ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRowListValue
 {
   ls::std::core::type::byte_field data{};
 
-  for (const auto &_value : this->value->getList())
+  for (const auto &_value : ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->value)->getList())
   {
     data += "  " + _value + ls::std::io::NewLine::get();
   }
@@ -46,7 +47,7 @@ void ls::std::io::SerializableSectionPairRowListValue::unmarshal(const ls::std::
     ::std::string::size_type positionOfNewLine = searchText.find(ls::std::io::NewLine::get());
     ::std::string line = ls::std::io::SerializableSectionPairRowListValue::_getLine(positionOfNewLine, searchText);
     line = line.substr(2);
-    this->value->add(line);
+    ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->value)->add(line);
     ls::std::io::SerializableSectionPairRowListValue::_updateSearchText(positionOfNewLine, searchText);
   }
 }
@@ -67,7 +68,7 @@ void ls::std::io::SerializableSectionPairRowListValue::unmarshal(const ls::std::
   return line;
 }
 
-void ls::std::io::SerializableSectionPairRowListValue::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value)
+void ls::std::io::SerializableSectionPairRowListValue::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
   this->value = _value;

+ 1 - 57
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-12
+* Changed:         2023-02-13
 *
 * */
 
@@ -121,7 +121,6 @@ namespace
   TEST_F(SectionPairRowListValueTest, marshal)
   {
     shared_ptr<SectionPairRowListValue> value = make_shared<SectionPairRowListValue>();
-    value->setSerializable(make_shared<SerializableSectionPairRowListValue>(value));
     value->add("Drumming");
     value->add("Reading");
     value->add("Coding");
@@ -131,46 +130,9 @@ namespace
     ASSERT_STREQ(expected.c_str(), value->marshal().c_str());
   }
 
-  TEST_F(SectionPairRowListValueTest, marshal_no_serializable)
-  {
-    SectionPairRowListValue list{};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            byte_field data = list.marshal();
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
-
-  TEST_F(SectionPairRowListValueTest, setSerializable_no_reference)
-  {
-    SectionPairRowListValue list{};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            list.setSerializable(nullptr);
-          }
-          catch (const IllegalArgumentException &_exception)
-          {
-            throw;
-          }
-        },
-        IllegalArgumentException);
-  }
-
   TEST_F(SectionPairRowListValueTest, unmarshal)
   {
     shared_ptr<SectionPairRowListValue> value = make_shared<SectionPairRowListValue>();
-    value->setSerializable(make_shared<SerializableSectionPairRowListValue>(value));
     ::std::string serializedListValue = "  Drumming" + NewLine::get() + "  Reading" + NewLine::get() + "  Coding" + NewLine::get();
     value->unmarshal(serializedListValue);
 
@@ -179,22 +141,4 @@ namespace
     ASSERT_STREQ("Reading", value->get(1).c_str());
     ASSERT_STREQ("Coding", value->get(2).c_str());
   }
-
-  TEST_F(SectionPairRowListValueTest, unmarshal_no_serializable)
-  {
-    SectionPairRowListValue list{};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            list.unmarshal("  Blue\nYellow\n");
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
 }

+ 0 - 2
test/cases/io/section-pair/model/SectionPairRowTest.cpp

@@ -113,7 +113,6 @@ namespace
     listValue->add("blue");
     listValue->add("red");
     listValue->add("purple");
-    listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
     row->setSerializable(make_shared<SerializableSectionPairRow>(row));
 
     string expected = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();
@@ -218,7 +217,6 @@ namespace
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
     row->setSerializable(make_shared<SerializableSectionPairRow>(row));
     shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
-    listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
 
     string data = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();
     row->unmarshal(data);

+ 0 - 2
test/cases/io/section-pair/serialization/SerializableSectionPairRowTest.cpp

@@ -71,7 +71,6 @@ namespace
     listValue->add("blue");
     listValue->add("red");
     listValue->add("purple");
-    listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
     SerializableSectionPairRow serializable{row};
 
     string expected = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();
@@ -95,7 +94,6 @@ namespace
   {
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
     shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
-    listValue->setSerializable(make_shared<SerializableSectionPairRowListValue>(listValue));
     SerializableSectionPairRow serializable{row};
 
     string data = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();