Clarified use of unions in C++.

Change-Id: I9654e0c6a45457c8e150f07dd5f7b39539266f9e
This commit is contained in:
Wouter van Oortmerssen
2015-09-14 11:00:12 -07:00
parent 5db12e9907
commit af1487bcfb
3 changed files with 18 additions and 3 deletions

View File

@@ -117,7 +117,20 @@ $(document).ready(function(){initNavTree('md__java_usage.html','');});
<div class="line">var preconstructedPos = <span class="keyword">new</span> Vec3();</div>
<div class="line">monster.GetPos(preconstructedPos);</div>
</div><!-- fragment --><h2>Text parsing</h2>
<p>There currently is no support for parsing text (Schema's and JSON) directly from Java or C#, though you could use the C++ parser through native call interfaces available to each language. Please see the C++ documentation for more on text parsing. </p>
<p>There currently is no support for parsing text (Schema's and JSON) directly from Java or C#, though you could use the C++ parser through native call interfaces available to each language. Please see the C++ documentation for more on text parsing.</p>
<h3>Mutating FlatBuffers</h3>
<p>As you saw above, typically once you have created a FlatBuffer, it is read-only from that moment on. There are however cases where you have just received a FlatBuffer, and you'd like to modify something about it before sending it on to another recipient. With the above functionality, you'd have to generate an entirely new FlatBuffer, while tracking what you modify in your own data structures. This is inconvenient.</p>
<p>For this reason FlatBuffers can also be mutated in-place. While this is great for making small fixes to an existing buffer, you generally want to create buffers from scratch whenever possible, since it is much more efficient and the API is much more general purpose.</p>
<p>To get non-const accessors, invoke <code>flatc</code> with <code>--gen-mutable</code>.</p>
<p>You now can:</p>
<div class="fragment"><div class="line">Monster monster = Monster.getRootAsMonster(bb);</div>
<div class="line">monster.mutateHp(10); <span class="comment">// Set table field.</span></div>
<div class="line">monster.pos().mutateZ(4); <span class="comment">// Set struct field.</span></div>
<div class="line">monster.mutateInventory(0, 1); <span class="comment">// Set vector element.</span></div>
</div><!-- fragment --><p>We use the somewhat verbose term <code>mutate</code> instead of <code>set</code> to indicate that this is a special use case, not to be confused with the default way of constructing FlatBuffer data.</p>
<p>After the above mutations, you can send on the FlatBuffer to a new recipient without any further work!</p>
<p>Note that any <code>mutate</code> functions on tables return a boolean, which is false if the field we're trying to set isn't present in the buffer. Fields are not present if they weren't set, or even if they happen to be equal to the default value. For example, in the creation code above we set the <code>mana</code> field to <code>150</code>, which is the default value, so it was never stored in the buffer. Trying to call mutateMana() on such data will return false, and the value won't actually be modified!</p>
<p>One way to solve this is to call <code>forceDefaults()</code> on a <code>FlatBufferBuilder</code> to force all fields you set to actually be written. This of course increases the size of the buffer somewhat, but this may be acceptable for a mutable buffer. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- Google Analytics -->