Browse Source

Extended Long class

- implemented ISerializable interface
- extended tests of Long class
pcmattulat 4 years ago
parent
commit
fc21d875f5
3 changed files with 61 additions and 4 deletions
  1. 25 1
      source/boxing/Long.cpp
  2. 8 2
      source/boxing/Long.hpp
  3. 28 1
      test/cases/boxing/LongTest.cpp

+ 25 - 1
source/boxing/Long.cpp

@@ -3,11 +3,13 @@
  * Company:         Lynar Studios
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  *
  * */
  * */
 
 
 #include "Long.hpp"
 #include "Long.hpp"
+
+#include <utility>
 #include "../exception/IllegalArithmeticOperationException.hpp"
 #include "../exception/IllegalArithmeticOperationException.hpp"
 
 
 ls_std::Long::Long(ls_std::long_type _value) : Class("Long"),
 ls_std::Long::Long(ls_std::long_type _value) : Class("Long"),
@@ -247,6 +249,17 @@ void ls_std::Long::operator--()
   this->value -= 1;
   this->value -= 1;
 }
 }
 
 
+ls_std::byte_field ls_std::Long::marshal()
+{
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Long::parse(std::string _parseText)
 void ls_std::Long::parse(std::string _parseText)
 {
 {
   this->value = std::stoi(_parseText);
   this->value = std::stoi(_parseText);
@@ -257,6 +270,17 @@ std::string ls_std::Long::toString()
   return std::to_string(this->value);
   return std::to_string(this->value);
 }
 }
 
 
+void ls_std::Long::unmarshal(const ls_std::byte_field& _data)
+{
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 ls_std::long_type ls_std::Long::getValue() const {
 ls_std::long_type ls_std::Long::getValue() const {
   return this->value;
   return this->value;
 }
 }
+
+void ls_std::Long::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}

+ 8 - 2
source/boxing/Long.hpp

@@ -3,19 +3,21 @@
  * Company:         Lynar Studios
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  *
  * */
  * */
 
 
 #ifndef LONG_HPP
 #ifndef LONG_HPP
 #define LONG_HPP
 #define LONG_HPP
 
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "IBoxing.hpp"
 #include "../base/Types.hpp"
 #include "../base/Types.hpp"
+#include "../serialization/ISerializable.hpp"
 
 
 namespace ls_std {
 namespace ls_std {
-  class Long : public Class, IBoxing {
+  class Long : public Class, public IBoxing, public ISerializable {
     public:
     public:
 
 
       explicit Long(ls_std::long_type _value);
       explicit Long(ls_std::long_type _value);
@@ -90,15 +92,19 @@ namespace ls_std {
 
 
       // implementation
       // implementation
 
 
+      ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
       void parse(std::string _parseText) override;
       std::string toString() override;
       std::string toString() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
 
 
       // additional functionality
       // additional functionality
 
 
       ls_std::long_type getValue() const;
       ls_std::long_type getValue() const;
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
 
     private:
     private:
 
 
+      std::shared_ptr<ISerializable> serializable {};
       ls_std::long_type value {};
       ls_std::long_type value {};
   };
   };
 }
 }

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

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
  * Created:         2020-08-17
- * Changed:         2020-08-26
+ * Changed:         2020-08-27
  *
  *
  * */
  * */
 
 
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Long.hpp"
 #include "../../../source/boxing/Long.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONLong.hpp"
 
 
 namespace {
 namespace {
   class LongTest : public ::testing::Test {
   class LongTest : public ::testing::Test {
@@ -337,6 +338,19 @@ namespace {
 
 
   // implementation
   // implementation
 
 
+  TEST_F(LongTest, marshal)
+  {
+    std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(3);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"class":"Long","value":3})", x->marshal().c_str());
+
+    *x = 17;
+    ASSERT_STREQ(R"({"class":"Long","value":17})", x->marshal().c_str());
+  }
+
   TEST_F(LongTest, parse)
   TEST_F(LongTest, parse)
   {
   {
     ls_std::Long x {};
     ls_std::Long x {};
@@ -354,6 +368,19 @@ namespace {
     ASSERT_STREQ("112", x.toString().c_str());
     ASSERT_STREQ("112", x.toString().c_str());
   }
   }
 
 
+  TEST_F(LongTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(13);
+    ASSERT_EQ(13, *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    x->unmarshal(R"({"class":"Long","value":1989})");
+
+    ASSERT_EQ(1989, *x);
+  }
+
   // additional functionality
   // additional functionality
 
 
   TEST_F(LongTest, getValue)
   TEST_F(LongTest, getValue)