Files
entt/md_docs_md_locator.html
2020-02-22 15:45:18 +01:00

124 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.8.16"/>
<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.3.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.16 -->
<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');
/* @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_md70"></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_md71"></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#a116dc62f5d2a1c65db9d7528c957e91f">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><!-- 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#a9265b7273a758003283a79f7a4de6250">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#ab72c1dab3eec4554690a82457894b385">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#ae24d16726620af40371fadeddf5b5e2e">entt::service_locator&lt;audio_interface&gt;::ref</a>();</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 -->
<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#l00025">locator.hpp:25</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_ae24d16726620af40371fadeddf5b5e2e"><div class="ttname"><a href="structentt_1_1service__locator.html#ae24d16726620af40371fadeddf5b5e2e">entt::service_locator::ref</a></div><div class="ttdeci">static Service &amp; ref() ENTT_NOEXCEPT</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#l00070">locator.hpp:70</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#l00099">locator.hpp:99</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_ab72c1dab3eec4554690a82457894b385"><div class="ttname"><a href="structentt_1_1service__locator.html#ab72c1dab3eec4554690a82457894b385">entt::service_locator::get</a></div><div class="ttdeci">static std::weak_ptr&lt; Service &gt; get() ENTT_NOEXCEPT</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#l00052">locator.hpp:52</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_a9265b7273a758003283a79f7a4de6250"><div class="ttname"><a href="structentt_1_1service__locator.html#a9265b7273a758003283a79f7a4de6250">entt::service_locator::empty</a></div><div class="ttdeci">static bool empty() ENTT_NOEXCEPT</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#l00038">locator.hpp:38</a></div></div>
<div class="ttc" id="astructentt_1_1service__locator_html_a116dc62f5d2a1c65db9d7528c957e91f"><div class="ttname"><a href="structentt_1_1service__locator.html#a116dc62f5d2a1c65db9d7528c957e91f">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#l00081">locator.hpp:81</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.16
</small></address>
</body>
</html>