DataBinding.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #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. using namespace Rml;
  35. namespace {
  36. static const String document_rml = R"(
  37. <rml>
  38. <head>
  39. <title>Test</title>
  40. <link type="text/rcss" href="/assets/rml.rcss"/>
  41. <link type="text/template" href="/assets/window.rml"/>
  42. <style>
  43. body.window
  44. {
  45. left: 50px;
  46. right: 50px;
  47. top: 30px;
  48. bottom: 30px;
  49. max-width: -1px;
  50. max-height: -1px;
  51. }
  52. div#content
  53. {
  54. text-align: left;
  55. padding: 50px;
  56. box-sizing: border-box;
  57. }
  58. </style>
  59. </head>
  60. <body template="window">
  61. <div data-model="basics">
  62. <input type="text" data-value="i0"/>
  63. <h1>Globals</h1>
  64. <p>{{ i0 }}</p>
  65. <p>{{ i1 }}</p>
  66. <p>{{ i2 }}</p>
  67. <p>{{ i3 }}</p>
  68. <p>{{ s0 }}</p>
  69. <p>{{ s1 }}</p>
  70. <p>{{ s2.val }}</p>
  71. <p>{{ s3.val }}</p>
  72. <p>{{ s4.val }}</p>
  73. <p>{{ s5.val }}</p>
  74. <h1>Basic</h1>
  75. <p>{{ basic.a }}</p>
  76. <p>{{ basic.b }}</p>
  77. <p>{{ basic.c }}</p>
  78. <p>{{ basic.d }}</p>
  79. <p>{{ basic.e }}</p>
  80. <p>{{ basic.f }}</p>
  81. <h1>Wrapped</h1>
  82. <p>{{ wrapped.a.val }}</p>
  83. <p>{{ wrapped.b.val }}</p>
  84. <p>{{ wrapped.c.val }}</p>
  85. <p>{{ wrapped.d.val }}</p>
  86. <p>{{ wrapped.e.val }}</p>
  87. <h1>Pointed</h1>
  88. <p>{{ pointed.a.val }}</p>
  89. <p>{{ pointed.b.val }}</p>
  90. <p>{{ pointed.c.val }}</p>
  91. <h1>Arrays</h1>
  92. <p><span data-for="arrays.a">{{ it }} </span></p>
  93. <p><span data-for="arrays.b">{{ it }} </span></p>
  94. <p><span data-for="arrays.c">{{ it.val }} </span></p>
  95. <p><span data-for="arrays.d">{{ it.val }} </span></p>
  96. <p><span data-for="arrays.e">{{ it.val }} </span></p>
  97. </div>
  98. </body>
  99. </rml>
  100. )";
  101. struct StringWrap
  102. {
  103. StringWrap(String val = "wrap_default") : val(val) {}
  104. String val;
  105. };
  106. struct Globals
  107. {
  108. int i0 = 0;
  109. int* i1 = new int(1);
  110. UniquePtr<int> i2 = MakeUnique<int>(2);
  111. SharedPtr<int> i3 = MakeShared<int>(3);
  112. String s0 = "s0";
  113. String* s1 = new String("s1");
  114. StringWrap s2 = StringWrap("s2");
  115. StringWrap* s3 = new StringWrap("s3");
  116. UniquePtr<StringWrap> s4 = MakeUnique<StringWrap>("s4");
  117. SharedPtr<StringWrap> s5 = MakeShared<StringWrap>("s5");
  118. // Invalid
  119. const int x0 = 100; // Invalid: const variable
  120. const int* x1 = new int(101); // Invalid: const pointer
  121. UniquePtr<const int> x2 = MakeUnique<int>(102); // Invalid: const pointer
  122. const StringWrap* x3 = new StringWrap("x2"); // Invalid: const pointer
  123. UniquePtr<const StringWrap> x4 = MakeUnique<StringWrap>("x3"); // Invalid: const pointer
  124. } globals;
  125. struct Basic
  126. {
  127. int a = 1;
  128. int* b = new int(2);
  129. int GetC() {
  130. static int v = 5;
  131. return v;
  132. }
  133. int& GetD() {
  134. static int v = 5;
  135. return v;
  136. }
  137. int* GetE() {
  138. static int v = 6;
  139. return &v;
  140. }
  141. UniquePtr<int> GetF() {
  142. return MakeUnique<int>(7);
  143. }
  144. // Invalid: const member
  145. const int x0 = 2;
  146. // Invalid: const pointer
  147. const int* x1 = new int(3);
  148. // Invalid: const qualified member function
  149. int GetX2() const {
  150. return 4;
  151. }
  152. // Invalid: const reference return
  153. const int& GetX3() {
  154. static int g = 7;
  155. return g;
  156. }
  157. // Invalid: const pointer return
  158. const int* GetX4() {
  159. static int h = 8;
  160. return &h;
  161. }
  162. // Invalid: Illegal signature
  163. int GetX5(int) {
  164. return 9;
  165. }
  166. // Invalid: Const qualified return type
  167. const int GetX6() {
  168. return 9;
  169. }
  170. };
  171. struct Wrapped
  172. {
  173. StringWrap a = { "a" };
  174. StringWrap* b = new StringWrap("b");
  175. UniquePtr<StringWrap> c = MakeUnique<StringWrap>("c");
  176. StringWrap& GetD() {
  177. static StringWrap v = { "e" };
  178. return v;
  179. }
  180. StringWrap* GetE() {
  181. static StringWrap v = { "f" };
  182. return &v;
  183. }
  184. // Invalid: const pointer
  185. const StringWrap* x0 = new StringWrap("x0");
  186. // Invalid (run-time): Returning non-scalar variable by value.
  187. StringWrap GetX1() {
  188. return { "x1" };
  189. }
  190. // Invalid (run-time): Returning non-scalar variable by value.
  191. UniquePtr<StringWrap> GetX2() {
  192. return MakeUnique<StringWrap>("x2");
  193. }
  194. };
  195. using StringWrapPtr = UniquePtr<StringWrap>;
  196. struct Pointed
  197. {
  198. StringWrapPtr a = MakeUnique<StringWrap>("a");
  199. StringWrapPtr& GetB() {
  200. static StringWrapPtr v = MakeUnique<StringWrap>("b");
  201. return v;
  202. }
  203. StringWrapPtr* GetC() {
  204. static StringWrapPtr v = MakeUnique<StringWrap>("c");
  205. return &v;
  206. }
  207. // Invalid: We disallow recursive pointer types (pointer to pointer)
  208. StringWrapPtr* x0 = new StringWrapPtr(new StringWrap("x0"));
  209. // Invalid (run-time error): Only scalar data members can be returned by value
  210. StringWrapPtr GetX1() {
  211. return MakeUnique<StringWrap>("x1");
  212. }
  213. };
  214. struct Arrays
  215. {
  216. Vector<int> a = { 10, 11, 12 };
  217. Vector<int*> b = { new int(20), new int(21), new int(22) };
  218. Vector<StringWrap> c = { StringWrap("c1"), StringWrap("c2"), StringWrap("c3") };
  219. Vector<StringWrap*> d = { new StringWrap("d1"), new StringWrap("d2"), new StringWrap("d3") };
  220. Vector<StringWrapPtr> e;
  221. // Invalid: const pointer
  222. Vector<const int*> x0 = { new int(30), new int(31), new int(32) };
  223. // Invalid: const pointer
  224. Vector<UniquePtr<const StringWrap>> x1;
  225. Arrays() {
  226. e.emplace_back(MakeUnique<StringWrap>("e1"));
  227. e.emplace_back(MakeUnique<StringWrap>("e2"));
  228. e.emplace_back(MakeUnique<StringWrap>("e3"));
  229. x1.emplace_back(MakeUnique<StringWrap>("x1_1"));
  230. x1.emplace_back(MakeUnique<StringWrap>("x1_2"));
  231. x1.emplace_back(MakeUnique<StringWrap>("x1_3"));
  232. }
  233. };
  234. DataModelHandle model_handle;
  235. bool InitializeDataBindings(Context* context)
  236. {
  237. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  238. if (!constructor)
  239. return false;
  240. if (auto handle = constructor.RegisterStruct<StringWrap>())
  241. {
  242. handle.RegisterMember("val", &StringWrap::val);
  243. }
  244. {
  245. // Globals
  246. constructor.Bind("i0", &globals.i0);
  247. constructor.Bind("i1", &globals.i1);
  248. constructor.Bind("i2", &globals.i2);
  249. constructor.Bind("i3", &globals.i3);
  250. constructor.Bind("s0", &globals.s0);
  251. constructor.Bind("s1", &globals.s1);
  252. constructor.Bind("s2", &globals.s2);
  253. constructor.Bind("s3", &globals.s3);
  254. constructor.Bind("s4", &globals.s4);
  255. constructor.Bind("s5", &globals.s5);
  256. // Invalid: Each of the following should give a compile-time failure.
  257. //constructor.Bind("x0", &globals.x0);
  258. //constructor.Bind("x1", &globals.x1);
  259. //constructor.Bind("x2", &globals.x2);
  260. //constructor.Bind("x3", &globals.x3);
  261. //constructor.Bind("x4", &globals.x4);
  262. }
  263. if (auto handle = constructor.RegisterStruct<Basic>())
  264. {
  265. handle.RegisterMember("a", &Basic::a);
  266. handle.RegisterMember("b", &Basic::b);
  267. handle.RegisterMember("c", &Basic::GetC);
  268. handle.RegisterMember("d", &Basic::GetD);
  269. handle.RegisterMember("e", &Basic::GetE);
  270. handle.RegisterMember("f", &Basic::GetF);
  271. //handle.RegisterMember("x0", &Basic::x0);
  272. //handle.RegisterMember("x1", &Basic::x1);
  273. //handle.RegisterMember("x2", &Basic::GetX2);
  274. //handle.RegisterMember("x3", &Basic::GetX3);
  275. //handle.RegisterMember("x4", &Basic::GetX4);
  276. //handle.RegisterMember("x6", &Basic::GetX6);
  277. }
  278. constructor.Bind("basic", new Basic);
  279. if (auto handle = constructor.RegisterStruct<Wrapped>())
  280. {
  281. handle.RegisterMember("a", &Wrapped::a);
  282. handle.RegisterMember("b", &Wrapped::b);
  283. handle.RegisterMember("c", &Wrapped::c);
  284. handle.RegisterMember("d", &Wrapped::GetD);
  285. handle.RegisterMember("e", &Wrapped::GetE);
  286. //handle.RegisterMember("x0", &Wrapped::x0);
  287. //handle.RegisterMember("x1", &Wrapped::GetX1);
  288. //handle.RegisterMember("x2", &Wrapped::GetX2);
  289. }
  290. constructor.Bind("wrapped", new Wrapped);
  291. if (auto handle = constructor.RegisterStruct<Pointed>())
  292. {
  293. handle.RegisterMember("a", &Pointed::a);
  294. handle.RegisterMember("b", &Pointed::GetB);
  295. handle.RegisterMember("c", &Pointed::GetC);
  296. //handle.RegisterMember("x0", &Pointed::x0);
  297. //handle.RegisterMember("x1", &Pointed::GetX1);
  298. }
  299. constructor.Bind("pointed", new Pointed);
  300. constructor.RegisterArray<decltype(Arrays::a)>();
  301. constructor.RegisterArray<decltype(Arrays::b)>();
  302. constructor.RegisterArray<decltype(Arrays::c)>();
  303. constructor.RegisterArray<decltype(Arrays::d)>();
  304. constructor.RegisterArray<decltype(Arrays::e)>();
  305. //constructor.RegisterArray<decltype(Arrays::x0)>();
  306. //constructor.RegisterArray<decltype(Arrays::x1)>();
  307. if (auto handle = constructor.RegisterStruct<Arrays>())
  308. {
  309. handle.RegisterMember("a", &Arrays::a);
  310. handle.RegisterMember("b", &Arrays::b);
  311. handle.RegisterMember("c", &Arrays::c);
  312. handle.RegisterMember("d", &Arrays::d);
  313. handle.RegisterMember("e", &Arrays::e);
  314. //handle.RegisterMember("x0", &Arrays::x0);
  315. //handle.RegisterMember("x1", &Arrays::x1);
  316. }
  317. constructor.Bind("arrays", new Arrays);
  318. model_handle = constructor.GetModelHandle();
  319. return true;
  320. }
  321. } // Anonymous namespace
  322. TEST_CASE("databinding")
  323. {
  324. Context* context = TestsShell::GetContext();
  325. REQUIRE(context);
  326. REQUIRE(InitializeDataBindings(context));
  327. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  328. REQUIRE(document);
  329. document->Show();
  330. context->Update();
  331. context->Render();
  332. TestsShell::RenderLoop();
  333. document->Close();
  334. TestsShell::ShutdownShell();
  335. }