DataController.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "../../Include/RmlUi/Core/Header.h"
  3. #include "../../Include/RmlUi/Core/Traits.h"
  4. #include "../../Include/RmlUi/Core/Types.h"
  5. namespace Rml {
  6. class Element;
  7. class DataModel;
  8. class DataControllerInstancer : public NonCopyMoveable {
  9. public:
  10. DataControllerInstancer() {}
  11. virtual ~DataControllerInstancer() {}
  12. virtual DataControllerPtr InstanceController(Element* element) = 0;
  13. };
  14. template <typename T>
  15. class DataControllerInstancerDefault final : public DataControllerInstancer {
  16. public:
  17. DataControllerPtr InstanceController(Element* element) override { return DataControllerPtr(new T(element)); }
  18. };
  19. /**
  20. Data controller.
  21. Data controllers are used to respond to some change in the document,
  22. usually by setting data variables. Such document changes are usually
  23. a result of user input.
  24. A data controller is declared in the document by the element attribute:
  25. data-[type]-[modifier]="[assignment_expression]"
  26. This is similar to declaration of data views, except that controllers
  27. instead take an assignment expression to set a variable. Note that, as
  28. opposed to views, controllers only respond to certain changes in the
  29. document, not to changed data variables.
  30. The modifier may or may not be required depending on the data controller.
  31. */
  32. class DataController : public Releasable {
  33. public:
  34. virtual ~DataController();
  35. // Initialize the data controller.
  36. // @param[in] model The data model the controller will be attached to.
  37. // @param[in] element The element which spawned the controller.
  38. // @param[in] expression The value of the element's 'data-' attribute which spawned the controller (see above).
  39. // @param[in] modifier The modifier for the given controller type (see above).
  40. // @return True on success.
  41. virtual bool Initialize(DataModel& model, Element* element, const String& expression, const String& modifier) = 0;
  42. // Returns the attached element if it still exists.
  43. Element* GetElement() const;
  44. // Returns true if the element still exists.
  45. bool IsValid() const;
  46. protected:
  47. DataController(Element* element);
  48. private:
  49. ObserverPtr<Element> attached_element;
  50. };
  51. class DataControllers : NonCopyMoveable {
  52. public:
  53. DataControllers();
  54. ~DataControllers();
  55. void Add(DataControllerPtr controller);
  56. void OnElementRemove(Element* element);
  57. private:
  58. using ElementControllersMap = UnorderedMultimap<Element*, DataControllerPtr>;
  59. ElementControllersMap controllers;
  60. };
  61. } // namespace Rml