DataBinding.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/DataModelHandle.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <doctest.h>
  34. #include <nanobench.h>
  35. using namespace Rml;
  36. using namespace ankerl;
  37. static const String document_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <link type="text/template" href="/assets/window.rml"/>
  43. <style>
  44. body.window
  45. {
  46. left: 50px;
  47. right: 50px;
  48. top: 30px;
  49. bottom: 30px;
  50. max-width: -1px;
  51. max-height: -1px;
  52. }
  53. div#content
  54. {
  55. text-align: left;
  56. padding: 50px;
  57. box-sizing: border-box;
  58. }
  59. </style>
  60. </head>
  61. <body template="window">
  62. <div data-model="basics">
  63. <h1>Globals</h1>
  64. <p id="i">0</p>
  65. <p id="i0">{{ i0 }}</p>
  66. <p id="i1">{{ i1 }}</p>
  67. <p id="i2">{{ i2 }}</p>
  68. <p id="i3">{{ i3 }}</p>
  69. <h1>Basic</h1>
  70. <p id="b_a">{{ basic.a }}</p>
  71. <p id="b_b">{{ basic.b }}</p>
  72. <p id="b_c">{{ basic.c.val }}</p>
  73. <h1>Arrays</h1>
  74. <p id="array"><span>10 </span><span>20 </span><span>30 </span></p>
  75. <p><span data-for="arrays.a">{{ it }} </span></p>
  76. <p><span data-for="arrays.b">{{ it }} </span></p>
  77. <p><span data-for="arrays.c">{{ it.val }} </span></p>
  78. <p><span data-for="arrays.d">{{ 'a: ' + it.a + ', b: ' + it.b + ', c: ' + it.c.val + ' :: ' }}</span></p>
  79. </div>
  80. </body>
  81. </rml>
  82. )";
  83. struct StringWrap {
  84. StringWrap(String val = "wrap_default") : val(val) {}
  85. String val;
  86. };
  87. struct Globals {
  88. int i0 = 0;
  89. int* i1 = new int(1);
  90. UniquePtr<int> i2 = MakeUnique<int>(2);
  91. SharedPtr<int> i3 = MakeShared<int>(3);
  92. } globals;
  93. struct Basic {
  94. int a = 1;
  95. int* b = new int(2);
  96. StringWrap* c = new StringWrap("basic.c");
  97. };
  98. struct Arrays {
  99. Vector<int> a = {10, 11, 12};
  100. Vector<int*> b = {new int(20), new int(21), new int(22)};
  101. Vector<StringWrap> c = {StringWrap("c1"), StringWrap("c2"), StringWrap("c3")};
  102. Vector<Basic> d = {Basic{10}, Basic{20}, Basic{30}};
  103. };
  104. static UniquePtr<Basic> basic;
  105. static UniquePtr<Arrays> arrays;
  106. static DataModelHandle InitializeDataBindings(Context* context)
  107. {
  108. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  109. if (!constructor)
  110. return DataModelHandle();
  111. if (auto handle = constructor.RegisterStruct<StringWrap>())
  112. handle.RegisterMember("val", &StringWrap::val);
  113. constructor.Bind("i0", &globals.i0);
  114. constructor.Bind("i1", &globals.i1);
  115. constructor.Bind("i2", &globals.i2);
  116. constructor.Bind("i3", &globals.i3);
  117. if (auto handle = constructor.RegisterStruct<Basic>())
  118. {
  119. handle.RegisterMember("a", &Basic::a);
  120. handle.RegisterMember("b", &Basic::b);
  121. handle.RegisterMember("c", &Basic::c);
  122. }
  123. basic = MakeUnique<Basic>();
  124. constructor.Bind("basic", basic.get());
  125. constructor.RegisterArray<decltype(Arrays::a)>();
  126. constructor.RegisterArray<decltype(Arrays::b)>();
  127. constructor.RegisterArray<decltype(Arrays::c)>();
  128. constructor.RegisterArray<decltype(Arrays::d)>();
  129. if (auto handle = constructor.RegisterStruct<Arrays>())
  130. {
  131. handle.RegisterMember("a", &Arrays::a);
  132. handle.RegisterMember("b", &Arrays::b);
  133. handle.RegisterMember("c", &Arrays::c);
  134. handle.RegisterMember("d", &Arrays::d);
  135. }
  136. arrays = MakeUnique<Arrays>();
  137. constructor.Bind("arrays", arrays.get());
  138. DataModelHandle model_handle = constructor.GetModelHandle();
  139. return model_handle;
  140. }
  141. TEST_CASE("data_binding")
  142. {
  143. Context* context = TestsShell::GetContext();
  144. REQUIRE(context);
  145. DataModelHandle model_handle = InitializeDataBindings(context);
  146. model_handle.DirtyAllVariables();
  147. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  148. REQUIRE(document);
  149. document->Show();
  150. context->Update();
  151. context->Render();
  152. SUBCASE("dirty")
  153. {
  154. nanobench::Bench bench;
  155. bench.title("Data bindings: Dirty variables");
  156. bench.relative(true);
  157. bench.run("Reference (Update)", [&] { context->Update(); });
  158. bench.run("Dirty one variable", [&] {
  159. model_handle.DirtyVariable("i0");
  160. context->Update();
  161. });
  162. bench.run("Dirty big variable", [&] {
  163. model_handle.DirtyVariable("arrays");
  164. context->Update();
  165. });
  166. bench.run("Dirty all variables", [&] {
  167. model_handle.DirtyAllVariables();
  168. context->Update();
  169. });
  170. }
  171. SUBCASE("update")
  172. {
  173. Element* element_i = document->GetElementById("i");
  174. Element* element_array = document->GetElementById("array");
  175. nanobench::Rng rng;
  176. nanobench::Bench bench;
  177. bench.title("Data bindings: Update");
  178. bench.relative(true);
  179. bench.run("Reference (Integer)", [&] {
  180. element_i->SetInnerRML(Rml::ToString(rng.bounded(1000)));
  181. context->Update();
  182. });
  183. bench.run("Integer", [&] {
  184. globals.i0 = rng.bounded(1000);
  185. model_handle.DirtyVariable("i0");
  186. context->Update();
  187. });
  188. bench.run("Basic", [&] {
  189. basic->a = rng.bounded(2000);
  190. *basic->b = rng.bounded(3000);
  191. basic->c->val = String("abc") + String(5, char('a' + rng.bounded('z' - 'a')));
  192. model_handle.DirtyVariable("basic");
  193. context->Update();
  194. });
  195. bench.run("Reference (Arrays)", [&] {
  196. element_array->SetInnerRML(
  197. Rml::CreateString("<span>%d </span><span>%d </span><span>%d </span>", rng.bounded(5000), rng.bounded(5000), rng.bounded(5000)));
  198. context->Update();
  199. });
  200. bench.run("Arrays", [&] {
  201. for (auto& v : arrays->a)
  202. v = rng.bounded(5000);
  203. model_handle.DirtyVariable("arrays");
  204. context->Update();
  205. });
  206. }
  207. TestsShell::RenderLoop();
  208. document->Close();
  209. TestsShell::ShutdownShell();
  210. }