SerializableJsonFactory.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2021-04-24
  6. * Changed: 2021-07-08
  7. *
  8. * */
  9. #include <ls_std/factory/serialization/json/SerializableJsonFactory.hpp>
  10. #include <ls_std/boxing/Boolean.hpp>
  11. #include <ls_std/boxing/Double.hpp>
  12. #include <ls_std/boxing/Float.hpp>
  13. #include <ls_std/boxing/Integer.hpp>
  14. #include <ls_std/boxing/Long.hpp>
  15. #include <ls_std/boxing/String.hpp>
  16. #include <ls_std/factory/serialization/json/boxing/SerializableJsonBooleanFactory.hpp>
  17. #include <ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.hpp>
  18. #include <ls_std/factory/serialization/json/boxing/SerializableJsonFloatFactory.hpp>
  19. #include <ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp>
  20. #include <ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.hpp>
  21. #include <ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.hpp>
  22. #include <ls_std/exception/IllegalArgumentException.hpp>
  23. #include <ls_std/exception/NullPointerException.hpp>
  24. ls_std::SerializableJsonFactory::SerializableJsonFactory() : ls_std::Class("SerializableJsonFactory")
  25. {
  26. this->_init();
  27. }
  28. bool ls_std::SerializableJsonFactory::addFactory(const std::pair<std::string, std::shared_ptr<ls_std::IFactory>> &_factoryInsertion)
  29. {
  30. bool factoryAdded{};
  31. if (_factoryInsertion.first.empty())
  32. {
  33. throw ls_std::IllegalArgumentException{};
  34. }
  35. if (_factoryInsertion.second == nullptr)
  36. {
  37. throw ls_std::NullPointerException{};
  38. }
  39. if (!this->_hasFactory(_factoryInsertion.first))
  40. {
  41. factoryAdded = this->factories.insert(_factoryInsertion).second;
  42. }
  43. return factoryAdded;
  44. }
  45. std::shared_ptr<ls_std::Class> ls_std::SerializableJsonFactory::build(const std::string &_relatedObjectName)
  46. {
  47. std::shared_ptr<ls_std::Class> requestedBuiltObject{};
  48. if (this->_hasFactory(_relatedObjectName))
  49. {
  50. requestedBuiltObject = this->factories.at(_relatedObjectName)->build();
  51. }
  52. return requestedBuiltObject;
  53. }
  54. bool ls_std::SerializableJsonFactory::clear()
  55. {
  56. this->factories.clear();
  57. return factories.empty();
  58. }
  59. bool ls_std::SerializableJsonFactory::hasFactory(const std::string &_relatedObjectName)
  60. {
  61. return this->_hasFactory(_relatedObjectName);
  62. }
  63. bool ls_std::SerializableJsonFactory::removeFactory(const std::string &_relatedObjectName)
  64. {
  65. bool wasRemoved{};
  66. if (this->_hasFactory(_relatedObjectName))
  67. {
  68. this->factories.erase(_relatedObjectName);
  69. wasRemoved = !this->_hasFactory(_relatedObjectName);
  70. }
  71. return wasRemoved;
  72. }
  73. bool ls_std::SerializableJsonFactory::_hasFactory(const std::string &_relatedObjectName)
  74. {
  75. return this->factories.find(_relatedObjectName) != this->factories.end();
  76. }
  77. void ls_std::SerializableJsonFactory::_init()
  78. {
  79. this->factories.insert({ls_std::Double{}.getClassName(), std::make_shared<ls_std::SerializableJsonDoubleFactory>()});
  80. this->factories.insert({ls_std::Float{}.getClassName(), std::make_shared<ls_std::SerializableJsonFloatFactory>()});
  81. this->factories.insert({ls_std::Integer{}.getClassName(), std::make_shared<ls_std::SerializableJsonIntegerFactory>()});
  82. this->factories.insert({ls_std::Long{}.getClassName(), std::make_shared<ls_std::SerializableJsonLongFactory>()});
  83. this->factories.insert({ls_std::String{}.getClassName(), std::make_shared<ls_std::SerializableJsonStringFactory>()});
  84. }