浏览代码

Add auto serializable creation for SectionPairRow class

Patrick-Christopher Mattulat 2 年之前
父节点
当前提交
4cb262b3a3

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

@@ -20,7 +20,7 @@
 
 namespace ls::std::io
 {
-  class LS_STD_DYNAMIC_GOAL SectionPairRow : public ls::std::core::Class, public ls::std::core::interface_type::ISerializable
+  class LS_STD_DYNAMIC_GOAL SectionPairRow : public ::std::enable_shared_from_this<SectionPairRow>, public ls::std::core::Class, public ls::std::core::interface_type::ISerializable
   {
     public:
 
@@ -33,7 +33,6 @@ namespace ls::std::io
       [[nodiscard]] bool isSingleValue();
       [[nodiscard]] ls::std::core::type::byte_field marshal() override;
       void setKey(const ls::std::io::section_pair_identifier &_key);
-      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:
@@ -42,6 +41,7 @@ namespace ls::std::io
       ::std::shared_ptr<ls::std::core::interface_type::ISerializable> serializable{};
       ::std::shared_ptr<ls::std::io::SectionPairRowValue> value{};
 
+      void _createSerializable();
       void _initValue(const ls::std::io::SectionPairRowEnumType &_type);
       void _setKey(const ls::std::io::section_pair_identifier &_key);
   };

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

@@ -3,15 +3,15 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-11
-* Changed:         2023-02-12
+* Changed:         2023-02-13
 *
 * */
 
 #ifndef LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_HPP
 #define LS_STD_SERIALIZABLE_SECTION_PAIR_ROW_HPP
 
+#include <ls-std/core/Class.hpp>
 #include <ls-std/core/interface/ISerializable.hpp>
-#include <ls-std/io/section-pair/model/SectionPairRow.hpp>
 #include <memory>
 #include <string>
 
@@ -21,19 +21,19 @@ namespace ls::std::io
   {
     public:
 
-      explicit SerializableSectionPairRow(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value);
+      explicit SerializableSectionPairRow(const ::std::shared_ptr<ls::std::core::Class> &_value);
       ~SerializableSectionPairRow() override;
 
-      [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairRow> 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::SectionPairRow> value{};
+      ::std::shared_ptr<ls::std::core::Class> value{};
 
       [[nodiscard]] ::std::string _marshalKey();
-      void _setValue(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value);
+      void _setValue(const ::std::shared_ptr<ls::std::core::Class> &_value);
       void _unmarshalListValue(const ls::std::core::type::byte_field &_data);
       void _unmarshalSingleValue(const ls::std::core::type::byte_field &_data);
   };

+ 8 - 12
source/ls-std/io/section-pair/model/SectionPairRow.cpp

@@ -7,14 +7,14 @@
 *
 * */
 
+#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/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
+#include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
 
 ls::std::io::SectionPairRow::SectionPairRow(const ls::std::io::section_pair_identifier &_key, const ls::std::io::SectionPairRowEnumType &_type) : ls::std::core::Class("SectionPairRow")
 {
@@ -46,8 +46,7 @@ bool ls::std::io::SectionPairRow::isSingleValue()
 
 ls::std::core::type::byte_field ls::std::io::SectionPairRow::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();
 }
 
@@ -56,18 +55,15 @@ void ls::std::io::SectionPairRow::setKey(const ls::std::io::section_pair_identif
   this->_setKey(_key);
 }
 
-void ls::std::io::SectionPairRow::setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable> &_serializable)
+void ls::std::io::SectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
 {
-  ::std::string message = this->getClassName() + ": argument \"_serializable\" is null!";
-  ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_serializable), message}.evaluate();
-  this->serializable = _serializable;
+  ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
+  this->serializable->unmarshal(_data);
 }
 
-void ls::std::io::SectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
+void ls::std::io::SectionPairRow::_createSerializable()
 {
-  ::std::string message = this->getClassName() + ": 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::SerializableSectionPairRow>(shared_from_this());
 }
 
 void ls::std::io::SectionPairRow::_initValue(const ls::std::io::SectionPairRowEnumType &_type)

+ 21 - 15
source/ls-std/io/section-pair/serialization/SerializableSectionPairRow.cpp

@@ -3,24 +3,25 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-11
-* Changed:         2023-02-12
+* Changed:         2023-02-13
 *
 * */
 
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
 #include <ls-std/io/NewLine.hpp>
+#include <ls-std/io/section-pair/model/SectionPairRow.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
 
-ls::std::io::SerializableSectionPairRow::SerializableSectionPairRow(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value)
+ls::std::io::SerializableSectionPairRow::SerializableSectionPairRow(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   this->_setValue(_value);
 }
 
 ls::std::io::SerializableSectionPairRow::~SerializableSectionPairRow() = default;
 
