cereal/assets/doxygen/classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html
2022-02-27 19:48:38 -08:00

175 lines
10 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.17"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cereal: cereal::memory_detail::EnableSharedStateHelper&lt; T &gt; Class Template Reference</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">cereal
</div>
<div id="projectbrief">A C++11 library for serialization</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.17 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><b>cereal</b></li><li class="navelem"><b>memory_detail</b></li><li class="navelem"><a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html">EnableSharedStateHelper</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper-members.html">List of all members</a> </div>
<div class="headertitle">
<div class="title">cereal::memory_detail::EnableSharedStateHelper&lt; T &gt; Class Template Reference</div> </div>
</div><!--header-->
<div class="contents">
<p><code>#include &lt;<a class="el" href="memory_8hpp_source.html">/home/shane/workspace/cereal/include/cereal/types/memory.hpp</a>&gt;</code></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:a7358952ac850c005295f899a5aa3b184"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html#a7358952ac850c005295f899a5aa3b184">EnableSharedStateHelper</a> (T *ptr)</td></tr>
<tr class="memdesc:a7358952ac850c005295f899a5aa3b184"><td class="mdescLeft">&#160;</td><td class="mdescRight">Saves the state of some type inheriting from enable_shared_from_this. <a href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html#a7358952ac850c005295f899a5aa3b184">More...</a><br /></td></tr>
<tr class="separator:a7358952ac850c005295f899a5aa3b184"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae9cd690a665f3c864b6963f9b69b64cf"><td class="memItemLeft" align="right" valign="top"><a id="ae9cd690a665f3c864b6963f9b69b64cf"></a>
void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html#ae9cd690a665f3c864b6963f9b69b64cf">restore</a> ()</td></tr>
<tr class="memdesc:ae9cd690a665f3c864b6963f9b69b64cf"><td class="mdescLeft">&#160;</td><td class="mdescRight">Restores the state of the held pointer (can only be done once) <br /></td></tr>
<tr class="separator:ae9cd690a665f3c864b6963f9b69b64cf"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a33651283607ae0b7cf4eb7918931a98a"><td class="memItemLeft" align="right" valign="top"><a id="a33651283607ae0b7cf4eb7918931a98a"></a>
&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html#a33651283607ae0b7cf4eb7918931a98a">~EnableSharedStateHelper</a> ()</td></tr>
<tr class="memdesc:a33651283607ae0b7cf4eb7918931a98a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Restores the state of the held pointer if not done previously. <br /></td></tr>
<tr class="separator:a33651283607ae0b7cf4eb7918931a98a"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><h3>template&lt;class T&gt;<br />
class cereal::memory_detail::EnableSharedStateHelper&lt; T &gt;</h3>
<p>A helper struct for saving and restoring the state of types that derive from std::enable_shared_from_this</p>
<p>This special struct is necessary because when a user uses load_and_construct, the weak_ptr (or whatever implementation defined variant) that allows enable_shared_from_this to function correctly will not be initialized properly.</p>
<p>This internal weak_ptr can also be modified by the shared_ptr that is created during the serialization of a polymorphic pointer, where cereal creates a wrapper shared_ptr out of a void pointer to the real data.</p>
<p>In the case of load_and_construct, this happens because it is the allocation of shared_ptr that perform this initialization, which we let happen on a buffer of memory (aligned_storage). This buffer is then used for placement new later on, effectively overwriting any initialized weak_ptr with a default initialized one, eventually leading to issues when the user calls shared_from_this.</p>
<p>To get around these issues, we will store the memory for the enable_shared_from_this portion of the class and replace it after whatever happens to modify it (e.g. the user performing construction or the wrapper shared_ptr in saving).</p>
<p>Note that this goes into undefined behavior territory, but as of the initial writing of this, all standard library implementations of std::enable_shared_from_this are compatible with this memory manipulation. It is entirely possible that this may someday break or may not work with convoluted use cases.</p>
<p>Example usage:</p>
<div class="fragment"><div class="line">T * myActualPointer;</div>
<div class="line">{</div>
<div class="line"> EnableSharedStateHelper&lt;T&gt; helper( myActualPointer ); <span class="comment">// save the state</span></div>
<div class="line"> std::shared_ptr&lt;T&gt; myPtr( myActualPointer ); <span class="comment">// modifies the internal weak_ptr</span></div>
<div class="line"> <span class="comment">// helper restores state when it goes out of scope</span></div>
<div class="line">}</div>
</div><!-- fragment --><p>When possible, this is designed to be used in an RAII fashion - it will save state on construction and restore it on destruction. The restore can be done at an earlier time (e.g. after construct() is called in load_and_construct) in which case the destructor will do nothing. Performing the restore immediately following construct() allows a user to call shared_from_this within their load_and_construct function.</p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
<table class="tparams">
<tr><td class="paramname">T</td><td>Type pointed to by shared_ptr </td></tr>
</table>
</dd>
</dl>
</div><h2 class="groupheader">Constructor &amp; Destructor Documentation</h2>
<a id="a7358952ac850c005295f899a5aa3b184"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7358952ac850c005295f899a5aa3b184">&#9670;&nbsp;</a></span>EnableSharedStateHelper()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class T &gt; </div>
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname"><a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html">cereal::memory_detail::EnableSharedStateHelper</a>&lt; T &gt;::<a class="el" href="classcereal_1_1memory__detail_1_1EnableSharedStateHelper.html">EnableSharedStateHelper</a> </td>
<td>(</td>
<td class="paramtype">T *&#160;</td>
<td class="paramname"><em>ptr</em></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">inline</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Saves the state of some type inheriting from enable_shared_from_this. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">ptr</td><td>The raw pointer held by the shared_ptr </td></tr>
</table>
</dd>
</dl>
</div>
</div>
<hr/>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="memory_8hpp_source.html">memory.hpp</a></li>
</ul>
</div><!-- contents -->
<!-- HTML footer for doxygen 1.8.3.1-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Sun Feb 27 2022 19:46:46 for cereal by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.17
</small></address>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-42360142-1', 'uscilab.github.io');
ga('send', 'pageview');
</script>
</body>
</html>