DataView.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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_DATAVIEW_H
  29. #define RMLUI_CORE_DATAVIEW_H
  30. #include "../../Include/RmlUi/Core/Header.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. #include "../../Include/RmlUi/Core/Traits.h"
  33. #include "../../Include/RmlUi/Core/DataTypes.h"
  34. namespace Rml {
  35. class Element;
  36. class DataModel;
  37. class DataViewInstancer : public NonCopyMoveable {
  38. public:
  39. DataViewInstancer() {}
  40. virtual ~DataViewInstancer() {}
  41. virtual DataViewPtr InstanceView(Element* element) = 0;
  42. };
  43. template<typename T>
  44. class DataViewInstancerDefault final : public DataViewInstancer {
  45. public:
  46. DataViewPtr InstanceView(Element* element) override {
  47. return DataViewPtr(new T(element));
  48. }
  49. };
  50. /**
  51. Data view.
  52. Data views are used to present a data variable in the document by different means.
  53. A data view is declared in the document by the element attribute:
  54. data-[type]-[modifier]="[expression]"
  55. The modifier may or may not be required depending on the data view.
  56. */
  57. class DataView : public Releasable {
  58. public:
  59. virtual ~DataView();
  60. // Initialize the data view.
  61. // @param[in] model The data model the view will be attached to.
  62. // @param[in] element The element which spawned the view.
  63. // @param[in] expression The value of the element's 'data-' attribute which spawned the view (see above).
  64. // @param[in] modifier_or_inner_rml The modifier for the given view type (see above), or the inner rml contents for structural data views.
  65. // @return True on success.
  66. virtual bool Initialize(DataModel& model, Element* element, const String& expression, const String& modifier_or_inner_rml) = 0;
  67. // Update the data view.
  68. // Returns true if the update resulted in a document change.
  69. virtual bool Update(DataModel& model) = 0;
  70. // Returns the list of data variable name(s) which can modify this view.
  71. virtual StringList GetVariableNameList() const = 0;
  72. // Returns the attached element if it still exists.
  73. Element* GetElement() const;
  74. // Data views are first sorted by the depth of the attached element in the
  75. // document tree, then optionally by an offset specified for each data view.
  76. int GetSortOrder() const;
  77. // Returns true if the element still exists.
  78. bool IsValid() const;
  79. protected:
  80. // @param[in] element The element this data view is attached to.
  81. // @param[in] sort_offset A number [-1000, 999] specifying the update order of this
  82. // data view at the same tree depth, negative numbers are updated first.
  83. DataView(Element* element, int sort_offset);
  84. private:
  85. ObserverPtr<Element> attached_element;
  86. int sort_order;
  87. };
  88. class DataViews : NonCopyMoveable {
  89. public:
  90. DataViews();
  91. ~DataViews();
  92. void Add(DataViewPtr view);
  93. void OnElementRemove(Element* element);
  94. bool Update(DataModel& model, const DirtyVariables& dirty_variables);
  95. private:
  96. using DataViewList = Vector<DataViewPtr>;
  97. DataViewList views;
  98. DataViewList views_to_add;
  99. DataViewList views_to_remove;
  100. using NameViewMap = UnorderedMultimap<String, DataView*>;
  101. NameViewMap name_view_map;
  102. };
  103. } // namespace Rml
  104. #endif