registry.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* registry.h - canvas registry & non-template tool/editor add functions
  2. * Copyright (C) 2017-2018 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 "abstract_editor.h"
  23. #include "tool.h"
  24. namespace rainynite::studio {
  25. class CanvasTool;
  26. class AbstractCanvasEditor;
  27. struct CanvasToolsInfo {
  28. virtual vector<AbstractFactory<CanvasTool>*> operator()() const = 0;
  29. };
  30. struct AbstractCanvasEditorsInfo {
  31. virtual unique_ptr<AbstractCanvasEditor> operator()(Type type) const = 0;
  32. };
  33. struct AbstractCanvasNodeEditorsInfo {
  34. virtual unique_ptr<AbstractCanvasEditor> operator()(std::type_index node_type) const = 0;
  35. };
  36. template <class CanvasT>
  37. struct CanvasToolsInfoInstance :
  38. public CanvasToolsInfo,
  39. private class_init::Registered<
  40. CanvasToolsInfoInstance<CanvasT>,
  41. CanvasT,
  42. CanvasToolsInfo
  43. >
  44. {
  45. vector<AbstractFactory<CanvasTool>*> operator()() const override {
  46. auto factory_list = get_canvas_tools<CanvasT>();
  47. vector<AbstractFactory<CanvasTool>*> result;
  48. std::sort(
  49. factory_list.begin(),
  50. factory_list.end(),
  51. [](auto a, auto b) {
  52. return a->priority() < b->priority();
  53. }
  54. );
  55. std::transform(
  56. factory_list.begin(),
  57. factory_list.end(),
  58. std::back_inserter(result),
  59. [](auto const& e) { return e; }
  60. );
  61. return result;
  62. }
  63. };
  64. template <class CanvasT>
  65. struct AbstractCanvasEditorsInfoInstance :
  66. public AbstractCanvasEditorsInfo,
  67. private class_init::Registered<
  68. AbstractCanvasEditorsInfoInstance<CanvasT>,
  69. CanvasT,
  70. AbstractCanvasEditorsInfo
  71. >
  72. {
  73. unique_ptr<AbstractCanvasEditor> operator()(Type type) const override {
  74. return make_canvas_editor<CanvasT>(type);
  75. }
  76. };
  77. template <class CanvasT>
  78. struct AbstractCanvasNodeEditorsInfoInstance :
  79. public AbstractCanvasNodeEditorsInfo,
  80. private class_init::Registered<
  81. AbstractCanvasNodeEditorsInfoInstance<CanvasT>,
  82. CanvasT,
  83. AbstractCanvasNodeEditorsInfo
  84. >
  85. {
  86. unique_ptr<AbstractCanvasEditor> operator()(std::type_index node_type) const override {
  87. return make_canvas_node_editor<CanvasT>(node_type);
  88. }
  89. };
  90. #define REGISTER_CANVAS(CanvasT) \
  91. template struct CanvasToolsInfoInstance<CanvasT>; \
  92. template struct AbstractCanvasEditorsInfoInstance<CanvasT>; \
  93. template struct AbstractCanvasNodeEditorsInfoInstance<CanvasT>
  94. inline vector<AbstractFactory<CanvasTool>*> get_canvas_tools_by_type(Type type) {
  95. return class_init::type_info<CanvasToolsInfo,vector<AbstractFactory<CanvasTool>*>>(type);
  96. }
  97. inline unique_ptr<AbstractCanvasEditor> make_canvas_editor_for(AbstractCanvas const& canvas, Type type) {
  98. return class_init::type_info<AbstractCanvasEditorsInfo,unique_ptr<AbstractCanvasEditor>>(typeid(canvas), type);
  99. }
  100. inline unique_ptr<AbstractCanvasEditor> make_canvas_node_editor_for(AbstractCanvas const& canvas, std::type_index node_type) {
  101. return class_init::type_info<AbstractCanvasNodeEditorsInfo,unique_ptr<AbstractCanvasEditor>>(typeid(canvas), node_type);
  102. }
  103. } // namespace rainynite::studio
  104. #endif