522 lines
67 KiB
HTML
522 lines
67 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: 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.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&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&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_md18"></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_md19"></a>
|
|
Any as in any type</h1>
|
|
<p><code>EnTT</code> comes with its own <code>any</code> type. It may seem redundant considering that C++17 introduced <code>std::any</code>, but it is not (hopefully).<br />
|
|
First of all, the <em>type</em> returned by an <code>std::any</code> is a const reference to an <code>std::type_info</code>, an implementation defined class that's not something everyone wants to see in a software. Furthermore, there is no way to connect it with the type system of the library and therefore with its integrated RTTI support.<br />
|
|
Note that this class is largely used internally by the library itself.</p>
|
|
<p>The API is very similar to that of its most famous counterpart, mainly because this class serves the same purpose of being an opaque container for any type of value.<br />
|
|
Instances of <code>any</code> also minimize the number of allocations by relying on a well known technique called <em>small buffer optimization</em> and a fake vtable.</p>
|
|
<p>Creating an object of the <code>any</code> type, whether empty or not, is trivial:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// an empty container</span></div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> empty{};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// a container for an int</span></div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> <a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>{0};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// in place construction</span></div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> <a class="code" href="namespaceentt.html#abc4d0fa77fae26e7c01793990996c581aa49f99ae99ae9daddd1fb79b73d2db28">in_place</a>{std::in_place_type<int>, 42};</div>
|
|
<div class="ttc" id="aclassentt_1_1basic__any_html"><div class="ttname"><a href="classentt_1_1basic__any.html">entt::basic_any<></a></div></div>
|
|
<div class="ttc" id="anamespaceentt_html_a4846741b8f485584c196304f588b94ad"><div class="ttname"><a href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">entt::any</a></div><div class="ttdeci">basic_any<> any</div><div class="ttdoc">Alias declaration for the most common use case.</div><div class="ttdef"><b>Definition:</b> <a href="core_2fwd_8hpp_source.html#l00017">fwd.hpp:17</a></div></div>
|
|
<div class="ttc" id="anamespaceentt_html_abc4d0fa77fae26e7c01793990996c581aa49f99ae99ae9daddd1fb79b73d2db28"><div class="ttname"><a href="namespaceentt.html#abc4d0fa77fae26e7c01793990996c581aa49f99ae99ae9daddd1fb79b73d2db28">entt::deletion_policy::in_place</a></div><div class="ttdeci">@ in_place</div><div class="ttdoc">In-place deletion policy.</div></div>
|
|
</div><!-- fragment --><p>Alternatively, the <code>make_any</code> function serves the same purpose but requires to always be explicit about the type:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> <a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a> = entt::make_any<int>(42);</div>
|
|
</div><!-- fragment --><p>In both cases, the <code>any</code> class takes the burden of destroying the contained element when required, regardless of the storage strategy used for the specific object.<br />
|
|
Furthermore, an instance of <code>any</code> isn't tied to an actual type. Therefore, the wrapper is reconfigured when it's assigned a new object of a type other than the one it contains.</p>
|
|
<p>There exists also a way to directly assign a value to the variable contained by an <code><a class="el" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad" title="Alias declaration for the most common use case.">entt::any</a></code>, without necessarily replacing it. This is especially useful when the object is used in <em>aliasing mode</em>, as described below:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> <a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>{42};</div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> value{3};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// assigns by copy</span></div>
|
|
<div class="line"><a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>.<a class="code" href="classentt_1_1basic__any.html#a641412fc4569ea9b2ec10d35a03835b3">assign</a>(value);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// assigns by move</span></div>
|
|
<div class="line"><a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>.<a class="code" href="classentt_1_1basic__any.html#a641412fc4569ea9b2ec10d35a03835b3">assign</a>(std::move(value));</div>
|
|
<div class="ttc" id="aclassentt_1_1basic__any_html_a641412fc4569ea9b2ec10d35a03835b3"><div class="ttname"><a href="classentt_1_1basic__any.html#a641412fc4569ea9b2ec10d35a03835b3">entt::basic_any::assign</a></div><div class="ttdeci">bool assign(const any &other)</div><div class="ttdoc">Assigns a value to the contained object without replacing it.</div><div class="ttdef"><b>Definition:</b> <a href="any_8hpp_source.html#l00309">any.hpp:309</a></div></div>
|
|
</div><!-- fragment --><p>The <code>any</code> class will also perform a check on the type information and whether or not the original type was copy or move assignable, as appropriate.<br />
|
|
In all cases, the <code>assign</code> function returns a boolean value to indicate the success or failure of the operation.</p>
|
|
<p>When in doubt about the type of object contained, the <code>type</code> member function of <code>any</code> returns a const reference to the <code>type_info</code> associated with its element, or <code>type_id<void>()</code> if the container is empty. The type is also used internally when comparing two <code>any</code> objects:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordflow">if</span>(any == empty) { <span class="comment">/* ... */</span> }</div>
|
|
</div><!-- fragment --><p>In this case, before proceeding with a comparison, it's verified that the <em>type</em> of the two objects is actually the same.<br />
|
|
Refer to the <code>EnTT</code> type system documentation for more details about how <code>type_info</code> works and on possible risks of a comparison.</p>
|
|
<p>A particularly interesting feature of this class is that it can also be used as an opaque container for const and non-const references:</p>
|
|
<div class="fragment"><div class="line"><span class="keywordtype">int</span> value = 42;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> <a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>{std::in_place_type<int &>(value)};</div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> cany = entt::make_any<const int &>(value);</div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> fwd = <a class="code" href="namespaceentt.html#a810f66c2c91deba50f96cae9119f2feb">entt::forward_as_any</a>(value);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><a class="code" href="namespaceentt.html#a4846741b8f485584c196304f588b94ad">any</a>.<a class="code" href="classentt_1_1basic__any.html#a612abee6a4ebf016df27929668839fc0">emplace</a><<span class="keyword">const</span> <span class="keywordtype">int</span> &>(value);</div>
|
|
<div class="ttc" id="aclassentt_1_1basic__any_html_a612abee6a4ebf016df27929668839fc0"><div class="ttname"><a href="classentt_1_1basic__any.html#a612abee6a4ebf016df27929668839fc0">entt::basic_any::emplace</a></div><div class="ttdeci">void emplace(Args &&...args)</div><div class="ttdoc">Replaces the contained object by creating a new instance directly.</div><div class="ttdef"><b>Definition:</b> <a href="any_8hpp_source.html#l00299">any.hpp:299</a></div></div>
|
|
<div class="ttc" id="anamespaceentt_html_a810f66c2c91deba50f96cae9119f2feb"><div class="ttname"><a href="namespaceentt.html#a810f66c2c91deba50f96cae9119f2feb">entt::forward_as_any</a></div><div class="ttdeci">basic_any< Len, Align > forward_as_any(Type &&value)</div><div class="ttdoc">Forwards its argument and avoids copies for lvalue references.</div><div class="ttdef"><b>Definition:</b> <a href="any_8hpp_source.html#l00484">any.hpp:484</a></div></div>
|
|
</div><!-- fragment --><p>In other words, whenever <code>any</code> is explicitly told to construct an <em>alias</em>, it acts as a pointer to the original instance rather than making a copy of it or moving it internally. The contained object is never destroyed and users must ensure that its lifetime exceeds that of the container.<br />
|
|
Similarly, it's possible to create non-owning copies of <code>any</code> from an existing object:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// aliasing constructor</span></div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__any.html">entt::any</a> ref = other.<a class="code" href="classentt_1_1basic__any.html#a7489b6cb743e74b8afcee0f66799b535">as_ref</a>();</div>
|
|
<div class="ttc" id="aclassentt_1_1basic__any_html_a7489b6cb743e74b8afcee0f66799b535"><div class="ttname"><a href="classentt_1_1basic__any.html#a7489b6cb743e74b8afcee0f66799b535">entt::basic_any::as_ref</a></div><div class="ttdeci">basic_any as_ref()</div><div class="ttdoc">Aliasing constructor.</div><div class="ttdef"><b>Definition:</b> <a href="any_8hpp_source.html#l00366">any.hpp:366</a></div></div>
|
|
</div><!-- fragment --><p>In this case, it doesn't matter if the original container actually holds an object or acts already as a reference for unmanaged elements, the new instance thus created won't create copies and will only serve as a reference for the original item.<br />
|
|
This means that, starting from the example above, both <code>ref</code> and <code>other</code> will point to the same object, whether it's initially contained in <code>other</code> or already an unmanaged element.</p>
|
|
<p>As a side note, it's worth mentioning that, while everything works transparently when it comes to non-const references, there are some exceptions when it comes to const references.<br />
|
|
In particular, the <code>data</code> member function invoked on a non-const instance of <code>any</code> that wraps a const reference will return a null pointer in all cases.</p>
|
|
<p>To cast an instance of <code>any</code> to a type, the library offers a set of <code>any_cast</code> functions in all respects similar to their most famous counterparts.<br />
|
|
The only difference is that, in the case of <code>EnTT</code>, these won't raise exceptions but will only trigger an assert in debug mode, otherwise resulting in undefined behavior in case of misuse in release mode.</p>
|
|
<h2><a class="anchor" id="autotoc_md20"></a>
|
|
Small buffer optimization</h2>
|
|
<p>The <code>any</code> class uses a technique called <em>small buffer optimization</em> to reduce the number of allocations where possible.<br />
|
|
The default reserved size for an instance of <code>any</code> is <code>sizeof(double[2])</code>. However, this is also configurable if needed. In fact, <code>any</code> is defined as an alias for <code>basic_any<Len></code>, where <code>Len</code> is the size above.<br />
|
|
Users can easily set a custom size or define their own aliases:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using</span> my_any = <a class="code" href="classentt_1_1basic__any.html">entt::basic_any</a><<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>[4])>;</div>
|
|
</div><!-- fragment --><p>This feature, in addition to allowing the choice of a size that best suits the needs of an application, also offers the possibility of forcing dynamic creation of objects during construction.<br />
|
|
In other terms, if the size is 0, <code>any</code> avoids the use of any optimization and always dynamically allocates objects (except for aliasing cases).</p>
|
|
<p>Note that the size of the internal storage as well as the alignment requirements are directly part of the type and therefore contribute to define different types that won't be able to interoperate with each other.</p>
|
|
<h2><a class="anchor" id="autotoc_md21"></a>
|
|
Alignment requirement</h2>
|
|
<p>The alignment requirement is optional and by default the most stringent (the largest) for any object whose size is at most equal to the one provided.<br />
|
|
The <code>basic_any</code> class template inspects the alignment requirements in each case, even when not provided and may decide not to use the small buffer optimization in order to meet them.</p>
|
|
<p>The alignment requirement is provided as an optional second parameter following the desired size for the internal storage:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">using</span> my_any = <a class="code" href="classentt_1_1basic__any.html">entt::basic_any</a><<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>[4]), <span class="keyword">alignof</span>(<span class="keywordtype">double</span>[4])>;</div>
|
|
</div><!-- fragment --><p>Note that the alignment requirements as well as the size of the internal storage are directly part of the type and therefore contribute to define different types that won't be able to interoperate with each other.</p>
|
|
<h1><a class="anchor" id="autotoc_md22"></a>
|
|
Compressed pair</h1>
|
|
<p>Primarily designed for internal use and far from being feature complete, the <code>compressed_pair</code> class does exactly what it promises: it tries to reduce the size of a pair by exploiting <em>Empty Base Class Optimization</em> (or <em>EBCO</em>).<br />
|
|
This class <b>is not</b> a drop-in replacement for <code>std::pair</code>. However, it offers enough functionalities to be a good alternative for when reducing memory usage is more important than having some cool and probably useless feature.</p>
|
|
<p>Although the API is very close to that of <code>std::pair</code> (apart from the fact that the template parameters are inferred from the constructor and therefore there is no<code>entt::make_compressed_pair</code>), the major difference is that <code>first</code> and <code>second</code> are functions for implementation needs:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="classentt_1_1compressed__pair.html">entt::compressed_pair</a> pair{0, 3.};</div>
|
|
<div class="line">pair.<a class="code" href="classentt_1_1compressed__pair.html#a805ab68ef0b7776421ee4bf8e06110b8">first</a>() = 42;</div>
|
|
<div class="ttc" id="aclassentt_1_1compressed__pair_html"><div class="ttname"><a href="classentt_1_1compressed__pair.html">entt::compressed_pair</a></div><div class="ttdoc">A compressed pair.</div><div class="ttdef"><b>Definition:</b> <a href="compressed__pair_8hpp_source.html#l00093">compressed_pair.hpp:95</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1compressed__pair_html_a805ab68ef0b7776421ee4bf8e06110b8"><div class="ttname"><a href="classentt_1_1compressed__pair.html#a805ab68ef0b7776421ee4bf8e06110b8">entt::compressed_pair::first</a></div><div class="ttdeci">first_type & first()</div><div class="ttdoc">Returns the first element that a pair stores.</div><div class="ttdef"><b>Definition:</b> <a href="compressed__pair_8hpp_source.html#l00172">compressed_pair.hpp:172</a></div></div>
|
|
</div><!-- fragment --><p>There isn't much to describe then. It's recommended to rely on documentation and intuition. At the end of the day, it's just a pair and nothing more.</p>
|
|
<h1><a class="anchor" id="autotoc_md23"></a>
|
|
Enum as bitmask</h1>
|
|
<p>Sometimes it's useful to be able to use enums as bitmasks. However, enum classes aren't really suitable for the purpose out of the box. Main problem is that they don't convert implicitly to their underlying type.<br />
|
|
All that remains is to make a choice between using old-fashioned enums (with all their problems that I don't want to discuss here) or writing <em>ugly</em> code.</p>
|
|
<p>Fortunately, there is also a third way: adding enough operators in the global scope to treat enum classes as bitmask transparently.<br />
|
|
The ultimate goal is to be able to write code like the following (or maybe something more meaningful, but this should give a grasp and remain simple at the same time):</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">enum class</span> my_flag {</div>
|
|
<div class="line"> unknown = 0x01,</div>
|
|
<div class="line"> enabled = 0x02,</div>
|
|
<div class="line"> disabled = 0x04</div>
|
|
<div class="line">};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">const</span> my_flag flags = my_flag::enabled;</div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keywordtype">bool</span> is_enabled = !!(flags & my_flag::enabled);</div>
|
|
</div><!-- fragment --><p>The problem with adding all operators to the global scope is that these will come into play even when not required, with the risk of introducing errors that are difficult to deal with.<br />
|
|
However, C++ offers enough tools to get around this problem. In particular, the library requires users to register all enum classes for which bitmask support should be enabled:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><></div>
|
|
<div class="line"><span class="keyword">struct </span><a class="code" href="structentt_1_1enum__as__bitmask.html">entt::enum_as_bitmask</a><my_flag></div>
|
|
<div class="line"> : std::true_type</div>
|
|
<div class="line">{};</div>
|
|
<div class="ttc" id="astructentt_1_1enum__as__bitmask_html"><div class="ttname"><a href="structentt_1_1enum__as__bitmask.html">entt::enum_as_bitmask</a></div><div class="ttdoc">Enable bitmask support for enum classes.</div><div class="ttdef"><b>Definition:</b> <a href="enum_8hpp_source.html#l00014">enum.hpp:14</a></div></div>
|
|
</div><!-- fragment --><p>This is handy when dealing with enum classes defined by third party libraries and over which the users have no control. However, it's also verbose and can be avoided by adding a specific value to the enum class itself:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">enum class</span> my_flag {</div>
|
|
<div class="line"> unknown = 0x01,</div>
|
|
<div class="line"> enabled = 0x02,</div>
|
|
<div class="line"> disabled = 0x04,</div>
|
|
<div class="line"> _entt_enum_as_bitmask</div>
|
|
<div class="line">};</div>
|
|
</div><!-- fragment --><p>In this case, there is no need to specialize the <code>enum_as_bitmask</code> traits, since <code>EnTT</code> will automatically detect the flag and enable the bitmask support.<br />
|
|
Once the enum class has been registered (in one way or the other) all the most common operators will be available, such as <code>&</code>, <code>|</code> but also <code>&=</code> and <code>|=</code>. Refer to the official documentation for the full list of operators.</p>
|
|
<h1><a class="anchor" id="autotoc_md24"></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 through the <code>data</code> member function or converting the instance 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#a7b625799426407d8083f7d418e2c6a9e">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 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#l00069">hashed_string.hpp:69</a></div></div>
|
|
<div class="ttc" id="aclassentt_1_1basic__hashed__string_html_a7b625799426407d8083f7d418e2c6a9e"><div class="ttname"><a href="classentt_1_1basic__hashed__string.html#a7b625799426407d8083f7d418e2c6a9e">entt::basic_hashed_string::hash_type</a></div><div class="ttdeci">typename base_type::hash_type hash_type</div><div class="ttdoc">Unsigned integer type.</div><div class="ttdef"><b>Definition:</b> <a href="hashed__string_8hpp_source.html#l00107">hashed_string.hpp:107</a></div></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"><span class="keyword">using namespace </span>entt::literals;</div>
|
|
<div class="line">constexpr <span class="keyword">auto</span> str = <span class="stringliteral">"text"</span>_hs;</div>
|
|
</div><!-- fragment --><p>To use it, remember that all user defined literals in <code>EnTT</code> are enclosed in the <code>entt::literals</code> namespace. Therefore, the entire namespace or selectively the literal of interest must be explicitly included before each use, a bit like <code>std::literals</code>.<br />
|
|
Finally, in case users need to create hashed strings at runtime, this class also offers the necessary functionalities:</p>
|
|
<div class="fragment"><div class="line">std::string orig{<span class="stringliteral">"text"</span>};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// create a full-featured hashed string...</span></div>
|
|
<div class="line"><a class="code" href="classentt_1_1basic__hashed__string.html">entt::hashed_string</a> str{orig.c_str()};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// ... or compute only the unique identifier</span></div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keyword">auto</span> hash = <a class="code" href="classentt_1_1basic__hashed__string.html#a40f85633eba5321389c5ceea4d19cebc">entt::hashed_string::value</a>(orig.c_str());</div>
|
|
<div class="ttc" id="aclassentt_1_1basic__hashed__string_html_a40f85633eba5321389c5ceea4d19cebc"><div class="ttname"><a href="classentt_1_1basic__hashed__string.html#a40f85633eba5321389c5ceea4d19cebc">entt::basic_hashed_string::value</a></div><div class="ttdeci">constexpr hash_type value() const</div><div class="ttdoc">Returns the numeric representation of a hashed string.</div><div class="ttdef"><b>Definition:</b> <a href="hashed__string_8hpp_source.html#l00192">hashed_string.hpp:192</a></div></div>
|
|
</div><!-- fragment --><p>This possibility shouldn't be exploited in tight loops, since the computation takes place at runtime and no longer at compile-time and could therefore impact performance to some degrees.</p>
|
|
<h2><a class="anchor" id="autotoc_md25"></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 = L<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_md26"></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_md27"></a>
|
|
Memory</h1>
|
|
<p>There are a handful of tools within EnTT to interact with memory in one way or another.<br />
|
|
Some are geared towards simplifying the implementation of (internal or external) allocator aware containers. Others, on the other hand, are designed to help the developer with everyday problems.</p>
|
|
<p>The former are very specific and for niche problems. These are tools designed to unwrap fancy or plain pointers (<code>to_address</code>) or to help forget the meaning of acronyms like <em>POCCA</em>, <em>POCMA</em> or <em>POCS</em>.<br />
|
|
I won't describe them here in detail. Instead, I recommend reading the inline documentation to those interested in the subject.</p>
|
|
<h2><a class="anchor" id="autotoc_md28"></a>
|
|
Power of two and fast modulus</h2>
|
|
<p>Finding out if a number is a power of two (<code>is_power_of_two</code>) or what the next power of two is given a random value (<code>next_power_of_two</code>) is very useful at times.<br />
|
|
For example, it helps to allocate memory in pages having a size suitable for the fast modulus:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">const</span> std::size_t result = <a class="code" href="namespaceentt.html#a28fe7952c2a11ebc2b47d95fb749adda">entt::fast_mod</a>(value, modulus);</div>
|
|
<div class="ttc" id="anamespaceentt_html_a28fe7952c2a11ebc2b47d95fb749adda"><div class="ttname"><a href="namespaceentt.html#a28fe7952c2a11ebc2b47d95fb749adda">entt::fast_mod</a></div><div class="ttdeci">constexpr std::size_t fast_mod(const std::size_t value, const std::size_t mod)</div><div class="ttdoc">Fast module utility function (powers of two only).</div><div class="ttdef"><b>Definition:</b> <a href="memory_8hpp_source.html#l00102">memory.hpp:102</a></div></div>
|
|
</div><!-- fragment --><p>Where <code>modulus</code> is necessarily a power of two. Perhaps not everyone knows that this type of operation is far superior in terms of performance to the basic modulus and for this reason preferred in many areas.</p>
|
|
<h2><a class="anchor" id="autotoc_md29"></a>
|
|
Allocator aware unique pointers</h2>
|
|
<p>A nasty thing in C++ (at least up to C++20) is the fact that shared pointers support allocators while unique pointers don't.<br />
|
|
There is a proposal at the moment that also shows among the other things how this can be implemented without any compiler support.</p>
|
|
<p>The <code>allocate_unique</code> function follows this proposal, making a virtue out of necessity:</p>
|
|
<div class="fragment"><div class="line">std::unique_ptr<my_type, entt::allocation_deleter<my_type>> ptr = entt::allocate_unique<my_type>(allocator, arguments);</div>
|
|
</div><!-- fragment --><p>Although the internal implementation is slightly different from what is proposed for the standard, this function offers an API that is a drop-in replacement for the same feature.</p>
|
|
<h1><a class="anchor" id="autotoc_md30"></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 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#l00021">monostate.hpp:21</a></div></div>
|
|
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md31"></a>
|
|
Type support</h1>
|
|
<p><code>EnTT</code> provides some basic information about types of all kinds.<br />
|
|
It also offers additional features that are not yet available in the standard library or that will never be.</p>
|
|
<h2><a class="anchor" id="autotoc_md32"></a>
|
|
Built-in RTTI support</h2>
|
|
<p>Runtime type identification support (or RTTI) is one of the most frequently disabled features in the C++ world, especially in the gaming sector. Regardless of the reasons for this, it's often a shame not to be able to rely on opaque type information at runtime.<br />
|
|
The library tries to fill this gap by offering a built-in system that doesn't serve as a replacement but comes very close to being one and offers similar information to that provided by its counterpart.</p>
|
|
<p>Basically, the whole system relies on a handful of classes. In particular:</p>
|
|
<ul>
|
|
<li>The unique sequential identifier associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> index = <a class="code" href="structentt_1_1type__index.html#ae35199e5bea69f748cef1866e52b5dc4">entt::type_index<a_type>::value</a>();</div>
|
|
<div class="ttc" id="astructentt_1_1type__index_html_ae35199e5bea69f748cef1866e52b5dc4"><div class="ttname"><a href="structentt_1_1type__index.html#ae35199e5bea69f748cef1866e52b5dc4">entt::type_index::value</a></div><div class="ttdeci">static id_type value()</div><div class="ttdoc">Returns the sequential identifier of a given type.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00084">type_info.hpp:84</a></div></div>
|
|
</div><!-- fragment --><p>The returned value isn't guaranteed to be stable across different runs. However, it can be very useful as index in associative and unordered associative containers or for positional accesses in a vector or an array.</p>
|
|
<p>So as not to conflict with the other tools available, the <code>family</code> class isn't used to generate these indexes. Therefore, the numeric identifiers returned by the two tools may differ.<br />
|
|
On the other hand, this leaves users with full powers over the <code>family</code> class and therefore the generation of custom runtime sequences of indices for their own purposes, if necessary.</p>
|
|
<p>An external generator can also be used if needed. In fact, <code>type_index</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__index.html">entt::type_index</a><Type, std::void_d<decltype(Type::index())>> {</div>
|
|
<div class="line"> <span class="keyword">static</span> <a class="code" href="namespaceentt.html#a13e040e7b38a8f86d1ab2f096f37b627">entt::id_type</a> <a class="code" href="structentt_1_1type__index.html#ae35199e5bea69f748cef1866e52b5dc4">value</a>() ENTT_NOEXCEPT {</div>
|
|
<div class="line"> <span class="keywordflow">return</span> Type::index();</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="ttc" id="anamespaceentt_html_a13e040e7b38a8f86d1ab2f096f37b627"><div class="ttname"><a href="namespaceentt.html#a13e040e7b38a8f86d1ab2f096f37b627">entt::id_type</a></div><div class="ttdeci">std::uint32_t id_type</div><div class="ttdoc">Alias declaration for type identifiers.</div><div class="ttdef"><b>Definition:</b> <a href="core_2fwd_8hpp_source.html#l00014">fwd.hpp:14</a></div></div>
|
|
<div class="ttc" id="astructentt_1_1type__index_html"><div class="ttname"><a href="structentt_1_1type__index.html">entt::type_index</a></div><div class="ttdoc">Type sequential identifier.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00079">type_info.hpp:79</a></div></div>
|
|
</div><!-- fragment --><p>Note that indexes <b>must</b> still be generated sequentially in this case.<br />
|
|
The tool is widely used within <code>EnTT</code>. Generating indices not sequentially would break an assumption and would likely lead to undesired behaviors.</p>
|
|
<ul>
|
|
<li>The hash value associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> hash = <a class="code" href="structentt_1_1type__hash.html#a95f5ae709a95f0959a7dfd1af6af6c74">entt::type_hash<a_type>::value</a>();</div>
|
|
<div class="ttc" id="astructentt_1_1type__hash_html_a95f5ae709a95f0959a7dfd1af6af6c74"><div class="ttname"><a href="structentt_1_1type__hash.html#a95f5ae709a95f0959a7dfd1af6af6c74">entt::type_hash::value</a></div><div class="ttdeci">static constexpr id_type value()</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#l00109">type_info.hpp:109</a></div></div>
|
|
</div><!-- fragment --><p>In general, the <code>value</code> function exposed by <code>type_hash</code> 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 ones).</p>
|
|
<p>This function <b>can</b> use non-standard features of the language for its own purposes. This makes it possible to provide compile-time identifiers that remain stable across different runs.<br />
|
|
In all cases, users can prevent the library from using these features by means of the <code>ENTT_STANDARD_CPP</code> definition. In this case, there is no guarantee that identifiers remain stable across executions. Moreover, they are generated at runtime and are no longer a compile-time thing.</p>
|
|
<p>As for <code>type_index</code>, also <code>type_hash</code> is a <em>sfinae-friendly</em> class that can be specialized in order to customize its behavior globally or on a per-type or per-traits basis.</p>
|
|
<ul>
|
|
<li>The name associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> name = <a class="code" href="structentt_1_1type__name.html#a053ef91f965558bdf95dc2fbb3fc550b">entt::type_name<a_type>::value</a>();</div>
|
|
<div class="ttc" id="astructentt_1_1type__name_html_a053ef91f965558bdf95dc2fbb3fc550b"><div class="ttname"><a href="structentt_1_1type__name.html#a053ef91f965558bdf95dc2fbb3fc550b">entt::type_name::value</a></div><div class="ttdeci">static constexpr std::string_view value()</div><div class="ttdoc">Returns the name of a given type.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00130">type_info.hpp:130</a></div></div>
|
|
</div><!-- fragment --><p>The name associated with a type is extracted from some information generally made available by the compiler in use. Therefore, it may differ depending on the compiler and may be empty in the event that this information isn't available.<br />
|
|
For example, given the following class:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">struct </span>my_type { <span class="comment">/* ... */</span> };</div>
|
|
</div><!-- fragment --><p>The name is <code>my_type</code> when compiled with GCC or CLang and <code>struct my_type</code> when MSVC is in use.<br />
|
|
Most of the time the name is also retrieved at compile-time and is therefore always returned through an <code>std::string_view</code>. Users can easily access it and modify it as needed, for example by removing the word <code>struct</code> to standardize the result. <code>EnTT</code> won't do this for obvious reasons, since it requires copying and creating a new string potentially at runtime.</p>
|
|
<p>This function <b>can</b> use non-standard features of the language for its own purposes. Users can prevent the library from using non-standard features by means of the <code>ENTT_STANDARD_CPP</code> definition. In this case, the name will be empty by default.</p>
|
|
<p>As for <code>type_index</code>, also <code>type_name</code> is a <em>sfinae-friendly</em> class that can be specialized in order to customize its behavior globally or on a per-type or per-traits basis.</p>
|
|
<p>These are then combined into utilities that aim to offer an API that is somewhat similar to that offered by the language.</p>
|
|
<h3><a class="anchor" id="autotoc_md33"></a>
|
|
Type info</h3>
|
|
<p>The <code>type_info</code> class isn't a drop-in replacement for <code>std::type_info</code> but can provide similar information which are not implementation defined and don't require to enable RTTI.<br />
|
|
Therefore, they can sometimes be even more reliable than those obtained otherwise.</p>
|
|
<p>Its type defines an opaque class that is also copyable and movable.<br />
|
|
Objects of this type are generally returned by the <code>type_id</code> functions:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// by type</span></div>
|
|
<div class="line"><span class="keyword">auto</span> info = entt::type_id<a_type>();</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// by value</span></div>
|
|
<div class="line"><span class="keyword">auto</span> other = <a class="code" href="namespaceentt.html#a2e2dcd5e9e474d7ab0f2db24dae09dd5">entt::type_id</a>(42);</div>
|
|
<div class="ttc" id="anamespaceentt_html_a2e2dcd5e9e474d7ab0f2db24dae09dd5"><div class="ttname"><a href="namespaceentt.html#a2e2dcd5e9e474d7ab0f2db24dae09dd5">entt::type_id</a></div><div class="ttdeci">const type_info & type_id()</div><div class="ttdoc">Returns the type info object associated to a given type.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00257">type_info.hpp:257</a></div></div>
|
|
</div><!-- fragment --><p>All elements thus received are nothing more than const references to instances of <code>type_info</code> with static storage duration.<br />
|
|
This is convenient for saving the entire object aside for the cost of a pointer. However, nothing prevents from constructing <code>type_info</code> objects directly:</p>
|
|
<div class="fragment"><div class="line"><a class="code" href="structentt_1_1type__info.html">entt::type_info</a> info{std::in_place_type<int>};</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">Implementation specific information about a type.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00141">type_info.hpp:141</a></div></div>
|
|
</div><!-- fragment --><p>These are the information made available by <code>type_info</code>:</p>
|
|
<ul>
|
|
<li>The index associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> idx = entt::type_id<a_type>().index();</div>
|
|
</div><!-- fragment --><p>This is also an alias for the following:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> idx = <a class="code" href="structentt_1_1type__index.html">entt::type_index<std::remove_cv_t<std::remove_reference_t<a_type></a>>>::value();</div>
|
|
</div><!-- fragment --><ul>
|
|
<li>The hash value associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> hash = entt::type_id<a_type>().hash();</div>
|
|
</div><!-- fragment --><p>This is also an alias for the following:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> hash = <a class="code" href="structentt_1_1type__hash.html">entt::type_hash<std::remove_cv_t<std::remove_reference_t<a_type></a>>>::value();</div>
|
|
<div class="ttc" id="astructentt_1_1type__hash_html"><div class="ttname"><a href="structentt_1_1type__hash.html">entt::type_hash</a></div><div class="ttdoc">Type hash.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00100">type_info.hpp:100</a></div></div>
|
|
</div><!-- fragment --><ul>
|
|
<li>The name associated with a given type:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> name = entt::type_id<my_type>().name();</div>
|
|
</div><!-- fragment --><p>This is also an alias for the following:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> name = <a class="code" href="structentt_1_1type__name.html">entt::type_name<std::remove_cv_t<std::remove_reference_t<a_type></a>>>::value();</div>
|
|
<div class="ttc" id="astructentt_1_1type__name_html"><div class="ttname"><a href="structentt_1_1type__name.html">entt::type_name</a></div><div class="ttdoc">Type name.</div><div class="ttdef"><b>Definition:</b> <a href="type__info_8hpp_source.html#l00125">type_info.hpp:125</a></div></div>
|
|
</div><!-- fragment --><p>Where all accessed features are available at compile-time, the <code>type_info</code> class is also fully <code>constexpr</code>. However, this cannot be guaranteed in advance and depends mainly on the compiler in use and any specializations of the classes described above.</p>
|
|
<h3><a class="anchor" id="autotoc_md34"></a>
|
|
Almost unique identifiers</h3>
|
|
<p>Since the default non-standard, compile-time implementation of <code>type_hash</code> makes use of hashed strings, it may happen that two types are assigned the same hash value.<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. In this case, also <code>type_name</code> will return the same value for the two types.<br />
|
|
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. Runtime identifiers don't suffer from the same problem in fact. 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_name</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>
|
|
<h2><a class="anchor" id="autotoc_md35"></a>
|
|
Type traits</h2>
|
|
<p>A handful of utilities and traits not present in the standard template library but which can be useful in everyday life.<br />
|
|
This list <b>is not</b> exhaustive and contains only some of the most useful classes. Refer to the inline documentation for more information on the features offered by this module.</p>
|
|
<h3><a class="anchor" id="autotoc_md36"></a>
|
|
Size of</h3>
|
|
<p>The standard operator <code>sizeof</code> complains when users provide it for example with function or incomplete types. On the other hand, it's guaranteed that its result is always nonzero, even if applied to an empty class type.<br />
|
|
This small class combines the two and offers an alternative to <code>sizeof</code> that works under all circumstances, returning zero if the type isn't supported:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keyword">auto</span> size = entt::size_of_v<void>;</div>
|
|
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md37"></a>
|
|
Is applicable</h3>
|
|
<p>The standard library offers the great <code>std::is_invocable</code> trait in several forms. This takes a function type and a series of arguments and returns true if the condition is satisfied.<br />
|
|
Moreover, users are also provided with <code>std::apply</code>, a tool for combining invocable elements and tuples of arguments.</p>
|
|
<p>It would therefore be a good idea to have a variant of <code>std::is_invocable</code> that also accepts its arguments in the form of a tuple-like type, so as to complete the offer:</p>
|
|
<div class="fragment"><div class="line">constexpr <span class="keywordtype">bool</span> result = <a class="code" href="structentt_1_1is__applicable.html">entt::is_applicable<Func, std::tuple<a_type, another_type></a>>;</div>
|
|
<div class="ttc" id="astructentt_1_1is__applicable_html"><div class="ttname"><a href="structentt_1_1is__applicable.html">entt::is_applicable</a></div><div class="ttdoc">Same as std::is_invocable, but with tuples.</div><div class="ttdef"><b>Definition:</b> <a href="core_2type__traits_8hpp_source.html#l00378">type_traits.hpp:378</a></div></div>
|
|
</div><!-- fragment --><p>This trait is built on top of <code>std::is_invocable</code> and does nothing but unpack a tuple-like type and simplify the code at the call site.</p>
|
|
<h3><a class="anchor" id="autotoc_md38"></a>
|
|
Constness as</h3>
|
|
<p>An utility to easily transfer the constness of a type to another type:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// type is const dst_type because of the constness of src_type</span></div>
|
|
<div class="line"><span class="keyword">using</span> type = <a class="code" href="namespaceentt.html#a7b051461867d3c5c97f77f10b662c26b">entt::constness_as_t<dst_type, const src_type></a>;</div>
|
|
<div class="ttc" id="anamespaceentt_html_a7b051461867d3c5c97f77f10b662c26b"><div class="ttname"><a href="namespaceentt.html#a7b051461867d3c5c97f77f10b662c26b">entt::constness_as_t</a></div><div class="ttdeci">typename constness_as< To, From >::type constness_as_t</div><div class="ttdoc">Alias template to facilitate the transcription of the constness.</div><div class="ttdef"><b>Definition:</b> <a href="core_2type__traits_8hpp_source.html#l00618">type_traits.hpp:618</a></div></div>
|
|
</div><!-- fragment --><p>The trait is subject to the rules of the language. Therefore, for example, transferring constness between references won't give the desired effect.</p>
|
|
<h3><a class="anchor" id="autotoc_md39"></a>
|
|
Member class type</h3>
|
|
<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 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="core_2type__traits_8hpp_source.html#l00647">type_traits.hpp:647</a></div></div>
|
|
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md40"></a>
|
|
Integral constant</h3>
|
|
<p>Since <code>std::integral_constant</code> may be annoying because of its form that requires to specify both a type and a value of that type, there is a more user-friendly shortcut for the creation of integral constants.<br />
|
|
This shortcut is the alias template <code><a class="el" href="namespaceentt.html#a0d9fd5898acf13553bbcf14b99159f4d" title="Wraps a static constant.">entt::integral_constant</a></code>:</p>
|
|
<div class="fragment"><div class="line">constexpr <span class="keyword">auto</span> constant = <a class="code" href="namespaceentt.html#a0d9fd5898acf13553bbcf14b99159f4d">entt::integral_constant<42></a>;</div>
|
|
<div class="ttc" id="anamespaceentt_html_a0d9fd5898acf13553bbcf14b99159f4d"><div class="ttname"><a href="namespaceentt.html#a0d9fd5898acf13553bbcf14b99159f4d">entt::integral_constant</a></div><div class="ttdeci">std::integral_constant< decltype(Value), Value > integral_constant</div><div class="ttdoc">Wraps a static constant.</div><div class="ttdef"><b>Definition:</b> <a href="core_2type__traits_8hpp_source.html#l00096">type_traits.hpp:96</a></div></div>
|
|
</div><!-- fragment --><p>Among the other uses, when combined with a hashed string it helps to define tags as human-readable <em>names</em> where actual types would be required otherwise:</p>
|
|
<div class="fragment"><div class="line">constexpr <span class="keyword">auto</span> enemy_tag = <a class="code" href="namespaceentt.html#a0d9fd5898acf13553bbcf14b99159f4d">entt::integral_constant</a><<span class="stringliteral">"enemy"</span>_hs>;</div>
|
|
<div class="line"><a class="code" href="namespaceentt.html#a292643317d1dbb13e45824f757bd1086">registry</a>.emplace<enemy_tag>(<a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entity</a>);</div>
|
|
<div class="ttc" id="anamespaceentt_html_a0b54e231d069e8a231e14b223388808a"><div class="ttname"><a href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entt::entity</a></div><div class="ttdeci">entity</div><div class="ttdoc">Default entity identifier.</div><div class="ttdef"><b>Definition:</b> <a href="entity_2fwd_8hpp_source.html#l00047">fwd.hpp:47</a></div></div>
|
|
<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#l00060">fwd.hpp:60</a></div></div>
|
|
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md41"></a>
|
|
Tag</h3>
|
|
<p>Since <code>id_type</code> is very important and widely used in <code>EnTT</code>, 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#a9f7bb8c357f08a01ad9c8dab0ea40c1c" title="Alias template to facilitate 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>.emplace<<a class="code" href="namespaceentt.html#a9f7bb8c357f08a01ad9c8dab0ea40c1c">entt::tag</a><<span class="stringliteral">"enemy"</span>_hs>>(<a class="code" href="namespaceentt.html#a0b54e231d069e8a231e14b223388808a">entity</a>);</div>
|
|
<div class="ttc" id="anamespaceentt_html_a9f7bb8c357f08a01ad9c8dab0ea40c1c"><div class="ttname"><a href="namespaceentt.html#a9f7bb8c357f08a01ad9c8dab0ea40c1c">entt::tag</a></div><div class="ttdeci">integral_constant< Value > tag</div><div class="ttdoc">Alias template to facilitate the creation of named values.</div><div class="ttdef"><b>Definition:</b> <a href="core_2type__traits_8hpp_source.html#l00103">type_traits.hpp:103</a></div></div>
|
|
</div><!-- fragment --><p>However, this isn't the only permitted use. Literally any value convertible to <code>id_type</code> is a good candidate, such as the named constants of an unscoped enum.</p>
|
|
<h3><a class="anchor" id="autotoc_md42"></a>
|
|
Type list and value list</h3>
|
|
<p>There is no respectable library where the much desired <em>type list</em> can be missing.<br />
|
|
<code>EnTT</code> is no exception and provides (making extensive use of it internally) the <code>type_list</code> type, in addition to its <code>value_list</code> counterpart dedicated to non-type template parameters.</p>
|
|
<p>Here is a (possibly incomplete) list of the functionalities that come with a type list:</p>
|
|
<ul>
|
|
<li><code>type_list_element[_t]</code> to get the N-th element of a type list.</li>
|
|
<li><code>type_list_cat[_t]</code> and a handy <code>operator+</code> to concatenate type lists.</li>
|
|
<li><code>type_list_unique[_t]</code> to remove duplicate types from a type list.</li>
|
|
<li><code>type_list_contains[_v]</code> to know if a type list contains a given type.</li>
|
|
<li><code>type_list_diff[_t]</code> to remove types from type lists.</li>
|
|
</ul>
|
|
<p>I'm also pretty sure that more and more utilities will be added over time as needs become apparent.<br />
|
|
Many of these functionalities also exist in their version dedicated to value lists. We therefore have <code>value_list_element[_v]</code> as well as <code>value_list_cat[_t]</code>and so on.</p>
|
|
<h1><a class="anchor" id="autotoc_md43"></a>
|
|
Unique sequential identifiers</h1>
|
|
<p>Sometimes it's useful to be able to give unique, sequential numeric identifiers to types either at compile-time or runtime.<br />
|
|
There are plenty of different solutions for this out there and I could have used one of them. However, I decided to spend my time to define a couple of tools that fully embraces what the modern C++ has to offer.</p>
|
|
<h2><a class="anchor" id="autotoc_md44"></a>
|
|
Compile-time generator</h2>
|
|
<p>To generate sequential numeric identifiers at compile-time, <code>EnTT</code> offers the <code>identifier</code> class template:</p>
|
|
<div class="fragment"><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 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#l00041">ident.hpp:41</a></div></div>
|
|
</div><!-- fragment --><p>This is all what this class template has to offer: a <code>type</code> inline variable that contains a numeric 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 stable across different runs. 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>Perhaps a bit ugly to see in a codebase but it gets the job done at least.</p>
|
|
<h2><a class="anchor" id="autotoc_md45"></a>
|
|
Runtime generator</h2>
|
|
<p>To generate sequential numeric identifiers at runtime, <code>EnTT</code> offers the <code>family</code> class template:</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// defines a custom generator</span></div>
|
|
<div class="line"><span class="keyword">using</span> <span class="keywordtype">id</span> = <a class="code" href="classentt_1_1family.html">entt::family<struct my_tag></a>;</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="keyword">auto</span> a_type_id = id::type<a_type>;</div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keyword">auto</span> another_type_id = id::type<another_type>;</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#l00017">family.hpp:17</a></div></div>
|
|
</div><!-- fragment --><p>This is all what a <em>family</em> has to offer: a <code>type</code> inline variable that contains a numeric identifier for the given type.<br />
|
|
The generator is customizable, so as to get different <em>sequences</em> for different purposes if needed.</p>
|
|
<p>Please, note that identifiers aren't guaranteed to be stable across different runs. Indeed it mostly depends on the flow of execution.</p>
|
|
<h1><a class="anchor" id="autotoc_md46"></a>
|
|
Utilities</h1>
|
|
<p>It's not possible to escape the temptation to add utilities of some kind to a library. In fact, <code>EnTT</code> also provides a handful of tools to simplify the life of developers:</p>
|
|
<ul>
|
|
<li><code><a class="el" href="structentt_1_1identity.html" title="Identity function object (waiting for C++20).">entt::identity</a></code>: the identity function object that will be available with C++20. It returns its argument unchanged and nothing more. It's useful as a sort of <em>do nothing</em> function in template programming.</li>
|
|
<li><code><a class="el" href="namespaceentt.html#a386a739051d9d37a5578785b77fc426b" title="Constant utility to disambiguate overloaded members of a class.">entt::overload</a></code>: a tool to disambiguate different overloads from their function type. It works with both free and member functions.<br />
|
|
Consider the following definition:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><span class="keyword">struct </span>clazz {</div>
|
|
<div class="line"> <span class="keywordtype">void</span> bar(<span class="keywordtype">int</span>) {}</div>
|
|
<div class="line"> <span class="keywordtype">void</span> bar() {}</div>
|
|
<div class="line">};</div>
|
|
</div><!-- fragment --><p>This utility can be used to get the <em>right</em> overload as:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> *member = entt::overload<void(int)>(&clazz::bar);</div>
|
|
</div><!-- fragment --><p>The line above is literally equivalent to:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">auto</span> *member = <span class="keyword">static_cast<</span><span class="keywordtype">void</span>(clazz:: *)(<span class="keywordtype">int</span>)<span class="keyword">></span>(&clazz::bar);</div>
|
|
</div><!-- fragment --><p>Just easier to read and shorter to type.</p>
|
|
<ul>
|
|
<li><code><a class="el" href="structentt_1_1overloaded.html" title="Helper type for visitors.">entt::overloaded</a></code>: a small class template used to create a new type with an overloaded <code>operator()</code> from a bunch of lambdas or functors.<br />
|
|
As an example:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><a class="code" href="structentt_1_1overloaded.html">entt::overloaded</a> func{</div>
|
|
<div class="line"> [](<span class="keywordtype">int</span> value) { <span class="comment">/* ... */</span> },</div>
|
|
<div class="line"> [](<span class="keywordtype">char</span> value) { <span class="comment">/* ... */</span> }</div>
|
|
<div class="line">};</div>
|
|
<div class="line"> </div>
|
|
<div class="line">func(42);</div>
|
|
<div class="line">func(<span class="charliteral">'c'</span>);</div>
|
|
<div class="ttc" id="astructentt_1_1overloaded_html"><div class="ttname"><a href="structentt_1_1overloaded.html">entt::overloaded</a></div><div class="ttdoc">Helper type for visitors.</div><div class="ttdef"><b>Definition:</b> <a href="core_2utility_8hpp_source.html#l00054">utility.hpp:54</a></div></div>
|
|
</div><!-- fragment --><p>Rather useful when doing metaprogramming and having to pass to a function a callable object that supports multiple types at once.</p>
|
|
<ul>
|
|
<li><code><a class="el" href="structentt_1_1y__combinator.html" title="Basic implementation of a y-combinator.">entt::y_combinator</a></code>: this is a C++ implementation of <b>the</b> <em>y-combinator</em>. If it's not clear what it is, there is probably no need for this utility.<br />
|
|
Below is a small example to show its use:</li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><a class="code" href="structentt_1_1y__combinator.html">entt::y_combinator</a> gauss([](<span class="keyword">const</span> <span class="keyword">auto</span> &<span class="keyword">self</span>, <span class="keyword">auto</span> value) -> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> {</div>
|
|
<div class="line"> <span class="keywordflow">return</span> value ? (value + <span class="keyword">self</span>(value-1u)) : 0;</div>
|
|
<div class="line">});</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keyword">const</span> <span class="keyword">auto</span> result = gauss(3u);</div>
|
|
<div class="ttc" id="astructentt_1_1y__combinator_html"><div class="ttname"><a href="structentt_1_1y__combinator.html">entt::y_combinator</a></div><div class="ttdoc">Basic implementation of a y-combinator.</div><div class="ttdef"><b>Definition:</b> <a href="core_2utility_8hpp_source.html#l00070">utility.hpp:70</a></div></div>
|
|
</div><!-- fragment --><p>Maybe convoluted at a first glance but certainly effective. Unfortunately, the language doesn't make it possible to do much better.</p>
|
|
<p>This is a rundown of the (actually few) utilities made available by <code>EnTT</code>. The list will probably grow over time but the size of each will remain rather small, as has been the case so far. </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.9.1
|
|
</small></address>
|
|
</body>
|
|
</html>
|