unity.scopes.VariantBuilder
Helper class for creating and populating Variant containers. More...
#include <unity/scopes/VariantBuilder.h>
Public Member Functions | |
void | add_tuple (std::initializer_list< std::pair< std::string, Variant >> const &tuple) |
Adds a tuple of key-value pairs to an array. More... | |
void | add_tuple (std::vector< std::pair< std::string, Variant >> const &tuple) |
Adds a tuple of key-value pairs to an array. More... | |
Variant | end () |
Retrieves a completed variant. More... | |
Copy and assignment | |
Copy and assignment operators (move and non-move versions) have the usual value semantics. | |
VariantBuilder (VariantBuilder const &other) | |
VariantBuilder (VariantBuilder &&other) | |
VariantBuilder & | operator= (VariantBuilder const &other) |
VariantBuilder & | operator= (VariantBuilder &&other) |
Detailed Description
Helper class for creating and populating Variant containers.
The main purpose of this class is to ease creation of variant containers needed for PreviewWidget instances or any other classes that require non-trivial variant definitions.
For example, the value of the "rating" key of the following JSON template
{ <span class="stringliteral">"type"</span>: <span class="stringliteral">"reviews"</span>, ... <span class="stringliteral">"reviews"</span>: [{<span class="stringliteral">"rating"</span>: null, <span class="stringliteral">"review"</span>: null, <span class="stringliteral">"author"</span>: null}] }
can be created with VariantBuilder as follows:
VariantBuilder builder; builder.add_tuple({{<span class="stringliteral">"rating"</span>, <a class="code" href="unity.scopes.Variant.md#a2bd2d5425fdec9af9340c22e3b47ac1c">Variant::null</a>())}, {<span class="stringliteral">"review"</span>, <a class="code" href="unity.scopes.Variant.md#a2bd2d5425fdec9af9340c22e3b47ac1c">Variant::null</a>()}, {<span class="stringliteral">"author"</span>, <a class="code" href="unity.scopes.Variant.md#a2bd2d5425fdec9af9340c22e3b47ac1c">Variant::null</a>()}});
Member Function Documentation
void unity::scopes::VariantBuilder::add_tuple | ( | std::initializer_list< std::pair< std::string, Variant >> const & | tuple | ) |
Adds a tuple of key-value pairs to an array.
It can be used multiple times to create an array of tuples, for example:
[{<span class="stringliteral">"a"</span>: 1, <span class="stringliteral">"b"</span>: 2}, {<span class="stringliteral">"c"</span>: 2, <span class="stringliteral">"d"</span> : 3}]
can be created with:
VariantBuilder builder; builder.add_tuple({{<span class="stringliteral">"a"</span>, Variant(1)}, {<span class="stringliteral">"b"</span>, Variant(2)}}); builder.add_tuple({{<span class="stringliteral">"c"</span>, Variant(2)}, {<span class="stringliteral">"d"</span>, Variant(3)}});
void unity::scopes::VariantBuilder::add_tuple | ( | std::vector< std::pair< std::string, Variant >> const & | tuple | ) |
Adds a tuple of key-value pairs to an array.
This is an overloaded version of add_tuple that accepts std::vector
instead of std::initializer_list
, making it more convenient for language bindings.
Variant unity::scopes::VariantBuilder::end | ( | ) |
Retrieves a completed variant.
Returns the completed variant and resets this builder, so the builder can be re-used.
- Returns
- The completed variant.
- Exceptions
-
unity::LogicException if the builder does not hold a variant.