Factory.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 "../../Include/RmlUi/Core/Factory.h"
  29. #include "../../Include/RmlUi/Core/StreamMemory.h"
  30. #include "../../Include/RmlUi/Core.h"
  31. #include "ContextInstancerDefault.h"
  32. #include "DataControllerDefault.h"
  33. #include "DataViewDefault.h"
  34. #include "DecoratorTiledBoxInstancer.h"
  35. #include "DecoratorTiledHorizontalInstancer.h"
  36. #include "DecoratorTiledImageInstancer.h"
  37. #include "DecoratorTiledVerticalInstancer.h"
  38. #include "DecoratorNinePatch.h"
  39. #include "DecoratorGradient.h"
  40. #include "ElementHandle.h"
  41. #include "ElementImage.h"
  42. #include "ElementTextDefault.h"
  43. #include "EventInstancerDefault.h"
  44. #include "FontEffectBlur.h"
  45. #include "FontEffectGlow.h"
  46. #include "FontEffectOutline.h"
  47. #include "FontEffectShadow.h"
  48. #include "PluginRegistry.h"
  49. #include "PropertyParserColour.h"
  50. #include "StreamFile.h"
  51. #include "StyleSheetFactory.h"
  52. #include "TemplateCache.h"
  53. #include "XMLNodeHandlerBody.h"
  54. #include "XMLNodeHandlerDefault.h"
  55. #include "XMLNodeHandlerHead.h"
  56. #include "XMLNodeHandlerTemplate.h"
  57. #include "XMLParseTools.h"
  58. namespace Rml {
  59. namespace Core {
  60. // Element instancers.
  61. using ElementInstancerMap = UnorderedMap< String, ElementInstancer* >;
  62. static ElementInstancerMap element_instancers;
  63. // Decorator instancers.
  64. using DecoratorInstancerMap = UnorderedMap< String, DecoratorInstancer* >;
  65. static DecoratorInstancerMap decorator_instancers;
  66. // Font effect instancers.
  67. using FontEffectInstancerMap = UnorderedMap< String, FontEffectInstancer* >;
  68. static FontEffectInstancerMap font_effect_instancers;
  69. // Data view instancers.
  70. using DataViewInstancerMap = UnorderedMap< String, DataViewInstancer* >;
  71. static DataViewInstancerMap data_view_instancers;
  72. // Data controller instancers.
  73. using DataControllerInstancerMap = UnorderedMap< String, DataControllerInstancer* >;
  74. static DataControllerInstancerMap data_controller_instancers;
  75. // Structural data view instancers.
  76. using StructuralDataViewInstancerMap = SmallUnorderedMap< String, DataViewInstancer* >;
  77. static StructuralDataViewInstancerMap structural_data_view_instancers;
  78. // Structural data view names.
  79. static StringList structural_data_view_attribute_names;
  80. // The context instancer.
  81. static ContextInstancer* context_instancer = nullptr;;
  82. // The event instancer
  83. static EventInstancer* event_instancer = nullptr;;
  84. // Event listener instancer.
  85. static EventListenerInstancer* event_listener_instancer = nullptr;
  86. // Default instancers are constructed and destroyed on Initialise and Shutdown, respectively.
  87. struct DefaultInstancers {
  88. template<typename T> using Ptr = UniquePtr<T>;
  89. Ptr<ContextInstancer> context_default;
  90. Ptr<EventInstancer> event_default;
  91. Ptr<ElementInstancer> element_default = std::make_unique<ElementInstancerElement>();
  92. Ptr<ElementInstancer> element_text_default = std::make_unique<ElementInstancerTextDefault>();
  93. Ptr<ElementInstancer> element_img = std::make_unique<ElementInstancerGeneric<ElementImage>>();
  94. Ptr<ElementInstancer> element_handle = std::make_unique<ElementInstancerGeneric<ElementHandle>>();
  95. Ptr<ElementInstancer> element_body = std::make_unique<ElementInstancerGeneric<ElementDocument>>();
  96. Ptr<DecoratorInstancer> decorator_tiled_horizontal = std::make_unique<DecoratorTiledHorizontalInstancer>();
  97. Ptr<DecoratorInstancer> decorator_tiled_vertical = std::make_unique<DecoratorTiledVerticalInstancer>();
  98. Ptr<DecoratorInstancer> decorator_tiled_box = std::make_unique<DecoratorTiledBoxInstancer>();
  99. Ptr<DecoratorInstancer> decorator_image = std::make_unique<DecoratorTiledImageInstancer>();
  100. Ptr<DecoratorInstancer> decorator_ninepatch = std::make_unique<DecoratorNinePatchInstancer>();
  101. Ptr<DecoratorInstancer> decorator_gradient = std::make_unique<DecoratorGradientInstancer>();
  102. Ptr<FontEffectInstancer> font_effect_blur = std::make_unique<FontEffectBlurInstancer>();
  103. Ptr<FontEffectInstancer> font_effect_glow = std::make_unique<FontEffectGlowInstancer>();
  104. Ptr<FontEffectInstancer> font_effect_outline = std::make_unique<FontEffectOutlineInstancer>();
  105. Ptr<FontEffectInstancer> font_effect_shadow = std::make_unique<FontEffectShadowInstancer>();
  106. Ptr<DataViewInstancer> data_view_attribute = std::make_unique<DataViewInstancerDefault< DataViewAttribute >>();
  107. Ptr<DataViewInstancer> data_view_class = std::make_unique<DataViewInstancerDefault< DataViewClass >>();
  108. Ptr<DataViewInstancer> data_view_if = std::make_unique<DataViewInstancerDefault< DataViewIf >>();
  109. Ptr<DataViewInstancer> data_view_visible = std::make_unique<DataViewInstancerDefault< DataViewVisible >>();
  110. Ptr<DataViewInstancer> data_view_rml = std::make_unique<DataViewInstancerDefault< DataViewRml >>();
  111. Ptr<DataViewInstancer> data_view_style = std::make_unique<DataViewInstancerDefault< DataViewStyle >>();
  112. Ptr<DataViewInstancer> data_view_text = std::make_unique<DataViewInstancerDefault< DataViewText >>();
  113. Ptr<DataViewInstancer> data_view_value = std::make_unique<DataViewInstancerDefault< DataViewValue >>();
  114. Ptr<DataViewInstancer> structural_data_view_for = std::make_unique<DataViewInstancerDefault< DataViewFor >>();
  115. Ptr<DataControllerInstancer> data_controller_value = std::make_unique<DataControllerInstancerDefault< DataControllerValue >>();
  116. Ptr<DataControllerInstancer> data_controller_event = std::make_unique<DataControllerInstancerDefault< DataControllerEvent >>();
  117. };
  118. static UniquePtr<DefaultInstancers> default_instancers;
  119. Factory::Factory()
  120. {
  121. }
  122. Factory::~Factory()
  123. {
  124. }
  125. bool Factory::Initialise()
  126. {
  127. default_instancers = std::make_unique<DefaultInstancers>();
  128. // Bind the default context instancer.
  129. if (!context_instancer)
  130. {
  131. default_instancers->context_default = std::make_unique<ContextInstancerDefault>();
  132. context_instancer = default_instancers->context_default.get();
  133. }
  134. // Bind default event instancer
  135. if (!event_instancer)
  136. {
  137. default_instancers->event_default = std::make_unique<EventInstancerDefault>();
  138. event_instancer = default_instancers->event_default.get();
  139. }
  140. // No default event listener instancer
  141. if (!event_listener_instancer)
  142. event_listener_instancer = nullptr;
  143. // Bind the default element instancers
  144. RegisterElementInstancer("*", default_instancers->element_default.get());
  145. RegisterElementInstancer("img", default_instancers->element_img.get());
  146. RegisterElementInstancer("#text", default_instancers->element_text_default.get());
  147. RegisterElementInstancer("handle", default_instancers->element_handle.get());
  148. RegisterElementInstancer("body", default_instancers->element_body.get());
  149. // Bind the default decorator instancers
  150. RegisterDecoratorInstancer("tiled-horizontal", default_instancers->decorator_tiled_horizontal.get());
  151. RegisterDecoratorInstancer("tiled-vertical", default_instancers->decorator_tiled_vertical.get());
  152. RegisterDecoratorInstancer("tiled-box", default_instancers->decorator_tiled_box.get());
  153. RegisterDecoratorInstancer("image", default_instancers->decorator_image.get());
  154. RegisterDecoratorInstancer("ninepatch", default_instancers->decorator_ninepatch.get());
  155. RegisterDecoratorInstancer("gradient", default_instancers->decorator_gradient.get());
  156. RegisterFontEffectInstancer("blur", default_instancers->font_effect_blur.get());
  157. RegisterFontEffectInstancer("glow", default_instancers->font_effect_glow.get());
  158. RegisterFontEffectInstancer("outline", default_instancers->font_effect_outline.get());
  159. RegisterFontEffectInstancer("shadow", default_instancers->font_effect_shadow.get());
  160. // Register the core XML node handlers.
  161. XMLParser::RegisterNodeHandler("", std::make_shared<XMLNodeHandlerDefault>());
  162. XMLParser::RegisterNodeHandler("body", std::make_shared<XMLNodeHandlerBody>());
  163. XMLParser::RegisterNodeHandler("head", std::make_shared<XMLNodeHandlerHead>());
  164. XMLParser::RegisterNodeHandler("template", std::make_shared<XMLNodeHandlerTemplate>());
  165. // Register the default data views
  166. RegisterDataViewInstancer(default_instancers->data_view_attribute.get(), "attr", false);
  167. RegisterDataViewInstancer(default_instancers->data_view_class.get(), "class", false);
  168. RegisterDataViewInstancer(default_instancers->data_view_if.get(), "if", false);
  169. RegisterDataViewInstancer(default_instancers->data_view_visible.get(), "visible", false);
  170. RegisterDataViewInstancer(default_instancers->data_view_rml.get(), "rml", false);
  171. RegisterDataViewInstancer(default_instancers->data_view_style.get(), "style", false);
  172. RegisterDataViewInstancer(default_instancers->data_view_text.get(), "text", false);
  173. RegisterDataViewInstancer(default_instancers->data_view_value.get(), "value", false);
  174. RegisterDataViewInstancer(default_instancers->structural_data_view_for.get(), "for", true );
  175. RegisterDataControllerInstancer(default_instancers->data_controller_value.get(), "value");
  176. RegisterDataControllerInstancer(default_instancers->data_controller_event.get(), "event");
  177. return true;
  178. }
  179. void Factory::Shutdown()
  180. {
  181. element_instancers.clear();
  182. decorator_instancers.clear();
  183. font_effect_instancers.clear();
  184. data_view_instancers.clear();
  185. structural_data_view_instancers.clear();
  186. structural_data_view_attribute_names.clear();
  187. context_instancer = nullptr;
  188. event_listener_instancer = nullptr;
  189. event_instancer = nullptr;
  190. XMLParser::ReleaseHandlers();
  191. default_instancers.reset();
  192. }
  193. // Registers the instancer to use when instancing contexts.
  194. void Factory::RegisterContextInstancer(ContextInstancer* instancer)
  195. {
  196. context_instancer = instancer;
  197. }
  198. // Instances a new context.
  199. ContextPtr Factory::InstanceContext(const String& name)
  200. {
  201. ContextPtr new_context = context_instancer->InstanceContext(name);
  202. if (new_context)
  203. new_context->SetInstancer(context_instancer);
  204. return new_context;
  205. }
  206. void Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  207. {
  208. element_instancers[StringUtilities::ToLower(name)] = instancer;
  209. }
  210. // Looks up the instancer for the given element
  211. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  212. {
  213. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  214. if (instancer_iterator == element_instancers.end())
  215. {
  216. instancer_iterator = element_instancers.find("*");
  217. if (instancer_iterator == element_instancers.end())
  218. return nullptr;
  219. }
  220. return instancer_iterator->second;
  221. }
  222. // Instances a single element.
  223. ElementPtr Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  224. {
  225. ElementInstancer* instancer = GetElementInstancer(instancer_name);
  226. if (instancer)
  227. {
  228. ElementPtr element = instancer->InstanceElement(parent, tag, attributes);
  229. // Process the generic attributes and bind any events
  230. if (element)
  231. {
  232. element->SetInstancer(instancer);
  233. element->SetAttributes(attributes);
  234. ElementUtilities::BindEventAttributes(element.get());
  235. PluginRegistry::NotifyElementCreate(element.get());
  236. }
  237. return element;
  238. }
  239. return nullptr;
  240. }
  241. struct ElementTextTraits {
  242. bool only_white_space = true;
  243. bool has_xml = false;
  244. bool has_curly_brackets = false;
  245. const char* error_str = nullptr; // Is set if and only if a parse error occurred.
  246. };
  247. static ElementTextTraits ParseElementTextTraits(const String& text)
  248. {
  249. ElementTextTraits result;
  250. bool in_brackets = false;
  251. char previous = 0;
  252. for (const char c : text)
  253. {
  254. if (!StringUtilities::IsWhitespace(c))
  255. result.only_white_space = false;
  256. if (in_brackets)
  257. {
  258. if (c == '}' && previous == '}')
  259. in_brackets = false;
  260. else if (c == '{' && previous == '{')
  261. result.error_str = "Nested double curly brackets are illegal.";
  262. else if (previous == '}' && c != '}')
  263. result.error_str = "Single closing curly bracket encountered, use double curly brackets to close data expression.";
  264. else if (previous == '/' && c == '>')
  265. result.error_str = "Xml end node encountered inside double curly brackets.";
  266. else if (previous == '<' && c == '/')
  267. result.error_str = "Xml end node encountered inside double curly brackets.";
  268. }
  269. else
  270. {
  271. if(c == '<')
  272. {
  273. result.has_xml = true;
  274. }
  275. else if (c == '{' && previous == '{')
  276. {
  277. in_brackets = true;
  278. result.has_curly_brackets = true;
  279. }
  280. else if (c == '}' && previous == '}')
  281. {
  282. result.error_str = "Closing double curly brackets encountered outside data expression.";
  283. }
  284. }
  285. if (result.error_str)
  286. break;
  287. previous = c;
  288. }
  289. return result;
  290. }
  291. // Instances a single text element containing a string.
  292. bool Factory::InstanceElementText(Element* parent, const String& text)
  293. {
  294. RMLUI_ASSERT(parent);
  295. String translated_data;
  296. if (SystemInterface* system_interface = GetSystemInterface())
  297. system_interface->TranslateString(translated_data, text);
  298. // Look for XML tags and detect double curly brackets for data bindings.
  299. ElementTextTraits traits = ParseElementTextTraits(text);
  300. if (traits.error_str)
  301. {
  302. Log::Message(Log::LT_ERROR, "Parse error in text: %s In element: %s", traits.error_str, parent->GetAddress().c_str());
  303. return false;
  304. }
  305. // If this text node only contains white-space we don't want to construct it.
  306. if (traits.only_white_space)
  307. return true;
  308. // If the text contains XML elements then run it through the XML parser again.
  309. if (traits.has_xml)
  310. {
  311. RMLUI_ZoneScopedNC("InstanceStream", 0xDC143C);
  312. auto stream = std::make_unique<StreamMemory>(translated_data.size() + 32);
  313. stream->Write("<body>", 6);
  314. stream->Write(translated_data);
  315. stream->Write("</body>", 7);
  316. stream->Seek(0, SEEK_SET);
  317. InstanceElementStream(parent, stream.get());
  318. }
  319. else
  320. {
  321. RMLUI_ZoneScopedNC("InstanceText", 0x8FBC8F);
  322. // Attempt to instance the element.
  323. XMLAttributes attributes;
  324. // If we have curly brackets in the text, we tag the element so that the appropriate data view (DataViewText) is constructed.
  325. if(traits.has_curly_brackets)
  326. attributes.emplace("data-text", Variant());
  327. ElementPtr element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  328. if (!element)
  329. {
  330. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned nullptr.", translated_data.c_str());
  331. return false;
  332. }
  333. // Assign the element its text value.
  334. ElementText* text_element = rmlui_dynamic_cast< ElementText* >(element.get());
  335. if (!text_element)
  336. {
  337. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s'. Found type '%s', was expecting a derivative of ElementText.", translated_data.c_str(), rmlui_type_name(*element));
  338. return false;
  339. }
  340. text_element->SetText(translated_data);
  341. // Add to active node.
  342. parent->AppendChild(std::move(element));
  343. }
  344. return true;
  345. }
  346. // Instances a element tree based on the stream
  347. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  348. {
  349. XMLParser parser(parent);
  350. parser.Parse(stream);
  351. return true;
  352. }
  353. // Instances a element tree based on the stream
  354. ElementPtr Factory::InstanceDocumentStream(Rml::Core::Context* context, Stream* stream)
  355. {
  356. RMLUI_ZoneScoped;
  357. ElementPtr element = Factory::InstanceElement(nullptr, "body", "body", XMLAttributes());
  358. if (!element)
  359. {
  360. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");
  361. return nullptr;
  362. }
  363. ElementDocument* document = rmlui_dynamic_cast< ElementDocument* >(element.get());
  364. if (!document)
  365. {
  366. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.", rmlui_type_name(*element));
  367. return nullptr;
  368. }
  369. document->context = context;
  370. XMLParser parser(element.get());
  371. parser.Parse(stream);
  372. return element;
  373. }
  374. // Registers an instancer that will be used to instance decorators.
  375. void Factory::RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer)
  376. {
  377. RMLUI_ASSERT(instancer);
  378. decorator_instancers[StringUtilities::ToLower(name)] = instancer;
  379. }
  380. // Retrieves a decorator instancer registered with the factory.
  381. DecoratorInstancer* Factory::GetDecoratorInstancer(const String& name)
  382. {
  383. auto iterator = decorator_instancers.find(name);
  384. if (iterator == decorator_instancers.end())
  385. return nullptr;
  386. return iterator->second;
  387. }
  388. // Registers an instancer that will be used to instance font effects.
  389. void Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  390. {
  391. RMLUI_ASSERT(instancer);
  392. font_effect_instancers[StringUtilities::ToLower(name)] = instancer;
  393. }
  394. FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
  395. {
  396. auto iterator = font_effect_instancers.find(name);
  397. if (iterator == font_effect_instancers.end())
  398. return nullptr;
  399. return iterator->second;
  400. }
  401. // Creates a style sheet containing the passed in styles.
  402. SharedPtr<StyleSheet> Factory::InstanceStyleSheetString(const String& string)
  403. {
  404. auto memory_stream = std::make_unique<StreamMemory>((const byte*) string.c_str(), string.size());
  405. return InstanceStyleSheetStream(memory_stream.get());
  406. }
  407. // Creates a style sheet from a file.
  408. SharedPtr<StyleSheet> Factory::InstanceStyleSheetFile(const String& file_name)
  409. {
  410. auto file_stream = std::make_unique<StreamFile>();
  411. file_stream->Open(file_name);
  412. return InstanceStyleSheetStream(file_stream.get());
  413. }
  414. // Creates a style sheet from an Stream.
  415. SharedPtr<StyleSheet> Factory::InstanceStyleSheetStream(Stream* stream)
  416. {
  417. SharedPtr<StyleSheet> style_sheet = std::make_shared<StyleSheet>();
  418. if (style_sheet->LoadStyleSheet(stream))
  419. {
  420. return style_sheet;
  421. }
  422. return nullptr;
  423. }
  424. // Clears the style sheet cache. This will force style sheets to be reloaded.
  425. void Factory::ClearStyleSheetCache()
  426. {
  427. StyleSheetFactory::ClearStyleSheetCache();
  428. }
  429. /// Clears the template cache. This will force templates to be reloaded.
  430. void Factory::ClearTemplateCache()
  431. {
  432. TemplateCache::Clear();
  433. }
  434. // Registers an instancer for all RmlEvents
  435. void Factory::RegisterEventInstancer(EventInstancer* instancer)
  436. {
  437. event_instancer = instancer;
  438. }
  439. // Instance an event object.
  440. EventPtr Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
  441. {
  442. EventPtr event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
  443. if (event)
  444. event->instancer = event_instancer;
  445. return event;
  446. }
  447. // Register an instancer for all event listeners
  448. void Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  449. {
  450. event_listener_instancer = instancer;
  451. }
  452. // Instance an event listener with the given string
  453. EventListener* Factory::InstanceEventListener(const String& value, Element* element)
  454. {
  455. // If we have an event listener instancer, use it
  456. if (event_listener_instancer)
  457. return event_listener_instancer->InstanceEventListener(value, element);
  458. return nullptr;
  459. }
  460. void Factory::RegisterDataViewInstancer(DataViewInstancer* instancer, const String& name, bool is_structural_view)
  461. {
  462. bool inserted = false;
  463. if (is_structural_view)
  464. {
  465. inserted = structural_data_view_instancers.emplace(name, instancer).second;
  466. if (inserted)
  467. structural_data_view_attribute_names.push_back(String("data-") + name);
  468. }
  469. else
  470. {
  471. inserted = data_view_instancers.emplace(name, instancer).second;
  472. }
  473. if (!inserted)
  474. Log::Message(Log::LT_WARNING, "Could not register data view instancer '%s'. The given name is already registered.", name.c_str());
  475. }
  476. void Factory::RegisterDataControllerInstancer(DataControllerInstancer* instancer, const String& name)
  477. {
  478. bool inserted = data_controller_instancers.emplace(name, instancer).second;
  479. if (!inserted)
  480. Log::Message(Log::LT_WARNING, "Could not register data controller instancer '%s'. The given name is already registered.", name.c_str());
  481. }
  482. DataViewPtr Factory::InstanceDataView(const String& type_name, Element* element, bool is_structural_view)
  483. {
  484. RMLUI_ASSERT(element);
  485. if (is_structural_view)
  486. {
  487. auto it = structural_data_view_instancers.find(type_name);
  488. if (it != structural_data_view_instancers.end())
  489. return it->second->InstanceView(element);
  490. }
  491. else
  492. {
  493. auto it = data_view_instancers.find(type_name);
  494. if (it != data_view_instancers.end())
  495. return it->second->InstanceView(element);
  496. }
  497. return nullptr;
  498. }
  499. DataControllerPtr Factory::InstanceDataController(const String& type_name, Element* element)
  500. {
  501. auto it = data_controller_instancers.find(type_name);
  502. if (it != data_controller_instancers.end())
  503. return it->second->InstanceController(element);
  504. return DataControllerPtr();
  505. }
  506. const StringList& Factory::GetStructuralDataViewAttributeNames()
  507. {
  508. return structural_data_view_attribute_names;
  509. }
  510. }
  511. }