222 lines
22 KiB
HTML
222 lines
22 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: core functionalities</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
|
|
 <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&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&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: core functionalities </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><h1><a class="anchor" id="autotoc_md1"></a>
|
|
Introduction</h1>
|
|
<p><code>EnTT</code> comes with a bunch of core functionalities mostly used by the other parts of the library itself.<br />
|
|
Hardly users will include these features in their code, but it's worth describing what <code>EnTT</code> offers so as not to reinvent the wheel in case of need.</p>
|
|
<h1><a class="anchor" id="autotoc_md2"></a>
|
|
Compile-time identifiers</h1>
|
|
<p>Sometimes it's useful to be able to give unique identifiers to types at compile-time.<br />
|
|
There are plenty of different solutions out there and I could have used one of them. However, I decided to spend my time to define a compact and versatile tool that fully embraces what the modern C++ has to offer.</p>
|
|
<p>The <em>result of my efforts</em> is the <code>identifier</code> class template:</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <ident.hpp></span></div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// defines the identifiers for the given types</span></div>
|
|
<div class="line"><span class="keyword">using</span> <span class="keywordtype">id</span> = <a class="code" href="classentt_1_1identifier.html">entt::identifier<a_type, another_type></a>;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// ...</span></div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keywordflow">switch</span>(a_type_identifier) {</div>
|
|
<div class="line"><span class="keywordflow">case</span> id::type<a_type>:</div>
|
|
<div class="line"> <span class="comment">// ...</span></div>
|
|
<div class="line"> <span class="keywordflow">break</span>;</div>
|
|
<div class="line"><span class="keywordflow">case</span> id::type<another_type>:</div>
|
|
<div class="line"> <span class="comment">// ...</span></div>
|
|
<div class="line"> <span class="keywordflow">break</span>;</div>
|
|
<div class="line"><span class="keywordflow">default</span>:</div>
|
|
<div class="line"> <span class="comment">// ...</span></div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>This is all what the class template has to offer: a <code>type</code> inline variable that contains a numerical identifier for the given type. It can be used in any context where constant expressions are required.</p>
|
|
<p>As long as the list remains unchanged, identifiers are also guaranteed to be the same for every run. In case they have been used in a production environment and a type has to be removed, one can just use a placeholder to left the other identifiers unchanged:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><<span class="keyword">typename</span>> <span class="keyword">struct </span>ignore_type {};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">using</span> <span class="keywordtype">id</span> = <a class="code" href="classentt_1_1identifier.html">entt::identifier</a><</div>
|
|
<div class="line"> a_type_still_valid,</div>
|
|
<div class="line"> ignore_type<a_type_no_longer_valid>,</div>
|
|
<div class="line"> another_type_still_valid</div>
|
|
<div class="line">>;</div>
|
|
</div><!-- fragment --><p>A bit ugly to see, but it works at least.</p>
|
|
<h1><a class="anchor" id="autotoc_md3"></a>
|
|
Runtime identifiers</h1>
|
|
<p>Sometimes it's useful to be able to give unique identifiers to types at runtime.<br />
|
|
There are plenty of different solutions out there and I could have used one of them. In fact, I adapted the most common one to my requirements and used it extensively within the entire library.</p>
|
|
<p>It's the <code>family</code> class. Here is an example of use directly from the entity-component system:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using</span> component_family = <a class="code" href="classentt_1_1family.html">entt::family<struct internal_registry_component_family></a>;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// ...</span></div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">template</span><<span class="keyword">typename</span> Component></div>
|
|
<div class="line">component_type component() const noexcept {</div>
|
|
<div class="line"> <span class="keywordflow">return</span> component_family::type<Component>;</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --><p>This is all what a <em>family</em> has to offer: a <code>type</code> inline variable that contains a numerical identifier for the given type.</p>
|
|
<p>Please, note that identifiers aren't guaranteed to be the same for every run. Indeed it mostly depends on the flow of execution.</p>
|
|
<h1><a class="anchor" id="autotoc_md4"></a>
|
|
Hashed strings</h1>
|
|
<p>A hashed string is a zero overhead unique identifier. Users can use human-readable identifiers in the codebase while using their numeric counterparts at runtime, thus without affecting performance.<br />
|
|
The class has an implicit <code>constexpr</code> constructor that chews a bunch of characters. Once created, all what one can do with it is getting back the original string or converting it into a number.<br />
|
|
The good part is that a hashed string can be used wherever a constant expression is required and no <em>string-to-number</em> conversion will take place at runtime if used carefully.</p>
|
|
<p>Example of use:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> load(<a class="code" href="classentt_1_1basic__hashed__string.html#a7ee7dca5383cf4507949dc1261ca5efd">entt::hashed_string::hash_type</a> resource) {</div>
|
|
<div class="line"> <span class="comment">// uses the numeric representation of the resource to load and return it</span></div>
|
|
<div class="line">}</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">auto</span> resource = load(<a class="code" href="classentt_1_1basic__hashed__string.html">entt::hashed_string</a>{<span class="stringliteral">"gui/background"</span>});</div>
|
|
</div><!-- fragment --><p>There is also a <em>user defined literal</em> dedicated to hashed strings to make them more user-friendly:</p>
|
|
<div class="fragment"><div class="line">constexpr <span class="keyword">auto</span> str = <span class="stringliteral">"text"</span>_hs;</div>
|
|
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md5"></a>
|
|
Wide characters</h2>
|
|
<p>The hashed string has a design that is close to that of an <code>std::basic_string</code>. It means that <code>hashed_string</code> is nothing more than an alias for <code>basic_hashed_string<char></code>. For those who want to use the C++ type for wide character representation, there exists also the alias <code>hashed_wstring</code> for <code>basic_hashed_string<wchar_t></code>.<br />
|
|
In this case, the user defined literal to use to create hashed strings on the fly is <code>_hws</code>:</p>
|
|
<div class="fragment"><div class="line">constexpr <span class="keyword">auto</span> str = <span class="stringliteral">"text"</span>_hws;</div>
|
|
</div><!-- fragment --><p>Note that the hash type of the <code>hashed_wstring</code> is the same of its counterpart.</p>
|
|
<h2><a class="anchor" id="autotoc_md6"></a>
|
|
Conflicts</h2>
|
|
<p>The hashed string class uses internally FNV-1a to compute the numeric counterpart of a string. Because of the <em>pigeonhole principle</em>, conflicts are possible. This is a fact.<br />
|
|
There is no silver bullet to solve the problem of conflicts when dealing with hashing functions. In this case, the best solution seemed to be to give up. That's all.<br />
|
|
After all, human-readable unique identifiers aren't something strictly defined and over which users have not the control. Choosing a slightly different identifier is probably the best solution to make the conflict disappear in this case.</p>
|
|
<h1><a class="anchor" id="autotoc_md7"></a>
|
|
Monostate</h1>
|
|
<p>The monostate pattern is often presented as an alternative to a singleton based configuration system. This is exactly its purpose in <code>EnTT</code>. Moreover, this implementation is thread safe by design (hopefully).<br />
|
|
Keys are represented by hashed strings, values are basic types like <code>int</code>s or <code>bool</code>s. Values of different types can be associated to each key, even more than one at a time. Because of this, users must pay attention to use the same type both during an assignment and when they try to read back their data. Otherwise, they will probably incur in unexpected results.</p>
|
|
<p>Example of use:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="structentt_1_1monostate.html">entt::monostate</a><<a class="code" href="classentt_1_1basic__hashed__string.html">entt::hashed_string</a>{<span class="stringliteral">"mykey"</span>}>{} = <span class="keyword">true</span>;</div>
|
|
<div class="line"><a class="code" href="structentt_1_1monostate.html">entt::monostate</a><<span class="stringliteral">"mykey"</span>_hs>{} = 42;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// ...</span></div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keywordtype">bool</span> b = <a class="code" href="structentt_1_1monostate.html">entt::monostate</a><<span class="stringliteral">"mykey"</span>_hs>{};</div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> i = <a class="code" href="structentt_1_1monostate.html">entt::monostate</a><<a class="code" href="classentt_1_1basic__hashed__string.html">entt::hashed_string</a>{<span class="stringliteral">"mykey"</span>}>{};</div>
|
|
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md8"></a>
|
|
Type info</h1>
|
|
<p>The <code>type_info</code> class template is meant to provide some basic information about types of all kinds.<br />
|
|
Currently, the only information available is the numeric identifier associated with a given type:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> <span class="keywordtype">id</span> = <a class="code" href="structentt_1_1type__info.html#a24c115a7d3906b5e855d6ba79dc46ff3">entt::type_info<my_type>::id</a>();</div>
|
|
</div><!-- fragment --><p>In general, the <code>id</code> function is also <code>constexpr</code> but this isn't guaranteed for all compilers and platforms (although it's valid with the most well-known and popular compilers).<br />
|
|
This function <b>can</b> use non-standard features of the language for its own purposes. This allows it to provide compile-time identifiers that remain stable between different runs. However, it's possible to force the use of standard features only by defining the macro <code>ENTT_STANDARD_CPP</code>. In this case, there is no guarantee that the identifiers are stable across executions though. Moreover, identifiers are generated at runtime and are no longer a compile-time thing.</p>
|
|
<p>An external type system can also be used if needed. In fact, <code>type_info</code> can be specialized by type and is also <em>sfinae-friendly</em> in order to allow more refined specializations such as:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><<span class="keyword">typename</span> Type></div>
|
|
<div class="line"><span class="keyword">struct </span><a class="code" href="structentt_1_1type__info.html">entt::type_info</a><Type, std::void_d<decltype(Type::custom_id())>> {</div>
|
|
<div class="line"> <span class="keyword">static</span> constexpr ENTT_ID_TYPE <a class="code" href="structentt_1_1type__info.html#a24c115a7d3906b5e855d6ba79dc46ff3">id</a>() ENTT_NOEXCEPT {</div>
|
|
<div class="line"> <span class="keywordflow">return</span> Type::custom_id();</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
</div><!-- fragment --><p>Note that this class template and its specializations are widely used within <code>EnTT</code>. It also plays a very important role in making <code>EnTT</code> work transparently across boundaries.<br />
|
|
Please refer to the dedicated section for more details.</p>
|
|
<h2><a class="anchor" id="autotoc_md9"></a>
|
|
Almost unique identifiers</h2>
|
|
<p>Since the default non-standard, compile-time implementation makes use of hashed strings, it may happen that two types are assigned the same numeric identifier.<br />
|
|
In fact, although this is quite rare, it's not entirely excluded.</p>
|
|
<p>Another case where two types are assigned the same identifier is when classes from different contexts (for example two or more libraries loaded at runtime) have the same fully qualified name.<br />
|
|
Since the default model is based on the name of the classes, if the types belong to the same namespace then their identifiers <em>could</em> be identical (they won't necessarily be the same though).</p>
|
|
<p>Fortunately, there are several easy ways to deal with this:</p>
|
|
<ul>
|
|
<li>The most trivial one is to define the <code>ENTT_STANDARD_CPP</code> macro. Note that runtime identifiers don't suffer from the sam problem. However, this solution doesn't work well with a plugin system, where the libraries aren't linked.</li>
|
|
<li>Another possibility is to specialize the <code>type_info</code> class for one of the conflicting types, in order to assign it a custom identifier. This is probably the easiest solution that also preserves the feature of the tool.</li>
|
|
<li>A fully customized identifier generation policy (based for example on enum classes or preprocessing steps) may represent yet another option.</li>
|
|
</ul>
|
|
<p>These are just some examples of possible approaches to the problem but there are many others. As already mentioned above, since users have full control over their types, this problem is in any case easy to solve and should not worry too much.<br />
|
|
In all likelihood, it will never happen to run into a conflict anyway.</p>
|
|
<h1><a class="anchor" id="autotoc_md10"></a>
|
|
Traits</h1>
|
|
<p>This section contains a handful of utilities and traits not present in the standard template library but which can be useful in everyday life.</p>
|
|
<h2><a class="anchor" id="autotoc_md11"></a>
|
|
Member class type</h2>
|
|
<p>The <code>auto</code> template parameter introduced with C++17 made it possible to simplify many class templates and template functions but also made the class type opaque when members are passed as template arguments.<br />
|
|
The purpose of this utility is to extract the class type in a few lines of code:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><<span class="keyword">typename</span> Member></div>
|
|
<div class="line"><span class="keyword">using</span> clazz = <a class="code" href="namespaceentt.html#a49f19d031690e5ebfffd6c7a4f6bd364">entt::member_class_t<Member></a>;</div>
|
|
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md12"></a>
|
|
Tags</h2>
|
|
<p>Since in <code>EnTT</code> the type identified by <code>ENTT_ID_TYPE</code> is very important and widely used, there is a more user-friendly shortcut for the creation of integral constants based on it.<br />
|
|
This shortcut is the alias template <code><a class="el" href="namespaceentt.html#a4f264daab30d8e01b88536aa43fe0c28" title="Alias template to ease the creation of named values.">entt::tag</a></code>.</p>
|
|
<p>If used in combination with hashed strings, it helps to use human-readable names where types would be required otherwise. As an example:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="namespaceentt.html#a292643317d1dbb13e45824f757bd1086">registry</a>.assign<<a class="code" href="namespaceentt.html#a4f264daab30d8e01b88536aa43fe0c28">entt::tag</a><<span class="stringliteral">"enemy"</span>_hs>>(entity);</div>
|
|
</div><!-- fragment --><p>However, this isn't the only permitted use. Literally any value convertible to <code>ENTT_ID_TYPE</code> is a good candidate, such as the named constants of an unscoped enum. </p>
|
|
</div></div><!-- contents -->
|
|
</div><!-- PageDoc -->
|
|
<div class="ttc" id="anamespaceentt_html_a292643317d1dbb13e45824f757bd1086"><div class="ttname"><a href="namespaceentt.html#a292643317d1dbb13e45824f757bd1086">entt::registry</a></div><div class="ttdeci">basic_registry< entity > registry</div><div class="ttdoc">Alias declaration for the most common use case.</div><div class="ttdef"><b>Definition:</b> <a href="entity_2fwd_8hpp_source.html#l00084">fwd.hpp:84</a></div></div>
|
|
<div class="ttc" id="anamespaceentt_html_a4f264daab30d8e01b88536aa43fe0c28"><div class="ttname"><a href="namespaceentt.html#a4f264daab30d8e01b88536aa43fe0c28">entt::tag</a></div><div class="ttdeci">std::integral_constant< ENTT_ID_TYPE, Value > tag</div><div class="ttdoc">Alias template to ease the creation of named values.</div><div class="ttdef"><b>Definition:</b> <a href="type__traits_8hpp_source.html#l00209">type_traits.hpp:209</a></div></div>
|
|
<div class="ttc" id="astructentt_1_1monostate_html"><div class="ttname"><a href="structentt_1_1monostate.html">entt::monostate</a></div><div class="ttdoc">Minimal implementation of the monostate pattern.</div><div class="ttdef"><b>Definition:</b> <a href="monostate_8hpp_source.html#l00023">monostate.hpp:23</a></div></div>
|
|
<div class="ttc" id="astructentt_1_1type__info_html"><div class="ttname"><a href="structentt_1_1type__info.html">entt::type_info</a></div><div class="ttdoc">Types identifiers.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00053">type_info.hpp:53</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1basic__hashed__string_html"><div class="ttname"><a href="classentt_1_1basic__hashed__string.html">entt::basic_hashed_string</a></div><div class="ttdoc">Zero overhead unique identifier.</div><div class="ttdef"><b>Definition:</b> <a href="hashed__string_8hpp_source.html#l00063">hashed_string.hpp:63</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1basic__hashed__string_html_a7ee7dca5383cf4507949dc1261ca5efd"><div class="ttname"><a href="classentt_1_1basic__hashed__string.html#a7ee7dca5383cf4507949dc1261ca5efd">entt::basic_hashed_string::hash_type</a></div><div class="ttdeci">ENTT_ID_TYPE hash_type</div><div class="ttdoc">Unsigned integer type.</div><div class="ttdef"><b>Definition:</b> <a href="hashed__string_8hpp_source.html#l00087">hashed_string.hpp:87</a></div></div>
|
|
<div class="ttc" id="astructentt_1_1type__info_html_a24c115a7d3906b5e855d6ba79dc46ff3"><div class="ttname"><a href="structentt_1_1type__info.html#a24c115a7d3906b5e855d6ba79dc46ff3">entt::type_info::id</a></div><div class="ttdeci">static ENTT_ID_TYPE id() ENTT_NOEXCEPT</div><div class="ttdoc">Returns the numeric representation of a given type.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00069">type_info.hpp:69</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1identifier_html"><div class="ttname"><a href="classentt_1_1identifier.html">entt::identifier</a></div><div class="ttdoc">Types identifiers.</div><div class="ttdef"><b>Definition:</b> <a href="ident_8hpp_source.html#l00043">ident.hpp:43</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1family_html"><div class="ttname"><a href="classentt_1_1family.html">entt::family</a></div><div class="ttdoc">Dynamic identifier generator.</div><div class="ttdef"><b>Definition:</b> <a href="family_8hpp_source.html#l00019">family.hpp:19</a></div></div>
|
|
<div class="ttc" id="anamespaceentt_html_a49f19d031690e5ebfffd6c7a4f6bd364"><div class="ttname"><a href="namespaceentt.html#a49f19d031690e5ebfffd6c7a4f6bd364">entt::member_class_t</a></div><div class="ttdeci">typename member_class< Member >::type member_class_t</div><div class="ttdoc">Helper type.</div><div class="ttdef"><b>Definition:</b> <a href="type__traits_8hpp_source.html#l00201">type_traits.hpp:201</a></div></div>
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated by  <a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
|
</a> 1.8.16
|
|
</small></address>
|
|
</body>
|
|
</html>
|