DataController.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_DATACONTROLLER_H
  29. #define RMLUI_CORE_DATACONTROLLER_H
  30. #include "../../Include/RmlUi/Core/Header.h"
  31. #include "../../Include/RmlUi/Core/Traits.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. namespace Rml {
  34. class Element;
  35. class DataModel;
  36. class DataControllerInstancer : public NonCopyMoveable {
  37. public:
  38. DataControllerInstancer() {}
  39. virtual ~DataControllerInstancer() {}
  40. virtual DataControllerPtr InstanceController(Element* element) = 0;
  41. };
  42. template <typename T>
  43. class DataControllerInstancerDefault final : public DataControllerInstancer {
  44. public:
  45. DataControllerPtr InstanceController(Element* element) override { return DataControllerPtr(new T(element)); }
  46. };
  47. /**
  48. Data controller.
  49. Data controllers are used to respond to some change in the document,
  50. usually by setting data variables. Such document changes are usually
  51. a result of user input.
  52. A data controller is declared in the document by the element attribute:
  53. data-[type]-[modifier]="[assignment_expression]"
  54. This is similar to declaration of data views, except that controllers
  55. instead take an assignment expression to set a variable. Note that, as
  56. opposed to views, controllers only respond to certain changes in the
  57. document, not to changed data variables.
  58. The modifier may or may not be required depending on the data controller.
  59. */
  60. class DataController : public Releasable {
  61. public:
  62. virtual ~DataController();
  63. // Initialize the data controller.
  64. // @param[in] model The data model the controller will be attached to.
  65. // @param[in] element The element which spawned the controller.
  66. // @param[in] expression The value of the element's 'data-' attribute which spawned the controller (see above).
  67. // @param[in] modifier The modifier for the given controller type (see above).
  68. // @return True on success.
  69. virtual bool Initialize(DataModel& model, Element* element, const String& expression, const String& modifier) = 0;
  70. // Returns the attached element if it still exists.
  71. Element* GetElement() const;
  72. // Returns true if the element still exists.
  73. bool IsValid() const;
  74. protected:
  75. DataController(Element* element);
  76. private:
  77. ObserverPtr<Element> attached_element;
  78. };
  79. class DataControllers : NonCopyMoveable {
  80. public:
  81. DataControllers();
  82. ~DataControllers();
  83. void Add(DataControllerPtr controller);
  84. void OnElementRemove(Element* element);
  85. private:
  86. using ElementControllersMap = UnorderedMultimap<Element*, DataControllerPtr>;
  87. ElementControllersMap controllers;
  88. };
  89. } // namespace Rml
  90. #endif