SerializableJsonFactory.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-12
  7. *
  8. * */
  9. #include <ls_std/factory/serialization/json/SerializableJsonFactory.hpp>
  10. #include <ls_std/boxing/Float.hpp>
  11. #include <ls_std/boxing/Integer.hpp>
  12. #include <ls_std/boxing/Long.hpp>
  13. #include <ls_std/boxing/String.hpp>
  14. #include <ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp>
  15. #include <ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.hpp>
  16. #include <ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.hpp>
  17. #include <ls_std/exception/IllegalArgumentException.hpp>
  18. #include <ls_std/exception/NullPointerException.hpp>
  19. ls_std::SerializableJsonFactory::SerializableJsonFactory() : ls_std::Class("SerializableJsonFactory")
  20. {
  21. this->_init();
  22. }
  23. bool ls_std::SerializableJsonFactory::addFactory(const std::pair<std::string, std::shared_ptr<ls_std::IFactory>> &_factoryInsertion)
  24. {
  25. bool factoryAdded{};
  26. if (_factoryInsertion.first.empty())
  27. {
  28. throw ls_std::IllegalArgumentException{};
  29. }
  30. if (_factoryInsertion.second == nullptr)
  31. {
  32. throw ls_std::NullPointerException{};
  33. }
  34. if (!this->_hasFactory(_factoryInsertion.first))
  35. {
  36. factoryAdded = this->factories.insert(_factoryInsertion).second;
  37. }
  38. return factoryAdded;
  39. }
  40. std::shared_ptr<ls_std::Class> ls_std::SerializableJsonFactory::build(const std::string &_relatedObjectName)
  41. {
  42. std::shared_ptr<ls_std::Class> requestedBuiltObject{};
  43. if (this->_hasFactory(_relatedObjectName))
  44. {
  45. requestedBuiltObject = this->factories.at(_relatedObjectName)->build();
  46. }
  47. return requestedBuiltObject;
  48. }
  49. bool ls_std::SerializableJsonFactory::clear()
  50. {
  51. this->factories.clear();
  52. return factories.empty();
  53. }
  54. bool ls_std::SerializableJsonFactory::hasFactory(const std::string &_relatedObjectName)
  55. {
  56. return this->_hasFactory(_relatedObjectName);
  57. }
  58. bool ls_std::SerializableJsonFactory::removeFactory(const std::string &_relatedObjectName)
  59. {
  60. bool wasRemoved{};
  61. if (this->_hasFactory(_relatedObjectName))
  62. {
  63. this->factories.erase(_relatedObjectName);
  64. wasRemoved = !this->_hasFactory(_relatedObjectName);
  65. }
  66. return wasRemoved;
  67. }
  68. bool ls_std::SerializableJsonFactory::_hasFactory(const std::string &_relatedObjectName)
  69. {
  70. return this->factories.find(_relatedObjectName) != this->factories.end();
  71. }
  72. void ls_std::SerializableJsonFactory::_init()
  73. {
  74. this->factories.insert({ls_std::Integer{}.getClassName(), std::make_shared<ls_std::SerializableJsonIntegerFactory>()});
  75. this->factories.insert({ls_std::Long{}.getClassName(), std::make_shared<ls_std::SerializableJsonLongFactory>()});
  76. this->factories.insert({ls_std::String{}.getClassName(), std::make_shared<ls_std::SerializableJsonStringFactory>()});
  77. }