Pārlūkot izejas kodu

Make SectionPairRowValue serializable

Since this is an abstract class, which can not be instantiated, inherited classes have to implement ISerializable interface.
SectionPairRowSingleValue comes now with required implementations.
Patrick-Christopher Mattulat 2 gadi atpakaļ
vecāks
revīzija
f4d03b0d2a

+ 7 - 1
include/ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp

@@ -12,8 +12,10 @@
 
 #include "SectionPairRowValue.hpp"
 #include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/ISerializable.hpp>
 #include <ls-std/io/section-pair/SectionPairTypes.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
+#include <memory>
 
 namespace ls::std::io
 {
@@ -26,11 +28,15 @@ namespace ls::std::io
 
       [[nodiscard]] ls::std::io::section_pair_row_value get();
       [[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:
 
-      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 _set(const ls::std::io::section_pair_row_value &_value);
   };

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

@@ -10,19 +10,22 @@
 #ifndef LS_STD_SECTION_PAIR_ROW_VALUE_HPP
 #define LS_STD_SECTION_PAIR_ROW_VALUE_HPP
 
+#include <ls-std/core/interface/ISerializable.hpp>
 #include <ls-std/io/section-pair/SectionPairRowEnumType.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 
 namespace ls::std::io
 {
-  class LS_STD_DYNAMIC_GOAL SectionPairRowValue
+  class LS_STD_DYNAMIC_GOAL SectionPairRowValue : public ls::std::core::interface_type::ISerializable
   {
     public:
 
       explicit SectionPairRowValue(const ls::std::io::SectionPairRowEnumType &_type);
-      virtual ~SectionPairRowValue();
+      ~SectionPairRowValue() override;
 
-      virtual ls::std::io::SectionPairRowEnumType getType() = 0;
+    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:
 

+ 22 - 0
source/ls-std/io/section-pair/model/SectionPairRowSingleValue.cpp

@@ -8,6 +8,8 @@
 * */
 
 #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>
 
@@ -28,11 +30,31 @@ ls::std::io::SectionPairRowEnumType ls::std::io::SectionPairRowSingleValue::getT
   return this->type;
 }
 
+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();
+  return this->serializable->marshal();
+}
+
 void ls::std::io::SectionPairRowSingleValue::set(const ls::std::io::section_pair_row_value &_value)
 {
   this->_set(_value);
 }
 
+void ls::std::io::SectionPairRowSingleValue::setSerializable(const ::std::shared_ptr<ls::std::core::interface_type::ISerializable> &_serializable)
+{
+  ls::std::core::NullPointerArgumentEvaluator{_serializable}.evaluate();
+  this->serializable = _serializable;
+}
+
+void ls::std::io::SectionPairRowSingleValue::unmarshal(const ls::std::core::type::byte_field &_data)
+{
+  ::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);
+}
+
 void ls::std::io::SectionPairRowSingleValue::_set(const ls::std::io::section_pair_row_value &_value)
 {
   ls::std::core::EmptyStringArgumentEvaluator{_value}.evaluate();

+ 8 - 0
source/ls-std/io/section-pair/model/SectionPairRowValue.cpp

@@ -13,3 +13,11 @@ ls::std::io::SectionPairRowValue::SectionPairRowValue(const ls::std::io::Section
 {}
 
 ls::std::io::SectionPairRowValue::~SectionPairRowValue() = default;
+
+ls::std::core::type::byte_field ls::std::io::SectionPairRowValue::marshal()
+{
+  return ls::std::core::type::byte_field{};
+}
+
+void ls::std::io::SectionPairRowValue::unmarshal(const ls::std::core::type::byte_field &_data)
+{}

+ 72 - 0
test/cases/io/section-pair/model/SectionPairRowSingleValueTest.cpp

@@ -12,6 +12,7 @@
 #include <ls-std/ls-std-io.hpp>
 
 using namespace ls::std::core;
+using namespace ls::std::core::type;
 using namespace ls::std::io;
 using namespace ::std;
 
@@ -80,6 +81,32 @@ namespace
     ASSERT_EQ(ls::std::io::SECTION_PAIR_ROW_SINGLE_VALUE, value.getType());
   }
 
+  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"};
@@ -115,4 +142,49 @@ 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);
+  }
 }