SerializableFactory.cpp 2.2 KB

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