Yaml Value¶
| Spice | |
|---|---|
YamlValue struct¶
Represents a single YAML node.
Composite nodes (sequences and mappings) own their children as heap pointers, so a YamlValue is always reached via YamlValue* once it has been parsed. Destroying a node recursively destroys everything below it, so a whole tree is released with a single deleteYamlValue call on its root. Accessors like getField hand out borrowed pointers that stay owned by their parent, while addSequenceItem and addMappingField take ownership of what they are given.
Note that the root cannot simply be held in a heap YamlValue* and left to the compiler: the heap qualifier only emits a raw deallocation when the variable goes out of scope and never runs the destructor of the pointee, so the children below the root would be lost.
Following the YAML 1.2 core schema, plain scalars resolve to null, booleans, integers or floats and everything else stays a string, while quoted scalars are always strings. Mapping entries preserve document order, and unlike TOML tables a mapping may hold an explicit null value.
Parsing lives in "std/text/yaml/yaml-parser" and serialization in "std/text/yaml/yaml-serializer", so programs only pull in what they use.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty YAML mapping, which is what a YAML document is usually rooted at
ctor¶
| Spice | |
|---|---|
Construct an empty YAML node of the given kind. Useful for building sequences and mappings from scratch.
Parameters
| Name | Type | Description |
|---|---|---|
kind |
YamlValueKind |
Kind of the node |
ctor¶
| Spice | |
|---|---|
Construct a YAML string node
Parameters
| Name | Type | Description |
|---|---|---|
value |
const String& |
String value |
ctor¶
| Spice | |
|---|---|
Construct a YAML boolean node
Parameters
| Name | Type | Description |
|---|---|---|
value |
bool |
Boolean value |
ctor¶
| Spice | |
|---|---|
Construct a YAML integer node
Parameters
| Name | Type | Description |
|---|---|---|
value |
long |
Integer value |
ctor¶
| Spice | |
|---|---|
Construct a YAML float node
Parameters
| Name | Type | Description |
|---|---|---|
value |
double |
Float value |
dtor¶
| Spice | |
|---|---|
Destruct the YAML node, releasing every sequence item and mapping entry it owns. Since the children are destroyed the same way, this tears down the whole subtree below the node.
Methods¶
getKind¶
| Spice | |
|---|---|
Retrieve the kind of the YAML node
Returns: YamlValueKind — Kind of the node
isNull¶
| Spice | |
|---|---|
Check whether the YAML node is null
Returns: bool — true if the node is null, false otherwise
isString¶
| Spice | |
|---|---|
Check whether the YAML node is a string
Returns: bool — true if the node is a string, false otherwise
isInteger¶
| Spice | |
|---|---|
Check whether the YAML node is an integer
Returns: bool — true if the node is an integer, false otherwise
isFloat¶
| Spice | |
|---|---|
Check whether the YAML node is a float
Returns: bool — true if the node is a float, false otherwise
isBool¶
| Spice | |
|---|---|
Check whether the YAML node is a boolean
Returns: bool — true if the node is a boolean, false otherwise
isSequence¶
| Spice | |
|---|---|
Check whether the YAML node is a sequence
Returns: bool — true if the node is a sequence, false otherwise
isMapping¶
| Spice | |
|---|---|
Check whether the YAML node is a mapping
Returns: bool — true if the node is a mapping, false otherwise
getString¶
| Spice | |
|---|---|
Retrieve the string payload. Panics if the node is not a string.
Returns: const String& — String value
getInteger¶
| Spice | |
|---|---|
Retrieve the integer payload. Panics if the node is not an integer.
Returns: long — Integer value
getFloat¶
| Spice | |
|---|---|
Retrieve the float payload. Panics if the node is not a float.
Returns: double — Float value
getBool¶
| Spice | |
|---|---|
Retrieve the boolean payload. Panics if the node is not a boolean.
Returns: bool — Boolean value
getSequenceSize¶
| Spice | |
|---|---|
Retrieve the number of items in the sequence. Panics if the node is not a sequence.
Returns: unsigned long — Number of sequence items
getSequenceItem¶
| Spice | |
|---|---|
Retrieve the sequence item at the given index. Panics if the node is not a sequence.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the item |
Returns: YamlValue* — Pointer to the sequence item
getSequenceItem¶
| Spice | |
|---|---|
Retrieve the sequence item at the given index. Panics if the node is not a sequence.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned int |
Index of the item |
Returns: YamlValue* — Pointer to the sequence item
addSequenceItem¶
| Spice | |
|---|---|
Append an item to the sequence. Panics if the node is not a sequence. The sequence takes ownership of the heap-allocated item.
Parameters
| Name | Type | Description |
|---|---|---|
item |
YamlValue* |
Item to append |
getMappingSize¶
| Spice | |
|---|---|
Retrieve the number of entries in the mapping. Panics if the node is not a mapping.
Returns: unsigned long — Number of mapping entries
getMappingKey¶
| Spice | |
|---|---|
Retrieve the key of the mapping entry at the given index, in document order. Panics if the node is not a mapping.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the entry |
Returns: const String& — Entry key
getMappingValue¶
| Spice | |
|---|---|
Retrieve the value of the mapping entry at the given index, in document order. Panics if the node is not a mapping.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the entry |
Returns: YamlValue* — Pointer to the entry value
hasField¶
| Spice | |
|---|---|
Check whether the mapping contains an entry with the given key
Parameters
| Name | Type | Description |
|---|---|---|
key |
const String& |
Entry key to look for |
Returns: bool — true if the entry exists, false otherwise
hasField¶
| Spice | |
|---|---|
Check whether the mapping contains an entry with the given key
Parameters
| Name | Type | Description |
|---|---|---|
key |
string |
Entry key to look for |
Returns: bool — true if the entry exists, false otherwise
getFieldOrNil¶
| Spice | |
|---|---|
Retrieve the value of the mapping entry with the given key, or nil if the node is not a mapping or the entry does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const String& |
Entry key to look up |
Returns: YamlValue* — Pointer to the entry value, or nil
getFieldOrNil¶
| Spice | |
|---|---|
Retrieve the value of the mapping entry with the given key, or nil if the node is not a mapping or the entry does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
key |
string |
Entry key to look up |
Returns: YamlValue* — Pointer to the entry value, or nil
getField¶
| Spice | |
|---|---|
Retrieve the value of the mapping entry with the given key. Panics if the node is not a mapping or the entry does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const String& |
Entry key to look up |
Returns: YamlValue* — Pointer to the entry value
getField¶
| Spice | |
|---|---|
Retrieve the value of the mapping entry with the given key. Panics if the node is not a mapping or the entry does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
key |
string |
Entry key to look up |
Returns: YamlValue* — Pointer to the entry value
addMappingField¶
| Spice | |
|---|---|
Append an entry to the mapping. Panics if the node is not a mapping. The mapping takes ownership of the heap-allocated entry value.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const String& |
Entry key |
value |
YamlValue* |
Entry value |
Functions¶
deleteYamlValue¶
| Spice | |
|---|---|
Destroys a heap-allocated YAML node together with everything it owns and sets the given handle to nil, so releasing a whole tree only takes one call on its root.
Note that this is what sDelete would normally be used for. It cannot be used here, because a node tree is destroyed recursively and the compiler does not emit an explicit dtor that is only reached through sDestruct.
Parameters
| Name | Type | Description |
|---|---|---|
value |
YamlValue*& |
Handle of the node to destroy |
copyYamlValue¶
| Spice | |
|---|---|
Creates a deep copy of the given node, which the caller owns and releases again with deleteYamlValue. This is what the parser uses to resolve an alias against its anchor, so that the resulting tree stays free of shared nodes.
Parameters
| Name | Type | Description |
|---|---|---|
value |
YamlValue* |
Node to copy |
Returns: YamlValue* — Pointer to the freshly allocated copy
yamlPlainScalarKind¶
| Spice | |
|---|---|
Determine which kind a plain scalar resolves to under the YAML 1.2 core schema. Both the parser and the serializer go through this, so that a string which would be read back as another kind can be quoted when it is written out again.
Parameters
| Name | Type | Description |
|---|---|---|
text |
const String& |
Literal text of the plain scalar |
Returns: YamlValueKind — Kind the scalar resolves to, never YAML_SEQUENCE or YAML_MAPPING
resolveYamlScalar¶
| Spice | |
|---|---|
Build the node a plain scalar denotes under the YAML 1.2 core schema. The caller owns the returned node and releases it again with deleteYamlValue.
Parameters
| Name | Type | Description |
|---|---|---|
text |
const String& |
Literal text of the plain scalar |
Returns: YamlValue* — Pointer to the freshly allocated node
yamlInfinity¶
| Spice | |
|---|---|
Retrieve positive infinity, which YAML spells .inf. Spice has no literal for it, so it is provided here for building and inspecting float values.
Returns: double — Positive infinity
yamlNaN¶
| Spice | |
|---|---|
Retrieve a quiet NaN, which YAML spells .nan. Spice has no literal for it, so it is provided here for building float values.
Returns: double — Not a number
yamlIsNaN¶
| Spice | |
|---|---|
Checks whether the given float is a NaN. The usual value != value trick does not work in Spice, since its float comparisons are ordered ones.
Parameters
| Name | Type | Description |
|---|---|---|
value |
double |
Float value to check |
Returns: bool — true if the value is a NaN, false otherwise
YamlValueKind enum¶
Tag for the variant stored inside a YamlValue.
| Item | Value | Description |
|---|---|---|
YAML_NULL |
||
YAML_STRING |
||
YAML_INTEGER |
||
YAML_FLOAT |
||
YAML_BOOL |
||
YAML_SEQUENCE |
||
YAML_MAPPING |