Răsfoiți Sursa

Extended Integer class (unstable)

- implemented IStorable interface for Integer class
- extended tests for Integer class
Patrick 4 ani în urmă
părinte
comite
2f15eef925
3 a modificat fișierele cu 62 adăugiri și 1 ștergeri
  1. 28 0
      source/boxing/Integer.cpp
  2. 6 1
      source/boxing/Integer.hpp
  3. 28 0
      test/cases/boxing/IntegerTest.cpp

+ 28 - 0
source/boxing/Integer.cpp

@@ -247,6 +247,18 @@ void ls_std::Integer::operator--()
   this->value -= 1;
 }
 
+ls_std::byte * ls_std::Integer::load()
+{
+  ls_std::byte* data {};
+
+  if(this->storable != nullptr && this->serializable != nullptr) {
+    data = this->storable->load();
+    this->serializable->unmarshal(data);
+  }
+
+  return data;
+}
+
 const ls_std::byte * ls_std::Integer::marshal()
 {
   ls_std::byte* data {};
@@ -263,6 +275,17 @@ void ls_std::Integer::parse(std::string _parseText)
   this->value = std::stoi(_parseText);
 }
 
+void ls_std::Integer::save(ls_std::byte *_data)
+{
+  if(this->serializable != nullptr) {
+    if(_data == nullptr) {
+      this->storable->save((char *) this->serializable->marshal());
+    } else {
+      this->storable->save(_data);
+    }
+  }
+}
+
 std::string ls_std::Integer::toString()
 {
   return std::to_string(this->value);
@@ -283,3 +306,8 @@ void ls_std::Integer::setSerializable(std::shared_ptr<ISerializable> _serializab
 {
   this->serializable = std::move(_serializable);
 }
+
+void ls_std::Integer::setStorable(std::shared_ptr<IStorable> _storable)
+{
+  this->storable = std::move(_storable);
+}

+ 6 - 1
source/boxing/Integer.hpp

@@ -14,9 +14,10 @@
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 
 namespace ls_std {
-  class Integer : public Class, public IBoxing, public ISerializable {
+  class Integer : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       explicit Integer(int _value);
@@ -104,8 +105,10 @@ namespace ls_std {
 
       // implementation
 
+      ls_std::byte* load() override;
       const ls_std::byte* marshal() override;
       void parse(std::string _parseText) override;
+      void save(ls_std::byte* _data) override;
       std::string toString() override;
       void unmarshal(const ls_std::byte* _data) override;
 
@@ -113,11 +116,13 @@ namespace ls_std {
 
       int getValue() const;
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
 
     private:
 
       int value {};
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
   };
 }
 

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

@@ -10,6 +10,9 @@
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Integer.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONInteger.hpp"
+#include "../../../source/io/StorableFile.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/FileWriter.hpp"
 
 namespace {
   class IntegerTest : public ::testing::Test {
@@ -350,6 +353,31 @@ namespace {
 
   // implementation
 
+  TEST_F(IntegerTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_integer.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"class":"Integer","value":1990})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    auto storable = std::make_shared<ls_std::StorableFile>(path);
+    x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
+
+    // check
+
+    x->load();
+    ASSERT_EQ(1990, *x);
+
+    file.remove();
+  }
+
   TEST_F(IntegerTest, marshal)
   {
     std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(3);