Table of Contents

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

value bool

The value to store.

Variant(short)

Creates a variant holding the 16-bit integer value.

public Variant(short value)

Parameters

value short

The value to store.

Variant(int)

Creates a variant holding the 32-bit integer value.

public Variant(int value)

Parameters

value int

The value to store.

Variant(long)

Creates a variant holding the 64-bit integer value.

public Variant(long value)

Parameters

value long

The value to store.

Variant(sbyte)

Creates a variant holding the signed-byte value.

public Variant(sbyte value)

Parameters

value sbyte

The value to store.

Variant(string, bool)

Creates a variant holding the string value.

public Variant(string value, bool truncate)

Parameters

value string

The string to store; its UTF-8 payload must fit in 61 bytes (64 minus the 3-byte type tag).

truncate bool

true to silently truncate an over-long string to fit; false to throw instead.

Exceptions

InvalidOperationException

value exceeds the 61-byte payload and truncate is false.

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

FieldType

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

other Variant

The 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

obj object

Returns

bool

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

other Variant

The value to compare against.

Returns

bool

true when the encodings are byte-for-byte equal.

GetHashCode()

32-bit MurmurHash2 over the encoded buffer bytes.

public override int GetHashCode()

Returns

int

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

string

Operators

operator ==(Variant, Variant)

Byte-wise equality.

public static bool operator ==(Variant left, Variant right)

Parameters

left Variant
right Variant

Returns

bool

explicit operator bool(Variant)

Extracts the stored boolean. See AsBool().

public static explicit operator bool(Variant v)

Parameters

v Variant

Returns

bool

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

v Variant

Returns

short

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

v Variant

Returns

int

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

v Variant

Returns

long

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

v Variant

Returns

sbyte

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

v Variant

Returns

string

Exceptions

InvalidOperationException

The stored type is not String.

operator !=(Variant, Variant)

Byte-wise inequality.

public static bool operator !=(Variant left, Variant right)

Parameters

left Variant
right Variant

Returns

bool