Files
entt/md_docs_md_locator.html
2021-12-21 13:20:51 +01:00

122 lines
9.7 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.9.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_md114"></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. This template based implementation tries to fill the gap and to get rid of the burden of defining a different specific locator for each application.<br />
This class is tiny, partially unsafe and thus risky to use. Moreover it doesn't fit probably most of the scenarios in which a service locator is required. Look at it as a small tool that can sometimes be useful if users know how to handle it.</p>
<h1><a class="anchor" id="autotoc_md115"></a>
Service locator</h1>
<p>The API is straightforward. The basic idea is that services are implemented by means of interfaces and rely on polymorphism.<br />
The locator is instantiated with the base type of the service if any and a concrete implementation is provided along with all the parameters required to initialize it. As an example:</p>
<div class="fragment"><div class="line"><span class="comment">// the service has no base type, a locator is used to treat it as a kind of singleton</span></div>
<div class="line"><a class="code" href="structentt_1_1service__locator.html#a1cab7cf8cff562d6ec95b7aaeaf12366">entt::service_locator&lt;my_service&gt;::set</a>(params...);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// sets up an opaque service</span></div>
<div class="line"><a class="code" href="structentt_1_1service__locator.html">entt::service_locator&lt;audio_interface&gt;::set&lt;audio_implementation&gt;</a>(params...);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// resets (destroys) the service</span></div>
<div class="line"><a class="code" href="structentt_1_1service__locator.html#a4d655acab54d87058ad41a266830d0e8">entt::service_locator&lt;audio_interface&gt;::reset</a>();</div>
<div class="ttc" id="astructentt_1_1service__locator_html"><div class="ttname"><a href="structentt_1_1service__locator.html">entt::service_locator</a></div><div class="ttdoc">Service locator, nothing more.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00022">locator.hpp:22</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_a1cab7cf8cff562d6ec95b7aaeaf12366"><div class="ttname"><a href="structentt_1_1service__locator.html#a1cab7cf8cff562d6ec95b7aaeaf12366">entt::service_locator::set</a></div><div class="ttdeci">static void set(Args &amp;&amp;...args)</div><div class="ttdoc">Sets or replaces a service.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00078">locator.hpp:78</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_a4d655acab54d87058ad41a266830d0e8"><div class="ttname"><a href="structentt_1_1service__locator.html#a4d655acab54d87058ad41a266830d0e8">entt::service_locator::reset</a></div><div class="ttdeci">static void reset()</div><div class="ttdoc">Resets a service.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00096">locator.hpp:96</a></div></div>
</div><!-- fragment --><p>The locator can also be queried to know if an active service is currently set and to retrieve it if necessary (either as a pointer or as a reference):</p>
<div class="fragment"><div class="line"><span class="comment">// no service currently set</span></div>
<div class="line"><span class="keyword">auto</span> empty = <a class="code" href="structentt_1_1service__locator.html#ad96474e6acfb0edd0e123c584f79296b">entt::service_locator&lt;audio_interface&gt;::empty</a>();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// gets a (possibly empty) shared pointer to the service ...</span></div>
<div class="line">std::shared_ptr&lt;audio_interface&gt; ptr = <a class="code" href="structentt_1_1service__locator.html#a88134390e70265359dda08f5aba2877f">entt::service_locator&lt;audio_interface&gt;::get</a>();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// ... or a reference, but it&#39;s undefined behaviour if the service isn&#39;t set yet</span></div>
<div class="line">audio_interface &amp;ref = <a class="code" href="structentt_1_1service__locator.html#a5379f8d798c2de5b1c41756ba3959e06">entt::service_locator&lt;audio_interface&gt;::ref</a>();</div>
<div class="ttc" id="astructentt_1_1service__locator_html_a5379f8d798c2de5b1c41756ba3959e06"><div class="ttname"><a href="structentt_1_1service__locator.html#a5379f8d798c2de5b1c41756ba3959e06">entt::service_locator::ref</a></div><div class="ttdeci">static Service &amp; ref()</div><div class="ttdoc">Returns a weak reference to a service implementation, if any.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00067">locator.hpp:67</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_a88134390e70265359dda08f5aba2877f"><div class="ttname"><a href="structentt_1_1service__locator.html#a88134390e70265359dda08f5aba2877f">entt::service_locator::get</a></div><div class="ttdeci">static std::weak_ptr&lt; Service &gt; get()</div><div class="ttdoc">Returns a weak pointer to a service implementation, if any.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00049">locator.hpp:49</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_ad96474e6acfb0edd0e123c584f79296b"><div class="ttname"><a href="structentt_1_1service__locator.html#ad96474e6acfb0edd0e123c584f79296b">entt::service_locator::empty</a></div><div class="ttdeci">static bool empty()</div><div class="ttdoc">Tests if a valid service implementation is set.</div><div class="ttdef"><b>Definition:</b> <a href="locator_8hpp_source.html#l00035">locator.hpp:35</a></div></div>
</div><!-- fragment --><p>A common use is to wrap the different locators in a container class, creating aliases for the various services:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>locator {</div>
<div class="line"> <span class="keyword">using</span> camera = <a class="code" href="structentt_1_1service__locator.html">entt::service_locator&lt;camera_interface&gt;</a>;</div>
<div class="line"> <span class="keyword">using</span> audio = <a class="code" href="structentt_1_1service__locator.html">entt::service_locator&lt;audio_interface&gt;</a>;</div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> init() {</div>
<div class="line"> locator::camera::set&lt;camera_null&gt;();</div>
<div class="line"> locator::audio::set&lt;audio_implementation&gt;(params...);</div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line">}</div>
</div><!-- fragment --> </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>