Browse Source

Add auto serializable creation for SectionPairRowSingleValue class

Patrick-Christopher Mattulat 2 years ago
parent
commit
a4a28b2312

+ 5 - 5
include/ls-std/io/section-pair/model/SectionPairRowSingleValue.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
 *
 * */
 
@@ -19,7 +19,7 @@
 
 namespace ls::std::io
 {
-  class LS_STD_DYNAMIC_GOAL SectionPairRowSingleValue : public ls::std::core::Class, public ls::std::io::SectionPairRowValue
+  class LS_STD_DYNAMIC_GOAL SectionPairRowSingleValue : public ::std::enable_shared_from_this<SectionPairRowSingleValue>, public ls::std::core::Class, public ls::std::io::SectionPairRowValue
   {
     public:
 
@@ -30,14 +30,14 @@ namespace ls::std::io
       [[nodiscard]] ls::std::io::SectionPairRowEnumType getType() override;
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
       void set(const ls::std::io::section_pair_row_value &_value);
-      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{};
-    ls::std::io::section_pair_row_value value{};
+      ::std::shared_ptr<ls::std::core::interface_type::ISerializable> serializable{};
+      ls::std::io::section_pair_row_value value{};
 
+      void _createSerializable();
       void _set(const ls::std::io::section_pair_row_value &_value);
   };
 }

+ 6 - 6
include/ls-std/io/section-pair/serialization/SerializableSectionPairRowSingleValue.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_SINGLE_VALUE_HPP
 #define LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_SINGLE_VALUE_HPP
 
+#include <ls-std/core/Class.hpp>
 #include <ls-std/core/interface/ISerializable.hpp>
-#include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 #include <memory>
 
@@ -21,18 +21,18 @@ namespace ls::std::io
   {
     public:
 
-      explicit SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value);
+      explicit SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::core::Class> &_value);
       ~SerializableSectionPairRowSingleValue() override;
 
-      [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> 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::SectionPairRowSingleValue> value{};
+      ::std::shared_ptr<ls::std::core::Class> value{};
 
-      void _setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value);
+      void _setValue(const ::std::shared_ptr<ls::std::core::Class> &_value);
   };
 }
 

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

@@ -3,15 +3,15 @@
 * 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/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/SectionPairRowSingleValue.hpp>
+#include <ls-std/io/section-pair/serialization/SerializableSectionPairRowSingleValue.hpp>
 
 ls::std::io::SectionPairRowSingleValue::SectionPairRowSingleValue(const ls::std::io::section_pair_row_value &_value) : ls::std::core::Class("SectionPairRowSingleValue"), ls::std::io::SectionPairRowValue(ls::std::io::SECTION_PAIR_ROW_SINGLE_VALUE)
 {
@@ -32,8 +32,7 @@ ls::std::io::SectionPairRowEnumType ls::std::io::SectionPairRowSingleValue::getT
 
 ls::std::core::type::byte_field ls::std::io::SectionPairRowSingleValue::marshal()
 {
-  ::std::string message = "member \"serializable\" for marshal attempt is null!";
-  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable), message}.evaluate();
+  ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
   return this->serializable->marshal();
 }
 
@@ -42,17 +41,15 @@ void ls::std::io::SectionPairRowSingleValue::set(const ls::std::io::section_pair
   this->_set(_value);
 }
 
-void ls::std::io::SectionPairRowSingleValue::setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable> &_serializable)
+void ls::std::io::SectionPairRowSingleValue::unmarshal(const ls::std::core::type::byte_field &_data)
 {
-  ls::std::core::NullPointerArgumentEvaluator{_serializable}.evaluate();
-  this->serializable = _serializable;
+  ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
+  this->serializable->unmarshal(_data);
 }
 
-void ls::std::io::SectionPairRowSingleValue::unmarshal(const ls::std::core::type::byte_field &_data)
+void ls::std::io::SectionPairRowSingleValue::_createSerializable()
 {
-  ::std::string message = "member \"serializable\" for unmarshal attempt is null!";
-  ls::std::core::NullPointerEvaluator{::std::reinterpret_pointer_cast<void>(this->serializable), message}.evaluate();
-  this->serializable->unmarshal(_data);
+  this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairRowSingleValue>(shared_from_this());
 }
 
 void ls::std::io::SectionPairRowSingleValue::_set(const ls::std::io::section_pair_row_value &_value)

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

@@ -3,38 +3,39 @@
 * 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/section-pair/model/SectionPairRowSingleValue.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowSingleValue.hpp>
 
