Factory.cpp 21 KB

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