265 lines
21 KiB
HTML
265 lines
21 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" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.6"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>AngelScript: Serializer</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="navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="resize.js"></script>
|
|
<script type="text/javascript" src="navtreedata.js"></script>
|
|
<script type="text/javascript" src="navtree.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>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function() { init_search(); });
|
|
/* @license-end */
|
|
</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 id="projectrow">
|
|
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
|
|
<td id="projectalign">
|
|
<div id="projectname">AngelScript
|
|
</div>
|
|
</td>
|
|
<td> <div id="MSearchBox" class="MSearchBoxInactive">
|
|
<span class="left">
|
|
<span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()"> </span>
|
|
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
|
|
onfocus="searchBox.OnSearchFieldFocus(true)"
|
|
onblur="searchBox.OnSearchFieldFocus(false)"
|
|
onkeyup="searchBox.OnSearchFieldChange(event)"/>
|
|
</span><span class="right">
|
|
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
|
|
</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.6 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function(){initNavTree('doc_addon_serializer.html',''); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- 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">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div><div class="header">
|
|
<div class="headertitle"><div class="title">Serializer </div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><p><b>Path:</b> /sdk/add_on/serializer/</p>
|
|
<p>The <code>CSerializer</code> implements support for serializing the values of global variables in a module, for example in order to reload a slightly modified version of the script without reinitializing everything. It will resolve primitives and script classes automatically, including references and handles.</p>
|
|
<p>For application registered types, the application needs to implement callback objects to show how these should be serialized. The exception is for pod types, as the serializer will simply keep a bitwise copy. Registered reference types without factories will not be serialized, i.e. the object will be kept as-is.</p>
|
|
<p>The implementation currently has some limitations:</p>
|
|
<ul>
|
|
<li>It can only serialize to memory, i.e. it is not possible to save the values to a file.</li>
|
|
<li>If the variables changed type when restoring, the serializer cannot restore the value.</li>
|
|
<li>The serializer will attempt to backup all objects, but in some cases an application may not want to backup the actual object, but only a reference to it, e.g. an internal application object referenced by the script. Currently there is no way of telling the serializer to do differently in this case.</li>
|
|
<li>If the module holds references to objects from another module it will probably fail in restoring the values.</li>
|
|
</ul>
|
|
<h1><a class="anchor" id="doc_addon_serializer_1"></a>
|
|
Public C++ interface</h1>
|
|
<div class="fragment"><div class="line"><span class="keyword">class </span>CSerializer</div>
|
|
<div class="line">{</div>
|
|
<div class="line"><span class="keyword">public</span>:</div>
|
|
<div class="line"> CSerializer();</div>
|
|
<div class="line"> ~CSerializer();</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Clear the serializer to free references held internally</span></div>
|
|
<div class="line"> <span class="keywordtype">void</span> Clear();</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Add implementation for serializing user types</span></div>
|
|
<div class="line"> <span class="keywordtype">void</span> AddUserType(CUserType *ref, <span class="keyword">const</span> std::string &name);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Store all global variables in the module</span></div>
|
|
<div class="line"> <span class="keywordtype">int</span> Store(<a class="code hl_class" href="classas_i_script_module.html">asIScriptModule</a> *mod);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Restore all global variables after reloading script</span></div>
|
|
<div class="line"> <span class="keywordtype">int</span> Restore(<a class="code hl_class" href="classas_i_script_module.html">asIScriptModule</a> *mod);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Store extra objects that are not seen from the module's global variables</span></div>
|
|
<div class="line"> <span class="keywordtype">void</span> AddExtraObjectToStore(<a class="code hl_class" href="classas_i_script_object.html">asIScriptObject</a> *<span class="keywordtype">object</span>);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Return new pointer to restored object</span></div>
|
|
<div class="line"> <span class="keywordtype">void</span> *GetPointerToRestoredObject(<span class="keywordtype">void</span> *originalObject);</div>
|
|
<div class="line">};</div>
|
|
<div class="ttc" id="aclassas_i_script_module_html"><div class="ttname"><a href="classas_i_script_module.html">asIScriptModule</a></div><div class="ttdoc">The interface to the script modules.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2232</div></div>
|
|
<div class="ttc" id="aclassas_i_script_object_html"><div class="ttname"><a href="classas_i_script_object.html">asIScriptObject</a></div><div class="ttdoc">The interface for an instance of a script object.</div><div class="ttdef"><b>Definition:</b> angelscript.h:3590</div></div>
|
|
</div><!-- fragment --><h1><a class="anchor" id="doc_addon_serializer_2"></a>
|
|
Example usage</h1>
|
|
<p>This first example shows how to serialize a script module that uses application registered types.</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">struct </span>CStringType;</div>
|
|
<div class="line"><span class="keyword">struct </span>CArrayType;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keywordtype">void</span> RecompileModule(<a class="code hl_class" href="classas_i_script_engine.html">asIScriptEngine</a> *engine, <a class="code hl_class" href="classas_i_script_module.html">asIScriptModule</a> *mod)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="keywordtype">string</span> modName = mod-><a class="code hl_function" href="classas_i_script_module.html#a9e1bae86cb54be53f90dbd352c6d04ed">GetName</a>();</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Tell the serializer how the user types should be serialized</span></div>
|
|
<div class="line"> <span class="comment">// by adding the implementations of the CUserType interface</span></div>
|
|
<div class="line"> CSerializer backup;</div>
|
|
<div class="line"> backup.AddUserType(<span class="keyword">new</span> CStringType(), <span class="stringliteral">"string"</span>);</div>
|
|
<div class="line"> backup.AddUserType(<span class="keyword">new</span> CArrayType(), <span class="stringliteral">"array"</span>);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Backup the values of the global variables</span></div>
|
|
<div class="line"> backup.Store(mod);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Application can now recompile the module</span></div>
|
|
<div class="line"> CompileModule(modName);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Restore the values of the global variables in the new module</span></div>
|
|
<div class="line"> mod = engine-><a class="code hl_function" href="classas_i_script_engine.html#a651dabbc259328ce32d6942621f27a27">GetModule</a>(modName.c_str(), <a class="code hl_enumvalue" href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca2feb963eb04c221e251867bc3a93d79d">asGM_ONLY_IF_EXISTS</a>);</div>
|
|
<div class="line"> backup.Restore(mod);</div>
|
|
<div class="line">}</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// This serializes the std::string type</span></div>
|
|
<div class="line"><span class="keyword">struct </span>CStringType : <span class="keyword">public</span> CUserType</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="keywordtype">void</span> *AllocateUnitializedMemory(CSerializedValue* value)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> <span class="comment">// This must not be done for strings</span></div>
|
|
<div class="line"> assert(<span class="keyword">false</span>);</div>
|
|
<div class="line"> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"> } </div>
|
|
<div class="line"> <span class="keywordtype">void</span> Store(CSerializedValue *val, <span class="keywordtype">void</span> *ptr)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> val->SetUserData(<span class="keyword">new</span> std::string(*(std::string*)ptr));</div>
|
|
<div class="line"> }</div>
|
|
<div class="line"> <span class="keywordtype">void</span> Restore(CSerializedValue *val, <span class="keywordtype">void</span> *ptr)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> std::string *buffer = (std::string*)val->GetUserData();</div>
|
|
<div class="line"> *(std::string*)ptr = *buffer;</div>
|
|
<div class="line"> }</div>
|
|
<div class="line"> <span class="keywordtype">void</span> CleanupUserData(CSerializedValue *val)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> std::string *buffer = (std::string*)val->GetUserData();</div>
|
|
<div class="line"> <span class="keyword">delete</span> buffer;</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="comment">// This serializes the CScriptArray type</span></div>
|
|
<div class="line"><span class="keyword">struct </span>CArrayType : <span class="keyword">public</span> CUserType</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="keywordtype">void</span>* AllocateUnitializedMemory(CSerializedValue* value)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> CScriptArray* arr = CScriptArray::Create(value->GetType());</div>
|
|
<div class="line"> <span class="keywordflow">return</span> arr;</div>
|
|
<div class="line"> }</div>
|
|
<div class="line"> <span class="keywordtype">void</span> Store(CSerializedValue *val, <span class="keywordtype">void</span> *ptr)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> CScriptArray *arr = (CScriptArray*)ptr;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="keywordflow">for</span>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i < arr->GetSize(); i++ )</div>
|
|
<div class="line"> val->m_children.push_back(<span class="keyword">new</span> CSerializedValue(val ,<span class="stringliteral">""</span>, <span class="stringliteral">""</span>, arr->At(i), arr->GetElementTypeId()));</div>
|
|
<div class="line"> }</div>
|
|
<div class="line"> <span class="keywordtype">void</span> Restore(CSerializedValue *val, <span class="keywordtype">void</span> *ptr)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> CScriptArray *arr = (CScriptArray*)ptr;</div>
|
|
<div class="line"> arr->Resize(<a class="code hl_typedef" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a>(val->m_children.size()));</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="keywordflow">for</span>( <span class="keywordtype">size_t</span> i = 0; i < val->m_children.size(); ++i )</div>
|
|
<div class="line"> val->m_children[i]->Restore(arr->At(<a class="code hl_typedef" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a>(i)), arr->GetElementTypeId());</div>
|
|
<div class="line"> }</div>
|
|
<div class="line">};</div>
|
|
<div class="ttc" id="aangelscript_8h_html_ac8186f029686800b7ce36bde4a55c815"><div class="ttname"><a href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815">asUINT</a></div><div class="ttdeci">unsigned int asUINT</div><div class="ttdoc">32 bit unsigned integer</div><div class="ttdef"><b>Definition:</b> angelscript.h:610</div></div>
|
|
<div class="ttc" id="aangelscript_8h_html_ae4cf50de5273eb8c03c6e91e6e014f0ca2feb963eb04c221e251867bc3a93d79d"><div class="ttname"><a href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca2feb963eb04c221e251867bc3a93d79d">asGM_ONLY_IF_EXISTS</a></div><div class="ttdeci">@ asGM_ONLY_IF_EXISTS</div><div class="ttdoc">Don't return any module if it is not found.</div><div class="ttdef"><b>Definition:</b> angelscript.h:530</div></div>
|
|
<div class="ttc" id="aclassas_i_script_engine_html"><div class="ttname"><a href="classas_i_script_engine.html">asIScriptEngine</a></div><div class="ttdoc">The engine interface.</div><div class="ttdef"><b>Definition:</b> angelscript.h:1102</div></div>
|
|
<div class="ttc" id="aclassas_i_script_engine_html_a651dabbc259328ce32d6942621f27a27"><div class="ttname"><a href="classas_i_script_engine.html#a651dabbc259328ce32d6942621f27a27">asIScriptEngine::GetModule</a></div><div class="ttdeci">virtual asIScriptModule * GetModule(const char *module, asEGMFlags flag=asGM_ONLY_IF_EXISTS)=0</div><div class="ttdoc">Return an interface pointer to the module.</div></div>
|
|
<div class="ttc" id="aclassas_i_script_module_html_a9e1bae86cb54be53f90dbd352c6d04ed"><div class="ttname"><a href="classas_i_script_module.html#a9e1bae86cb54be53f90dbd352c6d04ed">asIScriptModule::GetName</a></div><div class="ttdeci">virtual const char * GetName() const =0</div><div class="ttdoc">Gets the name of the module.</div></div>
|
|
</div><!-- fragment --><p>The following example shows how to serialize additional objects, not stored in the module itself.</p>
|
|
<div class="fragment"><div class="line"><span class="comment">// This object is stored outside of the module, so it has to be explicitly informed to the serializer</span></div>
|
|
<div class="line"><a class="code hl_class" href="classas_i_script_object.html">asIScriptObject</a> *scriptObj = <span class="keyword">reinterpret_cast<</span><a class="code hl_class" href="classas_i_script_object.html">asIScriptObject</a>*<span class="keyword">></span>(engine-><a class="code hl_function" href="classas_i_script_engine.html#a1a2b993e219d72c39181927c708d2aea">CreateScriptObject</a>(mod-><a class="code hl_function" href="classas_i_script_module.html#a530ac35b11b18e78629f907af3aabfcd">GetTypeInfoByName</a>(<span class="stringliteral">"CTest"</span>)));</div>
|
|
<div class="line"> </div>
|
|
<div class="line"><span class="keywordtype">void</span> RecompileModule(<a class="code hl_class" href="classas_i_script_engine.html">asIScriptEngine</a> *engine, <a class="code hl_class" href="classas_i_script_module.html">asIScriptModule</a> *mod)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="keywordtype">string</span> modName = mod-><a class="code hl_function" href="classas_i_script_module.html#a9e1bae86cb54be53f90dbd352c6d04ed">GetName</a>();</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Tell the serializer about the additional object stored outside of the module</span></div>
|
|
<div class="line"> CSerializer backup;</div>
|
|
<div class="line"> backup.AddExtraObjectToStore(scriptObj);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Backup the values of the global variables and the additional object</span></div>
|
|
<div class="line"> r = backup.Store(mod);</div>
|
|
<div class="line"> <span class="keywordflow">if</span>( r < 0 )</div>
|
|
<div class="line"> TEST_FAILED;</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Application can now recompile the module</span></div>
|
|
<div class="line"> CompileModule(modName);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Restore the values of the global variables in the new module</span></div>
|
|
<div class="line"> mod = engine-><a class="code hl_function" href="classas_i_script_engine.html#a651dabbc259328ce32d6942621f27a27">GetModule</a>(modName.c_str(), <a class="code hl_enumvalue" href="angelscript_8h.html#ae4cf50de5273eb8c03c6e91e6e014f0ca2feb963eb04c221e251867bc3a93d79d">asGM_ONLY_IF_EXISTS</a>);</div>
|
|
<div class="line"> backup.Restore(mod);</div>
|
|
<div class="line"> </div>
|
|
<div class="line"> <span class="comment">// Restore the extra object by looking up the new instance using the address of the old instance as key</span></div>
|
|
<div class="line"> <a class="code hl_class" href="classas_i_script_object.html">asIScriptObject</a> *obj2 = (<a class="code hl_class" href="classas_i_script_object.html">asIScriptObject</a>*)backup.GetPointerToRestoredObject(scriptObj);</div>
|
|
<div class="line"> scriptObj-><a class="code hl_function" href="classas_i_script_object.html#a4bed3c3ac9f16294985835747aa122d3">Release</a>();</div>
|
|
<div class="line"> scriptObj = obj2;</div>
|
|
<div class="line"> scriptObj-><a class="code hl_function" href="classas_i_script_object.html#a3e08890e31163e4d33c0f27dc9072662">AddRef</a>();</div>
|
|
<div class="line">}</div>
|
|
<div class="ttc" id="aclassas_i_script_engine_html_a1a2b993e219d72c39181927c708d2aea"><div class="ttname"><a href="classas_i_script_engine.html#a1a2b993e219d72c39181927c708d2aea">asIScriptEngine::CreateScriptObject</a></div><div class="ttdeci">virtual void * CreateScriptObject(const asITypeInfo *type)=0</div><div class="ttdoc">Creates an object defined by its type.</div></div>
|
|
<div class="ttc" id="aclassas_i_script_module_html_a530ac35b11b18e78629f907af3aabfcd"><div class="ttname"><a href="classas_i_script_module.html#a530ac35b11b18e78629f907af3aabfcd">asIScriptModule::GetTypeInfoByName</a></div><div class="ttdeci">virtual asITypeInfo * GetTypeInfoByName(const char *name) const =0</div><div class="ttdoc">Returns the type interface by name.</div></div>
|
|
<div class="ttc" id="aclassas_i_script_object_html_a3e08890e31163e4d33c0f27dc9072662"><div class="ttname"><a href="classas_i_script_object.html#a3e08890e31163e4d33c0f27dc9072662">asIScriptObject::AddRef</a></div><div class="ttdeci">virtual int AddRef() const =0</div><div class="ttdoc">Increase reference counter.</div></div>
|
|
<div class="ttc" id="aclassas_i_script_object_html_a4bed3c3ac9f16294985835747aa122d3"><div class="ttname"><a href="classas_i_script_object.html#a4bed3c3ac9f16294985835747aa122d3">asIScriptObject::Release</a></div><div class="ttdeci">virtual int Release() const =0</div><div class="ttdoc">Decrease reference counter.</div></div>
|
|
</div><!-- fragment --> </div></div><!-- contents -->
|
|
</div><!-- PageDoc -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="footer">Generated on Sun May 21 2023 10:12:39 for AngelScript by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|