autodoc.git / traditional_manual / chapter_9.html

version» Context lines:

autodoc.git/traditional_manual/chapter_9.html:4434:   </dt>   <dd><p><code><code class='object unresolved'>Buffer</code> <b><span class='method'>add_hstring</span>(</b><code class='datatype'>string(8bit)</code> <code class='argument'>data</code>, <code class='datatype'>int</code> <code class='argument'>size_size</code><b>)</b></code><br>   <code><code class='object unresolved'>Buffer</code> <b><span class='method'>add_hstring</span>(</b><code class='object unresolved'>Stdio.Buffer</code> <code class='argument'>data</code>, <code class='datatype'>int</code> <code class='argument'>size_size</code><b>)</b></code><br>   <code><code class='object unresolved'>Buffer</code> <b><span class='method'>add_hstring</span>(</b><code class='object unresolved'>System.Memory</code> <code class='argument'>data</code>, <code class='datatype'>int</code> <code class='argument'>size_size</code><b>)</b></code><br>   <code><code class='object unresolved'>Buffer</code> <b><span class='method'>add_hstring</span>(</b><code class='object unresolved'>String.Buffer</code> <code class='argument'>data</code>, <code class='datatype'>int</code> <code class='argument'>size_size</code><b>)</b></code><br>   <code><code class='object unresolved'>Buffer</code> <b><span class='method'>add_hstring</span>(</b><code class='datatype'>array</code> <code class='argument'>data</code>, <code class='datatype'>int</code> <code class='argument'>size_size</code><b>)</b></code></p></dd>      <dt class='head--doc'>Description</dt>   <dd class='body--doc'><p>Adds length of data followed by <code>data</code> to the buffer.</p>   <p> This is identical to -  <tt>sprintf("%"+size_size+"H",(string)Stdio.IObuffer(data))</tt> but +  <tt>sprintf("%"+size_size+"H",(string)Stdio.Buffer(data))</tt> but    significantly faster.</p>   <p> <code>size_size</code> is the number of bytes used to represent the length of the data.    It must be less than Int.NATIVE_MAX.</p>   <p> The supported <code>data</code> argument types are</p>   <table class='box'><tr><td><code><code class='datatype'>string(8bit)</code></code></td><td><p>An eight bit string.</p>   </td></tr>   <tr><td><code><code class='object unresolved'>System.Memory</code></code></td><td><p>A chunk of memory. The whole memory area is added.</p>   </td></tr>   <tr><td><code><code class='object unresolved'>Stdio.Buffer</code></code></td><td><p>A chunk of memory. The whole memory area is added.</p>   </td></tr>
autodoc.git/traditional_manual/chapter_9.html:5007:    buffer from being rewound.</p>   <p> The second version (<code>rewind_key</code>) requires you to explicitly    call <code>RewindKey.rewind</code> to do the rewind.</p>   <p> Take some care with these objects, if you create multiple ones    at once the results might be somewhat confusing if you do not    release them in the reverse order they were created in (then    again, you almost certainly really only need one)</p>   <p> You can call <code>RewindKey.update</code> in the generated object to    change where it will be rewound to.</p>   <p> The typical use-case of this functionality is when parsing a -  packet protocol with variable length packets where the lenght is +  packet protocol with variable length packets where the length is    not immediately known. It saves you from keeping track of how    much to rewind if you had not actually gotten the whole packet    yet.</p>   </dd>   <dt class='head--doc'>Example</dt>   <dd class='example'><pre><pre><code><span class='type'>void</span> parse_packet<span class='delim'>(</span> <span class='ns'>Stdio</span><span class='delim'>.</span>Buffer b <span class='delim'>)</span>   <span class='delim'>{</span>    <span class='ns'>Stdio</span><span class='delim'>.</span>Buffer<span class='delim'>.</span>RewindKey rewind <span class='delim'>=</span> b-&gt;rewind_on_error<span class='delim'>(</span><span class='delim'>)</span><span class='delim'>;</span>    b-&gt;set_error_mode<span class='delim'>(</span>1<span class='delim'>)</span><span class='delim'>;</span>       <span class='lang'>switch</span><span class='delim'>(</span> b-&gt;read_int8<span class='delim'>(</span><span class='delim'>)</span> <span class='delim'>)</span> <span class='comment'>// packet type</span>    <span class='delim'>{</span>    <span class='lang'>case</span> DATA<span class='delim'>:</span>    <span class='type'>int</span> channel <span class='delim'>=</span> b-&gt;read_int8<span class='delim'>(</span><span class='delim'>)</span><span class='delim'>;</span>    <span class='ns'>Stdio</span><span class='delim'>.</span>Buffer data <span class='delim'>=</span> b-&gt;read_hbuffer<span class='delim'>(</span> 4 <span class='delim'>)</span><span class='delim'>;</span>    <span class='comment'>// we have read the whole packet, so no longer rewind on error.</span>    rewind-&gt;release<span class='delim'>(</span><span class='delim'>)</span><span class='delim'>;</span> -  <span class='lang'>return</span> handle_data_packet<span class='delim'>(</span> chennel<span class='delim'>,</span> data <span class='delim'>)</span><span class='delim'>;</span> +  <span class='lang'>return</span> handle_data_packet<span class='delim'>(</span> channel<span class='delim'>,</span> data <span class='delim'>)</span><span class='delim'>;</span>    <span class='delim'>}</span>   <span class='delim'>}</span>   </code></pre></pre></dd>   <dt class='head--doc'>Note</dt>   <dd class='body--doc'><p>Just calling <code>rewind_on_error</code> without assigning the return    value to something will not do anything. You need to keep the    object around while the rewind-to position is still valid.</p>   <p> Keeping the object around forbids the buffer from moving data    inside itself, this means that it can only grow. So do not keep    the rewind key when it is not needed.</p>