-ls::std::io::SerializableSectionPairRowSingleValue::SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value)
+ls::std::io::SerializableSectionPairRowSingleValue::SerializableSectionPairRowSingleValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   this->_setValue(_value);
 }
 
 ls::std::io::SerializableSectionPairRowSingleValue::~SerializableSectionPairRowSingleValue() = default;
 
-::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> ls::std::io::SerializableSectionPairRowSingleValue::getValue()
+::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRowSingleValue::getValue()
 {
   return this->value;
 }
 
 ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRowSingleValue::marshal()
 {
-  return this->value->get();
+  return ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(this->value)->get();
 }
 
 void ls::std::io::SerializableSectionPairRowSingleValue::unmarshal(const ls::std::core::type::byte_field &_data)
 {
   ls::std::core::EmptyStringArgumentEvaluator{_data}.evaluate();
-  this->value->set(_data);
+  ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(this->value)->set(_data);
 }
 
-void ls::std::io::SerializableSectionPairRowSingleValue::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowSingleValue> &_value)
+void ls::std::io::SerializableSectionPairRowSingleValue::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   ls::std::core::NullPointerArgumentEvaluator{_value, "model reference for SerializableSectionPairRowSingleValue is null!"}.evaluate();
   this->value = _value;

+ 1 - 58
test/cases/io/section-pair/model/SectionPairRowSingleValueTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-10
-* Changed:         2023-02-11
+* Changed:         2023-02-13
 *
 * */
 
@@ -84,29 +84,9 @@ namespace
   TEST_F(SectionPairRowSingleValueTest, marshal)
   {
     shared_ptr<SectionPairRowSingleValue> value = make_shared<SectionPairRowSingleValue>("empty");
-    value->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(value));
-
     ASSERT_STREQ("empty", value->marshal().c_str());
   }
 
-  TEST_F(SectionPairRowSingleValueTest, marshal_no_serializable)
-  {
-    SectionPairRowSingleValue value{"blue"};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            byte_field data = value.marshal();
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
-
   TEST_F(SectionPairRowSingleValueTest, set_empty_value)
   {
     SectionPairRowSingleValue value{"empty"};
@@ -143,48 +123,11 @@ namespace
         IllegalArgumentException);
   }
 
-  TEST_F(SectionPairRowSingleValueTest, setSerializable_no_reference)
-  {
-    SectionPairRowSingleValue value{"empty"};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            value.setSerializable(nullptr);
-          }
-          catch (const IllegalArgumentException &_exception)
-          {
-            throw;
-          }
-        },
-        IllegalArgumentException);
-  }
-
   TEST_F(SectionPairRowSingleValueTest, unmarshal)
   {
     shared_ptr<SectionPairRowSingleValue> value = make_shared<SectionPairRowSingleValue>("empty");
-    value->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(value));
     value->unmarshal("blue");
 
     ASSERT_STREQ("blue", value->get().c_str());
   }
-
-  TEST_F(SectionPairRowSingleValueTest, unmarshal_no_serializable)
-  {
-    SectionPairRowSingleValue value{"blue"};
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            value.unmarshal("red");
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
 }

+ 1 - 3
test/cases/io/section-pair/model/SectionPairRowTest.cpp

@@ -101,7 +101,6 @@ namespace
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
     shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
     singleValue->set("blue");
-    singleValue->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(singleValue));
     row->setSerializable(make_shared<SerializableSectionPairRow>(row));
 
     ASSERT_STREQ("favourite-color=blue", row->marshal().c_str());
@@ -206,8 +205,7 @@ namespace
   {
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
     row->setSerializable(make_shared<SerializableSectionPairRow>(row));
-    shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue()); // TODO: should be created automatically
-    singleValue->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(singleValue));
+    shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
 
     row->unmarshal("favourite-color=blue");
 

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

@@ -59,7 +59,6 @@ namespace
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
     shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
     singleValue->set("blue");
-    singleValue->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(singleValue));
     SerializableSectionPairRow serializable{row};
 
     ASSERT_STREQ("favourite-color=blue", serializable.marshal().c_str());
@@ -84,7 +83,6 @@ namespace
   {
     shared_ptr<SectionPairRow> row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
     shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
-    singleValue->setSerializable(make_shared<SerializableSectionPairRowSingleValue>(singleValue));
     SerializableSectionPairRow serializable{row};
 
     serializable.unmarshal("favourite-color=blue");