Struct Variant
- Namespace
- Typhon.Schema.Definition
- Assembly
- Typhon.Schema.Definition.dll
Store data of a type determined at construction and formatted as a string
public readonly struct Variant : IComparable<Variant>, IEquatable<Variant>
- Implements
- Inherited Members
Remarks
This type allows to store in a field of a component a data that can be of a user set type at construction.
The variant has a fixed size 64 bytes as its only field is a String64 storing the data type and value in the form "tt:data".
There are methods to get or explicitly cast the variant to the literal type of the data it stores.
This struct is read-only.
Constructors
Variant(bool)
Creates a variant holding the boolean value.
public Variant(bool value)
Parameters
valueboolThe value to store.
Variant(short)
Creates a variant holding the 16-bit integer value.
public Variant(short value)
Parameters
valueshortThe value to store.
Variant(int)
Creates a variant holding the 32-bit integer value.
public Variant(int value)
Parameters
valueintThe value to store.
Variant(long)
Creates a variant holding the 64-bit integer value.
public Variant(long value)
Parameters
valuelongThe value to store.
Variant(sbyte)
Creates a variant holding the signed-byte value.
public Variant(sbyte value)
Parameters
valuesbyteThe value to store.
Variant(string, bool)
Creates a variant holding the string value.
public Variant(string value, bool truncate)
Parameters
valuestringThe string to store; its UTF-8 payload must fit in 61 bytes (64 minus the 3-byte type tag).
truncatebooltrueto silently truncate an over-long string to fit;falseto throw instead.
Exceptions
- InvalidOperationException
valueexceeds the 61-byte payload andtruncateisfalse.
Properties
FieldType
The type of the stored value, decoded from the leading two-character type tag; None when the buffer is not a valid
"tt:data" encoding.
public FieldType FieldType { get; }
Property Value
Methods
AsBool()
Returns the stored value as a boolean.
public bool AsBool()
Returns
- bool
The decoded boolean.
Exceptions
- InvalidOperationException
The stored type is not Boolean.
AsByte()
Returns the stored value as a signed byte.
public sbyte AsByte()
Returns
- sbyte
The decoded
sbyte.
Exceptions
- InvalidOperationException
The stored type is not Byte.
AsInt()
Returns the stored value as a 32-bit integer.
public int AsInt()
Returns
- int
The decoded
int.
Exceptions
- InvalidOperationException
The stored type is not Int.
AsLong()
Returns the stored value as a 64-bit integer.
public long AsLong()
Returns
- long
The decoded
long.
Exceptions
- InvalidOperationException
The stored type is not Long.
AsShort()
Returns the stored value as a 16-bit integer.
public short AsShort()
Returns
- short
The decoded
short.
Exceptions
- InvalidOperationException
The stored type is not Short.
AsString()
Returns the stored value as a string.
public string AsString()
Returns
- string
The decoded string payload.
Exceptions
- InvalidOperationException
The stored type is not String.
CompareTo(Variant)
Ordinal byte-wise comparison of the two variants' encoded buffers (type tag included).
public int CompareTo(Variant other)
Parameters
otherVariantThe value to compare against.
Returns
- int
Negative, zero, or positive per lexicographic byte ordering.
Equals(object)
Byte-wise equality; false when obj is not a Variant.
public override bool Equals(object obj)
Parameters
objobject
Returns
Equals(Variant)
Byte-wise equality of the two variants' encoded buffers — equal only when both the type tag and payload match.
public bool Equals(Variant other)
Parameters
otherVariantThe value to compare against.
Returns
- bool
truewhen the encodings are byte-for-byte equal.
GetHashCode()
32-bit MurmurHash2 over the encoded buffer bytes.
public override int GetHashCode()
Returns
ToString()
Formats the stored value as "value (type)" (e.g. "42 (int)"); returns an empty string when the stored type is unrecognized.
public override string ToString()
Returns
Operators
operator ==(Variant, Variant)
Byte-wise equality.
public static bool operator ==(Variant left, Variant right)
Parameters
Returns
explicit operator bool(Variant)
Extracts the stored boolean. See AsBool().
public static explicit operator bool(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not Boolean.
explicit operator short(Variant)
Extracts the stored 16-bit integer. See AsShort().
public static explicit operator short(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not Short.
explicit operator int(Variant)
Extracts the stored 32-bit integer. See AsInt().
public static explicit operator int(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not Int.
explicit operator long(Variant)
Extracts the stored 64-bit integer. See AsLong().
public static explicit operator long(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not Long.
explicit operator sbyte(Variant)
Extracts the stored signed byte. See AsByte().
public static explicit operator sbyte(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not Byte.
explicit operator string(Variant)
Extracts the stored string. See AsString().
public static explicit operator string(Variant v)
Parameters
vVariant
Returns
Exceptions
- InvalidOperationException
The stored type is not String.
operator !=(Variant, Variant)
Byte-wise inequality.
public static bool operator !=(Variant left, Variant right)