Selaa lähdekoodia

Reduce polymorphism of Double class

- remove ISerializable extension
- remove IStorable extension
- adjust tests for Double class
Patrick-Christopher Mattulat 3 vuotta sitten
vanhempi
commit
1076485a8f

+ 0 - 2
CMakeLists.txt

@@ -97,7 +97,6 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KvParser.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KvReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonFloatFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.cpp
@@ -163,7 +162,6 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvParserTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvReaderTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/SerializableFactoryTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonDoubleFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonFloatFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonIntegerFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonLongFactoryTest.cpp

+ 2 - 14
include/ls_std/boxing/Double.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-01
+ * Changed:         2021-07-12
  *
  * */
 
@@ -13,12 +13,10 @@
 #include <memory>
 #include <ls_std/base/Class.hpp>
 #include "IBoxing.hpp"
-#include <ls_std/serialization/ISerializable.hpp>
-#include <ls_std/io/IStorable.hpp>
 
 namespace ls_std
 {
-  class Double : public ls_std::Class, public ls_std::IBoxing, public ls_std::ISerializable, public ls_std::IStorable
+  class Double : public ls_std::Class, public ls_std::IBoxing
   {
     public:
 
@@ -79,31 +77,21 @@ 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;
 
       // additional functionality
 
       double getEpsilon();
       double getValue();
       void setEpsilon(double _epsilon);
-      void setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
-      void setStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
 
     private:
 
       double epsilon{};
-      std::shared_ptr<ls_std::ISerializable> serializable{};
-      std::shared_ptr<ls_std::IStorable> storable{};
       double value{};
 
       void _assignEpsilon(double _epsilon);
-      void _assignSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
-      void _assignStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
   };
 }
 

+ 0 - 29
include/ls_std/factory/serialization/json/boxing/SerializableJsonBooleanFactory.hpp

@@ -1,29 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-23
- * Changed:         2021-05-02
- *
- * */
-
-#ifndef LS_STD_SERIALIZABLE_JSON_BOOLEAN_FACTORY_HPP
-#define LS_STD_SERIALIZABLE_JSON_BOOLEAN_FACTORY_HPP
-
-#include <ls_std/factory/IFactory.hpp>
-#include <memory>
-
-namespace ls_std
-{
-  class SerializableJsonBooleanFactory : public ls_std::Class, public ls_std::IFactory
-  {
-    public:
-
-      SerializableJsonBooleanFactory();
-      ~SerializableJsonBooleanFactory() override = default;
-
-      std::shared_ptr<ls_std::Class> build() override;
-  };
-}
-
-#endif

+ 0 - 29
include/ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.hpp

@@ -1,29 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-24
- * Changed:         2021-05-02
- *
- * */
-
-#ifndef LS_STD_SERIALIZABLE_JSON_DOUBLE_FACTORY_HPP
-#define LS_STD_SERIALIZABLE_JSON_DOUBLE_FACTORY_HPP
-
-#include <ls_std/factory/IFactory.hpp>
-#include <memory>
-
-namespace ls_std
-{
-  class SerializableJsonDoubleFactory : public ls_std::Class, public ls_std::IFactory
-  {
-    public:
-
-      SerializableJsonDoubleFactory();
-      ~SerializableJsonDoubleFactory() override = default;
-
-      std::shared_ptr<ls_std::Class> build() override;
-  };
-}
-
-#endif

+ 1 - 3
include/ls_std/ls_std.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-10-29
- * Changed:         2021-05-27
+ * Changed:         2021-07-12
  *
  * */
 
@@ -97,8 +97,6 @@
 
 #include "factory/IFactory.hpp"
 #include "ls_std/factory/serialization/json/SerializableJsonFactory.hpp"
-#include "factory/serialization/json/boxing/SerializableJsonBooleanFactory.hpp"
-#include "factory/serialization/json/boxing/SerializableJsonDoubleFactory.hpp"
 #include "factory/serialization/json/boxing/SerializableJsonFloatFactory.hpp"
 #include "factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp"
 #include "factory/serialization/json/boxing/SerializableJsonLongFactory.hpp"

+ 1 - 79
source/ls_std/boxing/Double.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-01
+ * Changed:         2021-07-12
  *
  * */
 
@@ -197,64 +197,16 @@ void ls_std::Double::operator--()
   this->value -= 1.0f;
 }
 
-ls_std::byte_field ls_std::Double::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::Double::marshal()
-{
-  ls_std::byte_field data{};
-
-  if (this->serializable != nullptr)
-  {
-    data = this->serializable->marshal();
-  }
-
-  return data;
-}
-
 void ls_std::Double::parse(std::string _parseText)
 {
   this->value = std::stod(_parseText);
 }
 
-void ls_std::Double::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::Double::toString()
 {
   return std::to_string(this->value);
 }
 
-void ls_std::Double::unmarshal(const ls_std::byte_field &_data)
-{
-  if (this->serializable != nullptr)
-  {
-    this->serializable->unmarshal(_data);
-  }
-}
-
 double ls_std::Double::getEpsilon()
 {
   return this->epsilon;
@@ -270,16 +222,6 @@ void ls_std::Double::setEpsilon(double _epsilon)
   this->_assignEpsilon(_epsilon);
 }
 
-void ls_std::Double::setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable)
-{
-  this->_assignSerializable(_serializable);
-}
-
-void ls_std::Double::setStorable(const std::shared_ptr<ls_std::IStorable>& _storable)
-{
-  this->_assignStorable(_storable);
-}
-
 void ls_std::Double::_assignEpsilon(double _epsilon)
 {
   if (_epsilon <= 0.0)
@@ -289,23 +231,3 @@ void ls_std::Double::_assignEpsilon(double _epsilon)
 
   this->epsilon = _epsilon;
 }
