ui_factory.hpp 998 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef UI_FACTORY_HPP
  2. #define UI_FACTORY_HPP
  3. #include <vector>
  4. #include <memory>
  5. #include "simple.hpp"
  6. #include "implementaion.h"
  7. class ui_factory
  8. {
  9. std::vector<std::unique_ptr<i_ui_object>> elements;
  10. std::vector<i_graphic*> _graphics;
  11. std::vector<i_interactive*> _interactives;
  12. public:
  13. template <typename Element, typename ...Args>
  14. Element& make(Args&&... args)
  15. {
  16. auto element = std::make_unique<Element>(std::forward<Args>(args)...);
  17. Element& raw = *element.get();
  18. if constexpr (std::is_base_of_v<i_graphic, Element>)
  19. _graphics.push_back(&raw);
  20. if constexpr (std::is_base_of_v<i_interactive, Element>)
  21. _interactives.push_back(&raw);
  22. elements.push_back(std::move(element)); // assuming raw pointer will not change... that's safe right?
  23. return raw;
  24. }
  25. const std::vector<i_graphic*>& graphics() const noexcept
  26. { return _graphics; }
  27. const std::vector<i_interactive*>& interactives() const noexcept
  28. { return _interactives; }
  29. };
  30. #endif /* end of include guard */