-::std::shared_ptr<ls::std::io::SectionPairRow> ls::std::io::SerializableSectionPairRow::getValue()
+::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRow::getValue()
 {
   return this->value;
 }
@@ -28,17 +29,19 @@ ls::std::io::SerializableSectionPairRow::~SerializableSectionPairRow() = default
 ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRow::marshal()
 {
   ls::std::core::type::byte_field data = this->_marshalKey();
-  return data + this->value->getValue()->marshal();
+  return data + ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value)->getValue()->marshal();
 }
 
 void ls::std::io::SerializableSectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
 {
-  if (this->value->isSingleValue())
+  ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
+
+  if (row->isSingleValue())
   {
     this->_unmarshalSingleValue(_data);
   }
 
-  if (this->value->isList())
+  if (row->isList())
   {
     this->_unmarshalListValue(_data);
   }
@@ -47,21 +50,22 @@ void ls::std::io::SerializableSectionPairRow::unmarshal(const ls::std::core::typ
 ::std::string ls::std::io::SerializableSectionPairRow::_marshalKey()
 {
   ::std::string serializedKey{};
+  ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
 
-  if (this->value->isSingleValue())
+  if (row->isSingleValue())
   {
-    serializedKey = this->value->getKey() + "=";
+    serializedKey = row->getKey() + "=";
   }
 
-  if (this->value->isList())
+  if (row->isList())
   {
-    serializedKey = this->value->getKey() + ":" + ls::std::io::NewLine::get();
+    serializedKey = row->getKey() + ":" + ls::std::io::NewLine::get();
   }
 
   return serializedKey;
 }
 
-void ls::std::io::SerializableSectionPairRow::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value)
+void ls::std::io::SerializableSectionPairRow::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
 {
   ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
   this->value = _value;
@@ -74,9 +78,10 @@ void ls::std::io::SerializableSectionPairRow::_unmarshalListValue(const ls::std:
 
   if (separatorPosition != ::std::string::npos)
   {
-    this->value->setKey(_data.substr(0, separatorPosition));
+    ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
+    row->setKey(_data.substr(0, separatorPosition));
     ::std::string::size_type newLinePosition = _data.find(NewLine::get()) + (NewLine::get().size() - 1);
-    ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->value->getValue())->unmarshal(_data.substr(newLinePosition + 1));
+    ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(row->getValue())->unmarshal(_data.substr(newLinePosition + 1));
   }
 }
 
@@ -87,7 +92,8 @@ void ls::std::io::SerializableSectionPairRow::_unmarshalSingleValue(const ls::st
 
   if (position != ::std::string::npos)
   {
-    this->value->setKey(_data.substr(0, position));
-    ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(this->value->getValue())->unmarshal(_data.substr(position + 1));
+    ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
+    row->setKey(_data.substr(0, position));
+    ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(row->getValue())->unmarshal(_data.substr(position + 1));
   }
 }

+ 0 - 58
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");
-    row->setSerializable(make_shared<SerializableSectionPairRow>(row));
 
     ASSERT_STREQ("favourite-color=blue", row->marshal().c_str());
   }
@@ -113,31 +112,12 @@ namespace
     listValue->add("blue");
     listValue->add("red");
     listValue->add("purple");
-    row->setSerializable(make_shared<SerializableSectionPairRow>(row));
 
     string expected = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();
 
     ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
   }
 
-  TEST_F(SectionPairRowTest, marshal_no_serializable)
-  {
-    SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            byte_field data = row.marshal();
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
-
   TEST_F(SectionPairRowTest, setKey)
   {
     SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
@@ -182,28 +162,9 @@ namespace
         IllegalArgumentException);
   }
 
-  TEST_F(SectionPairRowTest, setSerializable_no_reference)
-  {
-    SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            row.setSerializable(nullptr);
-          }
-          catch (const IllegalArgumentException &_exception)
-          {
-            throw;
-          }
-        },
-        IllegalArgumentException);
-  }
-
   TEST_F(SectionPairRowTest, unmarshal_single_value)
   {
     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());
 
     row->unmarshal("favourite-color=blue");
@@ -215,7 +176,6 @@ namespace
   TEST_F(SectionPairRowTest, unmarshal_list_value)
   {
     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());
 
     string data = "favourite-colors:" + NewLine::get() + "  blue" + NewLine::get() + "  red" + NewLine::get() + "  purple" + NewLine::get();
@@ -227,22 +187,4 @@ namespace
     ASSERT_STREQ("purple", listValue->get(2).c_str());
     ASSERT_EQ(3, listValue->getSize());
   }
-
-  TEST_F(SectionPairRowTest, unmarshal_no_serializable)
-  {
-    SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
-
-    EXPECT_THROW(
-        {
-          try
-          {
-            row.unmarshal("hobbies:");
-          }
-          catch (const NullPointerException &_exception)
-          {
-            throw;
-          }
-        },
-        NullPointerException);
-  }
 }