-
-void ls_std::Double::_assignSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable)
-{
-  if (_serializable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->serializable = _serializable;
-}
-
-void ls_std::Double::_assignStorable(const std::shared_ptr<ls_std::IStorable>& _storable)
-{
-  if (_storable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->storable = _storable;
-}

+ 1 - 6
source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp

@@ -3,19 +3,15 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2021-04-24
- * Changed:         2021-07-08
+ * Changed:         2021-07-12
  *
  * */
 
 #include <ls_std/factory/serialization/json/SerializableJsonFactory.hpp>
-#include <ls_std/boxing/Boolean.hpp>
-#include <ls_std/boxing/Double.hpp>
 #include <ls_std/boxing/Float.hpp>
 #include <ls_std/boxing/Integer.hpp>
 #include <ls_std/boxing/Long.hpp>
 #include <ls_std/boxing/String.hpp>
-#include <ls_std/factory/serialization/json/boxing/SerializableJsonBooleanFactory.hpp>
-#include <ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.hpp>
 #include <ls_std/factory/serialization/json/boxing/SerializableJsonFloatFactory.hpp>
 #include <ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp>
 #include <ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.hpp>
@@ -93,7 +89,6 @@ bool ls_std::SerializableJsonFactory::_hasFactory(const std::string &_relatedObj
 
 void ls_std::SerializableJsonFactory::_init()
 {
-  this->factories.insert({ls_std::Double{}.getClassName(), std::make_shared<ls_std::SerializableJsonDoubleFactory>()});
   this->factories.insert({ls_std::Float{}.getClassName(), std::make_shared<ls_std::SerializableJsonFloatFactory>()});
   this->factories.insert({ls_std::Integer{}.getClassName(), std::make_shared<ls_std::SerializableJsonIntegerFactory>()});
   this->factories.insert({ls_std::Long{}.getClassName(), std::make_shared<ls_std::SerializableJsonLongFactory>()});

+ 0 - 24
source/ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.cpp

@@ -1,24 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-24
- * Changed:         2021-05-02
- *
- * */
-
-#include <ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.hpp>
-#include <ls_std/boxing/Double.hpp>
-#include <ls_std/serialization/json/boxing/SerializableJsonDouble.hpp>
-
-ls_std::SerializableJsonDoubleFactory::SerializableJsonDoubleFactory() : ls_std::Class("SerializableJsonDoubleFactory")
-{}
-
-std::shared_ptr<ls_std::Class> ls_std::SerializableJsonDoubleFactory::build()
-{
-  std::shared_ptr<ls_std::Double> requestedObject = std::make_shared<ls_std::Double>();
-  std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonDouble>(requestedObject);
-  requestedObject->setSerializable(serializable);
-
-  return std::dynamic_pointer_cast<ls_std::Class>(requestedObject);
-}

+ 1 - 87
test/cases/boxing/DoubleTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-01
+ * Changed:         2021-07-12
  *
  * */
 
@@ -26,24 +26,6 @@ namespace
 
       void TearDown() override
       {}
-
-      static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Double>> createPersistentTestDouble()
-      {
-        std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>();
-        std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_double.json";
-        std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
-        file->createNewFile();
-        ls_std::FileWriter writer{*file};
-        writer.write(R"({"value":3.14159})");
-
-        auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
-        number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-        auto storable = std::make_shared<ls_std::StorableFile>(path);
-        number->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
-
-        return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Double>>(file, number);
-      }
   };
 
   // assignment operators
@@ -314,31 +296,6 @@ namespace
 
   // implementation
 
-  TEST_F(DoubleTest, load)
-  {
-    // preparation
-
-    auto storableDouble = createPersistentTestDouble();
-
-    // check
-
-    storableDouble.second->load();
-    ASSERT_DOUBLE_EQ(3.14159, *storableDouble.second);
-
-    storableDouble.first->remove();
-  }
-
-  TEST_F(DoubleTest, marshal)
-  {
-    std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>(3.14159);
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
-    number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-    ls_std::String jsonString{number->marshal()};
-
-    ASSERT_TRUE(jsonString.contains(R"({"value":3.14159)"));
-  }
-
   TEST_F(DoubleTest, parse_with_positive_value)
   {
     ls_std::Double x{};
@@ -361,17 +318,6 @@ namespace
     ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
   }
 
-  TEST_F(DoubleTest, unmarshal)
-  {
-    std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>(3.14159);
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
-    number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-    number->unmarshal(R"({"value":17.4132})");
-
-    ASSERT_DOUBLE_EQ(17.4132, *number);
-  }
-
   // additional functionality
 
   TEST_F(DoubleTest, getEpsilon)
@@ -409,36 +355,4 @@ namespace
                    }
                  }, ls_std::IllegalArgumentException);
   }
-
-  TEST_F(DoubleTest, setSerializable_no_reference)
-  {
-    ls_std::Double x{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     x.setSerializable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
-
-  TEST_F(DoubleTest, setStorable_no_reference)
-  {
-    ls_std::Double x{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     x.setStorable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
 }

+ 1 - 2
test/cases/factory/serialization/json/SerializableFactoryTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2021-04-24
- * Changed:         2021-07-08
+ * Changed:         2021-07-12
  *
  * */
 
@@ -104,7 +104,6 @@ namespace
   TEST_F(SerializableFactoryTest, hasFactory_ofBoxingPackage)
   {
     ls_std::SerializableJsonFactory serializableFactory{};
-    ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Double{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Float{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Integer{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Long{}.getClassName()));