148 lines
12 KiB
HTML
148 lines
12 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.13.2"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>EnTT: Crash Course: service locator</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<script type="text/javascript" src="clipboard.js"></script>
|
|
<link href="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="cookie.js"></script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">EnTT<span id="projectnumber"> 3.15.0</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.13.2 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() { codefold.init(0); });
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="menudata.js"></script>
|
|
<script type="text/javascript" src="menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('',true,false,'search.php','Search',false);
|
|
$(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function(){ initResizable(false); });
|
|
/* @license-end */
|
|
</script>
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div><!-- top -->
|
|
<div id="doc-content">
|
|
<div><div class="header">
|
|
<div class="headertitle"><div class="title">Crash Course: service locator</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><p><a class="anchor" id="crash-course-service-locator"></a></p>
|
|
<h1><a class="anchor" id="table-of-contents-8"></a>
|
|
Table of Contents</h1>
|
|
<ul>
|
|
<li><a class="el" href="md_docs_2md_2config.html#introduction">Introduction</a></li>
|
|
<li><a class="el" href="#service-locator">Service locator</a><ul>
|
|
<li><a class="el" href="#opaque-handles">Opaque handles</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<h1><a class="anchor" id="introduction-7"></a>
|
|
Introduction</h1>
|
|
<p>Usually, service locators are tightly bound to the services they expose. It is hard to define a general purpose solution.<br />
|
|
This tiny class tries to fill the gap and gets rid of the burden of defining a different specific locator for each application.</p>
|
|
<h1><a class="anchor" id="service-locator"></a>
|
|
Service locator</h1>
|
|
<p>The service locator API tries to mimic that of <code>std::optional</code> and adds some extra functionalities on top of it such as allocator support.<br />
|
|
There are a couple of functions to set up a service, namely <code>emplace</code> and <code>allocate_emplace</code>:</p>
|
|
<div class="fragment"><div class="line"><a class="code hl_function" href="classentt_1_1locator.html#afb27544514506214a6adaf563ca7862b">entt::locator<interface>::emplace<service></a>(argument);</div>
|
|
<div class="line"><a class="code hl_class" href="classentt_1_1locator.html">entt::locator<interface>::allocate_emplace<service></a>(allocator, argument);</div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html"><div class="ttname"><a href="classentt_1_1locator.html">entt::locator</a></div><div class="ttdoc">Service locator, nothing more.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00027">locator.hpp:27</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_afb27544514506214a6adaf563ca7862b"><div class="ttname"><a href="classentt_1_1locator.html#afb27544514506214a6adaf563ca7862b">entt::locator::emplace</a></div><div class="ttdeci">static Service & emplace(Args &&...args)</div><div class="ttdoc">Sets or replaces a service.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00100">locator.hpp:100</a></div></div>
|
|
</div><!-- fragment --><p>The difference is that the latter expects an allocator as the first argument and uses it to allocate the service itself.<br />
|
|
Once a service is set up, it is retrieved using the <code>value</code> function:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">interface </span>&service = <a class="code hl_function" href="classentt_1_1locator.html#a5f2beb12925d1cfac5c55d5aca65e47e">entt::locator<interface>::value</a>();</div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_a5f2beb12925d1cfac5c55d5aca65e47e"><div class="ttname"><a href="classentt_1_1locator.html#a5f2beb12925d1cfac5c55d5aca65e47e">entt::locator::value</a></div><div class="ttdeci">static Service & value() noexcept</div><div class="ttdoc">Returns a reference to a valid service, if any.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00071">locator.hpp:71</a></div></div>
|
|
</div><!-- fragment --><p>Since the service may not be set (and therefore this function may result in an undefined behavior), the <code>has_value</code> and <code>value_or</code> functions are also available to test a service locator and to get a fallback service in case there is none:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordflow">if</span>(<a class="code hl_function" href="classentt_1_1locator.html#a7b01ea26c3def3465fb0223034a8fc72">entt::locator<interface>::has_value</a>()) {</div>
|
|
<div class="line"> <span class="comment">// ...</span></div>
|
|
<div class="line">}</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">interface </span>&service = <a class="code hl_function" href="classentt_1_1locator.html#ad7a9c3555e6ff2b67f9ad3af7f99a728">entt::locator<interface>::value_or<fallback_impl></a>(argument);</div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_a7b01ea26c3def3465fb0223034a8fc72"><div class="ttname"><a href="classentt_1_1locator.html#a7b01ea26c3def3465fb0223034a8fc72">entt::locator::has_value</a></div><div class="ttdeci">static bool has_value() noexcept</div><div class="ttdoc">Checks whether a service locator contains a value.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00058">locator.hpp:58</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_ad7a9c3555e6ff2b67f9ad3af7f99a728"><div class="ttname"><a href="classentt_1_1locator.html#ad7a9c3555e6ff2b67f9ad3af7f99a728">entt::locator::value_or</a></div><div class="ttdeci">static Service & value_or(Args &&...args)</div><div class="ttdoc">Returns a service if available or sets it from a fallback type.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00088">locator.hpp:88</a></div></div>
|
|
</div><!-- fragment --><p>All arguments are used only if necessary, that is, if a service does not already exist and therefore the fallback service is constructed and returned. In all other cases, they are discarded.<br />
|
|
Finally, to reset a service, use the <code>reset</code> function.</p>
|
|
<h2><a class="anchor" id="opaque-handles"></a>
|
|
Opaque handles</h2>
|
|
<p>Sometimes it is useful to <em>transfer</em> a copy of a service to another locator. For example, when working across boundaries it is common to <em>share</em> a service with a dynamically loaded module.<br />
|
|
Options are not much in this case. Among these is the possibility of <em>exporting</em> services and assigning them to a different locator.</p>
|
|
<p>This is what the <code>handle</code> and <code>reset</code> functions are meant for.<br />
|
|
The former returns an opaque object useful for <em>exporting</em> (or rather, obtaining a reference to) a service. The latter also accepts an optional argument to a handle which then allows users to reset a service by initializing it with an opaque handle:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> handle = <a class="code hl_function" href="classentt_1_1locator.html#a09b8c4659e08c600c113ce9e5b8fa7d3">entt::locator<interface>::handle</a>();</div>
|
|
<div class="line"><a class="code hl_function" href="classentt_1_1locator.html#a51c0f72339094183a1779da8bf01ee22">entt::locator<interface>::reset</a>(handle);</div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_a09b8c4659e08c600c113ce9e5b8fa7d3"><div class="ttname"><a href="classentt_1_1locator.html#a09b8c4659e08c600c113ce9e5b8fa7d3">entt::locator::handle</a></div><div class="ttdeci">static node_type handle() noexcept</div><div class="ttdoc">Returns a handle to the underlying service.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00124">locator.hpp:124</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1locator_html_a51c0f72339094183a1779da8bf01ee22"><div class="ttname"><a href="classentt_1_1locator.html#a51c0f72339094183a1779da8bf01ee22">entt::locator::reset</a></div><div class="ttdeci">static void reset(const node_type &other={}) noexcept</div><div class="ttdoc">Resets or replaces a service.</div><div class="ttdef"><b>Definition</b> <a href="locator_8hpp_source.html#l00134">locator.hpp:134</a></div></div>
|
|
</div><!-- fragment --><p>It is worth noting that it is possible to get handles for uninitialized services and use them with other locators. Of course, all a user will get is to have an uninitialized service elsewhere as well.</p>
|
|
<p>Note that exporting a service allows users to <em>share</em> the object currently set in a locator. Replacing it will not replace the element, even where a service has been configured with a handle to the previous item.<br />
|
|
In other words, if an audio service is replaced with a null object to silence an application and the original service was shared, this operation will not propagate to the other locators. Therefore, a module that shares the ownership of the original audio service is still able to emit sounds. </p>
|
|
</div></div><!-- contents -->
|
|
</div><!-- PageDoc -->
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2
|
|
</small></address>
|
|
</div><!-- doc-content -->
|
|
</body>
|
|
</html>
|