LS2Module:Building an Enum

From ismods.com: dedicated to promoting white hat, EULA-compliant Inner Space and LavishScript mods
Revision as of 11:56, 1 December 2012 by Whiteslax (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

An Enum is a special Type which inherits from System.Enum.

Designing an Enum is considerably easier than other kinds of Types.

Resolve System.Enum

Use ILS2StandardEnvironment::ResolveType to grab a reference to System.Enum
LavishScript2::LS2SmartRef<LavishScript2::ILS2CodeBoxType> pSystemEnum;
g_pLS2Environment->ResolveType(L"System.Enum",pSystemEnum);

Register your Type

Use ILS2StandardEnvironment::RegisterType to register your new Type and inherit from System.Enum
if (!g_pLS2Environment->RegisterType(L"SQLite.DataTypeEnum",pSystemEnum,&g_pLS2_SQLiteDataTypeEnum))
{
 return false;
}

Register Enum Values

For each Value to add to your Enum, you will need to:

  1. Set up a LS2CodeBoxValue_Enum via ILS2StandardEnvironment::NewEnum with its numeric value, e.g. g_pLS2Environment->NewEnum(1,pSystemEnum,pEnumValue)
  2. Register a Static Field with the value's Name and, for the C# Declaration, its initializer in the form Name=Value, e.g. g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Integer",L"",L"Integer=1",pEnumValue,0)
A fully-implemented example with 5 values
{
  LavishScript2::LS2SmartRef<LavishScript2::LS2CodeBoxValue_Enum> pEnumValue;
  g_pLS2Environment->NewEnum(1,pSystemEnum,pEnumValue);
  if (!g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Integer",L"",L"Integer=1",pEnumValue,0))
  {
    return false;
  }
  g_pLS2Environment->NewEnum(2,pSystemEnum,pEnumValue);
  g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Float",L"",L"Float=2",pEnumValue,0);
  g_pLS2Environment->NewEnum(3,pSystemEnum,pEnumValue);
  g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Text",L"",L"Text=3",pEnumValue,0);
  g_pLS2Environment->NewEnum(4,pSystemEnum,pEnumValue);
  g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Blob",L"",L"Blob=4",pEnumValue,0);			
  g_pLS2Environment->NewEnum(5,pSystemEnum,pEnumValue);
  g_pLS2_SQLiteDataTypeEnum->RegisterStaticField(L"Null",L"",L"Null=5",pEnumValue,0);			
}

See Also