NoesisGUI

Nullable Converters

Nullable Converters are Type Converters specialized for Nullable types. There are 3 implemented Nullable Converters for basic types:

  • NullableBooleanConverter: for Nullable<NsBool> type
  • NullableIntegerConverter<T>: where T is any of the integer types (NsInt, NsUInt, NsInt64, etc...)
  • NullableFloatConverter<T>: where T can be NsFloat32 or NsFloat64

Nullable converters can convert From and To the same types of its base converters (BooleanConverter, IntegerConverter and FloatConverter) plus the type of the Nullable (for example, NullableBooleanConverter can convert From and To bool) but they can't convert between Nullables of different types (for example, NullableIntegerConverter<NsInt> can't convert to Nullable<NsFloat32> but it can convert to NsFloat32).

Examples:

Ptr<ITypeConverter> tc = NsCreateComponent<NullableIntegerConverter<NsInt> >();

tc->CanConvertFrom(TypeOf<NsString>()); // true
tc->CanConvertFrom(TypeOf<NsInt>()); // true

Boxing::Unbox<Nullable<NsInt> >(tc->ConvertFromString(NST("-123"))).GetValue(); // -123
Boxing::Unbox<Nullable<NsInt> >(tc->ConvertFrom(Boxing::Box<NsString>(NST("-123")))).GetValue(); // -123
Boxing::Unbox<Nullable<NsInt> >(tc->ConvertFrom(Boxing::Box<NsInt>(-123))).GetValue(); // -123
Boxing::Unbox<Nullable<NsInt> >(tc->ConvertFrom(0)).HasValue(); // false

tc->CanConvertTo(TypeOf<NsInt>()); // true
tc->CanConvertTo(TypeOf<NsFloat32>()); // true

tc->ConvertToString(Boxing::Box<Nullable<NsInt> >(-123)); // NST("-123")
Boxing::Unbox<NsString>(tc->ConvertTo(Boxing::Box<Nullable<NsInt> >(Nullable<NsInt>(-123)), TypeOf<NsString>()); // NST("-123")
Boxing::Unbox<NsInt>(tc->ConvertTo(Boxing::Box<Nullable<NsInt> >(Nullable<NsInt>(-123)), TypeOf<NsInt>()); // -123
Boxing::Unbox<NsFloat32>(tc->ConvertTo(Boxing::Box<Nullable<NsInt> >(Nullable<NsInt>(-123)), TypeOf<NsFloat32>()); // -123.0f

tc->CanConvertFrom(TypeOf<Nullable<NsInt> >()); // false
tc->CanConvertFrom(TypeOf<Nullable<NsFloat32> >()); // false

Other Converters

For classes that implements the method static (type) Parse(const NsChar* str), there is the NullableConverter<T> helper, which allows to convert From and To string only.

© 2017 Noesis Technologies