SerializableJsonFactory.cpp 2.6 KB

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