Browse Source

Extended Integer class

- implemented ISerializable interface for Integer
class, so that an instance of Integer class can
marshal or unmarshal itself directly
- extended tests for Integer class
Patrick 4 năm trước cách đây
mục cha
commit
1650ee05c7
3 tập tin đã thay đổi với 60 bổ sung4 xóa
  1. 24 1
      source/boxing/Integer.cpp
  2. 8 2
      source/boxing/Integer.hpp
  3. 28 1
      test/cases/boxing/IntegerTest.cpp

+ 24 - 1
source/boxing/Integer.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-08-16
+ * Changed:         2020-08-22
  *
  * */
 
@@ -247,6 +247,17 @@ void ls_std::Integer::operator--()
   this->value -= 1;
 }
 
+const ls_std::byte * ls_std::Integer::marshal()
+{
+  ls_std::byte* data {};
+
+  if(this->serializable != nullptr) {
+    data = (char*) this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Integer::parse(std::string _parseText)
 {
   this->value = std::stoi(_parseText);
@@ -257,6 +268,18 @@ std::string ls_std::Integer::toString()
   return std::to_string(this->value);
 }
 
+void ls_std::Integer::unmarshal(const ls_std::byte *_data)
+{
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 int ls_std::Integer::getValue() const {
   return this->value;
 }
+
+void ls_std::Integer::setSerializable(std::shared_ptr<ISerializable> _serializable)
+{
+  this->serializable = std::move(_serializable);
+}

+ 8 - 2
source/boxing/Integer.hpp

@@ -3,18 +3,20 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-08-16
+ * Changed:         2020-08-22
  *
  * */
 
 #ifndef INTEGER_HPP
 #define INTEGER_HPP
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
+#include "../serialization/ISerializable.hpp"
 
 namespace ls_std {
-  class Integer : public Class, IBoxing {
+  class Integer : public Class, public IBoxing, public ISerializable {
     public:
 
       explicit Integer(int _value);
@@ -102,16 +104,20 @@ namespace ls_std {
 
       // implementation
 
+      const ls_std::byte* marshal() override;
       void parse(std::string _parseText) override;
       std::string toString() override;
+      void unmarshal(const ls_std::byte* _data) override;
 
       // additional functionality
 
       int getValue() const;
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
     private:
 
       int value {};
+      std::shared_ptr<ISerializable> serializable {};
   };
 }
 

+ 28 - 1
test/cases/boxing/IntegerTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2020-08-14
+ * Changed:         2020-08-22
  *
  * */
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Integer.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONInteger.hpp"
 
 namespace {
   class IntegerTest : public ::testing::Test {
@@ -349,6 +350,19 @@ namespace {
 
   // implementation
 
+  TEST_F(IntegerTest, marshal)
+  {
+    std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(3);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal());
+
+    *x = 17;
+    ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal());
+  }
+
   TEST_F(IntegerTest, parse)
   {
     ls_std::Integer x {};
@@ -366,6 +380,19 @@ namespace {
     ASSERT_STREQ("112", x.toString().c_str());
   }
 
+  TEST_F(IntegerTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(13);
+    ASSERT_EQ(13, *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    x->unmarshal(R"({"class":"Integer","value":1989})");
+
+    ASSERT_EQ(1989, *x);
+  }
+
   // additional functionality
 
   TEST_F(IntegerTest, getValue)