Files
entt/md_docs_md_locator.html
2022-04-18 19:54:29 +02:00

100 lines
6.1 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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<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>
<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.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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">EnTT
&#160;<span id="projectnumber">3.10.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- 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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Crash Course: service locator </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="autotoc_md117"></a>
Introduction</h1>
<p>Usually service locators are tightly bound to the services they expose and it's hard to define a general purpose solution.<br />
This tiny class tries to fill the gap and to get rid of the burden of defining a different specific locator for each application.</p>
<h1><a class="anchor" id="autotoc_md118"></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" href="structentt_1_1locator.html">entt::locator&lt;interface&gt;::emplace&lt;service&gt;</a>(argument);</div>
<div class="line"><a class="code" href="structentt_1_1locator.html">entt::locator&lt;interface&gt;::allocate_emplace&lt;service&gt;</a>(allocator, argument);</div>
<div class="ttc" id="astructentt_1_1locator_html"><div class="ttname"><a href="structentt_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><!-- 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 has been set up, it's retrieved using the value function:</p>
<div class="fragment"><div class="line"><span class="keyword">interface </span>&amp;service = <a class="code" href="structentt_1_1locator.html#a22699e0d2a72b24d942e4937b001e0f8">entt::locator&lt;interface&gt;::value</a>();</div>
<div class="ttc" id="astructentt_1_1locator_html_a22699e0d2a72b24d942e4937b001e0f8"><div class="ttname"><a href="structentt_1_1locator.html#a22699e0d2a72b24d942e4937b001e0f8">entt::locator::value</a></div><div class="ttdeci">static Service &amp; value()</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#l00053">locator.hpp:53</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" href="structentt_1_1locator.html">entt::locator&lt;interface&gt;::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>&amp;service = <a class="code" href="structentt_1_1locator.html">entt::locator&lt;interface&gt;::value_or&lt;fallback_impl&gt;</a>(argument);</div>
</div><!-- fragment --><p>All arguments are used only if necessary, that is, if a service doesn't 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>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>