Forráskód Böngészése

Extended Float class

- implemented IStorable interface
- extended tests for Float class
pcmattulat 4 éve
szülő
commit
169b8a8952
3 módosított fájl, 63 hozzáadás és 2 törlés
  1. 28 1
      source/boxing/Float.cpp
  2. 6 1
      source/boxing/Float.hpp
  3. 29 0
      test/cases/boxing/FloatTest.cpp

+ 28 - 1
source/boxing/Float.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-16
+ * Changed:         2020-09-04
  *
  * */
 
@@ -160,6 +160,18 @@ void ls_std::Float::operator--() {
   this->value -= 1.0f;
 }
 
+ls_std::byte_field ls_std::Float::load()
+{
+  ls_std::byte_field data {};
+
+  if(this->storable != nullptr && this->serializable != nullptr) {
+    data = this->storable->load();
+    this->serializable->unmarshal(data);
+  }
+
+  return data;
+}
+
 ls_std::byte_field ls_std::Float::marshal()
 {
   ls_std::byte_field data {};
@@ -175,6 +187,17 @@ void ls_std::Float::parse(std::string _parseText) {
   this->value = std::stof(_parseText);
 }
 
+void ls_std::Float::save(const ls_std::byte_field& _data)
+{
+  if(this->serializable != nullptr) {
+    if(_data.empty()) {
+      this->storable->save(this->serializable->marshal());
+    } else {
+      this->storable->save(_data);
+    }
+  }
+}
+
 std::string ls_std::Float::toString() {
   return std::to_string(this->value);
 }
@@ -201,3 +224,7 @@ void ls_std::Float::setEpsilon(float _epsilon) {
 void ls_std::Float::setSerializable(std::shared_ptr<ISerializable> _serializable) {
   this->serializable = std::move(_serializable);
 }
+
+void ls_std::Float::setStorable(std::shared_ptr<IStorable> _storable) {
+  this->storable = std::move(_storable);
+}

+ 6 - 1
source/boxing/Float.hpp

@@ -14,9 +14,10 @@
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 
 namespace ls_std {
-  class Float : public Class, public IBoxing, public ISerializable {
+  class Float : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       Float();
@@ -76,8 +77,10 @@ namespace ls_std {
 
       // implementation
 
+      ls_std::byte_field load() override;
       ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
+      void save(const ls_std::byte_field& _data) override;
       std::string toString() override;
       void unmarshal(const ls_std::byte_field& _data) override;
 
@@ -87,10 +90,12 @@ namespace ls_std {
       float getValue();
       void setEpsilon(float _epsilon);
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
 
     private:
 
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
 
       float value {};
       float epsilon {};

+ 29 - 0
test/cases/boxing/FloatTest.cpp

@@ -11,6 +11,10 @@
 #include "../../../source/boxing/Float.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONFloat.hpp"
 #include "../../../source/boxing/String.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/File.hpp"
+#include "../../../source/io/FileWriter.hpp"
+#include "../../../source/io/StorableFile.hpp"
 
 namespace {
   class FloatTest : public ::testing::Test {
@@ -270,6 +274,31 @@ namespace {
 
   // implementation
 
+  TEST_F(FloatTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::Float> x = std::make_shared<ls_std::Float>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_float.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"value":3.14159})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONFloat>(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_FLOAT_EQ(3.14159f, *x);
+
+    file.remove();
+  }
+
   TEST_F(FloatTest, marshal)
   {
     std::shared_ptr<ls_std::Float> x = std::make_shared<ls_std::Float>(3.14159f);