registry.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* registry.h - canvas registry & non-template tool/editor add functions
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_CANVAS_REGISTRY_H_20662128_B848_5B8E_B4FC_80D315C1A843
  18. #define STUDIO_CANVAS_REGISTRY_H_20662128_B848_5B8E_B4FC_80D315C1A843
  19. #include <core/std/memory.h>
  20. #include <core/std/vector.h>
  21. #include "abstract_canvas.h"
  22. #include "editor.h"
  23. #include "tool.h"
  24. namespace rainynite::studio {
  25. class CanvasTool;
  26. class CanvasEditor;
  27. struct CanvasToolsInfo {
  28. virtual vector<AbstractFactory<CanvasTool>*> operator()() const = 0;
  29. };
  30. struct CanvasEditorsInfo {
  31. virtual unique_ptr<CanvasEditor> operator()(Type type) const = 0;
  32. };
  33. template <class CanvasT>
  34. struct CanvasToolsInfoInstance :
  35. public CanvasToolsInfo,
  36. private class_init::Registered<
  37. CanvasToolsInfoInstance<CanvasT>,
  38. CanvasT,
  39. CanvasToolsInfo
  40. >
  41. {
  42. vector<AbstractFactory<CanvasTool>*> operator()() const override {
  43. auto const& factory_map = get_canvas_tools<CanvasT>();
  44. vector<AbstractFactory<CanvasTool>*> result;
  45. std::transform(
  46. factory_map.begin(),
  47. factory_map.end(),
  48. std::back_inserter(result),
  49. [](auto const& e) {
  50. return e.second;
  51. }
  52. );
  53. return result;
  54. }
  55. };
  56. template <class CanvasT>
  57. struct CanvasEditorsInfoInstance :
  58. public CanvasEditorsInfo,
  59. private class_init::Registered<
  60. CanvasEditorsInfoInstance<CanvasT>,
  61. CanvasT,
  62. CanvasEditorsInfo
  63. >
  64. {
  65. unique_ptr<CanvasEditor> operator()(Type type) const override {
  66. return make_canvas_editor<CanvasT>(type);
  67. }
  68. };
  69. #define REGISTER_CANVAS(CanvasT) \
  70. template struct CanvasToolsInfoInstance<CanvasT>; \
  71. template struct CanvasEditorsInfoInstance<CanvasT>
  72. inline vector<AbstractFactory<CanvasTool>*> get_canvas_tools_by_type(Type type) {
  73. return class_init::type_info<CanvasToolsInfo,vector<AbstractFactory<CanvasTool>*>>(type);
  74. }
  75. inline unique_ptr<CanvasEditor> make_canvas_editor_for(AbstractCanvas const& canvas, Type type) {
  76. return class_init::type_info<CanvasEditorsInfo,unique_ptr<CanvasEditor>>(typeid(canvas), type);
  77. }
  78. } // namespace rainynite::studio
  79. #endif